Skip to content

Commit 90cf371

Browse files
authored
planner: fix can't find proper physical plan caused by virtual column (#41132)
close #41014
1 parent a4158c2 commit 90cf371

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

executor/tiflashtest/tiflash_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,33 @@ func TestGroupStreamAggOnTiFlash(t *testing.T) {
12661266
}
12671267
}
12681268

1269+
// TestIssue41014 test issue that can't find proper physical plan
1270+
func TestIssue41014(t *testing.T) {
1271+
store := testkit.CreateMockStore(t, withMockTiFlash(2))
1272+
tk := testkit.NewTestKit(t, store)
1273+
tk.MustExec("use test;")
1274+
tk.MustExec("CREATE TABLE `tai1` (\n `aid` int(11) DEFAULT NULL,\n `rid` int(11) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
1275+
tk.MustExec("CREATE TABLE `tai2` (\n `rid` int(11) DEFAULT NULL,\n `prilan` varchar(20) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
1276+
tk.MustExec("alter table tai1 set tiflash replica 1")
1277+
tk.MustExec("alter table tai2 set tiflash replica 1")
1278+
tk.MustExec("alter table tai2 add index idx((lower(prilan)));")
1279+
tk.MustExec("set @@tidb_opt_distinct_agg_push_down = 1;")
1280+
1281+
tk.MustQuery("explain select count(distinct tai1.aid) as cb from tai1 inner join tai2 on tai1.rid = tai2.rid where lower(prilan) LIKE LOWER('%python%');").Check(
1282+
testkit.Rows("HashAgg_11 1.00 root funcs:count(distinct test.tai1.aid)->Column#8",
1283+
"└─HashJoin_15 9990.00 root inner join, equal:[eq(test.tai2.rid, test.tai1.rid)]",
1284+
" ├─Selection_20(Build) 8000.00 root like(lower(test.tai2.prilan), \"%python%\", 92)",
1285+
" │ └─Projection_19 10000.00 root test.tai2.rid, lower(test.tai2.prilan)",
1286+
" │ └─TableReader_18 9990.00 root data:Selection_17",
1287+
" │ └─Selection_17 9990.00 cop[tikv] not(isnull(test.tai2.rid))",
1288+
" │ └─TableFullScan_16 10000.00 cop[tikv] table:tai2 keep order:false, stats:pseudo",
1289+
" └─TableReader_23(Probe) 9990.00 root data:Selection_22",
1290+
" └─Selection_22 9990.00 cop[tikv] not(isnull(test.tai1.rid))",
1291+
" └─TableFullScan_21 10000.00 cop[tikv] table:tai1 keep order:false, stats:pseudo"))
1292+
tk.MustQuery("select count(distinct tai1.aid) as cb from tai1 inner join tai2 on tai1.rid = tai2.rid where lower(prilan) LIKE LOWER('%python%');").Check(
1293+
testkit.Rows("0"))
1294+
}
1295+
12691296
func TestTiflashEmptyDynamicPruneResult(t *testing.T) {
12701297
store := testkit.CreateMockStore(t, withMockTiFlash(2))
12711298
tk := testkit.NewTestKit(t, store)

planner/core/exhaust_physical_plans.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -2790,7 +2790,9 @@ func (la *LogicalAggregation) getStreamAggs(prop *property.PhysicalProperty) []P
27902790
if la.HasDistinct() {
27912791
// TODO: remove AllowDistinctAggPushDown after the cost estimation of distinct pushdown is implemented.
27922792
// If AllowDistinctAggPushDown is set to true, we should not consider RootTask.
2793-
if !la.ctx.GetSessionVars().AllowDistinctAggPushDown {
2793+
if !la.ctx.GetSessionVars().AllowDistinctAggPushDown || !la.canPushToCop(kv.TiKV) {
2794+
// if variable doesn't allow DistinctAggPushDown, just produce root task type.
2795+
// if variable does allow DistinctAggPushDown, but OP itself can't be pushed down to tikv, just produce root task type.
27942796
taskTypes = []property.TaskType{property.RootTaskType}
27952797
} else if !la.distinctArgsMeetsProperty() {
27962798
continue
@@ -2942,6 +2944,15 @@ func (la *LogicalAggregation) tryToGetMppHashAggs(prop *property.PhysicalPropert
29422944
return
29432945
}
29442946

2947+
// getHashAggs will generate some kinds of taskType here, which finally converted to different task plan.
2948+
// when deciding whether to add a kind of taskType, there is a rule here. [Not is Not, Yes is not Sure]
2949+
// eg: which means
2950+
//
2951+
// 1: when you find something here that block hashAgg to be pushed down to XXX, just skip adding the XXXTaskType.
2952+
// 2: when you find nothing here to block hashAgg to be pushed down to XXX, just add the XXXTaskType here.
2953+
// for 2, the final result for this physical operator enumeration is chosen or rejected is according to more factors later (hint/variable/partition/virtual-col/cost)
2954+
//
2955+
// That is to say, the non-complete positive judgement of canPushDownToMPP/canPushDownToTiFlash/canPushDownToTiKV is not that for sure here.
29452956
func (la *LogicalAggregation) getHashAggs(prop *property.PhysicalProperty) []PhysicalPlan {
29462957
if !prop.IsSortItemEmpty() {
29472958
return nil
@@ -2955,7 +2966,9 @@ func (la *LogicalAggregation) getHashAggs(prop *property.PhysicalProperty) []Phy
29552966
canPushDownToMPP := canPushDownToTiFlash && la.ctx.GetSessionVars().IsMPPAllowed() && la.checkCanPushDownToMPP()
29562967
if la.HasDistinct() {
29572968
// TODO: remove after the cost estimation of distinct pushdown is implemented.
2958-
if !la.ctx.GetSessionVars().AllowDistinctAggPushDown {
2969+
if !la.ctx.GetSessionVars().AllowDistinctAggPushDown || !la.canPushToCop(kv.TiKV) {
2970+
// if variable doesn't allow DistinctAggPushDown, just produce root task type.
2971+
// if variable does allow DistinctAggPushDown, but OP itself can't be pushed down to tikv, just produce root task type.
29592972
taskTypes = []property.TaskType{property.RootTaskType}
29602973
}
29612974
} else if !la.aggHints.preferAggToCop {

0 commit comments

Comments
 (0)