From 193562710e9b5fdf67d8f2a418a9ae3a7c6bc60b Mon Sep 17 00:00:00 2001 From: xuyifan <675434007@qq.com> Date: Wed, 8 Dec 2021 22:40:24 +0800 Subject: [PATCH] cherry pick 30273 to v5.3 --- planner/core/integration_test.go | 13 +++++++++++++ util/ranger/detacher.go | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 6395df84403a8..c1710cc0f444a 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -4786,3 +4786,16 @@ func (s *testIntegrationSuite) TestIssues29711(c *C) { )) } + +func (s *testIntegrationSerialSuite) TestIssue30271(c *C) { + defer collate.SetNewCollationEnabledForTest(false) + collate.SetNewCollationEnabledForTest(true) + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a char(10), b char(10), c char(10), index (a, b, c)) collate utf8mb4_bin;") + tk.MustExec("insert into t values ('b', 'a', '1'), ('b', 'A', '2'), ('c', 'a', '3');") + tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci;") + tk.MustQuery("select * from t where (a>'a' and b='a') or (b = 'A' and a < 'd') order by a,c;").Check(testkit.Rows("b a 1", "b A 2", "c a 3")) + +} diff --git a/util/ranger/detacher.go b/util/ranger/detacher.go index e19a777006876..92beec7fd9412 100644 --- a/util/ranger/detacher.go +++ b/util/ranger/detacher.go @@ -20,6 +20,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/parser/ast" + "github.com/pingcap/tidb/parser/charset" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/sessionctx/stmtctx" @@ -716,7 +717,11 @@ func isSameValue(sc *stmtctx.StatementContext, lhs, rhs *valueInfo) (bool, error if lhs == nil || rhs == nil || lhs.mutable || rhs.mutable || lhs.value.Kind() != rhs.value.Kind() { return false, nil } + collation := lhs.value.Collation() + // binary collator may not the best choice, but it can make sure the result is correct. + lhs.value.SetCollation(charset.CollationBin) cmp, err := lhs.value.CompareDatum(sc, rhs.value) + lhs.value.SetCollation(collation) if err != nil { return false, err }