Skip to content

Commit 0dbd42f

Browse files
authored
planner: fix panic error when subquery + always true predicate in where clause (#53525) (#55032)
close #46962
1 parent a8dd684 commit 0dbd42f

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

pkg/planner/core/casetest/physicalplantest/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 = 32,
13+
shard_count = 33,
1414
deps = [
1515
"//pkg/config",
1616
"//pkg/domain",

pkg/planner/core/casetest/physicalplantest/physical_plan_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,3 +1481,29 @@ func TestPointgetIndexChoosen(t *testing.T) {
14811481
tk.MustQuery("explain format = 'brief' " + ts).Check(testkit.Rows(output[i].Plan...))
14821482
}
14831483
}
1484+
1485+
// Test issue #46962 plan
1486+
func TestAlwaysTruePredicateWithSubquery(t *testing.T) {
1487+
var (
1488+
input []string
1489+
output []struct {
1490+
SQL string
1491+
Plan []string
1492+
Warning []string
1493+
}
1494+
)
1495+
planSuiteData := GetPlanSuiteData()
1496+
planSuiteData.LoadTestCases(t, &input, &output)
1497+
store := testkit.CreateMockStore(t)
1498+
tk := testkit.NewTestKit(t, store)
1499+
1500+
tk.MustExec("use test")
1501+
tk.MustExec(`CREATE TABLE t ( a int NOT NULL , b int NOT NULL ) `)
1502+
for i, ts := range input {
1503+
testdata.OnRecord(func() {
1504+
output[i].SQL = ts
1505+
output[i].Plan = testdata.ConvertRowsToStrings(tk.MustQuery(ts).Rows())
1506+
})
1507+
tk.MustQuery(ts).Check(testkit.Rows(output[i].Plan...))
1508+
}
1509+
}

pkg/planner/core/casetest/physicalplantest/testdata/plan_suite_in.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,5 +611,13 @@
611611
"select * from t where b=1 and c='1' and d='1';",
612612
"select * from t where b in (1,2,3) and c in ('1');"
613613
]
614+
},
615+
{
616+
"name": "TestAlwaysTruePredicateWithSubquery",
617+
"cases" : [
618+
"SHOW ERRORS WHERE TRUE = ALL ( SELECT TRUE GROUP BY 1 LIMIT 1 ) IS NULL IS NOT NULL;",
619+
"explain select * from t WHERE TRUE = ALL ( SELECT TRUE GROUP BY 1 LIMIT 1 ) IS NULL IS NOT NULL;",
620+
"explain select * from t WHERE TRUE = ALL ( SELECT TRUE from t GROUP BY 1 LIMIT 1 ) is null is not null;"
621+
]
614622
}
615623
]

pkg/planner/core/casetest/physicalplantest/testdata/plan_suite_out.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3736,5 +3736,43 @@
37363736
"Warning": null
37373737
}
37383738
]
3739+
},
3740+
{
3741+
"Name": "TestAlwaysTruePredicateWithSubquery",
3742+
"Cases": [
3743+
{
3744+
"SQL": "SHOW ERRORS WHERE TRUE = ALL ( SELECT TRUE GROUP BY 1 LIMIT 1 ) IS NULL IS NOT NULL;",
3745+
"Plan": null,
3746+
"Warning": null
3747+
},
3748+
{
3749+
"SQL": "explain select * from t WHERE TRUE = ALL ( SELECT TRUE GROUP BY 1 LIMIT 1 ) IS NULL IS NOT NULL;",
3750+
"Plan": [
3751+
"HashJoin_14 10000.00 root CARTESIAN inner join",
3752+
"├─StreamAgg_19(Build) 1.00 root funcs:count(1)->Column#13",
3753+
"│ └─Limit_22 1.00 root offset:0, count:1",
3754+
"│ └─HashAgg_23 1.00 root group by:1, ",
3755+
"│ └─TableDual_24 1.00 root rows:1",
3756+
"└─TableReader_17(Probe) 10000.00 root data:TableFullScan_16",
3757+
" └─TableFullScan_16 10000.00 cop[tikv] table:t keep order:false, stats:pseudo"
3758+
],
3759+
"Warning": null
3760+
},
3761+
{
3762+
"SQL": "explain select * from t WHERE TRUE = ALL ( SELECT TRUE from t GROUP BY 1 LIMIT 1 ) is null is not null;",
3763+
"Plan": [
3764+
"HashJoin_14 10000.00 root CARTESIAN inner join",
3765+
"├─StreamAgg_19(Build) 1.00 root funcs:count(1)->Column#16",
3766+
"│ └─Limit_22 1.00 root offset:0, count:1",
3767+
"│ └─HashAgg_27 1.00 root group by:Column#17, funcs:firstrow(Column#18)->test.t.a, funcs:firstrow(Column#19)->test.t.b, funcs:firstrow(Column#20)->test.t._tidb_rowid",
3768+
"│ └─TableReader_28 1.00 root data:HashAgg_23",
3769+
"│ └─HashAgg_23 1.00 cop[tikv] group by:1, funcs:firstrow(test.t.a)->Column#18, funcs:firstrow(test.t.b)->Column#19, funcs:firstrow(test.t._tidb_rowid)->Column#20",
3770+
"│ └─TableFullScan_26 10000.00 cop[tikv] table:t keep order:false, stats:pseudo",
3771+
"└─TableReader_17(Probe) 10000.00 root data:TableFullScan_16",
3772+
" └─TableFullScan_16 10000.00 cop[tikv] table:t keep order:false, stats:pseudo"
3773+
],
3774+
"Warning": null
3775+
}
3776+
]
37393777
}
37403778
]

pkg/planner/core/rule_build_key_info.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ func buildKeyInfo(lp LogicalPlan) {
4646

4747
// BuildKeyInfo implements LogicalPlan BuildKeyInfo interface.
4848
func (la *LogicalAggregation) BuildKeyInfo(selfSchema *expression.Schema, childSchema []*expression.Schema) {
49-
if la.IsPartialModeAgg() {
49+
// According to the issue#46962, we can ignore the judgment of partial agg
50+
// Sometimes, the agg inside of subquery and there is a true condition in where clause, the agg function is empty.
51+
// For example, ``` select xxxx from xxx WHERE TRUE = ALL ( SELECT TRUE GROUP BY 1 LIMIT 1 ) IS NULL IS NOT NULL;
52+
// In this case, the agg is complete mode and we can ignore this check.
53+
if len(la.AggFuncs) != 0 && la.IsPartialModeAgg() {
5054
return
5155
}
5256
la.logicalSchemaProducer.BuildKeyInfo(selfSchema, childSchema)

0 commit comments

Comments
 (0)