From 7f3fbe34a7b900de815823397d05d0a51339da1e Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Fri, 16 Apr 2021 23:15:51 +0800 Subject: [PATCH] util: fix range building for binary literal (#23699) (#24041) --- expression/integration_test.go | 9 +++++++++ util/ranger/checker.go | 2 +- util/ranger/points.go | 4 ++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index b60fbf938a15d..8bbd6c530ea78 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -6310,6 +6310,15 @@ func (s *testIntegrationSerialSuite) TestCollationBasic(c *C) { tk.MustQuery("select c from t where c = 'A';").Check(testkit.Rows("A")) tk.MustQuery("select c from t where c = 'b';").Check(testkit.Rows("B")) tk.MustQuery("select c from t where c = 'B';").Check(testkit.Rows("B")) + + tk.MustExec("drop table if exists t") + tk.MustExec("CREATE TABLE `t1` (" + + " `COL1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL," + + " PRIMARY KEY (`COL1`(5)) clustered" + + ")") + tk.MustExec("INSERT INTO `t1` VALUES ('Ȇ');") + tk.MustQuery("select * from t1 where col1 not in (0xc484, 0xe5a4bc, 0xc3b3);").Check(testkit.Rows("Ȇ")) + tk.MustQuery("select * from t1 where col1 >= 0xc484 and col1 <= 0xc3b3;").Check(testkit.Rows("Ȇ")) } func (s *testIntegrationSerialSuite) TestWeightString(c *C) { diff --git a/util/ranger/checker.go b/util/ranger/checker.go index e2a99009a469b..97a0c782ce3dc 100644 --- a/util/ranger/checker.go +++ b/util/ranger/checker.go @@ -90,7 +90,7 @@ func (c *conditionChecker) checkScalarFunction(scalar *expression.ScalarFunction if !c.checkColumn(scalar.GetArgs()[0]) { return false } - if scalar.GetArgs()[1].GetType().EvalType() == types.ETString && !collate.CompatibleCollate(scalar.GetArgs()[0].GetType().Collate, collation) { + if scalar.GetArgs()[0].GetType().EvalType() == types.ETString && !collate.CompatibleCollate(scalar.GetArgs()[0].GetType().Collate, collation) { return false } for _, v := range scalar.GetArgs()[1:] { diff --git a/util/ranger/points.go b/util/ranger/points.go index 094bba0e6b489..d8684d03a65a2 100644 --- a/util/ranger/points.go +++ b/util/ranger/points.go @@ -225,7 +225,7 @@ func (r *builder) buildFormBinOp(expr *expression.ScalarFunction) []*point { // 1. for string type since we may eval the constant to another collation instead of its own collation. // 2. for year type since 2-digit year value need adjustment, see https://dev.mysql.com/doc/refman/5.6/en/year.html refineValue := func(col *expression.Column, value *types.Datum) (err error) { - if col.RetType.EvalType() == types.ETString && value.Kind() == types.KindString { + if col.RetType.EvalType() == types.ETString && (value.Kind() == types.KindString || value.Kind() == types.KindBinaryLiteral) { value.SetString(value.GetString(), col.RetType.Collate) } if col.GetType().Tp == mysql.TypeYear { @@ -468,7 +468,7 @@ func (r *builder) buildFromIn(expr *expression.ScalarFunction) ([]*point, bool) hasNull = true continue } - if dt.Kind() == types.KindString { + if dt.Kind() == types.KindString || dt.Kind() == types.KindBinaryLiteral { dt.SetString(dt.GetString(), colCollate) } if expr.GetArgs()[0].GetType().Tp == mysql.TypeYear {