From 70acb5e8c60d8b312847ca9825c41a38e40c2aa7 Mon Sep 17 00:00:00 2001 From: Reminiscent Date: Wed, 1 Feb 2023 16:58:45 +0800 Subject: [PATCH 1/2] planner: report the warning when use the hint but the SQL has the binding --- planner/core/integration_test.go | 21 +++++++++++++++++++++ planner/optimize.go | 3 +++ 2 files changed, 24 insertions(+) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 81da36394968d..6b4d21a9485f7 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -1280,6 +1280,27 @@ func TestIssue15110(t *testing.T) { tk.MustExec("explain format = 'brief' SELECT count(*) FROM crm_rd_150m dataset_48 WHERE (CASE WHEN (month(dataset_48.customer_first_date)) <= 30 THEN '新客' ELSE NULL END) IS NOT NULL;") } +func TestIssue40910(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec(`create table t(a int, b int, index idx_a(a), index idx_b(b));`) + + tk.MustExec("select * from t where a > 1 and a < 10 order by b;") + tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("0")) + tk.MustExec("create session binding for select * from t where a > 1 and a < 10 order by b using select /*+ use_index(t, idx_a) */ * from t where a > 1 and a < 10 order by b;") + tk.MustExec("select * from t where a > 1 and a < 10 order by b;") + tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("1")) + + tk.MustExec("select /*+ use_index(t, idx_b) */ * from t where a > 1 and a < 10 order by b;") + tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("1")) + + tk.MustExec("select /*+ use_index(t, idx_b) */ * from t where a > 1 and a < 10 order by b;") + tk.MustQuery("show warnings").Check(testkit.Rows( + "Warning 1105 The system ignores the hints in the current query and uses the hints specified in the bindSQL: SELECT /*+ use_index(`t` `idx_a`)*/ * FROM `test`.`t` WHERE `a` > 1 AND `a` < 10 ORDER BY `b`")) +} + func TestReadFromStorageHint(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) diff --git a/planner/optimize.go b/planner/optimize.go index 295d68b92f5a2..1ccd1c52f307b 100644 --- a/planner/optimize.go +++ b/planner/optimize.go @@ -251,6 +251,9 @@ func Optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in } else { sessVars.StmtCtx.AppendExtraNote(errors.Errorf("Using the bindSQL: %v", chosenBinding.BindSQL)) } + if len(tableHints) > 0 { + sessVars.StmtCtx.AppendWarning(errors.Errorf("The system ignores the hints in the current query and uses the hints specified in the bindSQL: %v", chosenBinding.BindSQL)) + } } // Restore the hint to avoid changing the stmt node. hint.BindHint(stmtNode, originHints) From 715c42ebc41d0caa3afeef50184f2744d0b14b9c Mon Sep 17 00:00:00 2001 From: Reminiscent Date: Wed, 1 Feb 2023 17:59:38 +0800 Subject: [PATCH 2/2] fix tests --- bindinfo/capture_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bindinfo/capture_test.go b/bindinfo/capture_test.go index d1f375a6b63d7..21b31c883e582 100644 --- a/bindinfo/capture_test.go +++ b/bindinfo/capture_test.go @@ -434,8 +434,9 @@ func TestUpdateSubqueryCapture(t *testing.T) { rows := tk.MustQuery("show global bindings").Rows() require.Len(t, rows, 1) bindSQL := "UPDATE /*+ hash_join(@`upd_1` `test`.`t1`), use_index(@`upd_1` `test`.`t1` `idx_b`), use_index(@`sel_1` `test`.`t2` ), use_index(@`sel_2` `test`.`t2` )*/ `test`.`t1` SET `b`=1 WHERE `b` = 2 AND (`a` IN (SELECT `a` FROM `test`.`t2` WHERE `b` = 1) OR `c` IN (SELECT `a` FROM `test`.`t2` WHERE `b` = 1))" + originSQL := "UPDATE `test`.`t1` SET `b`=1 WHERE `b` = 2 AND (`a` IN (SELECT `a` FROM `test`.`t2` WHERE `b` = 1) OR `c` IN (SELECT `a` FROM `test`.`t2` WHERE `b` = 1))" require.Equal(t, bindSQL, rows[0][1]) - tk.MustExec(bindSQL) + tk.MustExec(originSQL) require.Len(t, tk.Session().GetSessionVars().StmtCtx.GetWarnings(), 0) }