From c5e3b49e6e4ac807568679f27eaecb912228d94c Mon Sep 17 00:00:00 2001 From: Shenghui Wu <793703860@qq.com> Date: Wed, 8 Apr 2020 09:45:55 +0800 Subject: [PATCH 1/3] cherry pick #16073 to release-3.0 Signed-off-by: sre-bot --- executor/executor.go | 9 + expression/integration_test.go | 621 +++++++++++++++++++++++++++++++++ 2 files changed, 630 insertions(+) diff --git a/executor/executor.go b/executor/executor.go index 9198a1d559c1a..0ebcf91adbfd7 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -1541,7 +1541,16 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { sc.Priority = opts.Priority sc.NotFillCache = !opts.SQLCache } +<<<<<<< HEAD sc.CastStrToIntStrict = true +======= + case *ast.UnionStmt: + sc.InSelectStmt = true + sc.OverflowAsWarning = true + sc.TruncateAsWarning = true + sc.IgnoreZeroInDate = true + sc.AllowInvalidDate = vars.SQLMode.HasAllowInvalidDatesMode() +>>>>>>> 96d96ce... executor: fix unexpected error in union stmt. (#16073) case *ast.ShowStmt: sc.IgnoreTruncate = true sc.IgnoreZeroInDate = true diff --git a/expression/integration_test.go b/expression/integration_test.go index 0709db984921e..1b25cf678c6fe 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4918,3 +4918,624 @@ func (s *testIntegrationSuite) TestValuesForBinaryLiteral(c *C) { tk.MustQuery("select a=0 from testValuesBinary;").Check(testkit.Rows("1")) tk.MustExec("drop table testValuesBinary;") } +<<<<<<< HEAD +======= + +func (s *testIntegrationSuite) TestIssue14159(c *C) { + tk := testkit.NewTestKitWithInit(c, s.store) + tk.MustExec("DROP TABLE IF EXISTS t") + tk.MustExec("CREATE TABLE t (v VARCHAR(100))") + tk.MustExec("INSERT INTO t VALUES ('3289742893213123732904809')") + tk.MustQuery("SELECT * FROM t WHERE v").Check(testkit.Rows("3289742893213123732904809")) +} + +func (s *testIntegrationSuite) TestIssue14146(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("create table tt(a varchar(10))") + tk.MustExec("insert into tt values(NULL)") + tk.MustExec("analyze table tt;") + tk.MustQuery("select * from tt").Check(testkit.Rows("")) +} + +func (s *testIntegrationSuite) TestCacheRegexpr(c *C) { + tk := testkit.NewTestKit(c, s.store) + orgEnable := plannercore.PreparedPlanCacheEnabled() + defer func() { + plannercore.SetPreparedPlanCache(orgEnable) + }() + plannercore.SetPreparedPlanCache(true) + var err error + tk.Se, err = session.CreateSession4TestWithOpt(s.store, &session.Opt{ + PreparedPlanCache: kvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64), + }) + c.Assert(err, IsNil) + + tk.MustExec("use test") + tk.MustExec("drop table if exists t1") + tk.MustExec("create table t1 (a varchar(40))") + tk.MustExec("insert into t1 values ('C1'),('R1')") + tk.MustExec("prepare stmt1 from 'select a from t1 where a rlike ?'") + tk.MustExec("set @a='^C.*'") + tk.MustQuery("execute stmt1 using @a").Check(testkit.Rows("C1")) + tk.MustExec("set @a='^R.*'") + tk.MustQuery("execute stmt1 using @a").Check(testkit.Rows("R1")) +} + +func (s *testIntegrationSuite) TestCacheRefineArgs(c *C) { + tk := testkit.NewTestKit(c, s.store) + orgEnable := plannercore.PreparedPlanCacheEnabled() + defer func() { + plannercore.SetPreparedPlanCache(orgEnable) + }() + plannercore.SetPreparedPlanCache(true) + var err error + tk.Se, err = session.CreateSession4TestWithOpt(s.store, &session.Opt{ + PreparedPlanCache: kvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64), + }) + c.Assert(err, IsNil) + + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(col_int int)") + tk.MustExec("insert into t values(null)") + tk.MustExec("prepare stmt from 'SELECT ((col_int is true) = ?) AS res FROM t'") + tk.MustExec("set @p0='0.8'") + tk.MustQuery("execute stmt using @p0").Check(testkit.Rows("0")) + tk.MustExec("set @p0='0'") + tk.MustQuery("execute stmt using @p0").Check(testkit.Rows("1")) + + tk.MustExec("delete from t") + tk.MustExec("insert into t values(1)") + tk.MustExec("prepare stmt from 'SELECT col_int < ? FROM t'") + tk.MustExec("set @p0='-184467440737095516167.1'") + tk.MustQuery("execute stmt using @p0").Check(testkit.Rows("0")) +} + +func (s *testIntegrationSuite) TestCollation(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (utf8_bin_c varchar(10) charset utf8 collate utf8_bin, utf8_gen_c varchar(10) charset utf8 collate utf8_general_ci, bin_c binary, num_c int, " + + "abin char collate ascii_bin, lbin char collate latin1_bin, u4bin char collate utf8mb4_bin, u4ci char collate utf8mb4_general_ci)") + tk.MustExec("insert into t values ('a', 'b', 'c', 4, 'a', 'a', 'a', 'a')") + tk.MustQuery("select collation(null)").Check(testkit.Rows("binary")) + tk.MustQuery("select collation(2)").Check(testkit.Rows("binary")) + tk.MustQuery("select collation(2 + 'a')").Check(testkit.Rows("binary")) + tk.MustQuery("select collation(2 + utf8_gen_c) from t").Check(testkit.Rows("binary")) + tk.MustQuery("select collation(2 + utf8_bin_c) from t").Check(testkit.Rows("binary")) + tk.MustQuery("select collation(concat(utf8_bin_c, 2)) from t").Check(testkit.Rows("utf8_bin")) + tk.MustQuery("select collation(concat(utf8_gen_c, 'abc')) from t").Check(testkit.Rows("utf8_general_ci")) + tk.MustQuery("select collation(concat(utf8_gen_c, null)) from t").Check(testkit.Rows("utf8_general_ci")) + tk.MustQuery("select collation(concat(utf8_gen_c, num_c)) from t").Check(testkit.Rows("utf8_general_ci")) + tk.MustQuery("select collation(concat(utf8_bin_c, utf8_gen_c)) from t").Check(testkit.Rows("utf8_bin")) + tk.MustQuery("select collation(upper(utf8_bin_c)) from t").Check(testkit.Rows("utf8_bin")) + tk.MustQuery("select collation(upper(utf8_gen_c)) from t").Check(testkit.Rows("utf8_general_ci")) + tk.MustQuery("select collation(upper(bin_c)) from t").Check(testkit.Rows("binary")) + tk.MustQuery("select collation(concat(abin, bin_c)) from t").Check(testkit.Rows("binary")) + tk.MustQuery("select collation(concat(lbin, bin_c)) from t").Check(testkit.Rows("binary")) + tk.MustQuery("select collation(concat(utf8_bin_c, bin_c)) from t").Check(testkit.Rows("binary")) + tk.MustQuery("select collation(concat(utf8_gen_c, bin_c)) from t").Check(testkit.Rows("binary")) + tk.MustQuery("select collation(concat(u4bin, bin_c)) from t").Check(testkit.Rows("binary")) + tk.MustQuery("select collation(concat(u4ci, bin_c)) from t").Check(testkit.Rows("binary")) + tk.MustQuery("select collation(concat(abin, u4bin)) from t").Check(testkit.Rows("utf8mb4_bin")) + tk.MustQuery("select collation(concat(lbin, u4bin)) from t").Check(testkit.Rows("utf8mb4_bin")) + tk.MustQuery("select collation(concat(utf8_bin_c, u4bin)) from t").Check(testkit.Rows("utf8mb4_bin")) + tk.MustQuery("select collation(concat(utf8_gen_c, u4bin)) from t").Check(testkit.Rows("utf8mb4_bin")) + tk.MustQuery("select collation(concat(u4ci, u4bin)) from t").Check(testkit.Rows("utf8mb4_bin")) + tk.MustQuery("select collation(concat(abin, u4ci)) from t").Check(testkit.Rows("utf8mb4_general_ci")) + tk.MustQuery("select collation(concat(lbin, u4ci)) from t").Check(testkit.Rows("utf8mb4_general_ci")) + tk.MustQuery("select collation(concat(utf8_bin_c, u4ci)) from t").Check(testkit.Rows("utf8mb4_general_ci")) + tk.MustQuery("select collation(concat(utf8_gen_c, u4ci)) from t").Check(testkit.Rows("utf8mb4_general_ci")) + tk.MustQuery("select collation(concat(abin, utf8_bin_c)) from t").Check(testkit.Rows("utf8_bin")) + tk.MustQuery("select collation(concat(lbin, utf8_bin_c)) from t").Check(testkit.Rows("utf8_bin")) + tk.MustQuery("select collation(concat(utf8_gen_c, utf8_bin_c)) from t").Check(testkit.Rows("utf8_bin")) + tk.MustQuery("select collation(concat(abin, utf8_gen_c)) from t").Check(testkit.Rows("utf8_general_ci")) + tk.MustQuery("select collation(concat(lbin, utf8_gen_c)) from t").Check(testkit.Rows("utf8_general_ci")) + tk.MustQuery("select collation(concat(abin, lbin)) from t").Check(testkit.Rows("latin1_bin")) + + tk.MustExec("set names utf8mb4 collate utf8mb4_bin") + tk.MustQuery("select collation('a')").Check(testkit.Rows("utf8mb4_bin")) + tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci") + tk.MustQuery("select collation('a')").Check(testkit.Rows("utf8mb4_general_ci")) + + tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci") + tk.MustExec("set @test_collate_var = 'a'") + tk.MustQuery("select collation(@test_collate_var)").Check(testkit.Rows("utf8mb4_general_ci")) + tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci") + tk.MustExec("set @test_collate_var = 1") + tk.MustQuery("select collation(@test_collate_var)").Check(testkit.Rows("utf8mb4_general_ci")) + tk.MustExec("set @test_collate_var = concat(\"a\", \"b\" collate utf8mb4_bin)") + tk.MustQuery("select collation(@test_collate_var)").Check(testkit.Rows("utf8mb4_bin")) +} + +func (s *testIntegrationSuite) TestCoercibility(c *C) { + tk := testkit.NewTestKit(c, s.store) + + type testCase struct { + expr string + result int + } + testFunc := func(cases []testCase, suffix string) { + for _, tc := range cases { + tk.MustQuery(fmt.Sprintf("select coercibility(%v) %v", tc.expr, suffix)).Check(testkit.Rows(fmt.Sprintf("%v", tc.result))) + } + } + testFunc([]testCase{ + // constants + {"1", 5}, {"null", 6}, {"'abc'", 4}, + // sys-constants + {"version()", 3}, {"user()", 3}, {"database()", 3}, + {"current_role()", 3}, {"current_user()", 3}, + // scalar functions after constant folding + {"1+null", 5}, {"null+'abcde'", 5}, {"concat(null, 'abcde')", 4}, + // non-deterministic functions + {"rand()", 5}, {"now()", 5}, {"sysdate()", 5}, + }, "") + + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (i int, r real, d datetime, t timestamp, c char(10), vc varchar(10), b binary(10), vb binary(10))") + tk.MustExec("insert into t values (null, null, null, null, null, null, null, null)") + testFunc([]testCase{ + {"i", 5}, {"r", 5}, {"d", 5}, {"t", 5}, + {"c", 2}, {"b", 2}, {"vb", 2}, {"vc", 2}, + {"i+r", 5}, {"i*r", 5}, {"cos(r)+sin(i)", 5}, {"d+2", 5}, + {"t*10", 5}, {"concat(c, vc)", 2}, {"replace(c, 'x', 'y')", 2}, + }, "from t") +} + +func (s *testIntegrationSuite) TestCacheConstEval(c *C) { + tk := testkit.NewTestKit(c, s.store) + orgEnable := plannercore.PreparedPlanCacheEnabled() + defer func() { + plannercore.SetPreparedPlanCache(orgEnable) + }() + plannercore.SetPreparedPlanCache(true) + var err error + tk.Se, err = session.CreateSession4TestWithOpt(s.store, &session.Opt{ + PreparedPlanCache: kvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64), + }) + c.Assert(err, IsNil) + + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(col_double double)") + tk.MustExec("insert into t values (1)") + tk.Se.GetSessionVars().EnableVectorizedExpression = false + tk.MustExec("insert into mysql.expr_pushdown_blacklist values('cast')") + tk.MustExec("admin reload expr_pushdown_blacklist") + tk.MustExec("prepare stmt from 'SELECT * FROM (SELECT col_double AS c0 FROM t) t WHERE (ABS((REPEAT(?, ?) OR 5617780767323292672)) < LN(EXP(c0)) + (? ^ ?))'") + tk.MustExec("set @a1 = 'JuvkBX7ykVux20zQlkwDK2DFelgn7'") + tk.MustExec("set @a2 = 1") + tk.MustExec("set @a3 = -112990.35179796701") + tk.MustExec("set @a4 = 87997.92704840179") + // Main purpose here is checking no error is reported. 1 is the result when plan cache is disabled, it is + // incompatible with MySQL actually, update the result after fixing it. + tk.MustQuery("execute stmt using @a1, @a2, @a3, @a4").Check(testkit.Rows("1")) + tk.Se.GetSessionVars().EnableVectorizedExpression = true + tk.MustExec("delete from mysql.expr_pushdown_blacklist") + tk.MustExec("admin reload expr_pushdown_blacklist") +} + +func (s *testIntegrationSerialSuite) TestCollationBasic(c *C) { + tk := testkit.NewTestKit(c, s.store) + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk.MustExec("use test") + tk.MustExec("create table t_ci(a varchar(10) collate utf8mb4_general_ci, unique key(a))") + tk.MustExec("insert into t_ci values ('a')") + tk.MustQuery("select * from t_ci").Check(testkit.Rows("a")) + tk.MustQuery("select * from t_ci").Check(testkit.Rows("a")) + tk.MustQuery("select * from t_ci where a='a'").Check(testkit.Rows("a")) + tk.MustQuery("select * from t_ci where a='A'").Check(testkit.Rows("a")) + tk.MustQuery("select * from t_ci where a='a '").Check(testkit.Rows("a")) + tk.MustQuery("select * from t_ci where a='a '").Check(testkit.Rows("a")) +} + +func (s *testIntegrationSerialSuite) TestWeightString(c *C) { + tk := testkit.NewTestKit(c, s.store) + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + + type testCase struct { + input []string + result []string + resultAsChar1 []string + resultAsChar3 []string + resultAsBinary1 []string + resultAsBinary5 []string + resultExplicitCollateBin []string + } + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (id int, a varchar(20) collate utf8mb4_general_ci)") + cases := testCase{ + input: []string{"aAÁàãăâ", "a", "a ", "中", "中 "}, + result: []string{"\x00A\x00A\x00A\x00A\x00A\x00A\x00A", "\x00A", "\x00A", "\x4E\x2D", "\x4E\x2D"}, + resultAsChar1: []string{"\x00A", "\x00A", "\x00A", "\x4E\x2D", "\x4E\x2D"}, + resultAsChar3: []string{"\x00A\x00A\x00A", "\x00A", "\x00A", "\x4E\x2D", "\x4E\x2D"}, + resultAsBinary1: []string{"a", "a", "a", "\xE4", "\xE4"}, + resultAsBinary5: []string{"aA\xc3\x81\xc3", "a\x00\x00\x00\x00", "a \x00\x00", "中\x00\x00", "中 \x00"}, + resultExplicitCollateBin: []string{"aAÁàãăâ", "a", "a", "中", "中"}, + } + values := make([]string, len(cases.input)) + for i, input := range cases.input { + values[i] = fmt.Sprintf("(%d, '%s')", i, input) + } + tk.MustExec("insert into t values " + strings.Join(values, ",")) + rows := tk.MustQuery("select weight_string(a) from t order by id").Rows() + for i, out := range cases.result { + c.Assert(rows[i][0].(string), Equals, out) + } + rows = tk.MustQuery("select weight_string(a as char(1)) from t order by id").Rows() + for i, out := range cases.resultAsChar1 { + c.Assert(rows[i][0].(string), Equals, out) + } + rows = tk.MustQuery("select weight_string(a as char(3)) from t order by id").Rows() + for i, out := range cases.resultAsChar3 { + c.Assert(rows[i][0].(string), Equals, out) + } + rows = tk.MustQuery("select weight_string(a as binary(1)) from t order by id").Rows() + for i, out := range cases.resultAsBinary1 { + c.Assert(rows[i][0].(string), Equals, out) + } + rows = tk.MustQuery("select weight_string(a as binary(5)) from t order by id").Rows() + for i, out := range cases.resultAsBinary5 { + c.Assert(rows[i][0].(string), Equals, out) + } + c.Assert(tk.MustQuery("select weight_string(NULL);").Rows()[0][0], Equals, "") + c.Assert(tk.MustQuery("select weight_string(7);").Rows()[0][0], Equals, "") + c.Assert(tk.MustQuery("select weight_string(cast(7 as decimal(5)));").Rows()[0][0], Equals, "") + c.Assert(tk.MustQuery("select weight_string(cast(20190821 as date));").Rows()[0][0], Equals, "2019-08-21") + c.Assert(tk.MustQuery("select weight_string(cast(20190821 as date) as binary(5));").Rows()[0][0], Equals, "2019-") + c.Assert(tk.MustQuery("select weight_string(7.0);").Rows()[0][0], Equals, "") + c.Assert(tk.MustQuery("select weight_string(7 AS BINARY(2));").Rows()[0][0], Equals, "7\x00") + // test explicit collation + c.Assert(tk.MustQuery("select weight_string('中 ' collate utf8mb4_general_ci);").Rows()[0][0], Equals, "\x4E\x2D") + c.Assert(tk.MustQuery("select weight_string('中 ' collate utf8mb4_bin);").Rows()[0][0], Equals, "中") + c.Assert(tk.MustQuery("select collation(a collate utf8mb4_general_ci) from t order by id").Rows()[0][0], Equals, "utf8mb4_general_ci") + c.Assert(tk.MustQuery("select collation('中 ' collate utf8mb4_general_ci);").Rows()[0][0], Equals, "utf8mb4_general_ci") + rows = tk.MustQuery("select weight_string(a collate utf8mb4_bin) from t order by id").Rows() + for i, out := range cases.resultExplicitCollateBin { + c.Assert(rows[i][0].(string), Equals, out) + } + tk.MustGetErrMsg("select weight_string(a collate utf8_general_ci) from t order by id", "[ddl:1253]COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'utf8mb4'") + tk.MustGetErrMsg("select weight_string('中' collate utf8_bin)", "[ddl:1253]COLLATION 'utf8_bin' is not valid for CHARACTER SET 'utf8mb4'") +} + +func (s *testIntegrationSerialSuite) TestCollationCreateIndex(c *C) { + tk := testkit.NewTestKit(c, s.store) + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (a varchar(10) collate utf8mb4_general_ci);") + tk.MustExec("insert into t values ('a');") + tk.MustExec("insert into t values ('A');") + tk.MustExec("insert into t values ('b');") + tk.MustExec("insert into t values ('B');") + tk.MustExec("insert into t values ('a');") + tk.MustExec("insert into t values ('A');") + tk.MustExec("create index idx on t(a);") + tk.MustQuery("select * from t order by a").Check(testkit.Rows("a", "A", "a", "A", "b", "B")) +} + +func (s *testIntegrationSerialSuite) TestCollateConstantPropagation(c *C) { + tk := testkit.NewTestKit(c, s.store) + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (a char(10) collate utf8mb4_bin, b char(10) collate utf8mb4_general_ci);") + tk.MustExec("insert into t values ('a', 'A');") + tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b='a' collate utf8mb4_general_ci;").Check(nil) + tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b>='a' collate utf8mb4_general_ci;").Check(nil) + tk.MustExec("drop table t;") + tk.MustExec("create table t (a char(10) collate utf8mb4_general_ci, b char(10) collate utf8mb4_general_ci);") + tk.MustExec("insert into t values ('A', 'a');") + tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b='a' collate utf8mb4_bin;").Check(testkit.Rows("A a A a")) + tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b>='a' collate utf8mb4_bin;").Check(testkit.Rows("A a A a")) +} +func (s *testIntegrationSerialSuite) prepare4Join(c *C) *testkit.TestKit { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("USE test") + tk.MustExec("drop table if exists t") + tk.MustExec("drop table if exists t_bin") + tk.MustExec("CREATE TABLE `t` ( `a` int(11) NOT NULL,`b` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL)") + tk.MustExec("CREATE TABLE `t_bin` ( `a` int(11) NOT NULL,`b` varchar(5) CHARACTER SET binary)") + tk.MustExec("insert into t values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')") + tk.MustExec("insert into t_bin values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')") + return tk +} + +func (s *testIntegrationSerialSuite) prepare4Join2(c *C) *testkit.TestKit { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("USE test") + tk.MustExec("drop table if exists t1") + tk.MustExec("drop table if exists t2") + tk.MustExec("create table t1 (id int, v varchar(5) character set binary, key(v))") + tk.MustExec("create table t2 (v varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci, key(v))") + tk.MustExec("insert into t1 values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')") + tk.MustExec("insert into t2 values ('a'), ('À'), ('á'), ('à'), ('b'), ('c'), (' ')") + return tk +} + +func (s *testIntegrationSerialSuite) TestCollateHashJoin(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := s.prepare4Join(c) + tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b order by t1.a").Check( + testkit.Rows("1 a", "1 a", "1 a", "1 a", "2 À", "2 À", "2 À", "2 À", "3 á", "3 á", "3 á", "3 á", "4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7 ")) + tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b order by t1.a").Check( + testkit.Rows("1 a", "2 À", "3 á", "4 à", "5 b", "6 c", "7 ")) + tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( + testkit.Rows("4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7 ")) + tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( + testkit.Rows("4 à", "5 b", "6 c", "7 ")) + tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( + testkit.Rows("4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7 ")) + tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( + testkit.Rows("4 à", "5 b", "6 c", "7 ")) + tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>t2.a order by t1.a").Check( + testkit.Rows("2 À", "3 á", "3 á", "4 à", "4 à", "4 à")) + tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>t2.a order by t1.a").Check( + testkit.Rows()) +} + +func (s *testIntegrationSerialSuite) TestCollateHashJoin2(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := s.prepare4Join2(c) + tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ * from t1, t2 where t1.v=t2.v order by t1.id").Check( + testkit.Rows("1 a a", "2 À À", "3 á á", "4 à à", "5 b b", "6 c c", "7 ")) +} + +func (s *testIntegrationSerialSuite) TestCollateMergeJoin(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := s.prepare4Join(c) + tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b order by t1.a").Check( + testkit.Rows("1 a", "1 a", "1 a", "1 a", "2 À", "2 À", "2 À", "2 À", "3 á", "3 á", "3 á", "3 á", "4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7 ")) + tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b order by t1.a").Check( + testkit.Rows("1 a", "2 À", "3 á", "4 à", "5 b", "6 c", "7 ")) + tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( + testkit.Rows("4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7 ")) + tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( + testkit.Rows("4 à", "5 b", "6 c", "7 ")) + tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( + testkit.Rows("4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7 ")) + tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( + testkit.Rows("4 à", "5 b", "6 c", "7 ")) + tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>t2.a order by t1.a").Check( + testkit.Rows("2 À", "3 á", "3 á", "4 à", "4 à", "4 à")) + tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>t2.a order by t1.a").Check( + testkit.Rows()) +} + +func (s *testIntegrationSerialSuite) TestCollateMergeJoin2(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := s.prepare4Join2(c) + tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ * from t1, t2 where t1.v=t2.v order by t1.id").Check( + testkit.Rows("1 a a", "2 À À", "3 á á", "4 à à", "5 b b", "6 c c", "7 ")) +} + +func (s *testIntegrationSerialSuite) TestCollateIndexMergeJoin(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (a varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci, b varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci, key(a), key(b))") + tk.MustExec("insert into t values ('a', 'x'), ('x', 'À'), ('á', 'x'), ('à', 'à'), ('à', 'x')") + + tk.MustExec("set tidb_enable_index_merge=1") + tk.MustQuery("select /*+ USE_INDEX_MERGE(t, a, b) */ * from t where a = 'a' or b = 'a'").Sort().Check( + testkit.Rows("a x", "x À", "à x", "à à", "á x")) +} + +func (s *testIntegrationSerialSuite) prepare4Collation(c *C, hasIndex bool) *testkit.TestKit { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("USE test") + tk.MustExec("drop table if exists t") + tk.MustExec("drop table if exists t_bin") + idxSQL := ", key(v)" + if !hasIndex { + idxSQL = "" + } + tk.MustExec(fmt.Sprintf("create table t (id int, v varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL %v)", idxSQL)) + tk.MustExec(fmt.Sprintf("create table t_bin (id int, v varchar(5) CHARACTER SET binary %v)", idxSQL)) + tk.MustExec("insert into t values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')") + tk.MustExec("insert into t_bin values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')") + return tk +} + +func (s *testIntegrationSerialSuite) TestCollateSelection(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := s.prepare4Collation(c, false) + tk.MustQuery("select v from t where v='a' order by id").Check(testkit.Rows("a", "À", "á", "à")) + tk.MustQuery("select v from t_bin where v='a' order by id").Check(testkit.Rows("a")) + tk.MustQuery("select v from t where v<'b' and id<=3").Check(testkit.Rows("a", "À", "á")) + tk.MustQuery("select v from t_bin where v<'b' and id<=3").Check(testkit.Rows("a")) +} + +func (s *testIntegrationSerialSuite) TestCollateSort(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := s.prepare4Collation(c, false) + tk.MustQuery("select id from t order by v, id").Check(testkit.Rows("7", "1", "2", "3", "4", "5", "6")) + tk.MustQuery("select id from t_bin order by v, id").Check(testkit.Rows("7", "1", "5", "6", "2", "4", "3")) + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a char(10) collate utf8mb4_general_ci, key(a))") + tk.MustExec("insert into t values ('a'), ('A'), ('b')") + tk.MustExec("insert into t values ('a'), ('A'), ('b')") + tk.MustExec("insert into t values ('a'), ('A'), ('b')") + tk.MustQuery("select * from t order by a collate utf8mb4_bin").Check(testkit.Rows("A", "A", "A", "a", "a", "a", "b", "b", "b")) +} + +func (s *testIntegrationSerialSuite) TestCollateHashAgg(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := s.prepare4Collation(c, false) + tk.HasPlan("select distinct(v) from t_bin", "HashAgg") + tk.MustQuery("select distinct(v) from t_bin").Sort().Check(testkit.Rows(" ", "a", "b", "c", "À", "à", "á")) + tk.HasPlan("select distinct(v) from t", "HashAgg") + tk.MustQuery("select distinct(v) from t").Sort().Check(testkit.Rows(" ", "a", "b", "c")) + tk.HasPlan("select v, count(*) from t_bin group by v", "HashAgg") + tk.MustQuery("select v, count(*) from t_bin group by v").Sort().Check(testkit.Rows(" 1", "a 1", "b 1", "c 1", "À 1", "à 1", "á 1")) + tk.HasPlan("select v, count(*) from t group by v", "HashAgg") + tk.MustQuery("select v, count(*) from t group by v").Sort().Check(testkit.Rows(" 1", "a 4", "b 1", "c 1")) + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a char(10) collate utf8mb4_general_ci, key(a))") + tk.MustExec("insert into t values ('a'), ('A'), ('b')") + tk.MustExec("insert into t values ('a'), ('A'), ('b')") + tk.MustExec("insert into t values ('a'), ('A'), ('b')") + tk.MustQuery("select count(1) from t group by a collate utf8mb4_bin").Check(testkit.Rows("3", "3", "3")) +} + +func (s *testIntegrationSerialSuite) TestCollateStreamAgg(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := s.prepare4Collation(c, true) + tk.HasPlan("select distinct(v) from t_bin", "StreamAgg") + tk.MustQuery("select distinct(v) from t_bin").Sort().Check(testkit.Rows(" ", "a", "b", "c", "À", "à", "á")) + tk.HasPlan("select distinct(v) from t", "StreamAgg") + tk.MustQuery("select distinct(v) from t").Sort().Check(testkit.Rows(" ", "a", "b", "c")) + tk.HasPlan("select v, count(*) from t_bin group by v", "StreamAgg") + tk.MustQuery("select v, count(*) from t_bin group by v").Sort().Check(testkit.Rows(" 1", "a 1", "b 1", "c 1", "À 1", "à 1", "á 1")) + tk.HasPlan("select v, count(*) from t group by v", "StreamAgg") + tk.MustQuery("select v, count(*) from t group by v").Sort().Check(testkit.Rows(" 1", "a 4", "b 1", "c 1")) +} + +func (s *testIntegrationSerialSuite) TestCollateIndexReader(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := s.prepare4Collation(c, true) + tk.HasPlan("select v from t where v < 'b' order by v", "IndexReader") + tk.MustQuery("select v from t where v < 'b' order by v").Check(testkit.Rows(" ", "a", "À", "á", "à")) + tk.HasPlan("select v from t where v < 'b' and v > ' ' order by v", "IndexReader") + tk.MustQuery("select v from t where v < 'b' and v > ' ' order by v").Check(testkit.Rows("a", "À", "á", "à")) + tk.HasPlan("select v from t_bin where v < 'b' order by v", "IndexReader") + tk.MustQuery("select v from t_bin where v < 'b' order by v").Sort().Check(testkit.Rows(" ", "a")) + tk.HasPlan("select v from t_bin where v < 'b' and v > ' ' order by v", "IndexReader") + tk.MustQuery("select v from t_bin where v < 'b' and v > ' ' order by v").Sort().Check(testkit.Rows("a")) +} + +func (s *testIntegrationSerialSuite) TestCollateIndexLookup(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := s.prepare4Collation(c, true) + + tk.HasPlan("select id from t where v < 'b'", "IndexLookUp") + tk.MustQuery("select id from t where v < 'b'").Sort().Check(testkit.Rows("1", "2", "3", "4", "7")) + tk.HasPlan("select id from t where v < 'b' and v > ' '", "IndexLookUp") + tk.MustQuery("select id from t where v < 'b' and v > ' '").Sort().Check(testkit.Rows("1", "2", "3", "4")) + tk.HasPlan("select id from t_bin where v < 'b'", "IndexLookUp") + tk.MustQuery("select id from t_bin where v < 'b'").Sort().Check(testkit.Rows("1", "7")) + tk.HasPlan("select id from t_bin where v < 'b' and v > ' '", "IndexLookUp") + tk.MustQuery("select id from t_bin where v < 'b' and v > ' '").Sort().Check(testkit.Rows("1")) +} + +func (s *testIntegrationSerialSuite) TestCollateStringFunction(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := testkit.NewTestKit(c, s.store) + + tk.MustQuery("select field('a', 'b', 'a');").Check(testkit.Rows("2")) + tk.MustQuery("select field('a', 'b', 'A');").Check(testkit.Rows("0")) + tk.MustQuery("select field('a', 'b', 'A' collate utf8mb4_bin);").Check(testkit.Rows("0")) + tk.MustQuery("select field('a', 'b', 'a ' collate utf8mb4_bin);").Check(testkit.Rows("2")) + tk.MustQuery("select field('a', 'b', 'A' collate utf8mb4_general_ci);").Check(testkit.Rows("2")) + tk.MustQuery("select field('a', 'b', 'a ' collate utf8mb4_general_ci);").Check(testkit.Rows("2")) + + tk.MustQuery("select FIND_IN_SET('a','b,a,c,d');").Check(testkit.Rows("2")) + tk.MustQuery("select FIND_IN_SET('a','b,A,c,d');").Check(testkit.Rows("0")) + tk.MustQuery("select FIND_IN_SET('a','b,A,c,d' collate utf8mb4_bin);").Check(testkit.Rows("0")) + tk.MustQuery("select FIND_IN_SET('a','b,a ,c,d' collate utf8mb4_bin);").Check(testkit.Rows("2")) + tk.MustQuery("select FIND_IN_SET('a','b,A,c,d' collate utf8mb4_general_ci);").Check(testkit.Rows("2")) + tk.MustQuery("select FIND_IN_SET('a','b,a ,c,d' collate utf8mb4_general_ci);").Check(testkit.Rows("2")) +} + +func (s *testIntegrationSerialSuite) TestCollateLike(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci") + tk.MustQuery("select 'a' like 'A'").Check(testkit.Rows("1")) + tk.MustQuery("select 'a' like 'A' collate utf8mb4_general_ci").Check(testkit.Rows("1")) + tk.MustQuery("select 'a' like 'À'").Check(testkit.Rows("1")) + tk.MustQuery("select 'a' like '%À'").Check(testkit.Rows("1")) + tk.MustQuery("select 'a' like '%À '").Check(testkit.Rows("0")) + tk.MustQuery("select 'a' like 'À%'").Check(testkit.Rows("1")) + tk.MustQuery("select 'a' like 'À_'").Check(testkit.Rows("0")) + tk.MustQuery("select 'a' like '%À%'").Check(testkit.Rows("1")) + tk.MustQuery("select 'aaa' like '%ÀAa%'").Check(testkit.Rows("1")) + tk.MustExec("set names utf8mb4 collate utf8mb4_bin") + + tk.MustExec("use test;") + tk.MustExec("drop table if exists t_like;") + tk.MustExec("create table t_like(id int, b varchar(20) collate utf8mb4_general_ci);") + tk.MustExec("insert into t_like values (1, 'aaa'), (2, 'abc'), (3, 'aac');") + tk.MustQuery("select b like 'AaÀ' from t_like order by id;").Check(testkit.Rows("1", "0", "0")) + tk.MustQuery("select b like 'Aa_' from t_like order by id;").Check(testkit.Rows("1", "0", "1")) + tk.MustQuery("select b like '_A_' from t_like order by id;").Check(testkit.Rows("1", "0", "1")) + tk.MustQuery("select b from t_like where b like 'Aa_' order by id;").Check(testkit.Rows("aaa", "aac")) + tk.MustQuery("select b from t_like where b like 'A%' order by id;").Check(testkit.Rows("aaa", "abc", "aac")) + tk.MustQuery("select b from t_like where b like '%A%' order by id;").Check(testkit.Rows("aaa", "abc", "aac")) + tk.MustExec("alter table t_like add index idx_b(b);") + tk.MustQuery("select b from t_like use index(idx_b) where b like 'Aa_' order by id;").Check(testkit.Rows("aaa", "aac")) + tk.MustQuery("select b from t_like use index(idx_b) where b like 'A%' order by id;").Check(testkit.Rows("aaa", "abc", "aac")) + tk.MustQuery("select b from t_like use index(idx_b) where b like '%A%' order by id;").Check(testkit.Rows("aaa", "abc", "aac")) +} + +func (s *testIntegrationSerialSuite) TestCollateSubQuery(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := s.prepare4Collation(c, false) + tk.MustQuery("select id from t where v in (select v from t_bin) order by id").Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7")) + tk.MustQuery("select id from t_bin where v in (select v from t) order by id").Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7")) + tk.MustQuery("select id from t where v not in (select v from t_bin) order by id").Check(testkit.Rows()) + tk.MustQuery("select id from t_bin where v not in (select v from t) order by id").Check(testkit.Rows()) + tk.MustQuery("select id from t where exists (select 1 from t_bin where t_bin.v=t.v) order by id").Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7")) + tk.MustQuery("select id from t_bin where exists (select 1 from t where t_bin.v=t.v) order by id").Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7")) + tk.MustQuery("select id from t where not exists (select 1 from t_bin where t_bin.v=t.v) order by id").Check(testkit.Rows()) + tk.MustQuery("select id from t_bin where not exists (select 1 from t where t_bin.v=t.v) order by id").Check(testkit.Rows()) +} + +func (s *testIntegrationSerialSuite) TestCollateDDL(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("create database t;") + tk.MustExec("use t;") + tk.MustExec("drop database t;") +} + +func (s *testIntegrationSuite) TestNegativeZeroForHashJoin(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test;") + tk.MustExec("CREATE TABLE t0(c0 float);") + tk.MustExec("CREATE TABLE t1(c0 float);") + tk.MustExec("INSERT INTO t1(c0) VALUES (0);") + tk.MustExec("INSERT INTO t0(c0) VALUES (0);") + tk.MustQuery("SELECT t1.c0 FROM t1, t0 WHERE t0.c0=-t1.c0;").Check(testkit.Rows("0")) + tk.MustExec("drop TABLE t0;") + tk.MustExec("drop table t1;") +} + +func (s *testIntegrationSuite) TestIssue15790(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test;") + tk.MustExec("CREATE TABLE t0(c0 INT);") + tk.MustExec("INSERT INTO t0(c0) VALUES (0);") + tk.MustQuery("SELECT * FROM t0 WHERE -10000000000000000000 | t0.c0 UNION SELECT * FROM t0;").Check(testkit.Rows("0")) + tk.MustQuery("SELECT * FROM t0 WHERE -10000000000000000000 | t0.c0 UNION all SELECT * FROM t0;").Check(testkit.Rows("0", "0")) + tk.MustExec("drop table t0;") +} +>>>>>>> 96d96ce... executor: fix unexpected error in union stmt. (#16073) From 3551825508964c148e0046028a44d8dbc1d1422f Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Wed, 8 Apr 2020 22:38:20 +0800 Subject: [PATCH 2/3] fix cherry-pick --- executor/executor.go | 3 - expression/integration_test.go | 611 --------------------------------- 2 files changed, 614 deletions(-) diff --git a/executor/executor.go b/executor/executor.go index 0ebcf91adbfd7..34d9fd852f60e 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -1541,16 +1541,13 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { sc.Priority = opts.Priority sc.NotFillCache = !opts.SQLCache } -<<<<<<< HEAD sc.CastStrToIntStrict = true -======= case *ast.UnionStmt: sc.InSelectStmt = true sc.OverflowAsWarning = true sc.TruncateAsWarning = true sc.IgnoreZeroInDate = true sc.AllowInvalidDate = vars.SQLMode.HasAllowInvalidDatesMode() ->>>>>>> 96d96ce... executor: fix unexpected error in union stmt. (#16073) case *ast.ShowStmt: sc.IgnoreTruncate = true sc.IgnoreZeroInDate = true diff --git a/expression/integration_test.go b/expression/integration_test.go index 1b25cf678c6fe..31b4df3bf4117 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4918,616 +4918,6 @@ func (s *testIntegrationSuite) TestValuesForBinaryLiteral(c *C) { tk.MustQuery("select a=0 from testValuesBinary;").Check(testkit.Rows("1")) tk.MustExec("drop table testValuesBinary;") } -<<<<<<< HEAD -======= - -func (s *testIntegrationSuite) TestIssue14159(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - tk.MustExec("DROP TABLE IF EXISTS t") - tk.MustExec("CREATE TABLE t (v VARCHAR(100))") - tk.MustExec("INSERT INTO t VALUES ('3289742893213123732904809')") - tk.MustQuery("SELECT * FROM t WHERE v").Check(testkit.Rows("3289742893213123732904809")) -} - -func (s *testIntegrationSuite) TestIssue14146(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("create table tt(a varchar(10))") - tk.MustExec("insert into tt values(NULL)") - tk.MustExec("analyze table tt;") - tk.MustQuery("select * from tt").Check(testkit.Rows("")) -} - -func (s *testIntegrationSuite) TestCacheRegexpr(c *C) { - tk := testkit.NewTestKit(c, s.store) - orgEnable := plannercore.PreparedPlanCacheEnabled() - defer func() { - plannercore.SetPreparedPlanCache(orgEnable) - }() - plannercore.SetPreparedPlanCache(true) - var err error - tk.Se, err = session.CreateSession4TestWithOpt(s.store, &session.Opt{ - PreparedPlanCache: kvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64), - }) - c.Assert(err, IsNil) - - tk.MustExec("use test") - tk.MustExec("drop table if exists t1") - tk.MustExec("create table t1 (a varchar(40))") - tk.MustExec("insert into t1 values ('C1'),('R1')") - tk.MustExec("prepare stmt1 from 'select a from t1 where a rlike ?'") - tk.MustExec("set @a='^C.*'") - tk.MustQuery("execute stmt1 using @a").Check(testkit.Rows("C1")) - tk.MustExec("set @a='^R.*'") - tk.MustQuery("execute stmt1 using @a").Check(testkit.Rows("R1")) -} - -func (s *testIntegrationSuite) TestCacheRefineArgs(c *C) { - tk := testkit.NewTestKit(c, s.store) - orgEnable := plannercore.PreparedPlanCacheEnabled() - defer func() { - plannercore.SetPreparedPlanCache(orgEnable) - }() - plannercore.SetPreparedPlanCache(true) - var err error - tk.Se, err = session.CreateSession4TestWithOpt(s.store, &session.Opt{ - PreparedPlanCache: kvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64), - }) - c.Assert(err, IsNil) - - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(col_int int)") - tk.MustExec("insert into t values(null)") - tk.MustExec("prepare stmt from 'SELECT ((col_int is true) = ?) AS res FROM t'") - tk.MustExec("set @p0='0.8'") - tk.MustQuery("execute stmt using @p0").Check(testkit.Rows("0")) - tk.MustExec("set @p0='0'") - tk.MustQuery("execute stmt using @p0").Check(testkit.Rows("1")) - - tk.MustExec("delete from t") - tk.MustExec("insert into t values(1)") - tk.MustExec("prepare stmt from 'SELECT col_int < ? FROM t'") - tk.MustExec("set @p0='-184467440737095516167.1'") - tk.MustQuery("execute stmt using @p0").Check(testkit.Rows("0")) -} - -func (s *testIntegrationSuite) TestCollation(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (utf8_bin_c varchar(10) charset utf8 collate utf8_bin, utf8_gen_c varchar(10) charset utf8 collate utf8_general_ci, bin_c binary, num_c int, " + - "abin char collate ascii_bin, lbin char collate latin1_bin, u4bin char collate utf8mb4_bin, u4ci char collate utf8mb4_general_ci)") - tk.MustExec("insert into t values ('a', 'b', 'c', 4, 'a', 'a', 'a', 'a')") - tk.MustQuery("select collation(null)").Check(testkit.Rows("binary")) - tk.MustQuery("select collation(2)").Check(testkit.Rows("binary")) - tk.MustQuery("select collation(2 + 'a')").Check(testkit.Rows("binary")) - tk.MustQuery("select collation(2 + utf8_gen_c) from t").Check(testkit.Rows("binary")) - tk.MustQuery("select collation(2 + utf8_bin_c) from t").Check(testkit.Rows("binary")) - tk.MustQuery("select collation(concat(utf8_bin_c, 2)) from t").Check(testkit.Rows("utf8_bin")) - tk.MustQuery("select collation(concat(utf8_gen_c, 'abc')) from t").Check(testkit.Rows("utf8_general_ci")) - tk.MustQuery("select collation(concat(utf8_gen_c, null)) from t").Check(testkit.Rows("utf8_general_ci")) - tk.MustQuery("select collation(concat(utf8_gen_c, num_c)) from t").Check(testkit.Rows("utf8_general_ci")) - tk.MustQuery("select collation(concat(utf8_bin_c, utf8_gen_c)) from t").Check(testkit.Rows("utf8_bin")) - tk.MustQuery("select collation(upper(utf8_bin_c)) from t").Check(testkit.Rows("utf8_bin")) - tk.MustQuery("select collation(upper(utf8_gen_c)) from t").Check(testkit.Rows("utf8_general_ci")) - tk.MustQuery("select collation(upper(bin_c)) from t").Check(testkit.Rows("binary")) - tk.MustQuery("select collation(concat(abin, bin_c)) from t").Check(testkit.Rows("binary")) - tk.MustQuery("select collation(concat(lbin, bin_c)) from t").Check(testkit.Rows("binary")) - tk.MustQuery("select collation(concat(utf8_bin_c, bin_c)) from t").Check(testkit.Rows("binary")) - tk.MustQuery("select collation(concat(utf8_gen_c, bin_c)) from t").Check(testkit.Rows("binary")) - tk.MustQuery("select collation(concat(u4bin, bin_c)) from t").Check(testkit.Rows("binary")) - tk.MustQuery("select collation(concat(u4ci, bin_c)) from t").Check(testkit.Rows("binary")) - tk.MustQuery("select collation(concat(abin, u4bin)) from t").Check(testkit.Rows("utf8mb4_bin")) - tk.MustQuery("select collation(concat(lbin, u4bin)) from t").Check(testkit.Rows("utf8mb4_bin")) - tk.MustQuery("select collation(concat(utf8_bin_c, u4bin)) from t").Check(testkit.Rows("utf8mb4_bin")) - tk.MustQuery("select collation(concat(utf8_gen_c, u4bin)) from t").Check(testkit.Rows("utf8mb4_bin")) - tk.MustQuery("select collation(concat(u4ci, u4bin)) from t").Check(testkit.Rows("utf8mb4_bin")) - tk.MustQuery("select collation(concat(abin, u4ci)) from t").Check(testkit.Rows("utf8mb4_general_ci")) - tk.MustQuery("select collation(concat(lbin, u4ci)) from t").Check(testkit.Rows("utf8mb4_general_ci")) - tk.MustQuery("select collation(concat(utf8_bin_c, u4ci)) from t").Check(testkit.Rows("utf8mb4_general_ci")) - tk.MustQuery("select collation(concat(utf8_gen_c, u4ci)) from t").Check(testkit.Rows("utf8mb4_general_ci")) - tk.MustQuery("select collation(concat(abin, utf8_bin_c)) from t").Check(testkit.Rows("utf8_bin")) - tk.MustQuery("select collation(concat(lbin, utf8_bin_c)) from t").Check(testkit.Rows("utf8_bin")) - tk.MustQuery("select collation(concat(utf8_gen_c, utf8_bin_c)) from t").Check(testkit.Rows("utf8_bin")) - tk.MustQuery("select collation(concat(abin, utf8_gen_c)) from t").Check(testkit.Rows("utf8_general_ci")) - tk.MustQuery("select collation(concat(lbin, utf8_gen_c)) from t").Check(testkit.Rows("utf8_general_ci")) - tk.MustQuery("select collation(concat(abin, lbin)) from t").Check(testkit.Rows("latin1_bin")) - - tk.MustExec("set names utf8mb4 collate utf8mb4_bin") - tk.MustQuery("select collation('a')").Check(testkit.Rows("utf8mb4_bin")) - tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci") - tk.MustQuery("select collation('a')").Check(testkit.Rows("utf8mb4_general_ci")) - - tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci") - tk.MustExec("set @test_collate_var = 'a'") - tk.MustQuery("select collation(@test_collate_var)").Check(testkit.Rows("utf8mb4_general_ci")) - tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci") - tk.MustExec("set @test_collate_var = 1") - tk.MustQuery("select collation(@test_collate_var)").Check(testkit.Rows("utf8mb4_general_ci")) - tk.MustExec("set @test_collate_var = concat(\"a\", \"b\" collate utf8mb4_bin)") - tk.MustQuery("select collation(@test_collate_var)").Check(testkit.Rows("utf8mb4_bin")) -} - -func (s *testIntegrationSuite) TestCoercibility(c *C) { - tk := testkit.NewTestKit(c, s.store) - - type testCase struct { - expr string - result int - } - testFunc := func(cases []testCase, suffix string) { - for _, tc := range cases { - tk.MustQuery(fmt.Sprintf("select coercibility(%v) %v", tc.expr, suffix)).Check(testkit.Rows(fmt.Sprintf("%v", tc.result))) - } - } - testFunc([]testCase{ - // constants - {"1", 5}, {"null", 6}, {"'abc'", 4}, - // sys-constants - {"version()", 3}, {"user()", 3}, {"database()", 3}, - {"current_role()", 3}, {"current_user()", 3}, - // scalar functions after constant folding - {"1+null", 5}, {"null+'abcde'", 5}, {"concat(null, 'abcde')", 4}, - // non-deterministic functions - {"rand()", 5}, {"now()", 5}, {"sysdate()", 5}, - }, "") - - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (i int, r real, d datetime, t timestamp, c char(10), vc varchar(10), b binary(10), vb binary(10))") - tk.MustExec("insert into t values (null, null, null, null, null, null, null, null)") - testFunc([]testCase{ - {"i", 5}, {"r", 5}, {"d", 5}, {"t", 5}, - {"c", 2}, {"b", 2}, {"vb", 2}, {"vc", 2}, - {"i+r", 5}, {"i*r", 5}, {"cos(r)+sin(i)", 5}, {"d+2", 5}, - {"t*10", 5}, {"concat(c, vc)", 2}, {"replace(c, 'x', 'y')", 2}, - }, "from t") -} - -func (s *testIntegrationSuite) TestCacheConstEval(c *C) { - tk := testkit.NewTestKit(c, s.store) - orgEnable := plannercore.PreparedPlanCacheEnabled() - defer func() { - plannercore.SetPreparedPlanCache(orgEnable) - }() - plannercore.SetPreparedPlanCache(true) - var err error - tk.Se, err = session.CreateSession4TestWithOpt(s.store, &session.Opt{ - PreparedPlanCache: kvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64), - }) - c.Assert(err, IsNil) - - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(col_double double)") - tk.MustExec("insert into t values (1)") - tk.Se.GetSessionVars().EnableVectorizedExpression = false - tk.MustExec("insert into mysql.expr_pushdown_blacklist values('cast')") - tk.MustExec("admin reload expr_pushdown_blacklist") - tk.MustExec("prepare stmt from 'SELECT * FROM (SELECT col_double AS c0 FROM t) t WHERE (ABS((REPEAT(?, ?) OR 5617780767323292672)) < LN(EXP(c0)) + (? ^ ?))'") - tk.MustExec("set @a1 = 'JuvkBX7ykVux20zQlkwDK2DFelgn7'") - tk.MustExec("set @a2 = 1") - tk.MustExec("set @a3 = -112990.35179796701") - tk.MustExec("set @a4 = 87997.92704840179") - // Main purpose here is checking no error is reported. 1 is the result when plan cache is disabled, it is - // incompatible with MySQL actually, update the result after fixing it. - tk.MustQuery("execute stmt using @a1, @a2, @a3, @a4").Check(testkit.Rows("1")) - tk.Se.GetSessionVars().EnableVectorizedExpression = true - tk.MustExec("delete from mysql.expr_pushdown_blacklist") - tk.MustExec("admin reload expr_pushdown_blacklist") -} - -func (s *testIntegrationSerialSuite) TestCollationBasic(c *C) { - tk := testkit.NewTestKit(c, s.store) - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk.MustExec("use test") - tk.MustExec("create table t_ci(a varchar(10) collate utf8mb4_general_ci, unique key(a))") - tk.MustExec("insert into t_ci values ('a')") - tk.MustQuery("select * from t_ci").Check(testkit.Rows("a")) - tk.MustQuery("select * from t_ci").Check(testkit.Rows("a")) - tk.MustQuery("select * from t_ci where a='a'").Check(testkit.Rows("a")) - tk.MustQuery("select * from t_ci where a='A'").Check(testkit.Rows("a")) - tk.MustQuery("select * from t_ci where a='a '").Check(testkit.Rows("a")) - tk.MustQuery("select * from t_ci where a='a '").Check(testkit.Rows("a")) -} - -func (s *testIntegrationSerialSuite) TestWeightString(c *C) { - tk := testkit.NewTestKit(c, s.store) - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - - type testCase struct { - input []string - result []string - resultAsChar1 []string - resultAsChar3 []string - resultAsBinary1 []string - resultAsBinary5 []string - resultExplicitCollateBin []string - } - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id int, a varchar(20) collate utf8mb4_general_ci)") - cases := testCase{ - input: []string{"aAÁàãăâ", "a", "a ", "中", "中 "}, - result: []string{"\x00A\x00A\x00A\x00A\x00A\x00A\x00A", "\x00A", "\x00A", "\x4E\x2D", "\x4E\x2D"}, - resultAsChar1: []string{"\x00A", "\x00A", "\x00A", "\x4E\x2D", "\x4E\x2D"}, - resultAsChar3: []string{"\x00A\x00A\x00A", "\x00A", "\x00A", "\x4E\x2D", "\x4E\x2D"}, - resultAsBinary1: []string{"a", "a", "a", "\xE4", "\xE4"}, - resultAsBinary5: []string{"aA\xc3\x81\xc3", "a\x00\x00\x00\x00", "a \x00\x00", "中\x00\x00", "中 \x00"}, - resultExplicitCollateBin: []string{"aAÁàãăâ", "a", "a", "中", "中"}, - } - values := make([]string, len(cases.input)) - for i, input := range cases.input { - values[i] = fmt.Sprintf("(%d, '%s')", i, input) - } - tk.MustExec("insert into t values " + strings.Join(values, ",")) - rows := tk.MustQuery("select weight_string(a) from t order by id").Rows() - for i, out := range cases.result { - c.Assert(rows[i][0].(string), Equals, out) - } - rows = tk.MustQuery("select weight_string(a as char(1)) from t order by id").Rows() - for i, out := range cases.resultAsChar1 { - c.Assert(rows[i][0].(string), Equals, out) - } - rows = tk.MustQuery("select weight_string(a as char(3)) from t order by id").Rows() - for i, out := range cases.resultAsChar3 { - c.Assert(rows[i][0].(string), Equals, out) - } - rows = tk.MustQuery("select weight_string(a as binary(1)) from t order by id").Rows() - for i, out := range cases.resultAsBinary1 { - c.Assert(rows[i][0].(string), Equals, out) - } - rows = tk.MustQuery("select weight_string(a as binary(5)) from t order by id").Rows() - for i, out := range cases.resultAsBinary5 { - c.Assert(rows[i][0].(string), Equals, out) - } - c.Assert(tk.MustQuery("select weight_string(NULL);").Rows()[0][0], Equals, "") - c.Assert(tk.MustQuery("select weight_string(7);").Rows()[0][0], Equals, "") - c.Assert(tk.MustQuery("select weight_string(cast(7 as decimal(5)));").Rows()[0][0], Equals, "") - c.Assert(tk.MustQuery("select weight_string(cast(20190821 as date));").Rows()[0][0], Equals, "2019-08-21") - c.Assert(tk.MustQuery("select weight_string(cast(20190821 as date) as binary(5));").Rows()[0][0], Equals, "2019-") - c.Assert(tk.MustQuery("select weight_string(7.0);").Rows()[0][0], Equals, "") - c.Assert(tk.MustQuery("select weight_string(7 AS BINARY(2));").Rows()[0][0], Equals, "7\x00") - // test explicit collation - c.Assert(tk.MustQuery("select weight_string('中 ' collate utf8mb4_general_ci);").Rows()[0][0], Equals, "\x4E\x2D") - c.Assert(tk.MustQuery("select weight_string('中 ' collate utf8mb4_bin);").Rows()[0][0], Equals, "中") - c.Assert(tk.MustQuery("select collation(a collate utf8mb4_general_ci) from t order by id").Rows()[0][0], Equals, "utf8mb4_general_ci") - c.Assert(tk.MustQuery("select collation('中 ' collate utf8mb4_general_ci);").Rows()[0][0], Equals, "utf8mb4_general_ci") - rows = tk.MustQuery("select weight_string(a collate utf8mb4_bin) from t order by id").Rows() - for i, out := range cases.resultExplicitCollateBin { - c.Assert(rows[i][0].(string), Equals, out) - } - tk.MustGetErrMsg("select weight_string(a collate utf8_general_ci) from t order by id", "[ddl:1253]COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'utf8mb4'") - tk.MustGetErrMsg("select weight_string('中' collate utf8_bin)", "[ddl:1253]COLLATION 'utf8_bin' is not valid for CHARACTER SET 'utf8mb4'") -} - -func (s *testIntegrationSerialSuite) TestCollationCreateIndex(c *C) { - tk := testkit.NewTestKit(c, s.store) - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a varchar(10) collate utf8mb4_general_ci);") - tk.MustExec("insert into t values ('a');") - tk.MustExec("insert into t values ('A');") - tk.MustExec("insert into t values ('b');") - tk.MustExec("insert into t values ('B');") - tk.MustExec("insert into t values ('a');") - tk.MustExec("insert into t values ('A');") - tk.MustExec("create index idx on t(a);") - tk.MustQuery("select * from t order by a").Check(testkit.Rows("a", "A", "a", "A", "b", "B")) -} - -func (s *testIntegrationSerialSuite) TestCollateConstantPropagation(c *C) { - tk := testkit.NewTestKit(c, s.store) - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a char(10) collate utf8mb4_bin, b char(10) collate utf8mb4_general_ci);") - tk.MustExec("insert into t values ('a', 'A');") - tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b='a' collate utf8mb4_general_ci;").Check(nil) - tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b>='a' collate utf8mb4_general_ci;").Check(nil) - tk.MustExec("drop table t;") - tk.MustExec("create table t (a char(10) collate utf8mb4_general_ci, b char(10) collate utf8mb4_general_ci);") - tk.MustExec("insert into t values ('A', 'a');") - tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b='a' collate utf8mb4_bin;").Check(testkit.Rows("A a A a")) - tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b>='a' collate utf8mb4_bin;").Check(testkit.Rows("A a A a")) -} -func (s *testIntegrationSerialSuite) prepare4Join(c *C) *testkit.TestKit { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("USE test") - tk.MustExec("drop table if exists t") - tk.MustExec("drop table if exists t_bin") - tk.MustExec("CREATE TABLE `t` ( `a` int(11) NOT NULL,`b` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL)") - tk.MustExec("CREATE TABLE `t_bin` ( `a` int(11) NOT NULL,`b` varchar(5) CHARACTER SET binary)") - tk.MustExec("insert into t values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')") - tk.MustExec("insert into t_bin values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')") - return tk -} - -func (s *testIntegrationSerialSuite) prepare4Join2(c *C) *testkit.TestKit { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("USE test") - tk.MustExec("drop table if exists t1") - tk.MustExec("drop table if exists t2") - tk.MustExec("create table t1 (id int, v varchar(5) character set binary, key(v))") - tk.MustExec("create table t2 (v varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci, key(v))") - tk.MustExec("insert into t1 values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')") - tk.MustExec("insert into t2 values ('a'), ('À'), ('á'), ('à'), ('b'), ('c'), (' ')") - return tk -} - -func (s *testIntegrationSerialSuite) TestCollateHashJoin(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := s.prepare4Join(c) - tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b order by t1.a").Check( - testkit.Rows("1 a", "1 a", "1 a", "1 a", "2 À", "2 À", "2 À", "2 À", "3 á", "3 á", "3 á", "3 á", "4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7 ")) - tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b order by t1.a").Check( - testkit.Rows("1 a", "2 À", "3 á", "4 à", "5 b", "6 c", "7 ")) - tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( - testkit.Rows("4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7 ")) - tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( - testkit.Rows("4 à", "5 b", "6 c", "7 ")) - tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( - testkit.Rows("4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7 ")) - tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( - testkit.Rows("4 à", "5 b", "6 c", "7 ")) - tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>t2.a order by t1.a").Check( - testkit.Rows("2 À", "3 á", "3 á", "4 à", "4 à", "4 à")) - tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>t2.a order by t1.a").Check( - testkit.Rows()) -} - -func (s *testIntegrationSerialSuite) TestCollateHashJoin2(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := s.prepare4Join2(c) - tk.MustQuery("select /*+ TIDB_HJ(t1, t2) */ * from t1, t2 where t1.v=t2.v order by t1.id").Check( - testkit.Rows("1 a a", "2 À À", "3 á á", "4 à à", "5 b b", "6 c c", "7 ")) -} - -func (s *testIntegrationSerialSuite) TestCollateMergeJoin(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := s.prepare4Join(c) - tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b order by t1.a").Check( - testkit.Rows("1 a", "1 a", "1 a", "1 a", "2 À", "2 À", "2 À", "2 À", "3 á", "3 á", "3 á", "3 á", "4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7 ")) - tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b order by t1.a").Check( - testkit.Rows("1 a", "2 À", "3 á", "4 à", "5 b", "6 c", "7 ")) - tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( - testkit.Rows("4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7 ")) - tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( - testkit.Rows("4 à", "5 b", "6 c", "7 ")) - tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( - testkit.Rows("4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7 ")) - tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check( - testkit.Rows("4 à", "5 b", "6 c", "7 ")) - tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>t2.a order by t1.a").Check( - testkit.Rows("2 À", "3 á", "3 á", "4 à", "4 à", "4 à")) - tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>t2.a order by t1.a").Check( - testkit.Rows()) -} - -func (s *testIntegrationSerialSuite) TestCollateMergeJoin2(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := s.prepare4Join2(c) - tk.MustQuery("select /*+ TIDB_SMJ(t1, t2) */ * from t1, t2 where t1.v=t2.v order by t1.id").Check( - testkit.Rows("1 a a", "2 À À", "3 á á", "4 à à", "5 b b", "6 c c", "7 ")) -} - -func (s *testIntegrationSerialSuite) TestCollateIndexMergeJoin(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci, b varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci, key(a), key(b))") - tk.MustExec("insert into t values ('a', 'x'), ('x', 'À'), ('á', 'x'), ('à', 'à'), ('à', 'x')") - - tk.MustExec("set tidb_enable_index_merge=1") - tk.MustQuery("select /*+ USE_INDEX_MERGE(t, a, b) */ * from t where a = 'a' or b = 'a'").Sort().Check( - testkit.Rows("a x", "x À", "à x", "à à", "á x")) -} - -func (s *testIntegrationSerialSuite) prepare4Collation(c *C, hasIndex bool) *testkit.TestKit { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("USE test") - tk.MustExec("drop table if exists t") - tk.MustExec("drop table if exists t_bin") - idxSQL := ", key(v)" - if !hasIndex { - idxSQL = "" - } - tk.MustExec(fmt.Sprintf("create table t (id int, v varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL %v)", idxSQL)) - tk.MustExec(fmt.Sprintf("create table t_bin (id int, v varchar(5) CHARACTER SET binary %v)", idxSQL)) - tk.MustExec("insert into t values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')") - tk.MustExec("insert into t_bin values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')") - return tk -} - -func (s *testIntegrationSerialSuite) TestCollateSelection(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := s.prepare4Collation(c, false) - tk.MustQuery("select v from t where v='a' order by id").Check(testkit.Rows("a", "À", "á", "à")) - tk.MustQuery("select v from t_bin where v='a' order by id").Check(testkit.Rows("a")) - tk.MustQuery("select v from t where v<'b' and id<=3").Check(testkit.Rows("a", "À", "á")) - tk.MustQuery("select v from t_bin where v<'b' and id<=3").Check(testkit.Rows("a")) -} - -func (s *testIntegrationSerialSuite) TestCollateSort(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := s.prepare4Collation(c, false) - tk.MustQuery("select id from t order by v, id").Check(testkit.Rows("7", "1", "2", "3", "4", "5", "6")) - tk.MustQuery("select id from t_bin order by v, id").Check(testkit.Rows("7", "1", "5", "6", "2", "4", "3")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(10) collate utf8mb4_general_ci, key(a))") - tk.MustExec("insert into t values ('a'), ('A'), ('b')") - tk.MustExec("insert into t values ('a'), ('A'), ('b')") - tk.MustExec("insert into t values ('a'), ('A'), ('b')") - tk.MustQuery("select * from t order by a collate utf8mb4_bin").Check(testkit.Rows("A", "A", "A", "a", "a", "a", "b", "b", "b")) -} - -func (s *testIntegrationSerialSuite) TestCollateHashAgg(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := s.prepare4Collation(c, false) - tk.HasPlan("select distinct(v) from t_bin", "HashAgg") - tk.MustQuery("select distinct(v) from t_bin").Sort().Check(testkit.Rows(" ", "a", "b", "c", "À", "à", "á")) - tk.HasPlan("select distinct(v) from t", "HashAgg") - tk.MustQuery("select distinct(v) from t").Sort().Check(testkit.Rows(" ", "a", "b", "c")) - tk.HasPlan("select v, count(*) from t_bin group by v", "HashAgg") - tk.MustQuery("select v, count(*) from t_bin group by v").Sort().Check(testkit.Rows(" 1", "a 1", "b 1", "c 1", "À 1", "à 1", "á 1")) - tk.HasPlan("select v, count(*) from t group by v", "HashAgg") - tk.MustQuery("select v, count(*) from t group by v").Sort().Check(testkit.Rows(" 1", "a 4", "b 1", "c 1")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(10) collate utf8mb4_general_ci, key(a))") - tk.MustExec("insert into t values ('a'), ('A'), ('b')") - tk.MustExec("insert into t values ('a'), ('A'), ('b')") - tk.MustExec("insert into t values ('a'), ('A'), ('b')") - tk.MustQuery("select count(1) from t group by a collate utf8mb4_bin").Check(testkit.Rows("3", "3", "3")) -} - -func (s *testIntegrationSerialSuite) TestCollateStreamAgg(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := s.prepare4Collation(c, true) - tk.HasPlan("select distinct(v) from t_bin", "StreamAgg") - tk.MustQuery("select distinct(v) from t_bin").Sort().Check(testkit.Rows(" ", "a", "b", "c", "À", "à", "á")) - tk.HasPlan("select distinct(v) from t", "StreamAgg") - tk.MustQuery("select distinct(v) from t").Sort().Check(testkit.Rows(" ", "a", "b", "c")) - tk.HasPlan("select v, count(*) from t_bin group by v", "StreamAgg") - tk.MustQuery("select v, count(*) from t_bin group by v").Sort().Check(testkit.Rows(" 1", "a 1", "b 1", "c 1", "À 1", "à 1", "á 1")) - tk.HasPlan("select v, count(*) from t group by v", "StreamAgg") - tk.MustQuery("select v, count(*) from t group by v").Sort().Check(testkit.Rows(" 1", "a 4", "b 1", "c 1")) -} - -func (s *testIntegrationSerialSuite) TestCollateIndexReader(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := s.prepare4Collation(c, true) - tk.HasPlan("select v from t where v < 'b' order by v", "IndexReader") - tk.MustQuery("select v from t where v < 'b' order by v").Check(testkit.Rows(" ", "a", "À", "á", "à")) - tk.HasPlan("select v from t where v < 'b' and v > ' ' order by v", "IndexReader") - tk.MustQuery("select v from t where v < 'b' and v > ' ' order by v").Check(testkit.Rows("a", "À", "á", "à")) - tk.HasPlan("select v from t_bin where v < 'b' order by v", "IndexReader") - tk.MustQuery("select v from t_bin where v < 'b' order by v").Sort().Check(testkit.Rows(" ", "a")) - tk.HasPlan("select v from t_bin where v < 'b' and v > ' ' order by v", "IndexReader") - tk.MustQuery("select v from t_bin where v < 'b' and v > ' ' order by v").Sort().Check(testkit.Rows("a")) -} - -func (s *testIntegrationSerialSuite) TestCollateIndexLookup(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := s.prepare4Collation(c, true) - - tk.HasPlan("select id from t where v < 'b'", "IndexLookUp") - tk.MustQuery("select id from t where v < 'b'").Sort().Check(testkit.Rows("1", "2", "3", "4", "7")) - tk.HasPlan("select id from t where v < 'b' and v > ' '", "IndexLookUp") - tk.MustQuery("select id from t where v < 'b' and v > ' '").Sort().Check(testkit.Rows("1", "2", "3", "4")) - tk.HasPlan("select id from t_bin where v < 'b'", "IndexLookUp") - tk.MustQuery("select id from t_bin where v < 'b'").Sort().Check(testkit.Rows("1", "7")) - tk.HasPlan("select id from t_bin where v < 'b' and v > ' '", "IndexLookUp") - tk.MustQuery("select id from t_bin where v < 'b' and v > ' '").Sort().Check(testkit.Rows("1")) -} - -func (s *testIntegrationSerialSuite) TestCollateStringFunction(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := testkit.NewTestKit(c, s.store) - - tk.MustQuery("select field('a', 'b', 'a');").Check(testkit.Rows("2")) - tk.MustQuery("select field('a', 'b', 'A');").Check(testkit.Rows("0")) - tk.MustQuery("select field('a', 'b', 'A' collate utf8mb4_bin);").Check(testkit.Rows("0")) - tk.MustQuery("select field('a', 'b', 'a ' collate utf8mb4_bin);").Check(testkit.Rows("2")) - tk.MustQuery("select field('a', 'b', 'A' collate utf8mb4_general_ci);").Check(testkit.Rows("2")) - tk.MustQuery("select field('a', 'b', 'a ' collate utf8mb4_general_ci);").Check(testkit.Rows("2")) - - tk.MustQuery("select FIND_IN_SET('a','b,a,c,d');").Check(testkit.Rows("2")) - tk.MustQuery("select FIND_IN_SET('a','b,A,c,d');").Check(testkit.Rows("0")) - tk.MustQuery("select FIND_IN_SET('a','b,A,c,d' collate utf8mb4_bin);").Check(testkit.Rows("0")) - tk.MustQuery("select FIND_IN_SET('a','b,a ,c,d' collate utf8mb4_bin);").Check(testkit.Rows("2")) - tk.MustQuery("select FIND_IN_SET('a','b,A,c,d' collate utf8mb4_general_ci);").Check(testkit.Rows("2")) - tk.MustQuery("select FIND_IN_SET('a','b,a ,c,d' collate utf8mb4_general_ci);").Check(testkit.Rows("2")) -} - -func (s *testIntegrationSerialSuite) TestCollateLike(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci") - tk.MustQuery("select 'a' like 'A'").Check(testkit.Rows("1")) - tk.MustQuery("select 'a' like 'A' collate utf8mb4_general_ci").Check(testkit.Rows("1")) - tk.MustQuery("select 'a' like 'À'").Check(testkit.Rows("1")) - tk.MustQuery("select 'a' like '%À'").Check(testkit.Rows("1")) - tk.MustQuery("select 'a' like '%À '").Check(testkit.Rows("0")) - tk.MustQuery("select 'a' like 'À%'").Check(testkit.Rows("1")) - tk.MustQuery("select 'a' like 'À_'").Check(testkit.Rows("0")) - tk.MustQuery("select 'a' like '%À%'").Check(testkit.Rows("1")) - tk.MustQuery("select 'aaa' like '%ÀAa%'").Check(testkit.Rows("1")) - tk.MustExec("set names utf8mb4 collate utf8mb4_bin") - - tk.MustExec("use test;") - tk.MustExec("drop table if exists t_like;") - tk.MustExec("create table t_like(id int, b varchar(20) collate utf8mb4_general_ci);") - tk.MustExec("insert into t_like values (1, 'aaa'), (2, 'abc'), (3, 'aac');") - tk.MustQuery("select b like 'AaÀ' from t_like order by id;").Check(testkit.Rows("1", "0", "0")) - tk.MustQuery("select b like 'Aa_' from t_like order by id;").Check(testkit.Rows("1", "0", "1")) - tk.MustQuery("select b like '_A_' from t_like order by id;").Check(testkit.Rows("1", "0", "1")) - tk.MustQuery("select b from t_like where b like 'Aa_' order by id;").Check(testkit.Rows("aaa", "aac")) - tk.MustQuery("select b from t_like where b like 'A%' order by id;").Check(testkit.Rows("aaa", "abc", "aac")) - tk.MustQuery("select b from t_like where b like '%A%' order by id;").Check(testkit.Rows("aaa", "abc", "aac")) - tk.MustExec("alter table t_like add index idx_b(b);") - tk.MustQuery("select b from t_like use index(idx_b) where b like 'Aa_' order by id;").Check(testkit.Rows("aaa", "aac")) - tk.MustQuery("select b from t_like use index(idx_b) where b like 'A%' order by id;").Check(testkit.Rows("aaa", "abc", "aac")) - tk.MustQuery("select b from t_like use index(idx_b) where b like '%A%' order by id;").Check(testkit.Rows("aaa", "abc", "aac")) -} - -func (s *testIntegrationSerialSuite) TestCollateSubQuery(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := s.prepare4Collation(c, false) - tk.MustQuery("select id from t where v in (select v from t_bin) order by id").Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7")) - tk.MustQuery("select id from t_bin where v in (select v from t) order by id").Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7")) - tk.MustQuery("select id from t where v not in (select v from t_bin) order by id").Check(testkit.Rows()) - tk.MustQuery("select id from t_bin where v not in (select v from t) order by id").Check(testkit.Rows()) - tk.MustQuery("select id from t where exists (select 1 from t_bin where t_bin.v=t.v) order by id").Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7")) - tk.MustQuery("select id from t_bin where exists (select 1 from t where t_bin.v=t.v) order by id").Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7")) - tk.MustQuery("select id from t where not exists (select 1 from t_bin where t_bin.v=t.v) order by id").Check(testkit.Rows()) - tk.MustQuery("select id from t_bin where not exists (select 1 from t where t_bin.v=t.v) order by id").Check(testkit.Rows()) -} - -func (s *testIntegrationSerialSuite) TestCollateDDL(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("create database t;") - tk.MustExec("use t;") - tk.MustExec("drop database t;") -} - -func (s *testIntegrationSuite) TestNegativeZeroForHashJoin(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test;") - tk.MustExec("CREATE TABLE t0(c0 float);") - tk.MustExec("CREATE TABLE t1(c0 float);") - tk.MustExec("INSERT INTO t1(c0) VALUES (0);") - tk.MustExec("INSERT INTO t0(c0) VALUES (0);") - tk.MustQuery("SELECT t1.c0 FROM t1, t0 WHERE t0.c0=-t1.c0;").Check(testkit.Rows("0")) - tk.MustExec("drop TABLE t0;") - tk.MustExec("drop table t1;") -} func (s *testIntegrationSuite) TestIssue15790(c *C) { tk := testkit.NewTestKit(c, s.store) @@ -5538,4 +4928,3 @@ func (s *testIntegrationSuite) TestIssue15790(c *C) { tk.MustQuery("SELECT * FROM t0 WHERE -10000000000000000000 | t0.c0 UNION all SELECT * FROM t0;").Check(testkit.Rows("0", "0")) tk.MustExec("drop table t0;") } ->>>>>>> 96d96ce... executor: fix unexpected error in union stmt. (#16073) From d3097057cb2bfdc1eebad51fb2034bbf4fb179f3 Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Thu, 9 Apr 2020 11:40:58 +0800 Subject: [PATCH 3/3] fix cherry-pick --- expression/integration_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/expression/integration_test.go b/expression/integration_test.go index 875308dcfa448..27173fe636c4a 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4928,6 +4928,7 @@ func (s *testIntegrationSuite) TestIssue15790(c *C) { tk.MustQuery("SELECT * FROM t0 WHERE -10000000000000000000 | t0.c0 UNION SELECT * FROM t0;").Check(testkit.Rows("0")) tk.MustQuery("SELECT * FROM t0 WHERE -10000000000000000000 | t0.c0 UNION all SELECT * FROM t0;").Check(testkit.Rows("0", "0")) tk.MustExec("drop table t0;") +} func (s *testIntegrationSuite) TestCacheRefineArgs(c *C) { tk := testkit.NewTestKit(c, s.store)