From c40d102541db3e6b73c07ac08c8c1a0273551698 Mon Sep 17 00:00:00 2001 From: wjhuang2016 Date: Tue, 30 Nov 2021 16:29:46 +0800 Subject: [PATCH 1/3] done Signed-off-by: wjhuang2016 --- planner/core/integration_test.go | 13 +++++++++++++ types/datum.go | 5 +++-- types/datum_test.go | 2 +- util/ranger/detacher.go | 3 ++- util/rowDecoder/decoder_test.go | 4 ++-- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 351a20ba4c552..c1a3ca5d77c70 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -4897,3 +4897,16 @@ func (s *testIntegrationSuite) TestIssue30094(c *C) { " └─TableFullScan 10000.00 cop[tikv] table:t30094 keep order:false, stats:pseudo", )) } + +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/types/datum.go b/types/datum.go index cd8db788d2d3c..9d11950dc76e1 100644 --- a/types/datum.go +++ b/types/datum.go @@ -2175,7 +2175,8 @@ func (ds *datumsSorter) Len() int { } func (ds *datumsSorter) Less(i, j int) bool { - cmp, err := ds.datums[i].CompareDatum(ds.sc, &ds.datums[j]) + // TODO: set collation explicitly when rewrites feedback. + cmp, err := ds.datums[i].Compare(ds.sc, &ds.datums[j], collate.GetCollator(ds.datums[i].Collation())) if err != nil { ds.err = errors.Trace(err) return true @@ -2357,7 +2358,7 @@ func ChangeReverseResultByUpperLowerBound( resRetType.Decimal = int(res.GetMysqlDecimal().GetDigitsInt()) } bound := getDatumBound(&resRetType, rType) - cmp, err := d.CompareDatum(sc, &bound) + cmp, err := d.Compare(sc, &bound, collate.GetCollator(resRetType.Collate)) if err != nil { return d, err } diff --git a/types/datum_test.go b/types/datum_test.go index 36f6776282e72..f1117dd29e023 100644 --- a/types/datum_test.go +++ b/types/datum_test.go @@ -490,7 +490,7 @@ func TestChangeReverseResultByUpperLowerBound(t *testing.T) { reverseRes, err := ChangeReverseResultByUpperLowerBound(sc, test.retType, test.a, test.roundType) require.NoError(t, err) var cmp int - cmp, err = reverseRes.CompareDatum(sc, &test.res) + cmp, err = reverseRes.Compare(sc, &test.res, collate.GetBinaryCollator()) require.NoError(t, err) require.Equalf(t, 0, cmp, "%dth got:%#v, expect:%#v", ith, reverseRes, test.res) } diff --git a/util/ranger/detacher.go b/util/ranger/detacher.go index f539f759c1675..7671d29fa7906 100644 --- a/util/ranger/detacher.go +++ b/util/ranger/detacher.go @@ -715,7 +715,8 @@ 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 } - cmp, err := lhs.value.CompareDatum(sc, rhs.value) + // binary collator may not the best choice, but it can make sure the result is correct. + cmp, err := lhs.value.Compare(sc, rhs.value, collate.GetBinaryCollator()) if err != nil { return false, err } diff --git a/util/rowDecoder/decoder_test.go b/util/rowDecoder/decoder_test.go index cb743418d1692..26abf03eb6e95 100644 --- a/util/rowDecoder/decoder_test.go +++ b/util/rowDecoder/decoder_test.go @@ -130,7 +130,7 @@ func TestRowDecoder(t *testing.T) { for i, col := range cols[:len(cols)-1] { v, ok := r[col.ID] if ok { - equal, err1 := v.CompareDatum(sc, &row.output[i]) + equal, err1 := v.Compare(sc, &row.output[i], collate.GetBinaryCollator()) require.Nil(t, err1) require.Equal(t, 0, equal) } else { @@ -144,7 +144,7 @@ func TestRowDecoder(t *testing.T) { for k, v := range r2 { v1, ok := r[k] require.True(t, ok) - equal, err1 := v.CompareDatum(sc, &v1) + equal, err1 := v.Compare(sc, &v1, collate.GetBinaryCollator()) require.Nil(t, err1) require.Equal(t, 0, equal) } From fded53e0ab4fd6ae8f6c54ce2855c1bc128bad90 Mon Sep 17 00:00:00 2001 From: wjhuang2016 Date: Tue, 30 Nov 2021 17:41:35 +0800 Subject: [PATCH 2/3] fix Signed-off-by: wjhuang2016 --- planner/core/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index c1a3ca5d77c70..b1a5ae3b94a5c 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -4907,6 +4907,6 @@ func (s *testIntegrationSerialSuite) TestIssue30271(c *C) { 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'")) + 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")) } From cccd70f88c80ac9e4e8a7c8de31da51fe27fb15c Mon Sep 17 00:00:00 2001 From: wjhuang2016 Date: Tue, 30 Nov 2021 18:01:16 +0800 Subject: [PATCH 3/3] fix Signed-off-by: wjhuang2016 --- planner/core/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index b1a5ae3b94a5c..d47d0132384c2 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -4907,6 +4907,6 @@ func (s *testIntegrationSerialSuite) TestIssue30271(c *C) { 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")) + 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")) }