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/cascades: add transformation rule PushLimitDownUnionAll. #14264

Merged
merged 6 commits into from
Dec 28, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
2 changes: 2 additions & 0 deletions planner/cascades/testdata/transformation_rules_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"select a, b, c from t t1 where t1.a in (select a from (select t2.a as a, t1.b as b from t t2 where t2.b > t1.b) x order by b limit 1)",
"select a, b from (select @i as a, @i := @i+1 as b from t) t order by a desc limit 1",
"(select a from t) union all (select b from t) order by a limit 2;",
"(select a from t) union all (select b from t) limit 2;",
francis0407 marked this conversation as resolved.
Show resolved Hide resolved
"(select a from t) union all (select b from t) limit 2 offset 5;",
"(select a from t) union all (select sum(a) from t where a > 2 group by b) order by a limit 2;",
"(select a from t) union all (select sum(a) from t where a > 2 group by b) order by a limit 1, 2;",
"(select a from t where a = 1) union all (select b from t where a = 2) union all (select c from t where a = 3) order by a limit 2;"
Expand Down
94 changes: 94 additions & 0 deletions planner/cascades/testdata/transformation_rules_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,100 @@
" TableScan_24 table:t"
]
},
{
"SQL": "(select a from t) union all (select b from t) limit 2;",
"Result": [
"Group#0 Schema:[Column#25]",
" Limit_8 input:[Group#1], offset:0, count:2",
"Group#1 Schema:[Column#25]",
" Union_5 input:[Group#2,Group#3]",
"Group#2 Schema:[Column#25]",
" Projection_6 input:[Group#4], test.t.a",
"Group#4 Schema:[test.t.a]",
" Projection_4 input:[Group#5], test.t.a",
"Group#5 Schema:[test.t.a]",
" Limit_25 input:[Group#6], offset:0, count:2",
"Group#6 Schema:[test.t.a]",
" TiKVSingleGather_10 input:[Group#7], table:t",
" TiKVSingleGather_22 input:[Group#8], table:t, index:e_d_c_str_prefix",
" TiKVSingleGather_20 input:[Group#9], table:t, index:c_d_e_str",
" TiKVSingleGather_18 input:[Group#10], table:t, index:f_g",
" TiKVSingleGather_16 input:[Group#11], table:t, index:g",
" TiKVSingleGather_14 input:[Group#12], table:t, index:f",
" TiKVSingleGather_12 input:[Group#13], table:t, index:c_d_e",
"Group#7 Schema:[test.t.a]",
" TableScan_9 table:t, pk col:test.t.a",
"Group#8 Schema:[test.t.a]",
" IndexScan_21 table:t, index:e_str, d_str, c_str",
"Group#9 Schema:[test.t.a]",
" IndexScan_19 table:t, index:c_str, d_str, e_str",
"Group#10 Schema:[test.t.a]",
" IndexScan_17 table:t, index:f, g",
"Group#11 Schema:[test.t.a]",
" IndexScan_15 table:t, index:g",
"Group#12 Schema:[test.t.a]",
" IndexScan_13 table:t, index:f",
"Group#13 Schema:[test.t.a]",
" IndexScan_11 table:t, index:c, d, e",
"Group#3 Schema:[Column#25]",
" Projection_7 input:[Group#14], test.t.b",
"Group#14 Schema:[test.t.b]",
" Projection_2 input:[Group#15], test.t.b",
"Group#15 Schema:[test.t.b]",
" Limit_25 input:[Group#16], offset:0, count:2",
"Group#16 Schema:[test.t.b]",
" TiKVSingleGather_24 input:[Group#17], table:t",
"Group#17 Schema:[test.t.b]",
" TableScan_23 table:t"
]
},
{
"SQL": "(select a from t) union all (select b from t) limit 2 offset 5;",
"Result": [
"Group#0 Schema:[Column#25]",
" Limit_8 input:[Group#1], offset:5, count:2",
"Group#1 Schema:[Column#25]",
" Union_5 input:[Group#2,Group#3]",
"Group#2 Schema:[Column#25]",
" Projection_6 input:[Group#4], test.t.a",
"Group#4 Schema:[test.t.a]",
" Projection_4 input:[Group#5], test.t.a",
"Group#5 Schema:[test.t.a]",
" Limit_25 input:[Group#6], offset:0, count:7",
"Group#6 Schema:[test.t.a]",
" TiKVSingleGather_10 input:[Group#7], table:t",
" TiKVSingleGather_22 input:[Group#8], table:t, index:e_d_c_str_prefix",
" TiKVSingleGather_20 input:[Group#9], table:t, index:c_d_e_str",
" TiKVSingleGather_18 input:[Group#10], table:t, index:f_g",
" TiKVSingleGather_16 input:[Group#11], table:t, index:g",
" TiKVSingleGather_14 input:[Group#12], table:t, index:f",
" TiKVSingleGather_12 input:[Group#13], table:t, index:c_d_e",
"Group#7 Schema:[test.t.a]",
" TableScan_9 table:t, pk col:test.t.a",
"Group#8 Schema:[test.t.a]",
" IndexScan_21 table:t, index:e_str, d_str, c_str",
"Group#9 Schema:[test.t.a]",
" IndexScan_19 table:t, index:c_str, d_str, e_str",
"Group#10 Schema:[test.t.a]",
" IndexScan_17 table:t, index:f, g",
"Group#11 Schema:[test.t.a]",
" IndexScan_15 table:t, index:g",
"Group#12 Schema:[test.t.a]",
" IndexScan_13 table:t, index:f",
"Group#13 Schema:[test.t.a]",
" IndexScan_11 table:t, index:c, d, e",
"Group#3 Schema:[Column#25]",
" Projection_7 input:[Group#14], test.t.b",
"Group#14 Schema:[test.t.b]",
" Projection_2 input:[Group#15], test.t.b",
"Group#15 Schema:[test.t.b]",
" Limit_25 input:[Group#16], offset:0, count:7",
"Group#16 Schema:[test.t.b]",
" TiKVSingleGather_24 input:[Group#17], table:t",
"Group#17 Schema:[test.t.b]",
" TableScan_23 table:t"
]
},
{
"SQL": "(select a from t) union all (select sum(a) from t where a > 2 group by b) order by a limit 2;",
"Result": [
Expand Down
51 changes: 51 additions & 0 deletions planner/cascades/transformation_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var defaultTransformationMap = map[memo.Operand][]Transformation{
memo.OperandLimit: {
NewRuleTransformLimitToTopN(),
NewRulePushLimitDownProjection(),
NewRulePushLimitDownUnionAll(),
},
memo.OperandProjection: {
NewRuleEliminateProjection(),
Expand Down Expand Up @@ -710,6 +711,56 @@ func (r *PushLimitDownProjection) OnTransform(old *memo.ExprIter) (newExprs []*m
return []*memo.GroupExpr{projExpr}, true, false, nil
}

// PushLimitDownUnionAll pushes limit to union all.
type PushLimitDownUnionAll struct {
baseRule
}

// NewRulePushLimitDownUnionAll creates a new Transformation PushLimitDownUnionAll.
// The pattern of this rule is `Limit->UnionAll->X`.
func NewRulePushLimitDownUnionAll() Transformation {
rule := &PushLimitDownUnionAll{}
rule.pattern = memo.BuildPattern(
memo.OperandLimit,
memo.EngineTiDBOnly,
memo.NewPattern(memo.OperandUnionAll, memo.EngineTiDBOnly),
)
return rule
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
}

// Match implements Transformation interface.
// Use appliedRuleSet in GroupExpr to avoid re-apply rules.
func (r *PushLimitDownUnionAll) Match(expr *memo.ExprIter) bool {
return !expr.GetExpr().HasAppliedRule(r)
}

// OnTransform implements Transformation interface.
// It will transform `Limit->UnionAll->X` to `Limit->UnionAll->Limit->X`.
func (r *PushLimitDownUnionAll) OnTransform(old *memo.ExprIter) (newExprs []*memo.GroupExpr, eraseOld bool, eraseAll bool, err error) {
limit := old.GetExpr().ExprNode.(*plannercore.LogicalLimit)
unionAll := old.Children[0].GetExpr().ExprNode.(*plannercore.LogicalUnionAll)
unionAllSchema := old.Children[0].Group.Prop.Schema

newLimit := plannercore.LogicalLimit{
Count: limit.Count + limit.Offset,
}.Init(limit.SCtx(), limit.SelectBlockOffset())

newUnionAllExpr := memo.NewGroupExpr(unionAll)
for _, childGroup := range old.Children[0].GetExpr().Children {
newLimitExpr := memo.NewGroupExpr(newLimit)
newLimitExpr.Children = append(newLimitExpr.Children, childGroup)
newLimitGroup := memo.NewGroupWithSchema(newLimitExpr, childGroup.Prop.Schema)

newUnionAllExpr.Children = append(newUnionAllExpr.Children, newLimitGroup)
}

newLimitExpr := memo.NewGroupExpr(limit)
newUnionAllGroup := memo.NewGroupWithSchema(newUnionAllExpr, unionAllSchema)
newLimitExpr.SetChildren(newUnionAllGroup)
newLimitExpr.AddAppliedRule(r)
return []*memo.GroupExpr{newLimitExpr}, true, false, nil
}

// PushSelDownJoin pushes Selection through Join.
type PushSelDownJoin struct {
baseRule
Expand Down
1 change: 1 addition & 0 deletions planner/cascades/transformation_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (s *testTransformationRuleSuite) TestTopNRules(c *C) {
memo.OperandLimit: {
NewRuleTransformLimitToTopN(),
NewRulePushLimitDownProjection(),
NewRulePushLimitDownUnionAll(),
},
memo.OperandDataSource: {
NewRuleEnumeratePaths(),
Expand Down