diff --git a/expression/builtin_string.go b/expression/builtin_string.go index 72c6bd5cf8..3ea1d56009 100644 --- a/expression/builtin_string.go +++ b/expression/builtin_string.go @@ -3531,7 +3531,12 @@ func (c *toBase64FunctionClass) getFunction(ctx sessionctx.Context, args []Expre charset, collate := ctx.GetSessionVars().GetCharsetInfo() bf.tp.SetCharset(charset) bf.tp.SetCollate(collate) - bf.tp.SetFlen(base64NeededEncodedLength(bf.args[0].GetType().GetFlen())) + + if bf.args[0].GetType().GetFlen() == types.UnspecifiedLength { + bf.tp.SetFlen(types.UnspecifiedLength) + } else { + bf.tp.SetFlen(base64NeededEncodedLength(bf.args[0].GetType().GetFlen())) + } valStr, _ := ctx.GetSessionVars().GetSystemVar(variable.MaxAllowedPacket) maxAllowedPacket, err := strconv.ParseUint(valStr, 10, 64) diff --git a/planner/core/plan_cache_test.go b/planner/core/plan_cache_test.go index 499d9ec7d3..7e061cef77 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -2417,6 +2417,22 @@ func TestIssue43667(t *testing.T) { tk.MustQueryWithContext(tctx, `select (val) from cycle where pk = 4`).Check(testkit.Rows("4")) } +func TestIssue45253(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 TO_BASE64('牵')`).Check(testkit.Rows("1")) + tk.MustQuery(`SELECT c1 FROM t1 WHERE TO_BASE64('牵')`).Check(testkit.Rows("1")) + tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1")) + tk.MustQuery(`SELECT c1 FROM t1 WHERE TO_BASE64('哈')`).Check(testkit.Rows("1")) + tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1")) + tk.MustQuery(`SELECT c1 FROM t1 WHERE TO_BASE64('')`).Check(testkit.Rows()) +} + func TestNonPreparedPlanCacheBuiltinFuncs(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store)