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 ImplementationRule for Window #14085

Merged
merged 8 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions planner/cascades/implementation_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ var defaultImplementationMap = map[memo.Operand][]ImplementationRule{
memo.OperandMaxOneRow: {
&ImplMaxOneRow{},
},
memo.OperandWindow: {
&ImplWindow{},
},
}

// ImplTableDual implements LogicalTableDual as PhysicalTableDual.
Expand Down Expand Up @@ -519,3 +522,38 @@ func (r *ImplMaxOneRow) OnImplement(expr *memo.GroupExpr, reqProp *property.Phys
&property.PhysicalProperty{ExpectedCnt: 2})
return impl.NewMaxOneRowImpl(physicalMaxOneRow), nil
}

// ImplWindow implements LogicalWindow to PhysicalWindow.
type ImplWindow struct {
}

// Match implements ImplementationRule Match interface.
func (w *ImplWindow) Match(expr *memo.GroupExpr, prop *property.PhysicalProperty) (matched bool) {
lw := expr.ExprNode.(*plannercore.LogicalWindow)
var byItems []property.Item
byItems = append(byItems, lw.PartitionBy...)
byItems = append(byItems, lw.OrderBy...)
childProperty := &property.PhysicalProperty{ExpectedCnt: math.MaxFloat64, Items: byItems}
Copy link
Member

Choose a reason for hiding this comment

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

ExpectedCnt is also unused here, you can remove it.

return prop.IsPrefix(childProperty)
}

// OnImplement implements ImplementationRule OnImplement interface.
func (w *ImplWindow) OnImplement(expr *memo.GroupExpr, reqProp *property.PhysicalProperty) (memo.Implementation, error) {
lw := expr.ExprNode.(*plannercore.LogicalWindow)
var byItems []property.Item
byItems = append(byItems, lw.PartitionBy...)
byItems = append(byItems, lw.OrderBy...)
physicalWindow := plannercore.PhysicalWindow{
WindowFuncDescs: lw.WindowFuncDescs,
PartitionBy: lw.PartitionBy,
OrderBy: lw.OrderBy,
Frame: lw.Frame,
}.Init(
lw.SCtx(),
expr.Group.Prop.Stats.ScaleByExpectCnt(reqProp.ExpectedCnt),
lw.SelectBlockOffset(),
&property.PhysicalProperty{ExpectedCnt: math.MaxFloat64, Items: byItems},
)
physicalWindow.SetSchema(expr.Group.Prop.Schema)
return impl.NewWindowImpl(physicalWindow), nil
}
3 changes: 2 additions & 1 deletion planner/cascades/testdata/integration_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"select a from t limit 1 offset 2",
"select b from t order by b limit 3",
"select a from t order by a limit 1 offset 2",
"select * from ((select a as aa from t t1) union all (select b as aa from t t2)) as t3 order by aa"
"select * from ((select a as aa from t t1) union all (select b as aa from t t2)) as t3 order by aa",
"select a, b, lag(a,1) over (order by b) from t order by b"
]
},
{
Expand Down
15 changes: 15 additions & 0 deletions planner/cascades/testdata/integration_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,21 @@
"33",
"44"
]
},
{
"SQL": "select a, b, lag(a,1) over (order by b) from t order by b",
"Plan": [
"Window_10 10000.00 root lag(test.t.a, 1)->Column#4 over(order by test.t.b asc)",
"└─Sort_15 10000.00 root test.t.b:asc",
" └─TableReader_13 10000.00 root data:TableScan_14",
" └─TableScan_14 10000.00 cop[tikv] table:t, range:[-inf,+inf], keep order:false, stats:pseudo"
],
"Result": [
"1 11 <nil>",
"2 22 1",
"3 33 2",
"4 44 3"
]
}
]
},
Expand Down
16 changes: 16 additions & 0 deletions planner/implementation/simple_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,19 @@ func (impl *MaxOneRowImpl) CalcCost(outCount float64, children ...memo.Implement
func NewMaxOneRowImpl(maxOneRow *plannercore.PhysicalMaxOneRow) *MaxOneRowImpl {
return &MaxOneRowImpl{baseImpl{plan: maxOneRow}}
}

// WindowImpl is the implementation of PhysicalWindow.
type WindowImpl struct {
baseImpl
}

// NewWindowImpl creates a new WindowImpl.
func NewWindowImpl(window *plannercore.PhysicalWindow) *WindowImpl {
return &WindowImpl{baseImpl{plan: window}}
}

// CalcCost implements Implementation CalcCost interface.
func (impl *WindowImpl) CalcCost(outCount float64, children ...memo.Implementation) float64 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consulted with @francis0407 . The Window cost is not included in the old planner and this is to be fixed later.

impl.cost = children[0].GetCost()
return impl.cost
}