From 0182b65f5f238f1dc859735f10c361aa85cf540e Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 12 May 2022 16:24:35 +0800 Subject: [PATCH] session: migrate test-infra to testify for parts of session_test (#34594) ref pingcap/tidb#28328 --- session/session_legacy_test.go | 365 -------------------------- tests/realtikvtest/session_test.go | 398 +++++++++++++++++++++++++++++ 2 files changed, 398 insertions(+), 365 deletions(-) diff --git a/session/session_legacy_test.go b/session/session_legacy_test.go index 90466835b560c..e3225a701f9a1 100644 --- a/session/session_legacy_test.go +++ b/session/session_legacy_test.go @@ -1154,42 +1154,6 @@ func (s *testSessionSuite) TestLastInsertID(c *C) { c.Assert(lastInsertID+2, Equals, currLastInsertID) } -func (s *testSessionSuite) TestPrepareZero(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(v timestamp)") - tk.MustExec("prepare s1 from 'insert into t (v) values (?)'") - tk.MustExec("set @v1='0'") - _, rs := tk.Exec("execute s1 using @v1") - c.Assert(rs, NotNil) - tk.MustExec("set @v2='" + types.ZeroDatetimeStr + "'") - tk.MustExec("set @orig_sql_mode=@@sql_mode; set @@sql_mode='';") - tk.MustExec("execute s1 using @v2") - tk.MustQuery("select v from t").Check(testkit.Rows("0000-00-00 00:00:00")) - tk.MustExec("set @@sql_mode=@orig_sql_mode;") -} - -func (s *testSessionSuite) TestPrimaryKeyAutoIncrement(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL, name varchar(255) UNIQUE NOT NULL, status int)") - tk.MustExec("insert t (name) values (?)", "abc") - id := tk.Se.LastInsertID() - c.Check(id != 0, IsTrue) - - tk1 := testkit.NewTestKitWithInit(c, s.store) - tk1.MustQuery("select * from t").Check(testkit.Rows(fmt.Sprintf("%d abc ", id))) - - tk.MustExec("update t set name = 'abc', status = 1 where id = ?", id) - tk1.MustQuery("select * from t").Check(testkit.Rows(fmt.Sprintf("%d abc 1", id))) - - // Check for pass bool param to tidb prepared statement - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id tinyint)") - tk.MustExec("insert t values (?)", true) - tk.MustQuery("select * from t").Check(testkit.Rows("1")) -} - func (s *testSessionSuite) TestAutoIncrementID(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) tk.MustExec("drop table if exists t") @@ -2419,53 +2383,6 @@ func (s *testSessionSerialSuite) TestTxnRetryErrMsg(c *C) { c.Assert(strings.Contains(err.Error(), kv.TxnRetryableMark), IsTrue, Commentf("error: %s", err)) } -// TestSetGroupConcatMaxLen is for issue #7034 -func (s *testSessionSuite2) TestSetGroupConcatMaxLen(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - - // Normal case - tk.MustExec("set global group_concat_max_len = 100") - tk.MustExec("set @@session.group_concat_max_len = 50") - result := tk.MustQuery("show global variables where variable_name='group_concat_max_len';") - result.Check(testkit.Rows("group_concat_max_len 100")) - - result = tk.MustQuery("show session variables where variable_name='group_concat_max_len';") - result.Check(testkit.Rows("group_concat_max_len 50")) - - result = tk.MustQuery("select @@group_concat_max_len;") - result.Check(testkit.Rows("50")) - - result = tk.MustQuery("select @@global.group_concat_max_len;") - result.Check(testkit.Rows("100")) - - result = tk.MustQuery("select @@session.group_concat_max_len;") - result.Check(testkit.Rows("50")) - - tk.MustExec("set @@group_concat_max_len = 1024") - - result = tk.MustQuery("select @@group_concat_max_len;") - result.Check(testkit.Rows("1024")) - - result = tk.MustQuery("select @@global.group_concat_max_len;") - result.Check(testkit.Rows("100")) - - result = tk.MustQuery("select @@session.group_concat_max_len;") - result.Check(testkit.Rows("1024")) - - // Test value out of range - tk.MustExec("set @@group_concat_max_len=1") - tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning|1292|Truncated incorrect group_concat_max_len value: '1'")) - result = tk.MustQuery("select @@group_concat_max_len;") - result.Check(testkit.Rows("4")) - - _, err := tk.Exec("set @@group_concat_max_len = 18446744073709551616") - c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue, Commentf("err %v", err)) - - // Test illegal type - _, err = tk.Exec("set @@group_concat_max_len='hello'") - c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue, Commentf("err %v", err)) -} - func (s *testSessionSuite2) TestUpdatePrivilege(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) tk.MustExec("drop table if exists t1, t2;") @@ -3735,120 +3652,6 @@ func (s *testSessionSuite) TestLocalTemporaryTableInsert(c *C) { tk.MustQuery("select * from tmp1 where id=5").Check(testkit.Rows()) } -func (s *testSessionSuite) TestLocalTemporaryTableInsertIgnore(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("create temporary table tmp1 (id int primary key auto_increment, u int unique, v int)") - tk.MustExec("insert into tmp1 values(1, 11, 101)") - tk.MustExec("insert into tmp1 values(2, 12, 102)") - - // test outside transaction - tk.MustExec("insert ignore into tmp1 values(1, 100, 1000)") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '1' for key 'PRIMARY'")) - tk.MustQuery("select * from tmp1 where id=1").Check(testkit.Rows("1 11 101")) - tk.MustExec("insert ignore into tmp1 values(5, 15, 105)") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select * from tmp1 where id=5").Check(testkit.Rows("5 15 105")) - - // test in transaction and rollback - tk.MustExec("begin") - tk.MustExec("insert ignore into tmp1 values(1, 100, 1000)") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '1' for key 'PRIMARY'")) - tk.MustQuery("select * from tmp1 where id=1").Check(testkit.Rows("1 11 101")) - tk.MustExec("insert ignore into tmp1 values(3, 13, 103)") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select * from tmp1 where id=3").Check(testkit.Rows("3 13 103")) - tk.MustExec("insert ignore into tmp1 values(3, 100, 1000)") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '3' for key 'PRIMARY'")) - tk.MustQuery("select * from tmp1 where id=3").Check(testkit.Rows("3 13 103")) - tk.MustExec("rollback") - tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 11 101", "2 12 102", "5 15 105")) - - // test commit - tk.MustExec("begin") - tk.MustExec("insert ignore into tmp1 values(1, 100, 1000)") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '1' for key 'PRIMARY'")) - tk.MustExec("insert ignore into tmp1 values(3, 13, 103)") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustExec("insert ignore into tmp1 values(3, 100, 1000)") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '3' for key 'PRIMARY'")) - tk.MustExec("commit") - tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 11 101", "2 12 102", "3 13 103", "5 15 105")) -} - -func (s *testSessionSuite) TestLocalTemporaryTableInsertOnDuplicateKeyUpdate(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("create temporary table tmp1 (id int primary key auto_increment, u int unique, v int)") - tk.MustExec("insert into tmp1 values(1, 11, 101)") - tk.MustExec("insert into tmp1 values(2, 12, 102)") - - // test outside transaction - tk.MustExec("insert ignore into tmp1 values(1, 100, 1000) on duplicate key update u=12") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '12' for key 'u'")) - tk.MustQuery("select * from tmp1 where id=1").Check(testkit.Rows("1 11 101")) - tk.MustExec("insert into tmp1 values(2, 100, 1000) on duplicate key update v=202") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select * from tmp1 where id=2").Check(testkit.Rows("2 12 202")) - tk.MustExec("insert into tmp1 values(3, 13, 103) on duplicate key update v=203") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select * from tmp1 where id=3").Check(testkit.Rows("3 13 103")) - - // test in transaction and rollback - tk.MustExec("begin") - tk.MustExec("insert ignore into tmp1 values(1, 100, 1000) on duplicate key update u=12") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '12' for key 'u'")) - tk.MustQuery("select * from tmp1 where id=1").Check(testkit.Rows("1 11 101")) - tk.MustExec("insert into tmp1 values(2, 100, 1000) on duplicate key update v=302") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select * from tmp1 where id=2").Check(testkit.Rows("2 12 302")) - tk.MustExec("insert into tmp1 values(4, 14, 104) on duplicate key update v=204") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select * from tmp1 where id=4").Check(testkit.Rows("4 14 104")) - tk.MustExec("rollback") - tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 11 101", "2 12 202", "3 13 103")) - - // test commit - tk.MustExec("begin") - tk.MustExec("insert ignore into tmp1 values(1, 100, 1000) on duplicate key update u=12") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '12' for key 'u'")) - tk.MustExec("insert into tmp1 values(2, 100, 1000) on duplicate key update v=302") - tk.MustExec("insert into tmp1 values(4, 14, 104) on duplicate key update v=204") - tk.MustExec("commit") - tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 11 101", "2 12 302", "3 13 103", "4 14 104")) -} - -func (s *testSessionSuite) TestLocalTemporaryTableReplace(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("create temporary table tmp1 (id int primary key auto_increment, u int unique, v int)") - tk.MustExec("insert into tmp1 values(1, 11, 101)") - tk.MustExec("insert into tmp1 values(2, 12, 102)") - tk.MustExec("insert into tmp1 values(3, 13, 103)") - - // out of transaction - tk.MustExec("replace into tmp1 values(1, 12, 1000)") - tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 12 1000", "3 13 103")) - tk.MustExec("replace into tmp1 values(4, 14, 104)") - tk.MustQuery("select * from tmp1 where id=4").Check(testkit.Rows("4 14 104")) - - // in transaction and rollback - tk.MustExec("begin") - tk.MustExec("replace into tmp1 values(1, 13, 999)") - tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 13 999", "4 14 104")) - tk.MustExec("replace into tmp1 values(5, 15, 105)") - tk.MustQuery("select * from tmp1 where id=5").Check(testkit.Rows("5 15 105")) - tk.MustExec("rollback") - tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 12 1000", "3 13 103", "4 14 104")) - - // out of transaction - tk.MustExec("begin") - tk.MustExec("replace into tmp1 values(1, 13, 999)") - tk.MustExec("replace into tmp1 values(5, 15, 105)") - tk.MustExec("commit") - tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 13 999", "4 14 104", "5 15 105")) -} - func (s *testSessionSuite) TestLocalTemporaryTableDelete(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") @@ -3941,174 +3744,6 @@ func (s *testSessionSuite) TestLocalTemporaryTableDelete(c *C) { assertDelete("delete from tmp1 where v>=1006 or v<=1002", []int{1, 2, 6, 7, 8, 9}) } -func (s *testSessionSuite) TestLocalTemporaryTablePointGet(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("create temporary table tmp1 (id int primary key auto_increment, u int unique, v int)") - tk.MustExec("insert into tmp1 values(1, 11, 101)") - tk.MustExec("insert into tmp1 values(2, 12, 102)") - tk.MustExec("insert into tmp1 values(4, 14, 104)") - - // check point get out transaction - tk.MustQuery("select * from tmp1 where id=1").Check(testkit.Rows("1 11 101")) - tk.MustQuery("select * from tmp1 where u=11").Check(testkit.Rows("1 11 101")) - tk.MustQuery("select * from tmp1 where id=2").Check(testkit.Rows("2 12 102")) - tk.MustQuery("select * from tmp1 where u=12").Check(testkit.Rows("2 12 102")) - - // check point get in transaction - tk.MustExec("begin") - tk.MustQuery("select * from tmp1 where id=1").Check(testkit.Rows("1 11 101")) - tk.MustQuery("select * from tmp1 where u=11").Check(testkit.Rows("1 11 101")) - tk.MustQuery("select * from tmp1 where id=2").Check(testkit.Rows("2 12 102")) - tk.MustQuery("select * from tmp1 where u=12").Check(testkit.Rows("2 12 102")) - tk.MustExec("insert into tmp1 values(3, 13, 103)") - tk.MustQuery("select * from tmp1 where id=3").Check(testkit.Rows("3 13 103")) - tk.MustQuery("select * from tmp1 where u=13").Check(testkit.Rows("3 13 103")) - tk.MustExec("update tmp1 set v=999 where id=2") - tk.MustQuery("select * from tmp1 where id=2").Check(testkit.Rows("2 12 999")) - tk.MustExec("delete from tmp1 where id=4") - tk.MustQuery("select * from tmp1 where id=4").Check(testkit.Rows()) - tk.MustQuery("select * from tmp1 where u=14").Check(testkit.Rows()) - tk.MustExec("commit") - - // check point get after transaction - tk.MustQuery("select * from tmp1 where id=3").Check(testkit.Rows("3 13 103")) - tk.MustQuery("select * from tmp1 where u=13").Check(testkit.Rows("3 13 103")) - tk.MustQuery("select * from tmp1 where id=2").Check(testkit.Rows("2 12 999")) - tk.MustQuery("select * from tmp1 where id=4").Check(testkit.Rows()) - tk.MustQuery("select * from tmp1 where u=14").Check(testkit.Rows()) -} - -func (s *testSessionSuite) TestLocalTemporaryTableBatchPointGet(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("create temporary table tmp1 (id int primary key auto_increment, u int unique, v int)") - tk.MustExec("insert into tmp1 values(1, 11, 101)") - tk.MustExec("insert into tmp1 values(2, 12, 102)") - tk.MustExec("insert into tmp1 values(3, 13, 103)") - tk.MustExec("insert into tmp1 values(4, 14, 104)") - - // check point get out transaction - tk.MustQuery("select * from tmp1 where id in (1, 3)").Check(testkit.Rows("1 11 101", "3 13 103")) - tk.MustQuery("select * from tmp1 where u in (11, 13)").Check(testkit.Rows("1 11 101", "3 13 103")) - tk.MustQuery("select * from tmp1 where id in (1, 3, 5)").Check(testkit.Rows("1 11 101", "3 13 103")) - tk.MustQuery("select * from tmp1 where u in (11, 13, 15)").Check(testkit.Rows("1 11 101", "3 13 103")) - - // check point get in transaction - tk.MustExec("begin") - tk.MustQuery("select * from tmp1 where id in (1, 3)").Check(testkit.Rows("1 11 101", "3 13 103")) - tk.MustQuery("select * from tmp1 where u in (11, 13)").Check(testkit.Rows("1 11 101", "3 13 103")) - tk.MustQuery("select * from tmp1 where id in (1, 3, 5)").Check(testkit.Rows("1 11 101", "3 13 103")) - tk.MustQuery("select * from tmp1 where u in (11, 13, 15)").Check(testkit.Rows("1 11 101", "3 13 103")) - tk.MustExec("insert into tmp1 values(6, 16, 106)") - tk.MustQuery("select * from tmp1 where id in (1, 6)").Check(testkit.Rows("1 11 101", "6 16 106")) - tk.MustQuery("select * from tmp1 where u in (11, 16)").Check(testkit.Rows("1 11 101", "6 16 106")) - tk.MustExec("update tmp1 set v=999 where id=3") - tk.MustQuery("select * from tmp1 where id in (1, 3)").Check(testkit.Rows("1 11 101", "3 13 999")) - tk.MustQuery("select * from tmp1 where u in (11, 13)").Check(testkit.Rows("1 11 101", "3 13 999")) - tk.MustExec("delete from tmp1 where id=4") - tk.MustQuery("select * from tmp1 where id in (1, 4)").Check(testkit.Rows("1 11 101")) - tk.MustQuery("select * from tmp1 where u in (11, 14)").Check(testkit.Rows("1 11 101")) - tk.MustExec("commit") - - // check point get after transaction - tk.MustQuery("select * from tmp1 where id in (1, 3, 6)").Check(testkit.Rows("1 11 101", "3 13 999", "6 16 106")) - tk.MustQuery("select * from tmp1 where u in (11, 13, 16)").Check(testkit.Rows("1 11 101", "3 13 999", "6 16 106")) - tk.MustQuery("select * from tmp1 where id in (1, 4)").Check(testkit.Rows("1 11 101")) - tk.MustQuery("select * from tmp1 where u in (11, 14)").Check(testkit.Rows("1 11 101")) -} - -func (s *testSessionSuite) TestLocalTemporaryTableScan(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("create temporary table tmp1 (id int primary key auto_increment, u int unique, v int)") - tk.MustExec("insert into tmp1 values" + - "(1, 101, 1001), (3, 113, 1003), (5, 105, 1005), (7, 117, 1007), (9, 109, 1009)," + - "(10, 110, 1010), (12, 112, 1012), (14, 114, 1014), (16, 116, 1016), (18, 118, 1018)", - ) - - assertSelectAsUnModified := func() { - // For TableReader - tk.MustQuery("select * from tmp1 where id>3 order by id").Check(testkit.Rows( - "5 105 1005", "7 117 1007", "9 109 1009", - "10 110 1010", "12 112 1012", "14 114 1014", "16 116 1016", "18 118 1018", - )) - - // For IndexLookUpReader - tk.MustQuery("select /*+ use_index(tmp1, u) */ * from tmp1 where u>101 order by u").Check(testkit.Rows( - "5 105 1005", "9 109 1009", "10 110 1010", - "12 112 1012", "3 113 1003", "14 114 1014", "16 116 1016", "7 117 1007", "18 118 1018", - )) - tk.MustQuery("show warnings").Check(testkit.Rows()) - - // For IndexReader - tk.MustQuery("select /*+ use_index(tmp1, u) */ id,u from tmp1 where u>101 order by id").Check(testkit.Rows( - "3 113", "5 105", "7 117", "9 109", "10 110", - "12 112", "14 114", "16 116", "18 118", - )) - tk.MustQuery("show warnings").Check(testkit.Rows()) - - // For IndexMerge, temporary table should not use index merge - tk.MustQuery("select /*+ use_index_merge(tmp1, primary, u) */ * from tmp1 where id>5 or u>110 order by u").Check(testkit.Rows( - "9 109 1009", "10 110 1010", - "12 112 1012", "3 113 1003", "14 114 1014", "16 116 1016", "7 117 1007", "18 118 1018", - )) - - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 IndexMerge is inapplicable or disabled. Cannot use IndexMerge on temporary table.")) - } - - doModify := func() { - tk.MustExec("insert into tmp1 values(2, 100, 1002)") - tk.MustExec("insert into tmp1 values(4, 104, 1004)") - tk.MustExec("insert into tmp1 values(11, 111, 1011)") - tk.MustExec("update tmp1 set v=9999 where id=7") - tk.MustExec("update tmp1 set u=132 where id=12") - tk.MustExec("delete from tmp1 where id=16") - } - - assertSelectAsModified := func() { - // For TableReader - tk.MustQuery("select * from tmp1 where id>3 order by id").Check(testkit.Rows( - "4 104 1004", "5 105 1005", "7 117 9999", "9 109 1009", - "10 110 1010", "11 111 1011", "12 132 1012", "14 114 1014", "18 118 1018", - )) - - // For IndexLookUpReader - tk.MustQuery("select /*+ use_index(tmp1, u) */ * from tmp1 where u>101 order by u").Check(testkit.Rows( - "4 104 1004", "5 105 1005", "9 109 1009", "10 110 1010", "11 111 1011", - "3 113 1003", "14 114 1014", "7 117 9999", "18 118 1018", "12 132 1012", - )) - tk.MustQuery("show warnings").Check(testkit.Rows()) - - // For IndexReader - tk.MustQuery("select /*+ use_index(tmp1, u) */ id,u from tmp1 where u>101 order by id").Check(testkit.Rows( - "3 113", "4 104", "5 105", "7 117", "9 109", - "10 110", "11 111", "12 132", "14 114", "18 118", - )) - tk.MustQuery("show warnings").Check(testkit.Rows()) - - // For IndexMerge, temporary table should not use index merge - tk.MustQuery("select /*+ use_index_merge(tmp1, primary, u) */ * from tmp1 where id>5 or u>110 order by u").Check(testkit.Rows( - "9 109 1009", "10 110 1010", "11 111 1011", - "3 113 1003", "14 114 1014", "7 117 9999", "18 118 1018", "12 132 1012", - )) - - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 IndexMerge is inapplicable or disabled. Cannot use IndexMerge on temporary table.")) - } - - assertSelectAsUnModified() - tk.MustExec("begin") - assertSelectAsUnModified() - doModify() - tk.MustExec("rollback") - assertSelectAsUnModified() - tk.MustExec("begin") - doModify() - assertSelectAsModified() - tk.MustExec("commit") - assertSelectAsModified() -} - func (s *testSessionSuite) TestLocalTemporaryTableUpdate(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/tests/realtikvtest/session_test.go b/tests/realtikvtest/session_test.go index 890594cbd66b3..2c773ad9b42b8 100644 --- a/tests/realtikvtest/session_test.go +++ b/tests/realtikvtest/session_test.go @@ -21,7 +21,10 @@ import ( "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/parser/mysql" + "github.com/pingcap/tidb/parser/terror" + "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/types" "github.com/stretchr/testify/require" ) @@ -450,3 +453,398 @@ func TestSetVarHint(t *testing.T) { require.Len(t, tk.Session().GetSessionVars().StmtCtx.GetWarnings(), 1) require.EqualError(t, tk.Session().GetSessionVars().StmtCtx.GetWarnings()[0].Err, "[planner:3126]Hint SET_VAR(group_concat_max_len=2048) is ignored as conflicting/duplicated.") } + +func TestPrepareZero(t *testing.T) { + store, clean := createMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(v timestamp)") + tk.MustExec("prepare s1 from 'insert into t (v) values (?)'") + tk.MustExec("set @v1='0'") + require.Error(t, tk.ExecToErr("execute s1 using @v1")) + tk.MustExec("set @v2='" + types.ZeroDatetimeStr + "'") + tk.MustExec("set @orig_sql_mode=@@sql_mode; set @@sql_mode='';") + tk.MustExec("execute s1 using @v2") + tk.MustQuery("select v from t").Check(testkit.Rows("0000-00-00 00:00:00")) + tk.MustExec("set @@sql_mode=@orig_sql_mode;") +} + +func TestPrimaryKeyAutoIncrement(t *testing.T) { + store, clean := createMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL, name varchar(255) UNIQUE NOT NULL, status int)") + tk.MustExec("insert t (name) values (?)", "abc") + id := tk.Session().LastInsertID() + require.NotZero(t, id) + + tk1 := testkit.NewTestKit(t, store) + tk1.MustExec("use test") + tk1.MustQuery("select * from t").Check(testkit.Rows(fmt.Sprintf("%d abc ", id))) + + tk.MustExec("update t set name = 'abc', status = 1 where id = ?", id) + tk1.MustQuery("select * from t").Check(testkit.Rows(fmt.Sprintf("%d abc 1", id))) + + // Check for pass bool param to tidb prepared statement + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (id tinyint)") + tk.MustExec("insert t values (?)", true) + tk.MustQuery("select * from t").Check(testkit.Rows("1")) +} + +// TestSetGroupConcatMaxLen is for issue #7034 +func TestSetGroupConcatMaxLen(t *testing.T) { + store, clean := createMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + + // Normal case + tk.MustExec("set global group_concat_max_len = 100") + tk.MustExec("set @@session.group_concat_max_len = 50") + result := tk.MustQuery("show global variables where variable_name='group_concat_max_len';") + result.Check(testkit.Rows("group_concat_max_len 100")) + + result = tk.MustQuery("show session variables where variable_name='group_concat_max_len';") + result.Check(testkit.Rows("group_concat_max_len 50")) + + result = tk.MustQuery("select @@group_concat_max_len;") + result.Check(testkit.Rows("50")) + + result = tk.MustQuery("select @@global.group_concat_max_len;") + result.Check(testkit.Rows("100")) + + result = tk.MustQuery("select @@session.group_concat_max_len;") + result.Check(testkit.Rows("50")) + + tk.MustExec("set @@group_concat_max_len = 1024") + + result = tk.MustQuery("select @@group_concat_max_len;") + result.Check(testkit.Rows("1024")) + + result = tk.MustQuery("select @@global.group_concat_max_len;") + result.Check(testkit.Rows("100")) + + result = tk.MustQuery("select @@session.group_concat_max_len;") + result.Check(testkit.Rows("1024")) + + // Test value out of range + tk.MustExec("set @@group_concat_max_len=1") + tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning|1292|Truncated incorrect group_concat_max_len value: '1'")) + result = tk.MustQuery("select @@group_concat_max_len;") + result.Check(testkit.Rows("4")) + + _, err := tk.Exec("set @@group_concat_max_len = 18446744073709551616") + require.True(t, terror.ErrorEqual(err, variable.ErrWrongTypeForVar), fmt.Sprintf("err %v", err)) + + // Test illegal type + _, err = tk.Exec("set @@group_concat_max_len='hello'") + require.True(t, terror.ErrorEqual(err, variable.ErrWrongTypeForVar), fmt.Sprintf("err %v", err)) +} + +func TestLocalTemporaryTableInsertIgnore(t *testing.T) { + store, clean := createMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create temporary table tmp1 (id int primary key auto_increment, u int unique, v int)") + tk.MustExec("insert into tmp1 values(1, 11, 101)") + tk.MustExec("insert into tmp1 values(2, 12, 102)") + + // test outside transaction + tk.MustExec("insert ignore into tmp1 values(1, 100, 1000)") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '1' for key 'PRIMARY'")) + tk.MustQuery("select * from tmp1 where id=1").Check(testkit.Rows("1 11 101")) + tk.MustExec("insert ignore into tmp1 values(5, 15, 105)") + tk.MustQuery("show warnings").Check(testkit.Rows()) + tk.MustQuery("select * from tmp1 where id=5").Check(testkit.Rows("5 15 105")) + + // test in transaction and rollback + tk.MustExec("begin") + tk.MustExec("insert ignore into tmp1 values(1, 100, 1000)") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '1' for key 'PRIMARY'")) + tk.MustQuery("select * from tmp1 where id=1").Check(testkit.Rows("1 11 101")) + tk.MustExec("insert ignore into tmp1 values(3, 13, 103)") + tk.MustQuery("show warnings").Check(testkit.Rows()) + tk.MustQuery("select * from tmp1 where id=3").Check(testkit.Rows("3 13 103")) + tk.MustExec("insert ignore into tmp1 values(3, 100, 1000)") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '3' for key 'PRIMARY'")) + tk.MustQuery("select * from tmp1 where id=3").Check(testkit.Rows("3 13 103")) + tk.MustExec("rollback") + tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 11 101", "2 12 102", "5 15 105")) + + // test commit + tk.MustExec("begin") + tk.MustExec("insert ignore into tmp1 values(1, 100, 1000)") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '1' for key 'PRIMARY'")) + tk.MustExec("insert ignore into tmp1 values(3, 13, 103)") + tk.MustQuery("show warnings").Check(testkit.Rows()) + tk.MustExec("insert ignore into tmp1 values(3, 100, 1000)") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '3' for key 'PRIMARY'")) + tk.MustExec("commit") + tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 11 101", "2 12 102", "3 13 103", "5 15 105")) +} + +func TestLocalTemporaryTableInsertOnDuplicateKeyUpdate(t *testing.T) { + store, clean := createMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create temporary table tmp1 (id int primary key auto_increment, u int unique, v int)") + tk.MustExec("insert into tmp1 values(1, 11, 101)") + tk.MustExec("insert into tmp1 values(2, 12, 102)") + + // test outside transaction + tk.MustExec("insert ignore into tmp1 values(1, 100, 1000) on duplicate key update u=12") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '12' for key 'u'")) + tk.MustQuery("select * from tmp1 where id=1").Check(testkit.Rows("1 11 101")) + tk.MustExec("insert into tmp1 values(2, 100, 1000) on duplicate key update v=202") + tk.MustQuery("show warnings").Check(testkit.Rows()) + tk.MustQuery("select * from tmp1 where id=2").Check(testkit.Rows("2 12 202")) + tk.MustExec("insert into tmp1 values(3, 13, 103) on duplicate key update v=203") + tk.MustQuery("show warnings").Check(testkit.Rows()) + tk.MustQuery("select * from tmp1 where id=3").Check(testkit.Rows("3 13 103")) + + // test in transaction and rollback + tk.MustExec("begin") + tk.MustExec("insert ignore into tmp1 values(1, 100, 1000) on duplicate key update u=12") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '12' for key 'u'")) + tk.MustQuery("select * from tmp1 where id=1").Check(testkit.Rows("1 11 101")) + tk.MustExec("insert into tmp1 values(2, 100, 1000) on duplicate key update v=302") + tk.MustQuery("show warnings").Check(testkit.Rows()) + tk.MustQuery("select * from tmp1 where id=2").Check(testkit.Rows("2 12 302")) + tk.MustExec("insert into tmp1 values(4, 14, 104) on duplicate key update v=204") + tk.MustQuery("show warnings").Check(testkit.Rows()) + tk.MustQuery("select * from tmp1 where id=4").Check(testkit.Rows("4 14 104")) + tk.MustExec("rollback") + tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 11 101", "2 12 202", "3 13 103")) + + // test commit + tk.MustExec("begin") + tk.MustExec("insert ignore into tmp1 values(1, 100, 1000) on duplicate key update u=12") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1062 Duplicate entry '12' for key 'u'")) + tk.MustExec("insert into tmp1 values(2, 100, 1000) on duplicate key update v=302") + tk.MustExec("insert into tmp1 values(4, 14, 104) on duplicate key update v=204") + tk.MustExec("commit") + tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 11 101", "2 12 302", "3 13 103", "4 14 104")) +} + +func TestLocalTemporaryTableReplace(t *testing.T) { + store, clean := createMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create temporary table tmp1 (id int primary key auto_increment, u int unique, v int)") + tk.MustExec("insert into tmp1 values(1, 11, 101)") + tk.MustExec("insert into tmp1 values(2, 12, 102)") + tk.MustExec("insert into tmp1 values(3, 13, 103)") + + // out of transaction + tk.MustExec("replace into tmp1 values(1, 12, 1000)") + tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 12 1000", "3 13 103")) + tk.MustExec("replace into tmp1 values(4, 14, 104)") + tk.MustQuery("select * from tmp1 where id=4").Check(testkit.Rows("4 14 104")) + + // in transaction and rollback + tk.MustExec("begin") + tk.MustExec("replace into tmp1 values(1, 13, 999)") + tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 13 999", "4 14 104")) + tk.MustExec("replace into tmp1 values(5, 15, 105)") + tk.MustQuery("select * from tmp1 where id=5").Check(testkit.Rows("5 15 105")) + tk.MustExec("rollback") + tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 12 1000", "3 13 103", "4 14 104")) + + // out of transaction + tk.MustExec("begin") + tk.MustExec("replace into tmp1 values(1, 13, 999)") + tk.MustExec("replace into tmp1 values(5, 15, 105)") + tk.MustExec("commit") + tk.MustQuery("select * from tmp1").Check(testkit.Rows("1 13 999", "4 14 104", "5 15 105")) +} + +func TestLocalTemporaryTablePointGet(t *testing.T) { + store, clean := createMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create temporary table tmp1 (id int primary key auto_increment, u int unique, v int)") + tk.MustExec("insert into tmp1 values(1, 11, 101)") + tk.MustExec("insert into tmp1 values(2, 12, 102)") + tk.MustExec("insert into tmp1 values(4, 14, 104)") + + // check point get out transaction + tk.MustQuery("select * from tmp1 where id=1").Check(testkit.Rows("1 11 101")) + tk.MustQuery("select * from tmp1 where u=11").Check(testkit.Rows("1 11 101")) + tk.MustQuery("select * from tmp1 where id=2").Check(testkit.Rows("2 12 102")) + tk.MustQuery("select * from tmp1 where u=12").Check(testkit.Rows("2 12 102")) + + // check point get in transaction + tk.MustExec("begin") + tk.MustQuery("select * from tmp1 where id=1").Check(testkit.Rows("1 11 101")) + tk.MustQuery("select * from tmp1 where u=11").Check(testkit.Rows("1 11 101")) + tk.MustQuery("select * from tmp1 where id=2").Check(testkit.Rows("2 12 102")) + tk.MustQuery("select * from tmp1 where u=12").Check(testkit.Rows("2 12 102")) + tk.MustExec("insert into tmp1 values(3, 13, 103)") + tk.MustQuery("select * from tmp1 where id=3").Check(testkit.Rows("3 13 103")) + tk.MustQuery("select * from tmp1 where u=13").Check(testkit.Rows("3 13 103")) + tk.MustExec("update tmp1 set v=999 where id=2") + tk.MustQuery("select * from tmp1 where id=2").Check(testkit.Rows("2 12 999")) + tk.MustExec("delete from tmp1 where id=4") + tk.MustQuery("select * from tmp1 where id=4").Check(testkit.Rows()) + tk.MustQuery("select * from tmp1 where u=14").Check(testkit.Rows()) + tk.MustExec("commit") + + // check point get after transaction + tk.MustQuery("select * from tmp1 where id=3").Check(testkit.Rows("3 13 103")) + tk.MustQuery("select * from tmp1 where u=13").Check(testkit.Rows("3 13 103")) + tk.MustQuery("select * from tmp1 where id=2").Check(testkit.Rows("2 12 999")) + tk.MustQuery("select * from tmp1 where id=4").Check(testkit.Rows()) + tk.MustQuery("select * from tmp1 where u=14").Check(testkit.Rows()) +} + +func TestLocalTemporaryTableBatchPointGet(t *testing.T) { + store, clean := createMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create temporary table tmp1 (id int primary key auto_increment, u int unique, v int)") + tk.MustExec("insert into tmp1 values(1, 11, 101)") + tk.MustExec("insert into tmp1 values(2, 12, 102)") + tk.MustExec("insert into tmp1 values(3, 13, 103)") + tk.MustExec("insert into tmp1 values(4, 14, 104)") + + // check point get out transaction + tk.MustQuery("select * from tmp1 where id in (1, 3)").Check(testkit.Rows("1 11 101", "3 13 103")) + tk.MustQuery("select * from tmp1 where u in (11, 13)").Check(testkit.Rows("1 11 101", "3 13 103")) + tk.MustQuery("select * from tmp1 where id in (1, 3, 5)").Check(testkit.Rows("1 11 101", "3 13 103")) + tk.MustQuery("select * from tmp1 where u in (11, 13, 15)").Check(testkit.Rows("1 11 101", "3 13 103")) + + // check point get in transaction + tk.MustExec("begin") + tk.MustQuery("select * from tmp1 where id in (1, 3)").Check(testkit.Rows("1 11 101", "3 13 103")) + tk.MustQuery("select * from tmp1 where u in (11, 13)").Check(testkit.Rows("1 11 101", "3 13 103")) + tk.MustQuery("select * from tmp1 where id in (1, 3, 5)").Check(testkit.Rows("1 11 101", "3 13 103")) + tk.MustQuery("select * from tmp1 where u in (11, 13, 15)").Check(testkit.Rows("1 11 101", "3 13 103")) + tk.MustExec("insert into tmp1 values(6, 16, 106)") + tk.MustQuery("select * from tmp1 where id in (1, 6)").Check(testkit.Rows("1 11 101", "6 16 106")) + tk.MustQuery("select * from tmp1 where u in (11, 16)").Check(testkit.Rows("1 11 101", "6 16 106")) + tk.MustExec("update tmp1 set v=999 where id=3") + tk.MustQuery("select * from tmp1 where id in (1, 3)").Check(testkit.Rows("1 11 101", "3 13 999")) + tk.MustQuery("select * from tmp1 where u in (11, 13)").Check(testkit.Rows("1 11 101", "3 13 999")) + tk.MustExec("delete from tmp1 where id=4") + tk.MustQuery("select * from tmp1 where id in (1, 4)").Check(testkit.Rows("1 11 101")) + tk.MustQuery("select * from tmp1 where u in (11, 14)").Check(testkit.Rows("1 11 101")) + tk.MustExec("commit") + + // check point get after transaction + tk.MustQuery("select * from tmp1 where id in (1, 3, 6)").Check(testkit.Rows("1 11 101", "3 13 999", "6 16 106")) + tk.MustQuery("select * from tmp1 where u in (11, 13, 16)").Check(testkit.Rows("1 11 101", "3 13 999", "6 16 106")) + tk.MustQuery("select * from tmp1 where id in (1, 4)").Check(testkit.Rows("1 11 101")) + tk.MustQuery("select * from tmp1 where u in (11, 14)").Check(testkit.Rows("1 11 101")) +} + +func TestLocalTemporaryTableScan(t *testing.T) { + store, clean := createMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create temporary table tmp1 (id int primary key auto_increment, u int unique, v int)") + tk.MustExec("insert into tmp1 values" + + "(1, 101, 1001), (3, 113, 1003), (5, 105, 1005), (7, 117, 1007), (9, 109, 1009)," + + "(10, 110, 1010), (12, 112, 1012), (14, 114, 1014), (16, 116, 1016), (18, 118, 1018)", + ) + + assertSelectAsUnModified := func() { + // For TableReader + tk.MustQuery("select * from tmp1 where id>3 order by id").Check(testkit.Rows( + "5 105 1005", "7 117 1007", "9 109 1009", + "10 110 1010", "12 112 1012", "14 114 1014", "16 116 1016", "18 118 1018", + )) + + // For IndexLookUpReader + tk.MustQuery("select /*+ use_index(tmp1, u) */ * from tmp1 where u>101 order by u").Check(testkit.Rows( + "5 105 1005", "9 109 1009", "10 110 1010", + "12 112 1012", "3 113 1003", "14 114 1014", "16 116 1016", "7 117 1007", "18 118 1018", + )) + tk.MustQuery("show warnings").Check(testkit.Rows()) + + // For IndexReader + tk.MustQuery("select /*+ use_index(tmp1, u) */ id,u from tmp1 where u>101 order by id").Check(testkit.Rows( + "3 113", "5 105", "7 117", "9 109", "10 110", + "12 112", "14 114", "16 116", "18 118", + )) + tk.MustQuery("show warnings").Check(testkit.Rows()) + + // For IndexMerge, temporary table should not use index merge + tk.MustQuery("select /*+ use_index_merge(tmp1, primary, u) */ * from tmp1 where id>5 or u>110 order by u").Check(testkit.Rows( + "9 109 1009", "10 110 1010", + "12 112 1012", "3 113 1003", "14 114 1014", "16 116 1016", "7 117 1007", "18 118 1018", + )) + + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 IndexMerge is inapplicable or disabled. Cannot use IndexMerge on temporary table.")) + } + + doModify := func() { + tk.MustExec("insert into tmp1 values(2, 100, 1002)") + tk.MustExec("insert into tmp1 values(4, 104, 1004)") + tk.MustExec("insert into tmp1 values(11, 111, 1011)") + tk.MustExec("update tmp1 set v=9999 where id=7") + tk.MustExec("update tmp1 set u=132 where id=12") + tk.MustExec("delete from tmp1 where id=16") + } + + assertSelectAsModified := func() { + // For TableReader + tk.MustQuery("select * from tmp1 where id>3 order by id").Check(testkit.Rows( + "4 104 1004", "5 105 1005", "7 117 9999", "9 109 1009", + "10 110 1010", "11 111 1011", "12 132 1012", "14 114 1014", "18 118 1018", + )) + + // For IndexLookUpReader + tk.MustQuery("select /*+ use_index(tmp1, u) */ * from tmp1 where u>101 order by u").Check(testkit.Rows( + "4 104 1004", "5 105 1005", "9 109 1009", "10 110 1010", "11 111 1011", + "3 113 1003", "14 114 1014", "7 117 9999", "18 118 1018", "12 132 1012", + )) + tk.MustQuery("show warnings").Check(testkit.Rows()) + + // For IndexReader + tk.MustQuery("select /*+ use_index(tmp1, u) */ id,u from tmp1 where u>101 order by id").Check(testkit.Rows( + "3 113", "4 104", "5 105", "7 117", "9 109", + "10 110", "11 111", "12 132", "14 114", "18 118", + )) + tk.MustQuery("show warnings").Check(testkit.Rows()) + + // For IndexMerge, temporary table should not use index merge + tk.MustQuery("select /*+ use_index_merge(tmp1, primary, u) */ * from tmp1 where id>5 or u>110 order by u").Check(testkit.Rows( + "9 109 1009", "10 110 1010", "11 111 1011", + "3 113 1003", "14 114 1014", "7 117 9999", "18 118 1018", "12 132 1012", + )) + + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 IndexMerge is inapplicable or disabled. Cannot use IndexMerge on temporary table.")) + } + + assertSelectAsUnModified() + tk.MustExec("begin") + assertSelectAsUnModified() + doModify() + tk.MustExec("rollback") + assertSelectAsUnModified() + tk.MustExec("begin") + doModify() + assertSelectAsModified() + tk.MustExec("commit") + assertSelectAsModified() +}