Skip to content

Commit 97b5a64

Browse files
committed
planner: fix wrong extend predicate simplification for subquery and nested expressions
Signed-off-by: Weizhen Wang <[email protected]>
1 parent c778996 commit 97b5a64

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

pkg/planner/core/issuetest/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ go_test(
1010
data = glob(["testdata/**"]),
1111
flaky = True,
1212
race = "on",
13-
shard_count = 7,
13+
shard_count = 8,
1414
deps = [
1515
"//pkg/parser",
1616
"//pkg/planner",

pkg/planner/core/issuetest/planner_issue_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,32 @@ ON base.c1 <=> base2.c1;`).Sort().Check(testkit.Rows(
231231
"1 Alice 1 100",
232232
"<nil> Bob <nil> <nil>"))
233233
}
234+
235+
func TestIssue58451(t *testing.T) {
236+
store := testkit.CreateMockStore(t)
237+
tk := testkit.NewTestKit(t, store)
238+
tk.MustExec("use test;")
239+
tk.MustExec("create table t1 (a1 int, b1 int);")
240+
tk.MustExec("insert into t1 values(1,1);")
241+
tk.MustQuery(`explain
242+
SELECT (4,5) IN (SELECT 8,0 UNION SELECT 8, 8) AS field1
243+
FROM t1 AS table1
244+
WHERE (EXISTS (SELECT SUBQUERY2_t1.a1 AS SUBQUERY2_field1 FROM t1 AS SUBQUERY2_t1)) OR table1.b1 >= 55
245+
GROUP BY field1;`).Check(testkit.Rows("HashJoin_34 2.00 root CARTESIAN left outer semi join, left side:HashAgg_35",
246+
"├─HashAgg_59(Build) 2.00 root group by:Column#18, Column#19, funcs:firstrow(1)->Column#45",
247+
"│ └─Union_63 0.00 root ",
248+
"│ ├─Projection_65 0.00 root 8->Column#18, 0->Column#19",
249+
"│ │ └─TableDual_66 0.00 root rows:0",
250+
"│ └─Projection_67 0.00 root 8->Column#18, 8->Column#19",
251+
"│ └─TableDual_68 0.00 root rows:0",
252+
"└─HashAgg_35(Probe) 2.00 root group by:Column#10, funcs:firstrow(1)->Column#42",
253+
" └─HashJoin_36 10000.00 root CARTESIAN left outer semi join, left side:TableReader_38",
254+
" ├─HashAgg_41(Build) 2.00 root group by:Column#8, Column#9, funcs:firstrow(1)->Column#44",
255+
" │ └─Union_45 0.00 root ",
256+
" │ ├─Projection_47 0.00 root 8->Column#8, 0->Column#9",
257+
" │ │ └─TableDual_48 0.00 root rows:0",
258+
" │ └─Projection_49 0.00 root 8->Column#8, 8->Column#9",
259+
" │ └─TableDual_50 0.00 root rows:0",
260+
" └─TableReader_38(Probe) 10000.00 root data:TableFullScan_37",
261+
" └─TableFullScan_37 10000.00 cop[tikv] table:table1 keep order:false, stats:pseudo"))
262+
}

pkg/planner/core/rule_decorrelate.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,15 @@ func pruneRedundantApply(p base.LogicalPlan) (base.LogicalPlan, bool) {
160160
if apply.JoinType != logicalop.LeftOuterJoin && apply.JoinType != logicalop.LeftOuterSemiJoin {
161161
return nil, false
162162
}
163-
163+
// add a strong limit for fix the https://github.com/pingcap/tidb/issues/58451. we can remove it when to have better implememnt.
164+
// But this problem has affected tiflash CI.
165+
applyChildren := apply.Children()
166+
if len(applyChildren) >= 2 {
167+
corCols := coreusage.ExtractCorColumnsBySchema4LogicalPlan(applyChildren[1], applyChildren[0].Schema())
168+
if len(corCols) == 0 {
169+
return nil, false
170+
}
171+
}
164172
// Simplify predicates from the LogicalSelection
165173
simplifiedPredicates := applyPredicateSimplification(p.SCtx(), logicalSelection.Conditions)
166174

tests/integrationtest/r/planner/core/casetest/predicate_simplification.result

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,13 @@ Projection 8000.00 root 1->Column#10
353353
explain format=brief
354354
SELECT 1 FROM t1 AS tab WHERE 1 = 1 OR a1 in (select a2 from t2);
355355
id estRows task access object operator info
356-
Projection 10000.00 root 1->Column#9
357-
└─TableReader 10000.00 root data:TableFullScan
358-
└─TableFullScan 10000.00 cop[tikv] table:tab keep order:false, stats:pseudo
356+
Projection 8000.00 root 1->Column#9
357+
└─Selection 8000.00 root or(1, Column#8)
358+
└─HashJoin 10000.00 root CARTESIAN left outer semi join, left side:TableReader, other cond:eq(planner__core__casetest__predicate_simplification.t1.a1, planner__core__casetest__predicate_simplification.t2.a2)
359+
├─TableReader(Build) 10000.00 root data:TableFullScan
360+
│ └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo
361+
└─TableReader(Probe) 10000.00 root data:TableFullScan
362+
└─TableFullScan 10000.00 cop[tikv] table:tab keep order:false, stats:pseudo
359363
explain format=brief
360364
SELECT 1 FROM t1
361365
WHERE

0 commit comments

Comments
 (0)