From 9b521342bffef684f0d70ffe491d4a88bef57036 Mon Sep 17 00:00:00 2001 From: wjHuang Date: Wed, 24 Jul 2019 14:04:38 +0800 Subject: [PATCH] planner: reject invalid conversion from like to = (#11320) --- go.sum | 1 + planner/core/expression_rewriter.go | 2 +- planner/core/expression_rewriter_test.go | 18 ++++++++++++++++++ planner/core/logical_plan_test.go | 2 +- planner/core/physical_plan_test.go | 6 ------ 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/go.sum b/go.sum index b9ccf000166a7..6d6d387061618 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,7 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/StackExchange/wmi v0.0.0-20180725035823-b12b22c5341f h1:5ZfJxyXo8KyX8DgGXC5B7ILL8y51fci/qYz2B4j8iLY= github.com/StackExchange/wmi v0.0.0-20180725035823-b12b22c5341f/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= diff --git a/planner/core/expression_rewriter.go b/planner/core/expression_rewriter.go index a241b1133a27a..6d5dbbea7610b 100644 --- a/planner/core/expression_rewriter.go +++ b/planner/core/expression_rewriter.go @@ -1237,7 +1237,7 @@ func (er *expressionRewriter) patternLikeToExpression(v *ast.PatternLikeExpr) { } if !isNull { patValue, patTypes := stringutil.CompilePattern(patString, v.Escape) - if stringutil.IsExactMatch(patTypes) { + if stringutil.IsExactMatch(patTypes) && er.ctxStack[l-2].GetType().EvalType() == types.ETString { op := ast.EQ if v.Not { op = ast.NE diff --git a/planner/core/expression_rewriter_test.go b/planner/core/expression_rewriter_test.go index 034634685e237..c180b6aea50cc 100644 --- a/planner/core/expression_rewriter_test.go +++ b/planner/core/expression_rewriter_test.go @@ -242,3 +242,21 @@ func (s *testExpressionRewriterSuite) TestCheckFullGroupBy(c *C) { err = tk.ExecToErr("select t1.a, (select t2.a, max(t2.b) from t t2) from t t1") c.Assert(terror.ErrorEqual(err, core.ErrMixOfGroupFuncAndFields), IsTrue, Commentf("err %v", err)) } + +func (s *testExpressionRewriterSuite) TestPatternLikeToExpression(c *C) { + defer testleak.AfterTest(c)() + store, dom, err := newStoreWithBootstrap() + c.Assert(err, IsNil) + tk := testkit.NewTestKit(c, store) + defer func() { + dom.Close() + store.Close() + }() + tk.MustQuery("select 0 like 'a string';").Check(testkit.Rows("0")) + tk.MustQuery("select 0.0 like 'a string';").Check(testkit.Rows("0")) + tk.MustQuery("select 0 like '0.00';").Check(testkit.Rows("0")) + tk.MustQuery("select cast(\"2011-5-3\" as datetime) like \"2011-05-03\";").Check(testkit.Rows("0")) + tk.MustQuery("select 1 like '1';").Check(testkit.Rows("1")) + tk.MustQuery("select 0 like '0';").Check(testkit.Rows("1")) + tk.MustQuery("select 0.00 like '0.00';").Check(testkit.Rows("1")) +} diff --git a/planner/core/logical_plan_test.go b/planner/core/logical_plan_test.go index b9faf05e017fa..a9839e100d6e5 100644 --- a/planner/core/logical_plan_test.go +++ b/planner/core/logical_plan_test.go @@ -83,7 +83,7 @@ func (s *testPlanSuite) TestPredicatePushDown(c *C) { }, { sql: "select * from t t1, t t2 where t1.a = t2.b and t2.b > 0 and t1.a = t1.c and t1.d like 'abc' and t2.d = t1.d", - best: "Join{DataScan(t1)->Sel([eq(cast(test.t1.d), cast(abc))])->DataScan(t2)->Sel([eq(cast(test.t2.d), cast(abc))])}(test.t1.a,test.t2.b)(test.t1.d,test.t2.d)->Projection", + best: "Join{DataScan(t1)->Sel([like(cast(test.t1.d), abc, 92)])->DataScan(t2)->Sel([like(cast(test.t2.d), abc, 92)])}(test.t1.a,test.t2.b)(test.t1.d,test.t2.d)->Projection", }, { sql: "select * from t ta join t tb on ta.d = tb.d and ta.d > 1 where tb.a = 0", diff --git a/planner/core/physical_plan_test.go b/planner/core/physical_plan_test.go index 459aa9cc5d113..fc3de15294eec 100644 --- a/planner/core/physical_plan_test.go +++ b/planner/core/physical_plan_test.go @@ -1170,12 +1170,6 @@ func (s *testPlanSuite) TestRefine(c *C) { sql: `select a from t where c_str like 123`, best: "IndexReader(Index(t.c_d_e_str)[[\"123\",\"123\"]])->Projection", }, - // c is type int which will be added cast to specified type when building function signature, - // and rewrite predicate like to predicate '=' when exact match , index still can be used. - { - sql: `select a from t where c like '1'`, - best: "IndexReader(Index(t.c_d_e)[[1,1]])->Projection", - }, { sql: `select a from t where c = 1.9 and d > 3`, best: "Dual",