Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: fix can't find proper physical plan caused by virtual column #41132

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a1f2eb1
fix can't find proper physical plan caused by virtual column
AilinKid Feb 7, 2023
37ac1b7
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
AilinKid Feb 7, 2023
ced97f0
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
AilinKid Feb 7, 2023
868d58f
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
AilinKid Feb 7, 2023
f35b9d3
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
AilinKid Feb 7, 2023
8124abe
Update planner/core/exhaust_physical_plans.go
AilinKid Feb 8, 2023
e16ee0e
address comment
AilinKid Feb 8, 2023
6687ab4
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
AilinKid Feb 8, 2023
6dc2635
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
AilinKid Feb 8, 2023
65ca5d2
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
ti-chi-bot Feb 8, 2023
23b3e98
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
AilinKid Feb 8, 2023
2fdcb58
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
AilinKid Feb 8, 2023
ff85247
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
ti-chi-bot Feb 8, 2023
4712372
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
ti-chi-bot Feb 8, 2023
3b06609
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
ti-chi-bot Feb 8, 2023
a280913
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
ti-chi-bot Feb 8, 2023
41e473b
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
AilinKid Feb 9, 2023
1905d27
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
AilinKid Feb 9, 2023
b93491a
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
fzzf678 Feb 9, 2023
d5ebe2b
Merge branch 'master' into fix-bad-plan-caused-by-virtual-col
ti-chi-bot Feb 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions executor/tiflashtest/tiflash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,33 @@ func TestGroupStreamAggOnTiFlash(t *testing.T) {
}
}

// TestIssue41014 test issue that can't find proper physical plan
func TestIssue41014(t *testing.T) {
store := testkit.CreateMockStore(t, withMockTiFlash(2))
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
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")
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")
tk.MustExec("alter table tai1 set tiflash replica 1")
tk.MustExec("alter table tai2 set tiflash replica 1")
tk.MustExec("alter table tai2 add index idx((lower(prilan)));")
tk.MustExec("set @@tidb_opt_distinct_agg_push_down = 1;")

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(
testkit.Rows("HashAgg_11 1.00 root funcs:count(distinct test.tai1.aid)->Column#8",
"└─HashJoin_15 9990.00 root inner join, equal:[eq(test.tai2.rid, test.tai1.rid)]",
" ├─Selection_20(Build) 8000.00 root like(lower(test.tai2.prilan), \"%python%\", 92)",
" │ └─Projection_19 10000.00 root test.tai2.rid, lower(test.tai2.prilan)",
" │ └─TableReader_18 9990.00 root data:Selection_17",
" │ └─Selection_17 9990.00 cop[tikv] not(isnull(test.tai2.rid))",
" │ └─TableFullScan_16 10000.00 cop[tikv] table:tai2 keep order:false, stats:pseudo",
" └─TableReader_23(Probe) 9990.00 root data:Selection_22",
" └─Selection_22 9990.00 cop[tikv] not(isnull(test.tai1.rid))",
" └─TableFullScan_21 10000.00 cop[tikv] table:tai1 keep order:false, stats:pseudo"))
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(
testkit.Rows("0"))
}

func TestTiflashEmptyDynamicPruneResult(t *testing.T) {
store := testkit.CreateMockStore(t, withMockTiFlash(2))
tk := testkit.NewTestKit(t, store)
Expand Down
17 changes: 15 additions & 2 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -2790,7 +2790,9 @@ func (la *LogicalAggregation) getStreamAggs(prop *property.PhysicalProperty) []P
if la.HasDistinct() {
// TODO: remove AllowDistinctAggPushDown after the cost estimation of distinct pushdown is implemented.
// If AllowDistinctAggPushDown is set to true, we should not consider RootTask.
if !la.ctx.GetSessionVars().AllowDistinctAggPushDown {
if !la.ctx.GetSessionVars().AllowDistinctAggPushDown || !la.canPushToCop(kv.TiKV) {
// if variable doesn't allow DistinctAggPushDown, just produce root task type.
// if variable does allow DistinctAggPushDown, but OP itself can't be pushed down to tikv, just produce root task type.
taskTypes = []property.TaskType{property.RootTaskType}
} else if !la.distinctArgsMeetsProperty() {
continue
Expand Down Expand Up @@ -2942,6 +2944,15 @@ func (la *LogicalAggregation) tryToGetMppHashAggs(prop *property.PhysicalPropert
return
}

// getHashAggs will generate some kinds of taskType here, which finally converted to different task plan.
// when deciding whether to add a kind of taskType, there is a rule here. [Not is Not, Yes is not Sure]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule seems a bit hard to understand 😆

// eg: which means
//
// 1: when you find something here that block hashAgg to be pushed down to XXX, just skip adding the XXXTaskType.
// 2: when you find nothing here to block hashAgg to be pushed down to XXX, just add the XXXTaskType here.
// 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)
//
// That is to say, the non-complete positive judgement of canPushDownToMPP/canPushDownToTiFlash/canPushDownToTiKV is not that for sure here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean: "canPushDownToMPP/canPushDownToTiFlash/canPushDownToTiKV might be false positive"

func (la *LogicalAggregation) getHashAggs(prop *property.PhysicalProperty) []PhysicalPlan {
if !prop.IsSortItemEmpty() {
return nil
Expand All @@ -2955,7 +2966,9 @@ func (la *LogicalAggregation) getHashAggs(prop *property.PhysicalProperty) []Phy
canPushDownToMPP := canPushDownToTiFlash && la.ctx.GetSessionVars().IsMPPAllowed() && la.checkCanPushDownToMPP()
if la.HasDistinct() {
// TODO: remove after the cost estimation of distinct pushdown is implemented.
if !la.ctx.GetSessionVars().AllowDistinctAggPushDown {
if !la.ctx.GetSessionVars().AllowDistinctAggPushDown || !la.canPushToCop(kv.TiKV) {
// if variable doesn't allow DistinctAggPushDown, just produce root task type.
// if variable does allow DistinctAggPushDown, but OP itself can't be pushed down to tikv, just produce root task type.
taskTypes = []property.TaskType{property.RootTaskType}
}
} else if !la.aggHints.preferAggToCop {
Expand Down