From 2c7976583f7aa6566d86c8469df8b77a8586b950 Mon Sep 17 00:00:00 2001 From: Yifan Xu <30385241+xuyifangreeneyes@users.noreply.github.com> Date: Tue, 12 Apr 2022 12:50:34 +0800 Subject: [PATCH] planner, util/ranger: fix wrong optimize order by (#30273) (#30551) close pingcap/tidb#30271 --- 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 ab7c048c8b431..c897974556754 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -4359,3 +4359,16 @@ func (s *testIntegrationSuite) TestIssue27797(c *C) { result = tk.MustQuery("select col2 from IDT_HP24172 where col1 = 8388607 and col1 in (select col1 from IDT_HP24172);") result.Check(testkit.Rows("")) } + +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 6a07c531275cf..c3952ddad7296 100644 --- a/util/ranger/detacher.go +++ b/util/ranger/detacher.go @@ -18,6 +18,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/parser/ast" + "github.com/pingcap/parser/charset" "github.com/pingcap/parser/model" "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/sessionctx" @@ -704,7 +705,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 }