250 lines
7.3 KiB
Go
250 lines
7.3 KiB
Go
// Copyright 2015 PingCAP, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package executor_test
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
. "github.com/pingcap/check"
|
|
"github.com/pingcap/tidb"
|
|
"github.com/pingcap/tidb/domain"
|
|
"github.com/pingcap/tidb/executor"
|
|
"github.com/pingcap/tidb/inspectkv"
|
|
"github.com/pingcap/tidb/kv"
|
|
"github.com/pingcap/tidb/model"
|
|
"github.com/pingcap/tidb/util/testkit"
|
|
)
|
|
|
|
func TestT(t *testing.T) {
|
|
TestingT(t)
|
|
}
|
|
|
|
var _ = Suite(&testSuite{})
|
|
|
|
type testSuite struct {
|
|
store kv.Storage
|
|
}
|
|
|
|
func (s *testSuite) SetUpSuite(c *C) {
|
|
store, err := tidb.NewStore("memory://test/test")
|
|
c.Assert(err, IsNil)
|
|
s.store = store
|
|
}
|
|
|
|
func (s *testSuite) TearDownSuite(c *C) {
|
|
s.store.Close()
|
|
}
|
|
|
|
func (s *testSuite) TestAdmin(c *C) {
|
|
tk := testkit.NewTestKit(c, s.store)
|
|
tk.MustExec("use test")
|
|
tk.MustExec("drop table if exists admin_test")
|
|
tk.MustExec("create table admin_test (c1 int, c2 int, c3 int default 1, index (c1))")
|
|
tk.MustExec("insert admin_test (c1) values (1),(2),(NULL)")
|
|
r, err := tk.Exec("admin show ddl")
|
|
c.Assert(err, IsNil)
|
|
row, err := r.Next()
|
|
c.Assert(err, IsNil)
|
|
c.Assert(row.Data, HasLen, 6)
|
|
txn, err := s.store.Begin()
|
|
c.Assert(err, IsNil)
|
|
ddlInfo, err := inspectkv.GetDDLInfo(txn)
|
|
c.Assert(err, IsNil)
|
|
c.Assert(row.Data[0], Equals, ddlInfo.SchemaVer)
|
|
rowOwnerInfos := strings.Split(row.Data[1].(string), ",")
|
|
ownerInfos := strings.Split(ddlInfo.Owner.String(), ",")
|
|
c.Assert(rowOwnerInfos[0], Equals, ownerInfos[0])
|
|
c.Assert(row.Data[2], Equals, "")
|
|
bgInfo, err := inspectkv.GetBgDDLInfo(txn)
|
|
c.Assert(err, IsNil)
|
|
c.Assert(row.Data[3], Equals, bgInfo.SchemaVer)
|
|
rowOwnerInfos = strings.Split(row.Data[4].(string), ",")
|
|
ownerInfos = strings.Split(bgInfo.Owner.String(), ",")
|
|
c.Assert(rowOwnerInfos[0], Equals, ownerInfos[0])
|
|
c.Assert(row.Data[5], Equals, "")
|
|
row, err = r.Next()
|
|
c.Assert(err, IsNil)
|
|
c.Assert(row, IsNil)
|
|
|
|
// check table test
|
|
tk.MustExec("create table admin_test1 (c1 int, c2 int default 1, index (c1))")
|
|
tk.MustExec("insert admin_test1 (c1) values (21),(22)")
|
|
r, err = tk.Exec("admin check table admin_test, admin_test1")
|
|
c.Assert(err, IsNil)
|
|
c.Assert(r, IsNil)
|
|
// error table name
|
|
r, err = tk.Exec("admin check table admin_test_error")
|
|
c.Assert(err, NotNil)
|
|
// different index values
|
|
domain, err := domain.NewDomain(s.store, 1*time.Second)
|
|
c.Assert(err, IsNil)
|
|
is := domain.InfoSchema()
|
|
c.Assert(is, NotNil)
|
|
tb, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("admin_test"))
|
|
c.Assert(err, IsNil)
|
|
c.Assert(tb.Indices(), HasLen, 1)
|
|
err = tb.Indices()[0].X.Create(txn, []interface{}{int64(10)}, 1)
|
|
c.Assert(err, IsNil)
|
|
err = txn.Commit()
|
|
c.Assert(err, IsNil)
|
|
r, err = tk.Exec("admin check table admin_test")
|
|
c.Assert(err, NotNil)
|
|
}
|
|
|
|
func (s *testSuite) TestPrepared(c *C) {
|
|
tk := testkit.NewTestKit(c, s.store)
|
|
tk.MustExec("use test")
|
|
tk.MustExec("drop table if exists prepare_test")
|
|
tk.MustExec("create table prepare_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1)")
|
|
tk.MustExec("insert prepare_test (c1) values (1),(2),(NULL)")
|
|
|
|
tk.MustExec(`prepare stmt_test_1 from 'select id from prepare_test where id > ?'; set @a = 1; execute stmt_test_1 using @a;`)
|
|
tk.MustExec(`prepare stmt_test_2 from 'select 1'`)
|
|
// Prepare multiple statement is not allowed.
|
|
_, err := tk.Exec(`prepare stmt_test_3 from 'select id from prepare_test where id > ?;select id from prepare_test where id > ?;'`)
|
|
c.Assert(executor.ErrPrepareMulti.Equal(err), IsTrue)
|
|
// The variable count does not match.
|
|
_, err = tk.Exec(`prepare stmt_test_4 from 'select id from prepare_test where id > ? and id < ?'; set @a = 1; execute stmt_test_4 using @a;`)
|
|
c.Assert(executor.ErrWrongParamCount.Equal(err), IsTrue)
|
|
// Prepare and deallocate prepared statement immediately.
|
|
tk.MustExec(`prepare stmt_test_5 from 'select id from prepare_test where id > ?'; deallocate prepare stmt_test_5;`)
|
|
|
|
// Statement not found.
|
|
_, err = tk.Exec("deallocate prepare stmt_test_5")
|
|
c.Assert(executor.ErrStmtNotFound.Equal(err), IsTrue)
|
|
|
|
// The `stmt_test5` should not be found.
|
|
_, err = tk.Exec(`set @a = 1; execute stmt_test_5 using @a;`)
|
|
c.Assert(executor.ErrStmtNotFound.Equal(err), IsTrue)
|
|
|
|
// Use parameter marker with argument will run prepared statement.
|
|
result := tk.MustQuery("select distinct c1, c2 from prepare_test where c1 = ?", 1)
|
|
result.Check([][]interface{}{{1, nil}})
|
|
|
|
// Call Session PrepareStmt directly to get stmtId.
|
|
stmtId, _, _, err := tk.Se.PrepareStmt("select c1, c2 from prepare_test where c1 = ?")
|
|
c.Assert(err, IsNil)
|
|
_, err = tk.Se.ExecutePreparedStmt(stmtId, 1)
|
|
c.Assert(err, IsNil)
|
|
|
|
// Make schema change.
|
|
tk.Exec("create table prepare2 (a int)")
|
|
|
|
// Should success as the changed schema do not affect the prepared statement.
|
|
_, err = tk.Se.ExecutePreparedStmt(stmtId, 1)
|
|
c.Assert(err, IsNil)
|
|
|
|
// Drop a column so the prepared statement become invalid.
|
|
tk.MustExec("alter table prepare_test drop column c2")
|
|
|
|
// There should be schema changed error.
|
|
_, err = tk.Se.ExecutePreparedStmt(stmtId, 1)
|
|
c.Assert(executor.ErrSchemaChanged.Equal(err), IsTrue)
|
|
|
|
// Coverage.
|
|
exec := &executor.ExecuteExec{}
|
|
exec.Fields()
|
|
exec.Next()
|
|
exec.Close()
|
|
}
|
|
|
|
func (s *testSuite) TestTablePKisHandleScan(c *C) {
|
|
tk := testkit.NewTestKit(c, s.store)
|
|
tk.MustExec("use test")
|
|
tk.MustExec("drop table if exists t")
|
|
tk.MustExec("create table t (a int PRIMARY KEY AUTO_INCREMENT)")
|
|
tk.MustExec("insert t values (),()")
|
|
tk.MustExec("insert t values (-100),(0)")
|
|
|
|
cases := []struct {
|
|
sql string
|
|
result [][]interface{}
|
|
}{
|
|
{
|
|
"select * from t",
|
|
testkit.Rows("-100", "0", "1", "2"),
|
|
},
|
|
{
|
|
"select * from t where a = 1",
|
|
testkit.Rows("1"),
|
|
},
|
|
{
|
|
"select * from t where a != 1",
|
|
testkit.Rows("-100", "0", "2"),
|
|
},
|
|
{
|
|
"select * from t where a >= '1.1'",
|
|
testkit.Rows("2"),
|
|
},
|
|
{
|
|
"select * from t where a < '1.1'",
|
|
testkit.Rows("-100", "0", "1"),
|
|
},
|
|
{
|
|
"select * from t where a > '-100.1' and a < 2",
|
|
testkit.Rows("-100", "0", "1"),
|
|
},
|
|
{
|
|
"select * from t where a is null",
|
|
testkit.Rows(),
|
|
}, {
|
|
"select * from t where a is true",
|
|
testkit.Rows("-100", "1", "2"),
|
|
}, {
|
|
"select * from t where a is false",
|
|
testkit.Rows("0"),
|
|
},
|
|
{
|
|
"select * from t where a in (0, 2)",
|
|
testkit.Rows("0", "2"),
|
|
},
|
|
{
|
|
"select * from t where a between 0 and 1",
|
|
testkit.Rows("0", "1"),
|
|
},
|
|
}
|
|
|
|
for _, ca := range cases {
|
|
result := tk.MustQuery(ca.sql)
|
|
result.Check(ca.result)
|
|
}
|
|
}
|
|
|
|
func (s *testSuite) TestJoin(c *C) {
|
|
tk := testkit.NewTestKit(c, s.store)
|
|
tk.MustExec("use test")
|
|
tk.MustExec("drop table if exists t")
|
|
tk.MustExec("create table t (c int)")
|
|
tk.MustExec("insert t values (1)")
|
|
cases := []struct {
|
|
sql string
|
|
result [][]interface{}
|
|
}{
|
|
{
|
|
"select 1 from t as a left join t as b on 0",
|
|
testkit.Rows("1"),
|
|
},
|
|
{
|
|
"select 1 from t as a join t as b on 1",
|
|
testkit.Rows("1"),
|
|
},
|
|
}
|
|
for _, ca := range cases {
|
|
result := tk.MustQuery(ca.sql)
|
|
result.Check(ca.result)
|
|
}
|
|
}
|