From f35f0f1709efb451739f35791380483d0ad1adc9 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Mon, 11 Nov 2024 12:20:13 +0800 Subject: [PATCH] expression: fix the wrong behavior of `conv` function (#53681) (#56889) close pingcap/tidb#53505 --- expression/builtin_math.go | 6 +++++- planner/core/plan_cache_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/expression/builtin_math.go b/expression/builtin_math.go index 35914aa21cd94..39277b813cff6 100644 --- a/expression/builtin_math.go +++ b/expression/builtin_math.go @@ -1186,7 +1186,11 @@ func (b *builtinConvSig) evalString(row chunk.Row) (res string, isNull bool, err switch x := b.args[0].(type) { case *Constant: if x.Value.Kind() == types.KindBinaryLiteral { - str = x.Value.GetBinaryLiteral().ToBitLiteralString(true) + datum, err := x.Eval(row) + if err != nil { + return "", false, err + } + str = datum.GetBinaryLiteral().ToBitLiteralString(true) } case *ScalarFunction: if x.FuncName.L == ast.Cast { diff --git a/planner/core/plan_cache_test.go b/planner/core/plan_cache_test.go index 8e998b976f2eb..847cedf7847a9 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -1364,6 +1364,20 @@ func TestPlanCacheExprBlacklistCompatibility(t *testing.T) { tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) } +func TestIssue53505(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec(`create table t (v varchar(16))`) + tk.MustExec(`insert into t values ('156')`) + tk.MustExec(`prepare stmt7 from 'select * from t where v = conv(?, 16, 8)'`) + tk.MustExec(`set @arg=0x6E`) + tk.MustQuery(`execute stmt7 using @arg`).Check(testkit.Rows("156")) + tk.MustQuery(`execute stmt7 using @arg`).Check(testkit.Rows("156")) + tk.MustExec(`set @arg=0x70`) + tk.MustQuery(`execute stmt7 using @arg`).Check(testkit.Rows()) // empty +} + func TestPlanCacheDiagInfo(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store)