Skip to content

Commit

Permalink
planner: introduce a new fix-control 43817 to control whether to allo…
Browse files Browse the repository at this point in the history
…w the optimizer to evaluate non-correlated sub-queries in advance (#53782)

ref #43817
  • Loading branch information
qw4990 authored Jun 4, 2024
1 parent d5e9c6e commit 3941867
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,10 @@ func init() {
// but the plan package cannot import the executor package because of the dependency cycle.
// So we assign a function implemented in the executor package to the plan package to avoid the dependency cycle.
plannercore.EvalSubqueryFirstRow = func(ctx context.Context, p base.PhysicalPlan, is infoschema.InfoSchema, pctx planctx.PlanContext) ([]types.Datum, error) {
if fixcontrol.GetBoolWithDefault(pctx.GetSessionVars().OptimizerFixControl, fixcontrol.Fix43817, false) {
return nil, errors.NewNoStackError("evaluate non-correlated sub-queries during optimization phase is not allowed by fix-control 43817")
}

defer func(begin time.Time) {
s := pctx.GetSessionVars()
s.StmtCtx.SetSkipPlanCache("query has uncorrelated sub-queries is un-cacheable")
Expand Down
2 changes: 1 addition & 1 deletion pkg/planner/core/casetest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ go_test(
],
data = glob(["testdata/**"]),
flaky = True,
shard_count = 25,
shard_count = 26,
deps = [
"//pkg/domain",
"//pkg/errno",
Expand Down
13 changes: 13 additions & 0 deletions pkg/planner/core/casetest/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,19 @@ func TestIssue50926(t *testing.T) {
tk.MustQuery("select sum(json_extract(b, '$.path')) from v group by a").Check(testkit.Rows()) // no error
}

func TestFixControl43817(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t1 (a int)`)
tk.MustExec(`create table t2 (a int)`)
tk.MustQuery(`select * from t1 where t1.a > (select max(a) from t2)`).Check(testkit.Rows()) // no error
tk.MustExec(`set tidb_opt_fix_control="43817:on"`)
tk.MustContainErrMsg(`select * from t1 where t1.a > (select max(a) from t2)`, "evaluate non-correlated sub-queries during optimization phase is not allowed by fix-control 43817")
tk.MustExec(`set tidb_opt_fix_control="43817:off"`)
tk.MustQuery(`select * from t1 where t1.a > (select max(a) from t2)`).Check(testkit.Rows()) // no error
}

func TestFixControl45132(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
4 changes: 4 additions & 0 deletions pkg/planner/util/fixcontrol/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const (
// tables (both prepared statments and non-prepared statements)
// See #33031
Fix33031 uint64 = 33031
// Fix43817 controls whether to allow optimizer to evaluate non-correlated sub-queries in the optimization phase.
// If it is not allowed, the optimizer will return a particular error when encountering non-correlated sub-queries.
// This fix-control is mainly for Index Advisor.
Fix43817 uint64 = 43817
// Fix44262 controls whether to allow to use dynamic-mode to access partitioning tables without global-stats (#44262).
Fix44262 uint64 = 44262
// Fix44389 controls whether to consider non-point ranges of some CNF item when building ranges.
Expand Down

0 comments on commit 3941867

Please sign in to comment.