Skip to content

Commit

Permalink
planner, util/ranger: fix wrong optimize order by (#30273) (#30552)
Browse files Browse the repository at this point in the history
close #30271
  • Loading branch information
xuyifangreeneyes authored Feb 22, 2022
1 parent 6b08504 commit b04545c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

}
5 changes: 5 additions & 0 deletions util/ranger/detacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit b04545c

Please sign in to comment.