From f7b071057951c99d36d7a7b7274c291a817fe523 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Mon, 16 Oct 2023 11:30:28 +0800 Subject: [PATCH] planner: consider Unspecified length when setting length for unhex (#45449) (#45451) close pingcap/tidb#45378 --- expression/builtin_string.go | 3 +++ planner/core/plan_cache_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/expression/builtin_string.go b/expression/builtin_string.go index 2da271b8c1a9a..b575bb541ac4f 100644 --- a/expression/builtin_string.go +++ b/expression/builtin_string.go @@ -1718,6 +1718,9 @@ func (c *unhexFunctionClass) getFunction(ctx sessionctx.Context, args []Expressi default: return nil, errors.Errorf("Unhex invalid args, need int or string but get %s", argType) } + if argType.GetFlen() == types.UnspecifiedLength { + retFlen = types.UnspecifiedLength + } bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETString, types.ETString) if err != nil { diff --git a/planner/core/plan_cache_test.go b/planner/core/plan_cache_test.go index 73fa0304abfeb..52572b99b3d25 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -2367,6 +2367,19 @@ func TestIssue45253(t *testing.T) { tk.MustQuery(`SELECT c1 FROM t1 WHERE TO_BASE64('')`).Check(testkit.Rows()) } +func TestIssue45378(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec(`set tidb_enable_non_prepared_plan_cache=1`) + tk.MustExec(`CREATE TABLE t1(c1 INT)`) + tk.MustExec(`INSERT INTO t1 VALUES (1)`) + + tk.MustQuery(`SELECT c1 FROM t1 WHERE UNHEX(2038330881)`).Check(testkit.Rows("1")) + tk.MustQuery(`SELECT c1 FROM t1 WHERE UNHEX(2038330881)`).Check(testkit.Rows("1")) + tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1")) +} + func TestNonPreparedPlanCacheBuiltinFuncs(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store)