From 43d42b8ad12bfd66939d4f96f28cad947d8e0793 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Tue, 20 Feb 2024 21:03:56 +0800 Subject: [PATCH] parser: restore set_var value to string instead of plain text (#50515) (#50536) close pingcap/tidb#50507 --- pkg/bindinfo/BUILD.bazel | 2 +- pkg/bindinfo/handle_test.go | 26 ++++++++++++++++++++++++++ pkg/bindinfo/tests/bind_test.go | 2 +- pkg/parser/ast/misc.go | 2 +- pkg/parser/parser_test.go | 3 +-- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/pkg/bindinfo/BUILD.bazel b/pkg/bindinfo/BUILD.bazel index 333c75d7934bb..6e9946f7f1b74 100644 --- a/pkg/bindinfo/BUILD.bazel +++ b/pkg/bindinfo/BUILD.bazel @@ -57,7 +57,7 @@ go_test( embed = [":bindinfo"], flaky = True, race = "on", - shard_count = 42, + shard_count = 43, deps = [ "//pkg/bindinfo/internal", "//pkg/config", diff --git a/pkg/bindinfo/handle_test.go b/pkg/bindinfo/handle_test.go index 984fdb0189be7..9781eb6485e1c 100644 --- a/pkg/bindinfo/handle_test.go +++ b/pkg/bindinfo/handle_test.go @@ -607,3 +607,29 @@ func TestReloadBindings(t *testing.T) { rows = tk.MustQuery("show global bindings").Rows() require.Equal(t, 0, len(rows)) } + +func TestSetVarFixControlWithBinding(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + + tk.MustExec(`create table t(id int, a varchar(100), b int, c int, index idx_ab(a, b))`) + tk.MustQuery(`explain select * from t where c = 10 and (a = 'xx' or (a = 'kk' and b = 1))`).Check( + testkit.Rows( + `IndexLookUp_12 0.01 root `, + `├─Selection_10(Build) 0.02 cop[tikv] or(eq(test.t.a, "xx"), and(eq(test.t.a, "kk"), eq(test.t.b, 1)))`, + `│ └─IndexRangeScan_8 20.00 cop[tikv] table:t, index:idx_ab(a, b) range:["kk","kk"], ["xx","xx"], keep order:false, stats:pseudo`, + `└─Selection_11(Probe) 0.01 cop[tikv] eq(test.t.c, 10)`, + ` └─TableRowIDScan_9 0.02 cop[tikv] table:t keep order:false, stats:pseudo`)) + + tk.MustExec(`create global binding for select * from t where c = 10 and (a = 'xx' or (a = 'kk' and b = 1)) using select /*+ set_var(tidb_opt_fix_control='44389:ON') */ * from t where c = 10 and (a = 'xx' or (a = 'kk' and b = 1))`) + tk.MustQuery(`show warnings`).Check(testkit.Rows()) // no warning + + // the fix control can take effect + tk.MustQuery(`explain select * from t where c = 10 and (a = 'xx' or (a = 'kk' and b = 1))`).Check( + testkit.Rows(`IndexLookUp_11 0.01 root `, + `├─IndexRangeScan_8(Build) 10.10 cop[tikv] table:t, index:idx_ab(a, b) range:["kk" 1,"kk" 1], ["xx","xx"], keep order:false, stats:pseudo`, + `└─Selection_10(Probe) 0.01 cop[tikv] eq(test.t.c, 10)`, + ` └─TableRowIDScan_9 10.10 cop[tikv] table:t keep order:false, stats:pseudo`)) + tk.MustQuery(`select @@last_plan_from_binding`).Check(testkit.Rows("1")) +} diff --git a/pkg/bindinfo/tests/bind_test.go b/pkg/bindinfo/tests/bind_test.go index b13164011457a..53a098fb376b1 100644 --- a/pkg/bindinfo/tests/bind_test.go +++ b/pkg/bindinfo/tests/bind_test.go @@ -674,7 +674,7 @@ func TestRuntimeHintsInEvolveTasks(t *testing.T) { tk.MustExec("admin flush bindings") rows := tk.MustQuery("show global bindings").Rows() require.Len(t, rows, 2) - require.Equal(t, "SELECT /*+ use_index(@`sel_1` `test`.`t` `idx_c`), no_order_index(@`sel_1` `test`.`t` `idx_c`), max_execution_time(5000), set_var(tikv_client_read_timeout = 20)*/ * FROM `test`.`t` WHERE `a` >= 4 AND `b` >= 1 AND `c` = 0", rows[0][1]) + require.Equal(t, "SELECT /*+ use_index(@`sel_1` `test`.`t` `idx_c`), no_order_index(@`sel_1` `test`.`t` `idx_c`), max_execution_time(5000), set_var(tikv_client_read_timeout = '20')*/ * FROM `test`.`t` WHERE `a` >= 4 AND `b` >= 1 AND `c` = 0", rows[0][1]) } func TestCaptureBaselinesScope(t *testing.T) { diff --git a/pkg/parser/ast/misc.go b/pkg/parser/ast/misc.go index 3d74d11c2afc4..aa74310355a04 100644 --- a/pkg/parser/ast/misc.go +++ b/pkg/parser/ast/misc.go @@ -3788,7 +3788,7 @@ func (n *TableOptimizerHint) Restore(ctx *format.RestoreCtx) error { hintData := n.HintData.(HintSetVar) ctx.WritePlain(hintData.VarName) ctx.WritePlain(" = ") - ctx.WritePlain(hintData.Value) + ctx.WriteString(hintData.Value) } ctx.WritePlain(")") return nil diff --git a/pkg/parser/parser_test.go b/pkg/parser/parser_test.go index 09cba1f081703..ff9170b0f5819 100644 --- a/pkg/parser/parser_test.go +++ b/pkg/parser/parser_test.go @@ -1141,9 +1141,8 @@ AAAAAAAAAAAA5gm5Mg== {"query watch add SQL TEXT SIMILAR 'select 1'", false, ""}, {"query watch remove 1", true, "QUERY WATCH REMOVE 1"}, {"query watch remove", false, ""}, - // for issue 34325, "replace into" with hints - {"replace /*+ SET_VAR(sql_mode='ALLOW_INVALID_DATES') */ into t values ('2004-04-31');", true, "REPLACE /*+ SET_VAR(sql_mode = ALLOW_INVALID_DATES)*/ INTO `t` VALUES (_UTF8MB4'2004-04-31')"}, + {"replace /*+ SET_VAR(sql_mode='ALLOW_INVALID_DATES') */ into t values ('2004-04-31');", true, "REPLACE /*+ SET_VAR(sql_mode = 'ALLOW_INVALID_DATES')*/ INTO `t` VALUES (_UTF8MB4'2004-04-31')"}, } RunTest(t, table, false) }