Skip to content

Commit

Permalink
resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongjiwei committed Sep 9, 2021
1 parent f58604a commit ac41de7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 324 deletions.
7 changes: 4 additions & 3 deletions expression/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func newBaseBuiltinFunc(ctx sessionctx.Context, funcName string, args []Expressi
if ctx == nil {
return baseBuiltinFunc{}, errors.New("unexpected nil session ctx")
}
if err := checkIllegalMixCollation(funcName, args, retType); err != nil {
if err := CheckIllegalMixCollation(funcName, args, retType); err != nil {
return baseBuiltinFunc{}, err
}
derivedCharset, derivedCollate := DeriveCollationFromExprs(ctx, args...)
Expand All @@ -113,7 +113,8 @@ var (
coerString = []string{"EXPLICIT", "NONE", "IMPLICIT", "SYSCONST", "COERCIBLE", "NUMERIC", "IGNORABLE"}
)

func checkIllegalMixCollation(funcName string, args []Expression, evalType types.EvalType) error {
// CheckIllegalMixCollation check the if the aggregate expression is legal.
func CheckIllegalMixCollation(funcName string, args []Expression, evalType types.EvalType) error {
if len(args) < 2 {
return nil
}
Expand Down Expand Up @@ -170,7 +171,7 @@ func newBaseBuiltinFuncWithTp(ctx sessionctx.Context, funcName string, args []Ex
}
}

if err = checkIllegalMixCollation(funcName, args, retType); err != nil {
if err = CheckIllegalMixCollation(funcName, args, retType); err != nil {
return
}

Expand Down
265 changes: 0 additions & 265 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8060,271 +8060,6 @@ func (s *testIntegrationSuite) TestDatetimeUserVariable(c *C) {
c.Check(tk.MustQuery("select @p").Rows()[0][0] != "", IsTrue)
}

<<<<<<< HEAD
=======
func (s *testIntegrationSuite) TestIssue12205(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("use test")
tk.MustExec("drop table if exists t12205;")
tk.MustExec("create table t12205(\n `col_varchar_64` varchar(64) DEFAULT NULL,\n `col_varchar_64_key` varchar(64) DEFAULT NULL\n);")
tk.MustExec("insert into t12205 values('-1038024704','-527892480');")
tk.MustQuery("select SEC_TO_TIME( ( `col_varchar_64` & `col_varchar_64_key` ) ),`col_varchar_64` & `col_varchar_64_key` from t12205; ").Check(
testkit.Rows("838:59:59 18446744072635875328"))
tk.MustQuery("show warnings;").Check(
testkit.Rows("Warning 1292 Truncated incorrect time value: '18446744072635875000'"))
}

// for issue 20128
func (s *testIntegrationSerialSuite) TestIssue20128(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(b enum('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z') DEFAULT NULL, c decimal(40,20));")
tk.MustExec("insert into t values('z', 19.18040000000000000000);")
tk.MustExec("insert into t values('z', 26.18040000000000000000);")
tk.MustExec("insert into t values('z', 25.18040000000000000000);")
tk.MustQuery("select * from t where t.b > t.c;").Check(testkit.Rows("z 19.18040000000000000000", "z 25.18040000000000000000"))
tk.MustQuery("select * from t where t.b < t.c;").Check(testkit.Rows("z 26.18040000000000000000"))
}

func (s *testIntegrationSuite) TestIssue21677(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(1e int);")
tk.MustExec("insert into t values (1);")
tk.MustQuery("select t.1e from test.t;").Check(testkit.Rows("1"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(99e int, r10 int);")
tk.MustExec("insert into t values (1, 10), (2, 2);")
tk.MustQuery("select 99e+r10 from t;").Check(testkit.Rows("11", "4"))
tk.MustQuery("select .78$123;").Check(testkit.Rows("0.78"))
tk.MustGetErrCode("select .78$421+1;", mysql.ErrParse)
tk.MustQuery("select t. `r10` > 3 from t;").Check(testkit.Rows("1", "0"))
tk.MustQuery("select * from t where t. `r10` > 3;").Check(testkit.Rows("1 10"))
}

func (s *testIntegrationSerialSuite) TestLikeWithCollation(c *C) {
tk := testkit.NewTestKit(c, s.store)
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)

tk.MustQuery(`select 'a' like 'A' collate utf8mb4_unicode_ci;`).Check(testkit.Rows("1"))
tk.MustGetErrMsg(`select 'a' collate utf8mb4_bin like 'A' collate utf8mb4_unicode_ci;`, "[expression:1270]Illegal mix of collations (utf8mb4_bin,EXPLICIT), (utf8mb4_unicode_ci,EXPLICIT), (binary,NUMERIC) for operation 'like'")
tk.MustQuery(`select '😛' collate utf8mb4_general_ci like '😋';`).Check(testkit.Rows("1"))
tk.MustQuery(`select '😛' collate utf8mb4_general_ci = '😋';`).Check(testkit.Rows("1"))
tk.MustQuery(`select '😛' collate utf8mb4_unicode_ci like '😋';`).Check(testkit.Rows("0"))
tk.MustQuery(`select '😛' collate utf8mb4_unicode_ci = '😋';`).Check(testkit.Rows("1"))
}

func (s *testIntegrationSuite) TestIssue11333(c *C) {
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t(col1 decimal);")
tk.MustExec(" insert into t values(0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000);")
tk.MustQuery(`select * from t;`).Check(testkit.Rows("0"))
tk.MustExec("create table t1(col1 decimal(65,30));")
tk.MustExec(" insert into t1 values(0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000);")
tk.MustQuery(`select * from t1;`).Check(testkit.Rows("0.000000000000000000000000000000"))
tk.MustQuery(`select 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;`).Check(testkit.Rows("0.000000000000000000000000000000000000000000000000000000000000000000000000"))
tk.MustQuery(`select 0.0000000000000000000000000000000000000000000000000000000000000000000000012;`).Check(testkit.Rows("0.000000000000000000000000000000000000000000000000000000000000000000000001"))
tk.MustQuery(`select 0.000000000000000000000000000000000000000000000000000000000000000000000001;`).Check(testkit.Rows("0.000000000000000000000000000000000000000000000000000000000000000000000001"))
}

func (s *testSuite) TestIssue12206(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t12206;")
tk.MustExec("create table t12206(\n `col_tinyint_unsigned` tinyint(3) unsigned DEFAULT NULL,\n `col_double_unsigned` double unsigned DEFAULT NULL,\n `col_year_key` year(4) DEFAULT NULL\n);")
tk.MustExec("insert into t12206 values(73,0,0000);")
tk.MustQuery("SELECT TIME_FORMAT( `col_tinyint_unsigned`, ( IFNULL( `col_double_unsigned`, `col_year_key` ) ) ) AS field1 FROM `t12206`;").Check(
testkit.Rows("<nil>"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect time value: '73'"))
}

func (s *testIntegrationSuite2) TestCastCoer(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustQuery("select coercibility(binary('a'))").Check(testkit.Rows("2"))
tk.MustQuery("select coercibility(cast('a' as char(10)))").Check(testkit.Rows("2"))
tk.MustQuery("select coercibility(convert('abc', char(10)));").Check(testkit.Rows("2"))
}

func (s *testIntegrationSuite) TestIssue12209(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("use test")
tk.MustExec("drop table if exists t12209;")
tk.MustExec("create table t12209(a bigint(20));")
tk.MustExec("insert into t12209 values(1);")
tk.MustQuery("select `a` DIV ( ROUND( ( SCHEMA() ), '1978-05-18 03:35:52.043591' ) ) from `t12209`;").Check(
testkit.Rows("<nil>"))
}

func (s *testIntegrationSerialSuite) TestCrossDCQuery(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec(`create table t1 (c int primary key, d int,e int,index idx_d(d),index idx_e(e))
PARTITION BY RANGE (c) (
PARTITION p0 VALUES LESS THAN (6),
PARTITION p1 VALUES LESS THAN (11)
);`)
defer tk.MustExec("drop table if exists t1")

tk.MustExec(`insert into t1 (c,d,e) values (1,1,1);`)
tk.MustExec(`insert into t1 (c,d,e) values (2,3,5);`)
tk.MustExec(`insert into t1 (c,d,e) values (3,5,7);`)

is := s.dom.InfoSchema()

tb, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
c.Assert(err, IsNil)
setBundle := func(parName, dc string) {
pid, err := tables.FindPartitionByName(tb.Meta(), parName)
c.Assert(err, IsNil)
groupID := placement.GroupID(pid)
is.SetBundle(&placement.Bundle{
ID: groupID,
Rules: []*placement.Rule{
{
GroupID: groupID,
Role: placement.Leader,
Count: 1,
Constraints: []placement.Constraint{
{
Key: placement.DCLabelKey,
Op: placement.In,
Values: []string{dc},
},
},
},
},
})
}
setBundle("p0", "sh")
setBundle("p1", "bj")

testcases := []struct {
name string
txnScope string
zone string
sql string
expectErr error
}{
// FIXME: block by https://github.com/pingcap/tidb/issues/21872
//{
// name: "cross dc read to sh by holding bj, IndexReader",
// txnScope: "bj",
// sql: "select /*+ USE_INDEX(t1, idx_d) */ d from t1 where c < 5 and d < 1;",
// expectErr: fmt.Errorf(".*can not be read by.*"),
//},
// FIXME: block by https://github.com/pingcap/tidb/issues/21847
//{
// name: "cross dc read to sh by holding bj, BatchPointGet",
// txnScope: "bj",
// sql: "select * from t1 where c in (1,2,3,4);",
// expectErr: fmt.Errorf(".*can not be read by.*"),
//},
{
name: "cross dc read to sh by holding bj, PointGet",
txnScope: "local",
zone: "bj",
sql: "select * from t1 where c = 1",
expectErr: fmt.Errorf(".*can not be read by.*"),
},
{
name: "cross dc read to sh by holding bj, IndexLookUp",
txnScope: "local",
zone: "bj",
sql: "select * from t1 use index (idx_d) where c < 5 and d < 5;",
expectErr: fmt.Errorf(".*can not be read by.*"),
},
{
name: "cross dc read to sh by holding bj, IndexMerge",
txnScope: "local",
zone: "bj",
sql: "select /*+ USE_INDEX_MERGE(t1, idx_d, idx_e) */ * from t1 where c <5 and (d =5 or e=5);",
expectErr: fmt.Errorf(".*can not be read by.*"),
},
{
name: "cross dc read to sh by holding bj, TableReader",
txnScope: "local",
zone: "bj",
sql: "select * from t1 where c < 6",
expectErr: fmt.Errorf(".*can not be read by.*"),
},
{
name: "cross dc read to global by holding bj",
txnScope: "local",
zone: "bj",
sql: "select * from t1",
expectErr: fmt.Errorf(".*can not be read by.*"),
},
{
name: "read sh dc by holding sh",
txnScope: "local",
zone: "sh",
sql: "select * from t1 where c < 6",
expectErr: nil,
},
{
name: "read sh dc by holding global",
txnScope: "global",
zone: "",
sql: "select * from t1 where c < 6",
expectErr: nil,
},
}
tk.MustExec("set global tidb_enable_local_txn = on;")
for _, testcase := range testcases {
c.Log(testcase.name)
failpoint.Enable("tikvclient/injectTxnScope",
fmt.Sprintf(`return("%v")`, testcase.zone))
tk.MustExec(fmt.Sprintf("set @@txn_scope='%v'", testcase.txnScope))
tk.Exec("begin")
res, err := tk.Exec(testcase.sql)
_, resErr := session.GetRows4Test(context.Background(), tk.Se, res)
var checkErr error
if err != nil {
checkErr = err
} else {
checkErr = resErr
}
if testcase.expectErr != nil {
c.Assert(checkErr, NotNil)
c.Assert(checkErr.Error(), Matches, ".*can not be read by.*")
} else {
c.Assert(checkErr, IsNil)
}
if res != nil {
res.Close()
}
tk.Exec("commit")
}
failpoint.Disable("tikvclient/injectTxnScope")
tk.MustExec("set global tidb_enable_local_txn = off;")
}

func (s *testIntegrationSerialSuite) TestCollationUnion(c *C) {
// For issue 19694.
tk := testkit.NewTestKit(c, s.store)

tk.MustQuery("select cast('2010-09-09' as date) a union select '2010-09-09 ' order by a;").Check(testkit.Rows("2010-09-09", "2010-09-09 "))
res := tk.MustQuery("select cast('2010-09-09' as date) a union select '2010-09-09 ';")
c.Check(len(res.Rows()), Equals, 2)
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)
res = tk.MustQuery("select cast('2010-09-09' as date) a union select '2010-09-09 ';")
c.Check(len(res.Rows()), Equals, 1)
}

>>>>>>> d5baa5dad... planner: fix expression rewrite makes between expr infers wrong collation. (#27254)
func (s *testIntegrationSuite) TestIssue22098(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
2 changes: 2 additions & 0 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,8 @@ func (er *expressionRewriter) betweenToExpression(v *ast.BetweenExpr) {
}
l.SetCharsetAndCollation(dstCharset, dstCollation)
r.SetCharsetAndCollation(dstCharset, dstCollation)
l = expression.FoldConstant(l)
r = expression.FoldConstant(r)
function, err := er.newFunction(ast.LogicAnd, &v.Type, l, r)
if err != nil {
er.err = err
Expand Down
57 changes: 1 addition & 56 deletions planner/core/expression_rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,26 +410,6 @@ func (s *testExpressionRewriterSuite) TestIssue22818(c *C) {
tk.MustQuery("select * from t where a between \"23:22:22\" and \"23:22:22\"").Check(
testkit.Rows("23:22:22"))
}
<<<<<<< HEAD
=======

func (s *testExpressionRewriterSuite) TestIssue24705(c *C) {
defer testleak.AfterTest(c)()
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
defer func() {
dom.Close()
store.Close()
}()

tk.MustExec("use test;")
tk.MustExec("drop table if exists t1,t2;")
tk.MustExec("create table t1 (c_int int, c_str varchar(40) character set utf8 collate utf8_general_ci);")
tk.MustExec("create table t2 (c_int int, c_str varchar(40) character set utf8 collate utf8_unicode_ci);")
err = tk.ExecToErr("select * from t1 where c_str < any (select c_str from t2 where c_int between 6 and 9);")
c.Assert(err.Error(), Equals, "[expression:1267]Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation '<'")
}

func (s *testExpressionRewriterSuiteSerial) TestBetweenExprCollation(c *C) {
collate.SetNewCollationEnabledForTest(true)
Expand All @@ -450,42 +430,7 @@ func (s *testExpressionRewriterSuiteSerial) TestBetweenExprCollation(c *C) {
tk.MustExec("insert into t1 values ('a', 'B');")
tk.MustExec("insert into t1 values ('c', 'D');")
tk.MustQuery("select * from t1 where a between 'B' and c;").Check(testkit.Rows("c D"))
tk.MustQuery("explain select * from t1 where 'a' between 'g' and 'f';").Check(testkit.Rows("TableDual_6 0.00 root rows:0"))

tk.MustGetErrMsg("select * from t1 where a between 'B' collate utf8mb4_general_ci and c collate utf8mb4_unicode_ci;", "[expression:1270]Illegal mix of collations (latin1_bin,IMPLICIT), (utf8mb4_general_ci,EXPLICIT), (utf8mb4_unicode_ci,EXPLICIT) for operation 'between'")
}

func (s *testExpressionRewriterSuite) TestMultiColInExpression(c *C) {
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
defer func() {
dom.Close()
store.Close()
}()

tk.MustExec("use test;")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int, b int)")
tk.MustExec("insert into t1 values(1,1),(2,null),(null,3),(4,4)")
tk.MustExec("analyze table t1")
tk.MustExec("create table t2(a int, b int)")
tk.MustExec("insert into t2 values(1,1),(2,null),(null,3),(5,4)")
tk.MustExec("analyze table t2")
var input []string
var output []struct {
SQL string
Plan []string
Res []string
}
s.testData.GetTestCases(c, &input, &output)
for i, tt := range input {
s.testData.OnRecord(func() {
output[i].SQL = tt
output[i].Plan = s.testData.ConvertRowsToStrings(tk.MustQuery("explain format = 'brief' " + tt).Rows())
output[i].Res = s.testData.ConvertRowsToStrings(tk.MustQuery(tt).Sort().Rows())
})
tk.MustQuery("explain format = 'brief' " + tt).Check(testkit.Rows(output[i].Plan...))
tk.MustQuery(tt).Sort().Check(testkit.Rows(output[i].Res...))
}
}
>>>>>>> d5baa5dad... planner: fix expression rewrite makes between expr infers wrong collation. (#27254)

0 comments on commit ac41de7

Please sign in to comment.