Skip to content

Commit 7326e2f

Browse files
authored
planner: report the warning when use the hint but the SQL has the binding (#40949)
close #40910
1 parent 774913c commit 7326e2f

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

bindinfo/capture_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,9 @@ func TestUpdateSubqueryCapture(t *testing.T) {
434434
rows := tk.MustQuery("show global bindings").Rows()
435435
require.Len(t, rows, 1)
436436
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))"
437+
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))"
437438
require.Equal(t, bindSQL, rows[0][1])
438-
tk.MustExec(bindSQL)
439+
tk.MustExec(originSQL)
439440
require.Len(t, tk.Session().GetSessionVars().StmtCtx.GetWarnings(), 0)
440441
}
441442

planner/core/integration_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,27 @@ func TestIssue15110(t *testing.T) {
12801280
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;")
12811281
}
12821282

1283+
func TestIssue40910(t *testing.T) {
1284+
store := testkit.CreateMockStore(t)
1285+
tk := testkit.NewTestKit(t, store)
1286+
tk.MustExec("use test")
1287+
tk.MustExec("drop table if exists t")
1288+
tk.MustExec(`create table t(a int, b int, index idx_a(a), index idx_b(b));`)
1289+
1290+
tk.MustExec("select * from t where a > 1 and a < 10 order by b;")
1291+
tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("0"))
1292+
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;")
1293+
tk.MustExec("select * from t where a > 1 and a < 10 order by b;")
1294+
tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("1"))
1295+
1296+
tk.MustExec("select /*+ use_index(t, idx_b) */ * from t where a > 1 and a < 10 order by b;")
1297+
tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("1"))
1298+
1299+
tk.MustExec("select /*+ use_index(t, idx_b) */ * from t where a > 1 and a < 10 order by b;")
1300+
tk.MustQuery("show warnings").Check(testkit.Rows(
1301+
"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`"))
1302+
}
1303+
12831304
func TestReadFromStorageHint(t *testing.T) {
12841305
store := testkit.CreateMockStore(t)
12851306
tk := testkit.NewTestKit(t, store)

planner/optimize.go

+3
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ func Optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
251251
} else {
252252
sessVars.StmtCtx.AppendExtraNote(errors.Errorf("Using the bindSQL: %v", chosenBinding.BindSQL))
253253
}
254+
if len(tableHints) > 0 {
255+
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))
256+
}
254257
}
255258
// Restore the hint to avoid changing the stmt node.
256259
hint.BindHint(stmtNode, originHints)

0 commit comments

Comments
 (0)