github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/executor/executor_test.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package executor_test
    15  
    16  import (
    17  	"flag"
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/insionng/yougam/libraries/ngaut/log"
    24  	. "github.com/insionng/yougam/libraries/pingcap/check"
    25  	"github.com/insionng/yougam/libraries/pingcap/tidb"
    26  	"github.com/insionng/yougam/libraries/pingcap/tidb/domain"
    27  	"github.com/insionng/yougam/libraries/pingcap/tidb/executor"
    28  	"github.com/insionng/yougam/libraries/pingcap/tidb/inspectkv"
    29  	"github.com/insionng/yougam/libraries/pingcap/tidb/kv"
    30  	"github.com/insionng/yougam/libraries/pingcap/tidb/model"
    31  	"github.com/insionng/yougam/libraries/pingcap/tidb/optimizer/plan"
    32  	"github.com/insionng/yougam/libraries/pingcap/tidb/store/tikv"
    33  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testkit"
    34  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak"
    35  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    36  )
    37  
    38  func TestT(t *testing.T) {
    39  	TestingT(t)
    40  }
    41  
    42  var _ = Suite(&testSuite{})
    43  
    44  type testSuite struct {
    45  	store kv.Storage
    46  }
    47  
    48  var mockTikv = flag.Bool("mockTikv", true, "use mock tikv store in executor test")
    49  
    50  func (s *testSuite) SetUpSuite(c *C) {
    51  	flag.Lookup("mockTikv")
    52  	useMockTikv := *mockTikv
    53  	if useMockTikv {
    54  		s.store = tikv.NewMockTikvStore()
    55  		tidb.SetSchemaLease(0)
    56  	} else {
    57  		store, err := tidb.NewStore("memory://test/test")
    58  		c.Assert(err, IsNil)
    59  		s.store = store
    60  	}
    61  	log.SetLevelByString("warn")
    62  	executor.BaseLookupTableTaskSize = 2
    63  }
    64  
    65  func (s *testSuite) TearDownSuite(c *C) {
    66  	executor.BaseLookupTableTaskSize = 512
    67  	s.store.Close()
    68  }
    69  
    70  func (s *testSuite) TestAdmin(c *C) {
    71  	defer testleak.AfterTest(c)()
    72  	tk := testkit.NewTestKit(c, s.store)
    73  	tk.MustExec("use test")
    74  	tk.MustExec("drop table if exists admin_test")
    75  	tk.MustExec("create table admin_test (c1 int, c2 int, c3 int default 1, index (c1))")
    76  	tk.MustExec("insert admin_test (c1) values (1),(2),(NULL)")
    77  	r, err := tk.Exec("admin show ddl")
    78  	c.Assert(err, IsNil)
    79  	row, err := r.Next()
    80  	c.Assert(err, IsNil)
    81  	c.Assert(row.Data, HasLen, 6)
    82  	txn, err := s.store.Begin()
    83  	c.Assert(err, IsNil)
    84  	ddlInfo, err := inspectkv.GetDDLInfo(txn)
    85  	c.Assert(err, IsNil)
    86  	c.Assert(row.Data[0].GetInt64(), Equals, ddlInfo.SchemaVer)
    87  	rowOwnerInfos := strings.Split(row.Data[1].GetString(), ",")
    88  	ownerInfos := strings.Split(ddlInfo.Owner.String(), ",")
    89  	c.Assert(rowOwnerInfos[0], Equals, ownerInfos[0])
    90  	c.Assert(row.Data[2].GetString(), Equals, "")
    91  	bgInfo, err := inspectkv.GetBgDDLInfo(txn)
    92  	c.Assert(err, IsNil)
    93  	c.Assert(row.Data[3].GetInt64(), Equals, bgInfo.SchemaVer)
    94  	rowOwnerInfos = strings.Split(row.Data[4].GetString(), ",")
    95  	ownerInfos = strings.Split(bgInfo.Owner.String(), ",")
    96  	c.Assert(rowOwnerInfos[0], Equals, ownerInfos[0])
    97  	c.Assert(row.Data[5].GetString(), Equals, "")
    98  	row, err = r.Next()
    99  	c.Assert(err, IsNil)
   100  	c.Assert(row, IsNil)
   101  
   102  	// check table test
   103  	tk.MustExec("create table admin_test1 (c1 int, c2 int default 1, index (c1))")
   104  	tk.MustExec("insert admin_test1 (c1) values (21),(22)")
   105  	r, err = tk.Exec("admin check table admin_test, admin_test1")
   106  	c.Assert(err, IsNil)
   107  	c.Assert(r, IsNil)
   108  	// error table name
   109  	r, err = tk.Exec("admin check table admin_test_error")
   110  	c.Assert(err, NotNil)
   111  	// different index values
   112  	domain, err := domain.NewDomain(s.store, 1*time.Second)
   113  	c.Assert(err, IsNil)
   114  	is := domain.InfoSchema()
   115  	c.Assert(is, NotNil)
   116  	tb, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("admin_test"))
   117  	c.Assert(err, IsNil)
   118  	c.Assert(tb.Indices(), HasLen, 1)
   119  	err = tb.Indices()[0].X.Create(txn, types.MakeDatums(int64(10)), 1)
   120  	c.Assert(err, IsNil)
   121  	err = txn.Commit()
   122  	c.Assert(err, IsNil)
   123  	r, err = tk.Exec("admin check table admin_test")
   124  	c.Assert(err, NotNil)
   125  }
   126  
   127  func (s *testSuite) TestPrepared(c *C) {
   128  	defer testleak.AfterTest(c)()
   129  	tk := testkit.NewTestKit(c, s.store)
   130  	tk.MustExec("use test")
   131  	tk.MustExec("drop table if exists prepare_test")
   132  	tk.MustExec("create table prepare_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1)")
   133  	tk.MustExec("insert prepare_test (c1) values (1),(2),(NULL)")
   134  
   135  	tk.MustExec(`prepare stmt_test_1 from 'select id from prepare_test where id > ?'; set @a = 1; execute stmt_test_1 using @a;`)
   136  	tk.MustExec(`prepare stmt_test_2 from 'select 1'`)
   137  	// Prepare multiple statement is not allowed.
   138  	_, err := tk.Exec(`prepare stmt_test_3 from 'select id from prepare_test where id > ?;select id from prepare_test where id > ?;'`)
   139  	c.Assert(executor.ErrPrepareMulti.Equal(err), IsTrue)
   140  	// The variable count does not match.
   141  	_, 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;`)
   142  	c.Assert(executor.ErrWrongParamCount.Equal(err), IsTrue)
   143  	// Prepare and deallocate prepared statement immediately.
   144  	tk.MustExec(`prepare stmt_test_5 from 'select id from prepare_test where id > ?'; deallocate prepare stmt_test_5;`)
   145  
   146  	// Statement not found.
   147  	_, err = tk.Exec("deallocate prepare stmt_test_5")
   148  	c.Assert(executor.ErrStmtNotFound.Equal(err), IsTrue)
   149  
   150  	// The `stmt_test5` should not be found.
   151  	_, err = tk.Exec(`set @a = 1; execute stmt_test_5 using @a;`)
   152  	c.Assert(executor.ErrStmtNotFound.Equal(err), IsTrue)
   153  
   154  	// Use parameter marker with argument will run prepared statement.
   155  	result := tk.MustQuery("select distinct c1, c2 from prepare_test where c1 = ?", 1)
   156  	result.Check([][]interface{}{{1, nil}})
   157  
   158  	// Call Session PrepareStmt directly to get stmtId.
   159  	stmtId, _, _, err := tk.Se.PrepareStmt("select c1, c2 from prepare_test where c1 = ?")
   160  	c.Assert(err, IsNil)
   161  	_, err = tk.Se.ExecutePreparedStmt(stmtId, 1)
   162  	c.Assert(err, IsNil)
   163  
   164  	// Make schema change.
   165  	tk.Exec("create table prepare2 (a int)")
   166  
   167  	// Should success as the changed schema do not affect the prepared statement.
   168  	_, err = tk.Se.ExecutePreparedStmt(stmtId, 1)
   169  	c.Assert(err, IsNil)
   170  
   171  	// Drop a column so the prepared statement become invalid.
   172  	tk.MustExec("alter table prepare_test drop column c2")
   173  
   174  	// There should be schema changed error.
   175  	_, err = tk.Se.ExecutePreparedStmt(stmtId, 1)
   176  	c.Assert(executor.ErrSchemaChanged.Equal(err), IsTrue)
   177  
   178  	// Coverage.
   179  	exec := &executor.ExecuteExec{}
   180  	exec.Fields()
   181  	exec.Next()
   182  	exec.Close()
   183  }
   184  
   185  func (s *testSuite) fillData(tk *testkit.TestKit, table string) {
   186  	tk.MustExec("use test")
   187  	tk.MustExec(fmt.Sprintf("create table %s(id int not null default 1, name varchar(255), PRIMARY KEY(id));", table))
   188  
   189  	// insert data
   190  	tk.MustExec(fmt.Sprintf("insert INTO %s VALUES (1, \"hello\");", table))
   191  	tk.CheckExecResult(1, 0)
   192  	tk.MustExec(fmt.Sprintf("insert into %s values (2, \"hello\");", table))
   193  	tk.CheckExecResult(1, 0)
   194  }
   195  
   196  func (s *testSuite) TestDelete(c *C) {
   197  	defer testleak.AfterTest(c)()
   198  	tk := testkit.NewTestKit(c, s.store)
   199  	s.fillData(tk, "delete_test")
   200  
   201  	tk.MustExec(`update delete_test set name = "abc" where id = 2;`)
   202  	tk.CheckExecResult(1, 0)
   203  
   204  	tk.MustExec(`delete from delete_test where id = 2 limit 1;`)
   205  	tk.CheckExecResult(1, 0)
   206  
   207  	// Test delete with false condition
   208  	tk.MustExec(`delete from delete_test where 0;`)
   209  	tk.CheckExecResult(0, 0)
   210  
   211  	tk.MustExec("insert into delete_test values (2, 'abc')")
   212  	tk.MustExec(`delete from delete_test where delete_test.id = 2 limit 1`)
   213  	tk.CheckExecResult(1, 0)
   214  
   215  	// Select data
   216  	tk.MustExec("begin")
   217  	rows := tk.MustQuery(`SELECT * from delete_test limit 2;`)
   218  	rowStr := fmt.Sprintf("%v %v", "1", []byte("hello"))
   219  	rows.Check(testkit.Rows(rowStr))
   220  	tk.MustExec("commit")
   221  
   222  	tk.MustExec(`delete from delete_test ;`)
   223  	tk.CheckExecResult(1, 0)
   224  }
   225  
   226  func (s *testSuite) fillDataMultiTable(tk *testkit.TestKit) {
   227  	tk.MustExec("use test")
   228  	tk.MustExec("drop table if exists t1, t2, t3")
   229  	// Create and fill table t1
   230  	tk.MustExec("create table t1 (id int, data int);")
   231  	tk.MustExec("insert into t1 values (11, 121), (12, 122), (13, 123);")
   232  	tk.CheckExecResult(3, 0)
   233  	// Create and fill table t2
   234  	tk.MustExec("create table t2 (id int, data int);")
   235  	tk.MustExec("insert into t2 values (11, 221), (22, 222), (23, 223);")
   236  	tk.CheckExecResult(3, 0)
   237  	// Create and fill table t3
   238  	tk.MustExec("create table t3 (id int, data int);")
   239  	tk.MustExec("insert into t3 values (11, 321), (22, 322), (23, 323);")
   240  	tk.CheckExecResult(3, 0)
   241  }
   242  
   243  func (s *testSuite) TestMultiTableDelete(c *C) {
   244  	defer testleak.AfterTest(c)()
   245  	tk := testkit.NewTestKit(c, s.store)
   246  	s.fillDataMultiTable(tk)
   247  
   248  	tk.MustExec(`delete t1, t2 from t1 inner join t2 inner join t3 where t1.id=t2.id and t2.id=t3.id;`)
   249  	tk.CheckExecResult(2, 0)
   250  
   251  	// Select data
   252  	r := tk.MustQuery("select * from t3")
   253  	c.Assert(r.Rows(), HasLen, 3)
   254  }
   255  
   256  func (s *testSuite) TestQualifedDelete(c *C) {
   257  	defer testleak.AfterTest(c)()
   258  	tk := testkit.NewTestKit(c, s.store)
   259  	tk.MustExec("use test")
   260  	tk.MustExec("drop table if exists t1")
   261  	tk.MustExec("drop table if exists t2")
   262  	tk.MustExec("create table t1 (c1 int, c2 int, index (c1))")
   263  	tk.MustExec("create table t2 (c1 int, c2 int)")
   264  	tk.MustExec("insert into t1 values (1, 1), (2, 2)")
   265  
   266  	// delete with index
   267  	tk.MustExec("delete from t1 where t1.c1 = 1")
   268  	tk.CheckExecResult(1, 0)
   269  
   270  	// delete with no index
   271  	tk.MustExec("delete from t1 where t1.c2 = 2")
   272  	tk.CheckExecResult(1, 0)
   273  
   274  	r := tk.MustQuery("select * from t1")
   275  	c.Assert(r.Rows(), HasLen, 0)
   276  
   277  	_, err := tk.Exec("delete from t1 as a where a.c1 = 1")
   278  	c.Assert(err, NotNil)
   279  
   280  	tk.MustExec("insert into t1 values (1, 1), (2, 2)")
   281  	tk.MustExec("insert into t2 values (2, 1), (3,1)")
   282  	tk.MustExec("delete t1, t2 from t1 join t2 where t1.c1 = t2.c2")
   283  	tk.CheckExecResult(3, 0)
   284  
   285  	tk.MustExec("insert into t2 values (2, 1), (3,1)")
   286  	tk.MustExec("delete a, b from t1 as a join t2 as b where a.c2 = b.c1")
   287  	tk.CheckExecResult(2, 0)
   288  
   289  	_, err = tk.Exec("delete t1, t2 from t1 as a join t2 as b where a.c2 = b.c1")
   290  	c.Assert(err, NotNil)
   291  
   292  	tk.MustExec("drop table t1, t2")
   293  }
   294  
   295  func (s *testSuite) TestInsert(c *C) {
   296  	defer testleak.AfterTest(c)()
   297  	tk := testkit.NewTestKit(c, s.store)
   298  	tk.MustExec("use test")
   299  	testSQL := `drop table if exists insert_test;create table insert_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1);`
   300  	tk.MustExec(testSQL)
   301  	testSQL = `insert insert_test (c1) values (1),(2),(NULL);`
   302  	tk.MustExec(testSQL)
   303  
   304  	errInsertSelectSQL := `insert insert_test (c1) values ();`
   305  	tk.MustExec("begin")
   306  	_, err := tk.Exec(errInsertSelectSQL)
   307  	c.Assert(err, NotNil)
   308  	tk.MustExec("rollback")
   309  
   310  	errInsertSelectSQL = `insert insert_test (c1, c2) values (1,2),(1);`
   311  	tk.MustExec("begin")
   312  	_, err = tk.Exec(errInsertSelectSQL)
   313  	c.Assert(err, NotNil)
   314  	tk.MustExec("rollback")
   315  
   316  	errInsertSelectSQL = `insert insert_test (xxx) values (3);`
   317  	tk.MustExec("begin")
   318  	_, err = tk.Exec(errInsertSelectSQL)
   319  	c.Assert(err, NotNil)
   320  	tk.MustExec("rollback")
   321  
   322  	errInsertSelectSQL = `insert insert_test_xxx (c1) values ();`
   323  	tk.MustExec("begin")
   324  	_, err = tk.Exec(errInsertSelectSQL)
   325  	c.Assert(err, NotNil)
   326  	tk.MustExec("rollback")
   327  
   328  	insertSetSQL := `insert insert_test set c1 = 3;`
   329  	tk.MustExec(insertSetSQL)
   330  
   331  	errInsertSelectSQL = `insert insert_test set c1 = 4, c1 = 5;`
   332  	tk.MustExec("begin")
   333  	_, err = tk.Exec(errInsertSelectSQL)
   334  	c.Assert(err, NotNil)
   335  	tk.MustExec("rollback")
   336  
   337  	errInsertSelectSQL = `insert insert_test set xxx = 6;`
   338  	tk.MustExec("begin")
   339  	_, err = tk.Exec(errInsertSelectSQL)
   340  	c.Assert(err, NotNil)
   341  	tk.MustExec("rollback")
   342  
   343  	insertSelectSQL := `create table insert_test_1 (id int, c1 int);`
   344  	tk.MustExec(insertSelectSQL)
   345  	insertSelectSQL = `insert insert_test_1 select id, c1 from insert_test;`
   346  	tk.MustExec(insertSelectSQL)
   347  
   348  	insertSelectSQL = `create table insert_test_2 (id int, c1 int);`
   349  	tk.MustExec(insertSelectSQL)
   350  	insertSelectSQL = `insert insert_test_1 select id, c1 from insert_test union select id * 10, c1 * 10 from insert_test;`
   351  	tk.MustExec(insertSelectSQL)
   352  
   353  	errInsertSelectSQL = `insert insert_test_1 select c1 from insert_test;`
   354  	tk.MustExec("begin")
   355  	_, err = tk.Exec(errInsertSelectSQL)
   356  	c.Assert(err, NotNil)
   357  	tk.MustExec("rollback")
   358  
   359  	insertSQL := `insert into insert_test (id, c2) values (1, 1) on duplicate key update c2=10;`
   360  	tk.MustExec(insertSQL)
   361  
   362  	insertSQL = `insert into insert_test (id, c2) values (1, 1) on duplicate key update insert_test.c2=10;`
   363  	tk.MustExec(insertSQL)
   364  
   365  	_, err = tk.Exec(`insert into insert_test (id, c2) values(1, 1) on duplicate key update t.c2 = 10`)
   366  	c.Assert(err, NotNil)
   367  }
   368  
   369  func (s *testSuite) TestInsertAutoInc(c *C) {
   370  	defer testleak.AfterTest(c)()
   371  	tk := testkit.NewTestKit(c, s.store)
   372  	tk.MustExec("use test")
   373  	createSQL := `drop table if exists insert_autoinc_test; create table insert_autoinc_test (id int primary key auto_increment, c1 int);`
   374  	tk.MustExec(createSQL)
   375  
   376  	insertSQL := `insert into insert_autoinc_test(c1) values (1), (2)`
   377  	tk.MustExec(insertSQL)
   378  	tk.MustExec("begin")
   379  	r := tk.MustQuery("select * from insert_autoinc_test;")
   380  	rowStr1 := fmt.Sprintf("%v %v", "1", "1")
   381  	rowStr2 := fmt.Sprintf("%v %v", "2", "2")
   382  	r.Check(testkit.Rows(rowStr1, rowStr2))
   383  	tk.MustExec("commit")
   384  
   385  	tk.MustExec("begin")
   386  	insertSQL = `insert into insert_autoinc_test(id, c1) values (5,5)`
   387  	tk.MustExec(insertSQL)
   388  	insertSQL = `insert into insert_autoinc_test(c1) values (6)`
   389  	tk.MustExec(insertSQL)
   390  	tk.MustExec("commit")
   391  	tk.MustExec("begin")
   392  	r = tk.MustQuery("select * from insert_autoinc_test;")
   393  	rowStr3 := fmt.Sprintf("%v %v", "5", "5")
   394  	rowStr4 := fmt.Sprintf("%v %v", "6", "6")
   395  	r.Check(testkit.Rows(rowStr1, rowStr2, rowStr3, rowStr4))
   396  	tk.MustExec("commit")
   397  
   398  	tk.MustExec("begin")
   399  	insertSQL = `insert into insert_autoinc_test(id, c1) values (3,3)`
   400  	tk.MustExec(insertSQL)
   401  	tk.MustExec("commit")
   402  	tk.MustExec("begin")
   403  	r = tk.MustQuery("select * from insert_autoinc_test;")
   404  	rowStr5 := fmt.Sprintf("%v %v", "3", "3")
   405  	r.Check(testkit.Rows(rowStr1, rowStr2, rowStr5, rowStr3, rowStr4))
   406  	tk.MustExec("commit")
   407  
   408  	tk.MustExec("begin")
   409  	insertSQL = `insert into insert_autoinc_test(c1) values (7)`
   410  	tk.MustExec(insertSQL)
   411  	tk.MustExec("commit")
   412  	tk.MustExec("begin")
   413  	r = tk.MustQuery("select * from insert_autoinc_test;")
   414  	rowStr6 := fmt.Sprintf("%v %v", "7", "7")
   415  	r.Check(testkit.Rows(rowStr1, rowStr2, rowStr5, rowStr3, rowStr4, rowStr6))
   416  	tk.MustExec("commit")
   417  
   418  	// issue-962
   419  	createSQL = `drop table if exists insert_autoinc_test; create table insert_autoinc_test (id int primary key auto_increment, c1 int);`
   420  	tk.MustExec(createSQL)
   421  	insertSQL = `insert into insert_autoinc_test(id, c1) values (0.3, 1)`
   422  	tk.MustExec(insertSQL)
   423  	r = tk.MustQuery("select * from insert_autoinc_test;")
   424  	rowStr1 = fmt.Sprintf("%v %v", "1", "1")
   425  	r.Check(testkit.Rows(rowStr1))
   426  	insertSQL = `insert into insert_autoinc_test(id, c1) values (-0.3, 2)`
   427  	tk.MustExec(insertSQL)
   428  	r = tk.MustQuery("select * from insert_autoinc_test;")
   429  	rowStr2 = fmt.Sprintf("%v %v", "2", "2")
   430  	r.Check(testkit.Rows(rowStr1, rowStr2))
   431  	insertSQL = `insert into insert_autoinc_test(id, c1) values (-3.3, 3)`
   432  	tk.MustExec(insertSQL)
   433  	r = tk.MustQuery("select * from insert_autoinc_test;")
   434  	rowStr3 = fmt.Sprintf("%v %v", "-3", "3")
   435  	r.Check(testkit.Rows(rowStr3, rowStr1, rowStr2))
   436  	insertSQL = `insert into insert_autoinc_test(id, c1) values (4.3, 4)`
   437  	tk.MustExec(insertSQL)
   438  	r = tk.MustQuery("select * from insert_autoinc_test;")
   439  	rowStr4 = fmt.Sprintf("%v %v", "4", "4")
   440  	r.Check(testkit.Rows(rowStr3, rowStr1, rowStr2, rowStr4))
   441  	insertSQL = `insert into insert_autoinc_test(c1) values (5)`
   442  	tk.MustExec(insertSQL)
   443  	r = tk.MustQuery("select * from insert_autoinc_test;")
   444  	rowStr5 = fmt.Sprintf("%v %v", "5", "5")
   445  	r.Check(testkit.Rows(rowStr3, rowStr1, rowStr2, rowStr4, rowStr5))
   446  	insertSQL = `insert into insert_autoinc_test(id, c1) values (null, 6)`
   447  	tk.MustExec(insertSQL)
   448  	r = tk.MustQuery("select * from insert_autoinc_test;")
   449  	rowStr6 = fmt.Sprintf("%v %v", "6", "6")
   450  	r.Check(testkit.Rows(rowStr3, rowStr1, rowStr2, rowStr4, rowStr5, rowStr6))
   451  }
   452  
   453  func (s *testSuite) TestReplace(c *C) {
   454  	defer testleak.AfterTest(c)()
   455  	tk := testkit.NewTestKit(c, s.store)
   456  	tk.MustExec("use test")
   457  	testSQL := `drop table if exists replace_test;
   458      create table replace_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1);`
   459  	tk.MustExec(testSQL)
   460  	testSQL = `replace replace_test (c1) values (1),(2),(NULL);`
   461  	tk.MustExec(testSQL)
   462  
   463  	errReplaceSQL := `replace replace_test (c1) values ();`
   464  	tk.MustExec("begin")
   465  	_, err := tk.Exec(errReplaceSQL)
   466  	c.Assert(err, NotNil)
   467  	tk.MustExec("rollback")
   468  
   469  	errReplaceSQL = `replace replace_test (c1, c2) values (1,2),(1);`
   470  	tk.MustExec("begin")
   471  	_, err = tk.Exec(errReplaceSQL)
   472  	c.Assert(err, NotNil)
   473  	tk.MustExec("rollback")
   474  
   475  	errReplaceSQL = `replace replace_test (xxx) values (3);`
   476  	tk.MustExec("begin")
   477  	_, err = tk.Exec(errReplaceSQL)
   478  	c.Assert(err, NotNil)
   479  	tk.MustExec("rollback")
   480  
   481  	errReplaceSQL = `replace replace_test_xxx (c1) values ();`
   482  	tk.MustExec("begin")
   483  	_, err = tk.Exec(errReplaceSQL)
   484  	c.Assert(err, NotNil)
   485  	tk.MustExec("rollback")
   486  
   487  	replaceSetSQL := `replace replace_test set c1 = 3;`
   488  	tk.MustExec(replaceSetSQL)
   489  
   490  	errReplaceSetSQL := `replace replace_test set c1 = 4, c1 = 5;`
   491  	tk.MustExec("begin")
   492  	_, err = tk.Exec(errReplaceSetSQL)
   493  	c.Assert(err, NotNil)
   494  	tk.MustExec("rollback")
   495  
   496  	errReplaceSetSQL = `replace replace_test set xxx = 6;`
   497  	tk.MustExec("begin")
   498  	_, err = tk.Exec(errReplaceSetSQL)
   499  	c.Assert(err, NotNil)
   500  	tk.MustExec("rollback")
   501  
   502  	replaceSelectSQL := `create table replace_test_1 (id int, c1 int);`
   503  	tk.MustExec(replaceSelectSQL)
   504  	replaceSelectSQL = `replace replace_test_1 select id, c1 from replace_test;`
   505  	tk.MustExec(replaceSelectSQL)
   506  
   507  	replaceSelectSQL = `create table replace_test_2 (id int, c1 int);`
   508  	tk.MustExec(replaceSelectSQL)
   509  	replaceSelectSQL = `replace replace_test_1 select id, c1 from replace_test union select id * 10, c1 * 10 from replace_test;`
   510  	tk.MustExec(replaceSelectSQL)
   511  
   512  	errReplaceSelectSQL := `replace replace_test_1 select c1 from replace_test;`
   513  	tk.MustExec("begin")
   514  	_, err = tk.Exec(errReplaceSelectSQL)
   515  	c.Assert(err, NotNil)
   516  	tk.MustExec("rollback")
   517  
   518  	replaceUniqueIndexSQL := `create table replace_test_3 (c1 int, c2 int, UNIQUE INDEX (c2));`
   519  	tk.MustExec(replaceUniqueIndexSQL)
   520  	replaceUniqueIndexSQL = `replace into replace_test_3 set c2=1;`
   521  	tk.MustExec(replaceUniqueIndexSQL)
   522  	replaceUniqueIndexSQL = `replace into replace_test_3 set c2=1;`
   523  	tk.MustExec(replaceUniqueIndexSQL)
   524  	c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(1))
   525  	replaceUniqueIndexSQL = `replace into replace_test_3 set c1=1, c2=1;`
   526  	tk.MustExec(replaceUniqueIndexSQL)
   527  	c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(2))
   528  
   529  	replaceUniqueIndexSQL = `replace into replace_test_3 set c2=NULL;`
   530  	tk.MustExec(replaceUniqueIndexSQL)
   531  	replaceUniqueIndexSQL = `replace into replace_test_3 set c2=NULL;`
   532  	tk.MustExec(replaceUniqueIndexSQL)
   533  	c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(1))
   534  
   535  	replaceUniqueIndexSQL = `create table replace_test_4 (c1 int, c2 int, c3 int, UNIQUE INDEX (c1, c2));`
   536  	tk.MustExec(replaceUniqueIndexSQL)
   537  	replaceUniqueIndexSQL = `replace into replace_test_4 set c2=NULL;`
   538  	tk.MustExec(replaceUniqueIndexSQL)
   539  	replaceUniqueIndexSQL = `replace into replace_test_4 set c2=NULL;`
   540  	tk.MustExec(replaceUniqueIndexSQL)
   541  	c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(1))
   542  
   543  	replacePrimaryKeySQL := `create table replace_test_5 (c1 int, c2 int, c3 int, PRIMARY KEY (c1, c2));`
   544  	tk.MustExec(replacePrimaryKeySQL)
   545  	replacePrimaryKeySQL = `replace into replace_test_5 set c1=1, c2=2;`
   546  	tk.MustExec(replacePrimaryKeySQL)
   547  	replacePrimaryKeySQL = `replace into replace_test_5 set c1=1, c2=2;`
   548  	tk.MustExec(replacePrimaryKeySQL)
   549  	c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(1))
   550  
   551  	// For Issue989
   552  	issue989SQL := `CREATE TABLE tIssue989 (a int, b int, PRIMARY KEY(a), UNIQUE KEY(b));`
   553  	tk.MustExec(issue989SQL)
   554  	issue989SQL = `insert into tIssue989 (a, b) values (1, 2);`
   555  	tk.MustExec(issue989SQL)
   556  	issue989SQL = `replace into tIssue989(a, b) values (111, 2);`
   557  	tk.MustExec(issue989SQL)
   558  	r := tk.MustQuery("select * from tIssue989;")
   559  	r.Check(testkit.Rows("111 2"))
   560  
   561  	// For Issue1012
   562  	issue1012SQL := `CREATE TABLE tIssue1012 (a int, b int, PRIMARY KEY(a), UNIQUE KEY(b));`
   563  	tk.MustExec(issue1012SQL)
   564  	issue1012SQL = `insert into tIssue1012 (a, b) values (1, 2);`
   565  	tk.MustExec(issue1012SQL)
   566  	issue1012SQL = `insert into tIssue1012 (a, b) values (2, 1);`
   567  	tk.MustExec(issue1012SQL)
   568  	issue1012SQL = `replace into tIssue1012(a, b) values (1, 1);`
   569  	tk.MustExec(issue1012SQL)
   570  	c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(3))
   571  	r = tk.MustQuery("select * from tIssue1012;")
   572  	r.Check(testkit.Rows("1 1"))
   573  }
   574  
   575  func (s *testSuite) TestSelectWithoutFrom(c *C) {
   576  	defer testleak.AfterTest(c)()
   577  	tk := testkit.NewTestKit(c, s.store)
   578  	tk.MustExec("use test")
   579  
   580  	tk.MustExec("begin")
   581  	r := tk.MustQuery("select 1 + 2*3")
   582  	r.Check(testkit.Rows("7"))
   583  	tk.MustExec("commit")
   584  
   585  	tk.MustExec("begin")
   586  	r = tk.MustQuery(`select _utf8"string";`)
   587  	r.Check(testkit.Rows("string"))
   588  	tk.MustExec("commit")
   589  }
   590  
   591  func (s *testSuite) TestSelectLimit(c *C) {
   592  	defer testleak.AfterTest(c)()
   593  	tk := testkit.NewTestKit(c, s.store)
   594  	tk.MustExec("use test")
   595  	s.fillData(tk, "select_limit")
   596  
   597  	tk.MustExec("insert INTO select_limit VALUES (3, \"hello\");")
   598  	tk.CheckExecResult(1, 0)
   599  	tk.MustExec("insert INTO select_limit VALUES (4, \"hello\");")
   600  	tk.CheckExecResult(1, 0)
   601  
   602  	tk.MustExec("begin")
   603  	r := tk.MustQuery("select * from select_limit limit 1;")
   604  	rowStr1 := fmt.Sprintf("%v %v", 1, []byte("hello"))
   605  	r.Check(testkit.Rows(rowStr1))
   606  	tk.MustExec("commit")
   607  
   608  	tk.MustExec("begin")
   609  	r = tk.MustQuery("select * from select_limit limit 18446744073709551615 offset 0;")
   610  	rowStr2 := fmt.Sprintf("%v %v", 2, []byte("hello"))
   611  	rowStr3 := fmt.Sprintf("%v %v", 3, []byte("hello"))
   612  	rowStr4 := fmt.Sprintf("%v %v", 4, []byte("hello"))
   613  	r.Check(testkit.Rows(rowStr1, rowStr2, rowStr3, rowStr4))
   614  	tk.MustExec("commit")
   615  
   616  	tk.MustExec("begin")
   617  	r = tk.MustQuery("select * from select_limit limit 18446744073709551615 offset 1;")
   618  	r.Check(testkit.Rows(rowStr2, rowStr3, rowStr4))
   619  	tk.MustExec("commit")
   620  
   621  	tk.MustExec("begin")
   622  	r = tk.MustQuery("select * from select_limit limit 18446744073709551615 offset 3;")
   623  	r.Check(testkit.Rows(rowStr4))
   624  	tk.MustExec("commit")
   625  
   626  	tk.MustExec("begin")
   627  	_, err := tk.Exec("select * from select_limit limit 18446744073709551616 offset 3;")
   628  	c.Assert(err, NotNil)
   629  	tk.MustExec("rollback")
   630  }
   631  
   632  func (s *testSuite) TestSelectOrderBy(c *C) {
   633  	defer testleak.AfterTest(c)()
   634  	tk := testkit.NewTestKit(c, s.store)
   635  	tk.MustExec("use test")
   636  	s.fillData(tk, "select_order_test")
   637  
   638  	tk.MustExec("begin")
   639  	// Test star field
   640  	r := tk.MustQuery("select * from select_order_test where id = 1 order by id limit 1 offset 0;")
   641  	rowStr := fmt.Sprintf("%v %v", 1, []byte("hello"))
   642  	r.Check(testkit.Rows(rowStr))
   643  	tk.MustExec("commit")
   644  
   645  	tk.MustExec("begin")
   646  	// Test limit
   647  	r = tk.MustQuery("select * from select_order_test order by name, id limit 1 offset 0;")
   648  	rowStr = fmt.Sprintf("%v %v", 1, []byte("hello"))
   649  	r.Check(testkit.Rows(rowStr))
   650  	tk.MustExec("commit")
   651  
   652  	tk.MustExec("begin")
   653  	// Test limit overflow
   654  	r = tk.MustQuery("select * from select_order_test order by name, id limit 100 offset 0;")
   655  	rowStr1 := fmt.Sprintf("%v %v", 1, []byte("hello"))
   656  	rowStr2 := fmt.Sprintf("%v %v", 2, []byte("hello"))
   657  	r.Check(testkit.Rows(rowStr1, rowStr2))
   658  	tk.MustExec("commit")
   659  
   660  	tk.MustExec("begin")
   661  	// Test offset overflow
   662  	r = tk.MustQuery("select * from select_order_test order by name, id limit 1 offset 100;")
   663  	r.Check(testkit.Rows())
   664  	tk.MustExec("commit")
   665  
   666  	tk.MustExec("begin")
   667  	// Test multiple field
   668  	r = tk.MustQuery("select id, name from select_order_test where id = 1 group by id, name limit 1 offset 0;")
   669  	rowStr = fmt.Sprintf("%v %v", 1, []byte("hello"))
   670  	r.Check(testkit.Rows(rowStr))
   671  	tk.MustExec("commit")
   672  
   673  	// Test limit + order by
   674  	tk.MustExec("begin")
   675  	executor.SortBufferSize = 10
   676  	for i := 3; i <= 10; i += 1 {
   677  		tk.MustExec(fmt.Sprintf("insert INTO select_order_test VALUES (%d, \"zz\");", i))
   678  	}
   679  	tk.MustExec("insert INTO select_order_test VALUES (10086, \"hi\");")
   680  	for i := 11; i <= 20; i += 1 {
   681  		tk.MustExec(fmt.Sprintf("insert INTO select_order_test VALUES (%d, \"hh\");", i))
   682  	}
   683  	for i := 21; i <= 30; i += 1 {
   684  		tk.MustExec(fmt.Sprintf("insert INTO select_order_test VALUES (%d, \"zz\");", i))
   685  	}
   686  	tk.MustExec("insert INTO select_order_test VALUES (1501, \"aa\");")
   687  	r = tk.MustQuery("select * from select_order_test order by name, id limit 1 offset 3;")
   688  	rowStr = fmt.Sprintf("%v %v", 11, []byte("hh"))
   689  	r.Check(testkit.Rows(rowStr))
   690  	tk.MustExec("commit")
   691  	executor.SortBufferSize = 500
   692  	tk.MustExec("drop table select_order_test")
   693  }
   694  
   695  func (s *testSuite) TestSelectDistinct(c *C) {
   696  	defer testleak.AfterTest(c)()
   697  	tk := testkit.NewTestKit(c, s.store)
   698  	tk.MustExec("use test")
   699  	s.fillData(tk, "select_distinct_test")
   700  
   701  	tk.MustExec("begin")
   702  	r := tk.MustQuery("select distinct name from select_distinct_test;")
   703  	rowStr := fmt.Sprintf("%v", []byte("hello"))
   704  	r.Check(testkit.Rows(rowStr))
   705  	tk.MustExec("commit")
   706  
   707  	tk.MustExec("drop table select_distinct_test")
   708  }
   709  
   710  func (s *testSuite) TestSelectHaving(c *C) {
   711  	defer testleak.AfterTest(c)()
   712  	tk := testkit.NewTestKit(c, s.store)
   713  	tk.MustExec("use test")
   714  	s.fillData(tk, "select_having_test")
   715  
   716  	tk.MustExec("begin")
   717  	r := tk.MustQuery("select id, name from select_having_test where id in (1,3) having name like 'he%';")
   718  	rowStr := fmt.Sprintf("%v %v", 1, []byte("hello"))
   719  	r.Check(testkit.Rows(rowStr))
   720  	tk.MustExec("commit")
   721  
   722  	r = tk.MustQuery("select * from select_having_test group by id having null is not null;")
   723  
   724  	tk.MustExec("drop table select_having_test")
   725  }
   726  
   727  func (s *testSuite) TestSelectErrorRow(c *C) {
   728  	defer testleak.AfterTest(c)()
   729  	tk := testkit.NewTestKit(c, s.store)
   730  	tk.MustExec("use test")
   731  
   732  	tk.MustExec("begin")
   733  	_, err := tk.Exec("select row(1, 1) from test")
   734  	c.Assert(err, NotNil)
   735  
   736  	_, err = tk.Exec("select * from test group by row(1, 1);")
   737  	c.Assert(err, NotNil)
   738  
   739  	_, err = tk.Exec("select * from test order by row(1, 1);")
   740  	c.Assert(err, NotNil)
   741  
   742  	_, err = tk.Exec("select * from test having row(1, 1);")
   743  	c.Assert(err, NotNil)
   744  
   745  	_, err = tk.Exec("select (select 1, 1) from test;")
   746  	c.Assert(err, NotNil)
   747  
   748  	_, err = tk.Exec("select * from test group by (select 1, 1);")
   749  	c.Assert(err, NotNil)
   750  
   751  	_, err = tk.Exec("select * from test order by (select 1, 1);")
   752  	c.Assert(err, NotNil)
   753  
   754  	_, err = tk.Exec("select * from test having (select 1, 1);")
   755  	c.Assert(err, NotNil)
   756  
   757  	tk.MustExec("commit")
   758  }
   759  
   760  func (s *testSuite) TestUpdate(c *C) {
   761  	defer testleak.AfterTest(c)()
   762  	tk := testkit.NewTestKit(c, s.store)
   763  	tk.MustExec("use test")
   764  	s.fillData(tk, "update_test")
   765  
   766  	updateStr := `UPDATE update_test SET name = "abc" where id > 0;`
   767  	tk.MustExec(updateStr)
   768  	tk.CheckExecResult(2, 0)
   769  
   770  	// select data
   771  	tk.MustExec("begin")
   772  	r := tk.MustQuery(`SELECT * from update_test limit 2;`)
   773  	rowStr1 := fmt.Sprintf("%v %v", 1, []byte("abc"))
   774  	rowStr2 := fmt.Sprintf("%v %v", 2, []byte("abc"))
   775  	r.Check(testkit.Rows(rowStr1, rowStr2))
   776  	tk.MustExec("commit")
   777  
   778  	tk.MustExec(`UPDATE update_test SET name = "foo"`)
   779  	tk.CheckExecResult(2, 0)
   780  
   781  	// table option is auto-increment
   782  	tk.MustExec("begin")
   783  	tk.MustExec("drop table if exists update_test;")
   784  	tk.MustExec("commit")
   785  	tk.MustExec("begin")
   786  	tk.MustExec("create table update_test(id int not null auto_increment, name varchar(255), primary key(id))")
   787  	tk.MustExec("insert into update_test(name) values ('aa')")
   788  	tk.MustExec("update update_test set id = 8 where name = 'aa'")
   789  	tk.MustExec("insert into update_test(name) values ('bb')")
   790  	tk.MustExec("commit")
   791  	tk.MustExec("begin")
   792  	r = tk.MustQuery("select * from update_test;")
   793  	rowStr1 = fmt.Sprintf("%v %v", 8, []byte("aa"))
   794  	rowStr2 = fmt.Sprintf("%v %v", 9, []byte("bb"))
   795  	r.Check(testkit.Rows(rowStr1, rowStr2))
   796  	tk.MustExec("commit")
   797  
   798  	tk.MustExec("begin")
   799  	tk.MustExec("drop table if exists update_test;")
   800  	tk.MustExec("commit")
   801  	tk.MustExec("begin")
   802  	tk.MustExec("create table update_test(id int not null auto_increment, name varchar(255), index(id))")
   803  	tk.MustExec("insert into update_test(name) values ('aa')")
   804  	_, err := tk.Exec("update update_test set id = null where name = 'aa'")
   805  	c.Assert(err, NotNil)
   806  	c.Assert(err.Error(), DeepEquals, "Column 'id' cannot be null")
   807  
   808  	tk.MustExec("drop table update_test")
   809  }
   810  
   811  func (s *testSuite) fillMultiTableForUpdate(tk *testkit.TestKit) {
   812  	// Create and fill table items
   813  	tk.MustExec("CREATE TABLE items (id int, price TEXT);")
   814  	tk.MustExec(`insert into items values (11, "items_price_11"), (12, "items_price_12"), (13, "items_price_13");`)
   815  	tk.CheckExecResult(3, 0)
   816  	// Create and fill table month
   817  	tk.MustExec("CREATE TABLE month (mid int, mprice TEXT);")
   818  	tk.MustExec(`insert into month values (11, "month_price_11"), (22, "month_price_22"), (13, "month_price_13");`)
   819  	tk.CheckExecResult(3, 0)
   820  }
   821  
   822  func (s *testSuite) TestMultipleTableUpdate(c *C) {
   823  	defer testleak.AfterTest(c)()
   824  	tk := testkit.NewTestKit(c, s.store)
   825  	tk.MustExec("use test")
   826  	s.fillMultiTableForUpdate(tk)
   827  
   828  	tk.MustExec(`UPDATE items, month  SET items.price=month.mprice WHERE items.id=month.mid;`)
   829  	tk.MustExec("begin")
   830  	r := tk.MustQuery("SELECT * FROM items")
   831  	rowStr1 := fmt.Sprintf("%v %v", 11, []byte("month_price_11"))
   832  	rowStr2 := fmt.Sprintf("%v %v", 12, []byte("items_price_12"))
   833  	rowStr3 := fmt.Sprintf("%v %v", 13, []byte("month_price_13"))
   834  	r.Check(testkit.Rows(rowStr1, rowStr2, rowStr3))
   835  	tk.MustExec("commit")
   836  
   837  	// Single-table syntax but with multiple tables
   838  	tk.MustExec(`UPDATE items join month on items.id=month.mid SET items.price=month.mid;`)
   839  	tk.MustExec("begin")
   840  	r = tk.MustQuery("SELECT * FROM items")
   841  	rowStr1 = fmt.Sprintf("%v %v", 11, []byte("11"))
   842  	rowStr2 = fmt.Sprintf("%v %v", 12, []byte("items_price_12"))
   843  	rowStr3 = fmt.Sprintf("%v %v", 13, []byte("13"))
   844  	r.Check(testkit.Rows(rowStr1, rowStr2, rowStr3))
   845  	tk.MustExec("commit")
   846  
   847  	// JoinTable with alias table name.
   848  	tk.MustExec(`UPDATE items T0 join month T1 on T0.id=T1.mid SET T0.price=T1.mprice;`)
   849  	tk.MustExec("begin")
   850  	r = tk.MustQuery("SELECT * FROM items")
   851  	rowStr1 = fmt.Sprintf("%v %v", 11, []byte("month_price_11"))
   852  	rowStr2 = fmt.Sprintf("%v %v", 12, []byte("items_price_12"))
   853  	rowStr3 = fmt.Sprintf("%v %v", 13, []byte("month_price_13"))
   854  	r.Check(testkit.Rows(rowStr1, rowStr2, rowStr3))
   855  	tk.MustExec("commit")
   856  }
   857  
   858  // For https://yougam/libraries/pingcap/tidb/issues/345
   859  func (s *testSuite) TestIssue345(c *C) {
   860  	defer testleak.AfterTest(c)()
   861  	tk := testkit.NewTestKit(c, s.store)
   862  	tk.MustExec("use test")
   863  	tk.MustExec(`drop table if exists t1, t2`)
   864  	tk.MustExec(`create table t1 (c1 int);`)
   865  	tk.MustExec(`create table t2 (c2 int);`)
   866  	tk.MustExec(`insert into t1 values (1);`)
   867  	tk.MustExec(`insert into t2 values (2);`)
   868  	tk.MustExec(`update t1, t2 set t1.c1 = 2, t2.c2 = 1;`)
   869  	tk.MustExec(`update t1, t2 set c1 = 2, c2 = 1;`)
   870  	tk.MustExec(`update t1 as a, t2 as b set a.c1 = 2, b.c2 = 1;`)
   871  
   872  	// Check t1 content
   873  	tk.MustExec("begin")
   874  	r := tk.MustQuery("SELECT * FROM t1;")
   875  	r.Check(testkit.Rows("2"))
   876  	tk.MustExec("commit")
   877  	// Check t2 content
   878  	tk.MustExec("begin")
   879  	r = tk.MustQuery("SELECT * FROM t2;")
   880  	r.Check(testkit.Rows("1"))
   881  	tk.MustExec("commit")
   882  
   883  	tk.MustExec(`update t1 as a, t2 as t1 set a.c1 = 1, t1.c2 = 2;`)
   884  	// Check t1 content
   885  	tk.MustExec("begin")
   886  	r = tk.MustQuery("SELECT * FROM t1;")
   887  	r.Check(testkit.Rows("1"))
   888  	tk.MustExec("commit")
   889  	// Check t2 content
   890  	tk.MustExec("begin")
   891  	r = tk.MustQuery("SELECT * FROM t2;")
   892  	r.Check(testkit.Rows("2"))
   893  
   894  	_, err := tk.Exec(`update t1 as a, t2 set t1.c1 = 10;`)
   895  	c.Assert(err, NotNil)
   896  
   897  	tk.MustExec("commit")
   898  }
   899  
   900  func (s *testSuite) TestMultiUpdate(c *C) {
   901  	defer testleak.AfterTest(c)()
   902  	tk := testkit.NewTestKit(c, s.store)
   903  	tk.MustExec("use test")
   904  	// fix https://yougam/libraries/pingcap/tidb/issues/369
   905  	testSQL := `
   906  		DROP TABLE IF EXISTS t1, t2;
   907  		create table t1 (c int);
   908  		create table t2 (c varchar(256));
   909  		insert into t1 values (1), (2);
   910  		insert into t2 values ("a"), ("b");
   911  		update t1, t2 set t1.c = 10, t2.c = "abc";`
   912  	tk.MustExec(testSQL)
   913  
   914  	// fix https://yougam/libraries/pingcap/tidb/issues/376
   915  	testSQL = `DROP TABLE IF EXISTS t1, t2;
   916  		create table t1 (c1 int);
   917  		create table t2 (c2 int);
   918  		insert into t1 values (1), (2);
   919  		insert into t2 values (1), (2);
   920  		update t1, t2 set t1.c1 = 10, t2.c2 = 2 where t2.c2 = 1;`
   921  	tk.MustExec(testSQL)
   922  
   923  	r := tk.MustQuery("select * from t1")
   924  	r.Check(testkit.Rows("10", "10"))
   925  }
   926  
   927  func (s *testSuite) TestUnion(c *C) {
   928  	defer testleak.AfterTest(c)()
   929  	tk := testkit.NewTestKit(c, s.store)
   930  	tk.MustExec("use test")
   931  	testSQL := `select 1 union select 0;`
   932  	tk.MustExec(testSQL)
   933  
   934  	testSQL = `drop table if exists union_test; create table union_test(id int);`
   935  	tk.MustExec(testSQL)
   936  
   937  	testSQL = `drop table if exists union_test;`
   938  	tk.MustExec(testSQL)
   939  	testSQL = `create table union_test(id int);`
   940  	tk.MustExec(testSQL)
   941  	testSQL = `insert union_test values (1),(2); select id from union_test union select 1;`
   942  	tk.MustExec(testSQL)
   943  
   944  	testSQL = `select id from union_test union select id from union_test;`
   945  	tk.MustExec("begin")
   946  	r := tk.MustQuery(testSQL)
   947  	r.Check(testkit.Rows("1", "2"))
   948  
   949  	testSQL = `select * from (select id from union_test union select id from union_test) t;`
   950  	tk.MustExec("begin")
   951  	r = tk.MustQuery(testSQL)
   952  	r.Check(testkit.Rows("1", "2"))
   953  
   954  	r = tk.MustQuery("select 1 union all select 1")
   955  	r.Check(testkit.Rows("1", "1"))
   956  
   957  	r = tk.MustQuery("select 1 union all select 1 union select 1")
   958  	r.Check(testkit.Rows("1"))
   959  
   960  	r = tk.MustQuery("select 1 union (select 2) limit 1")
   961  	r.Check(testkit.Rows("1"))
   962  
   963  	r = tk.MustQuery("select 1 union (select 2) limit 1, 1")
   964  	r.Check(testkit.Rows("2"))
   965  
   966  	r = tk.MustQuery("select id from union_test union all (select 1) order by id desc")
   967  	r.Check(testkit.Rows("2", "1", "1"))
   968  
   969  	r = tk.MustQuery("select id as a from union_test union (select 1) order by a desc")
   970  	r.Check(testkit.Rows("2", "1"))
   971  
   972  	r = tk.MustQuery(`select null union select "abc"`)
   973  	rowStr1 := fmt.Sprintf("%v", nil)
   974  	r.Check(testkit.Rows(rowStr1, "abc"))
   975  
   976  	r = tk.MustQuery(`select "abc" union select 1`)
   977  	r.Check(testkit.Rows("abc", "1"))
   978  
   979  	tk.MustExec("commit")
   980  }
   981  
   982  func (s *testSuite) TestTablePKisHandleScan(c *C) {
   983  	defer testleak.AfterTest(c)()
   984  	tk := testkit.NewTestKit(c, s.store)
   985  	tk.MustExec("use test")
   986  	tk.MustExec("drop table if exists t")
   987  	tk.MustExec("create table t (a int PRIMARY KEY AUTO_INCREMENT)")
   988  	tk.MustExec("insert t values (),()")
   989  	tk.MustExec("insert t values (-100),(0)")
   990  
   991  	cases := []struct {
   992  		sql    string
   993  		result [][]interface{}
   994  	}{
   995  		{
   996  			"select * from t",
   997  			testkit.Rows("-100", "1", "2", "3"),
   998  		},
   999  		{
  1000  			"select * from t where a = 1",
  1001  			testkit.Rows("1"),
  1002  		},
  1003  		{
  1004  			"select * from t where a != 1",
  1005  			testkit.Rows("-100", "2", "3"),
  1006  		},
  1007  		{
  1008  			"select * from t where a >= '1.1'",
  1009  			testkit.Rows("2", "3"),
  1010  		},
  1011  		{
  1012  			"select * from t where a < '1.1'",
  1013  			testkit.Rows("-100", "1"),
  1014  		},
  1015  		{
  1016  			"select * from t where a > '-100.1' and a < 2",
  1017  			testkit.Rows("-100", "1"),
  1018  		},
  1019  		{
  1020  			"select * from t where a is null",
  1021  			testkit.Rows(),
  1022  		}, {
  1023  			"select * from t where a is true",
  1024  			testkit.Rows("-100", "1", "2", "3"),
  1025  		}, {
  1026  			"select * from t where a is false",
  1027  			testkit.Rows(),
  1028  		},
  1029  		{
  1030  			"select * from t where a in (1, 2)",
  1031  			testkit.Rows("1", "2"),
  1032  		},
  1033  		{
  1034  			"select * from t where a between 1 and 2",
  1035  			testkit.Rows("1", "2"),
  1036  		},
  1037  	}
  1038  
  1039  	for _, ca := range cases {
  1040  		result := tk.MustQuery(ca.sql)
  1041  		result.Check(ca.result)
  1042  	}
  1043  }
  1044  
  1045  func (s *testSuite) TestJoin(c *C) {
  1046  	defer testleak.AfterTest(c)()
  1047  	tk := testkit.NewTestKit(c, s.store)
  1048  	tk.MustExec("use test")
  1049  	tk.MustExec("drop table if exists t")
  1050  	tk.MustExec("create table t (c int)")
  1051  	tk.MustExec("insert t values (1)")
  1052  	cases := []struct {
  1053  		sql    string
  1054  		result [][]interface{}
  1055  	}{
  1056  		{
  1057  			"select 1 from t as a left join t as b on 0",
  1058  			testkit.Rows("1"),
  1059  		},
  1060  		{
  1061  			"select 1 from t as a join t as b on 1",
  1062  			testkit.Rows("1"),
  1063  		},
  1064  	}
  1065  	for _, ca := range cases {
  1066  		result := tk.MustQuery(ca.sql)
  1067  		result.Check(ca.result)
  1068  	}
  1069  
  1070  	tk.MustExec("drop table if exists t")
  1071  	tk.MustExec("drop table if exists t1")
  1072  	tk.MustExec("create table t(c1 int, c2 int)")
  1073  	tk.MustExec("create table t1(c1 int, c2 int)")
  1074  	tk.MustExec("insert into t values(1,1),(2,2)")
  1075  	tk.MustExec("insert into t1 values(2,3),(4,4)")
  1076  	result := tk.MustQuery("select * from t left outer join t1 on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
  1077  	result.Check(testkit.Rows("1 1 <nil> <nil>"))
  1078  	result = tk.MustQuery("select * from t1 right outer join t on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
  1079  	result.Check(testkit.Rows("<nil> <nil> 1 1"))
  1080  	result = tk.MustQuery("select * from t right outer join t1 on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
  1081  	result.Check(testkit.Rows())
  1082  	result = tk.MustQuery("select * from t left outer join t1 on t.c1 = t1.c1 where t1.c1 = 3 or false")
  1083  	result.Check(testkit.Rows())
  1084  	result = tk.MustQuery("select * from t left outer join t1 on t.c1 = t1.c1 and t.c1 != 1")
  1085  	result.Check(testkit.Rows("1 1 <nil> <nil>", "2 2 2 3"))
  1086  
  1087  	tk.MustExec("drop table if exists t1")
  1088  	tk.MustExec("drop table if exists t2")
  1089  	tk.MustExec("drop table if exists t3")
  1090  
  1091  	tk.MustExec("create table t1 (c1 int, c2 int)")
  1092  	tk.MustExec("create table t2 (c1 int, c2 int)")
  1093  	tk.MustExec("create table t3 (c1 int, c2 int)")
  1094  
  1095  	tk.MustExec("insert into t1 values (1,1), (2,2), (3,3)")
  1096  	tk.MustExec("insert into t2 values (1,1), (3,3), (5,5)")
  1097  	tk.MustExec("insert into t3 values (1,1), (5,5), (9,9)")
  1098  
  1099  	result = tk.MustQuery("select * from t1 left join t2 on t1.c1 = t2.c1 right join t3 on t2.c1 = t3.c1 order by t1.c1, t1.c2, t2.c1, t2.c2, t3.c1, t3.c2;")
  1100  	result.Check(testkit.Rows("<nil> <nil> <nil> <nil> 5 5", "<nil> <nil> <nil> <nil> 9 9", "1 1 1 1 1 1"))
  1101  }
  1102  
  1103  func (s *testSuite) TestNewJoin(c *C) {
  1104  	plan.UseNewPlanner = true
  1105  	defer testleak.AfterTest(c)()
  1106  	tk := testkit.NewTestKit(c, s.store)
  1107  	tk.MustExec("use test")
  1108  	tk.MustExec("drop table if exists t")
  1109  	tk.MustExec("create table t (c int)")
  1110  	tk.MustExec("insert t values (1)")
  1111  	cases := []struct {
  1112  		sql    string
  1113  		result [][]interface{}
  1114  	}{
  1115  		{
  1116  			"select 1 from t as a left join t as b on 0",
  1117  			testkit.Rows("1"),
  1118  		},
  1119  		{
  1120  			"select 1 from t as a join t as b on 1",
  1121  			testkit.Rows("1"),
  1122  		},
  1123  	}
  1124  	for _, ca := range cases {
  1125  		result := tk.MustQuery(ca.sql)
  1126  		result.Check(ca.result)
  1127  	}
  1128  
  1129  	tk.MustExec("drop table if exists t")
  1130  	tk.MustExec("drop table if exists t1")
  1131  	tk.MustExec("create table t(c1 int, c2 int)")
  1132  	tk.MustExec("create table t1(c1 int, c2 int)")
  1133  	tk.MustExec("insert into t values(1,1),(2,2)")
  1134  	tk.MustExec("insert into t1 values(2,3),(4,4)")
  1135  	result := tk.MustQuery("select * from t left outer join t1 on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
  1136  	result.Check(testkit.Rows("1 1 <nil> <nil>"))
  1137  	result = tk.MustQuery("select * from t1 right outer join t on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
  1138  	result.Check(testkit.Rows("<nil> <nil> 1 1"))
  1139  	result = tk.MustQuery("select * from t right outer join t1 on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
  1140  	result.Check(testkit.Rows())
  1141  	result = tk.MustQuery("select * from t left outer join t1 on t.c1 = t1.c1 where t1.c1 = 3 or false")
  1142  	result.Check(testkit.Rows())
  1143  	result = tk.MustQuery("select * from t left outer join t1 on t.c1 = t1.c1 and t.c1 != 1")
  1144  	result.Check(testkit.Rows("1 1 <nil> <nil>", "2 2 2 3"))
  1145  
  1146  	tk.MustExec("drop table if exists t1")
  1147  	tk.MustExec("drop table if exists t2")
  1148  	tk.MustExec("drop table if exists t3")
  1149  
  1150  	tk.MustExec("create table t1 (c1 int, c2 int)")
  1151  	tk.MustExec("create table t2 (c1 int, c2 int)")
  1152  	tk.MustExec("create table t3 (c1 int, c2 int)")
  1153  
  1154  	tk.MustExec("insert into t1 values (1,1), (2,2), (3,3)")
  1155  	tk.MustExec("insert into t2 values (1,1), (3,3), (5,5)")
  1156  	tk.MustExec("insert into t3 values (1,1), (5,5), (9,9)")
  1157  
  1158  	result = tk.MustQuery("select * from t1 left join t2 on t1.c1 = t2.c1 right join t3 on t2.c1 = t3.c1 order by t1.c1, t1.c2, t2.c1, t2.c2, t3.c1, t3.c2;")
  1159  	result.Check(testkit.Rows("<nil> <nil> <nil> <nil> 5 5", "<nil> <nil> <nil> <nil> 9 9", "1 1 1 1 1 1"))
  1160  
  1161  	tk.MustExec("drop table if exists t1")
  1162  	tk.MustExec("create table t1 (c1 int)")
  1163  	tk.MustExec("insert into t1 values (1), (1), (1)")
  1164  	result = tk.MustQuery("select * from t1 a join t1 b on a.c1 = b.c1;")
  1165  	result.Check(testkit.Rows("1 1", "1 1", "1 1", "1 1", "1 1", "1 1", "1 1", "1 1", "1 1"))
  1166  
  1167  	plan.UseNewPlanner = false
  1168  }
  1169  
  1170  func (s *testSuite) TestIndexScan(c *C) {
  1171  	defer testleak.AfterTest(c)()
  1172  	tk := testkit.NewTestKit(c, s.store)
  1173  	tk.MustExec("use test")
  1174  	tk.MustExec("drop table if exists t")
  1175  	tk.MustExec("create table t (a int unique)")
  1176  	tk.MustExec("insert t values (-1), (2), (3), (5), (6), (7), (8), (9)")
  1177  	result := tk.MustQuery("select a from t where a < 0 or (a >= 2.1 and a < 5.1) or ( a > 5.9 and a <= 7.9) or a > '8.1'")
  1178  	result.Check(testkit.Rows("-1", "3", "5", "6", "7", "9"))
  1179  }
  1180  
  1181  func (s *testSuite) TestSubquerySameTable(c *C) {
  1182  	defer testleak.AfterTest(c)()
  1183  	tk := testkit.NewTestKit(c, s.store)
  1184  	tk.MustExec("use test")
  1185  	tk.MustExec("drop table if exists t")
  1186  	tk.MustExec("create table t (a int)")
  1187  	tk.MustExec("insert t values (1), (2)")
  1188  	result := tk.MustQuery("select a from t where exists(select 1 from t as x where x.a < t.a)")
  1189  	result.Check(testkit.Rows("2"))
  1190  }
  1191  
  1192  func (s *testSuite) TestIndexReverseOrder(c *C) {
  1193  	defer testleak.AfterTest(c)()
  1194  	tk := testkit.NewTestKit(c, s.store)
  1195  	tk.MustExec("use test")
  1196  	tk.MustExec("drop table if exists t")
  1197  	tk.MustExec("create table t (a int primary key auto_increment, b int, index idx (b))")
  1198  	tk.MustExec("insert t (b) values (0), (1), (2), (3), (4), (5), (6), (7), (8), (9)")
  1199  	result := tk.MustQuery("select b from t order by b desc")
  1200  	result.Check(testkit.Rows("9", "8", "7", "6", "5", "4", "3", "2", "1", "0"))
  1201  	result = tk.MustQuery("select b from t where b <3 or (b >=6 and b < 8) order by b desc")
  1202  	result.Check(testkit.Rows("7", "6", "2", "1", "0"))
  1203  
  1204  	tk.MustExec("drop table if exists t")
  1205  	tk.MustExec("create table t (a int, b int, index idx (b, a))")
  1206  	tk.MustExec("insert t values (0, 2), (1, 2), (2, 2), (0, 1), (1, 1), (2, 1), (0, 0), (1, 0), (2, 0)")
  1207  	result = tk.MustQuery("select b, a from t order by b, a desc")
  1208  	result.Check(testkit.Rows("0 2", "0 1", "0 0", "1 2", "1 1", "1 0", "2 2", "2 1", "2 0"))
  1209  }
  1210  
  1211  func (s *testSuite) TestTableReverseOrder(c *C) {
  1212  	defer testleak.AfterTest(c)()
  1213  	tk := testkit.NewTestKit(c, s.store)
  1214  	tk.MustExec("use test")
  1215  	tk.MustExec("drop table if exists t")
  1216  	tk.MustExec("create table t (a int primary key auto_increment, b int)")
  1217  	tk.MustExec("insert t (b) values (1), (2), (3), (4), (5), (6), (7), (8), (9)")
  1218  	result := tk.MustQuery("select b from t order by a desc")
  1219  	result.Check(testkit.Rows("9", "8", "7", "6", "5", "4", "3", "2", "1"))
  1220  	result = tk.MustQuery("select a from t where a <3 or (a >=6 and a < 8) order by a desc")
  1221  	result.Check(testkit.Rows("7", "6", "2", "1"))
  1222  }
  1223  
  1224  func (s *testSuite) TestInSubquery(c *C) {
  1225  	defer testleak.AfterTest(c)()
  1226  	tk := testkit.NewTestKit(c, s.store)
  1227  	tk.MustExec("use test")
  1228  	tk.MustExec("drop table if exists t")
  1229  	tk.MustExec("create table t (a int, b int)")
  1230  	tk.MustExec("insert t values (1, 1), (2, 1)")
  1231  	result := tk.MustQuery("select m1.a from t as m1 where m1.a in (select m2.b from t as m2)")
  1232  	result.Check(testkit.Rows("1"))
  1233  	result = tk.MustQuery("select m1.a from t as m1 where m1.a in (select m2.b+? from t as m2)", 1)
  1234  	result.Check(testkit.Rows("2"))
  1235  	tk.MustExec(`prepare stmt1 from 'select m1.a from t as m1 where m1.a in (select m2.b+? from t as m2)'`)
  1236  	tk.MustExec("set @a = 1")
  1237  	result = tk.MustQuery(`execute stmt1 using @a;`)
  1238  	result.Check(testkit.Rows("2"))
  1239  	tk.MustExec("set @a = 0")
  1240  	result = tk.MustQuery(`execute stmt1 using @a;`)
  1241  	result.Check(testkit.Rows("1"))
  1242  
  1243  	result = tk.MustQuery("select m1.a from t as m1 where m1.a in (1, 3, 5)")
  1244  	result.Check(testkit.Rows("1"))
  1245  
  1246  	tk.MustExec("drop table if exists t1")
  1247  	tk.MustExec("create table t1 (a float)")
  1248  	tk.MustExec("insert t1 values (281.37)")
  1249  	tk.MustQuery("select a from t1 where (a in (select a from t1))").Check(testkit.Rows("281.37"))
  1250  }
  1251  
  1252  func (s *testSuite) TestDefaultNull(c *C) {
  1253  	defer testleak.AfterTest(c)()
  1254  	tk := testkit.NewTestKit(c, s.store)
  1255  	tk.MustExec("use test")
  1256  	tk.MustExec("drop table if exists t")
  1257  	tk.MustExec("create table t (a int primary key auto_increment, b int default 1, c int)")
  1258  	tk.MustExec("insert t values ()")
  1259  	tk.MustQuery("select * from t").Check(testkit.Rows("1 1 <nil>"))
  1260  	tk.MustExec("update t set b = NULL where a = 1")
  1261  	tk.MustQuery("select * from t").Check(testkit.Rows("1 <nil> <nil>"))
  1262  	tk.MustExec("update t set c = 1")
  1263  	tk.MustQuery("select * from t ").Check(testkit.Rows("1 <nil> 1"))
  1264  	tk.MustExec("delete from t where a = 1")
  1265  	tk.MustExec("insert t (a) values (1)")
  1266  	tk.MustQuery("select * from t").Check(testkit.Rows("1 1 <nil>"))
  1267  }
  1268  
  1269  func (s *testSuite) TestUsignedPKColumn(c *C) {
  1270  	defer testleak.AfterTest(c)()
  1271  	tk := testkit.NewTestKit(c, s.store)
  1272  	tk.MustExec("use test")
  1273  	tk.MustExec("drop table if exists t")
  1274  	tk.MustExec("create table t (a int unsigned primary key, b int, c int, key idx_ba (b, c, a));")
  1275  	tk.MustExec("insert t values (1, 1, 1)")
  1276  	result := tk.MustQuery("select * from t;")
  1277  	result.Check(testkit.Rows("1 1 1"))
  1278  	tk.MustExec("update t set c=2 where a=1;")
  1279  	result = tk.MustQuery("select * from t where b=1;")
  1280  	result.Check(testkit.Rows("1 1 2"))
  1281  }