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 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
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
edytagarbarz marked this conversation as resolved.
Show resolved Hide resolved
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, Enforced: true}
edytagarbarz marked this conversation as resolved.
Show resolved Hide resolved
return prop.IsPrefix(childProperty)
}

// OnImplement implements ImplementationRule OnImplement interface
edytagarbarz marked this conversation as resolved.
Show resolved Hide resolved
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,
edytagarbarz marked this conversation as resolved.
Show resolved Hide resolved
lw.SelectBlockOffset(),
&property.PhysicalProperty{ExpectedCnt: math.MaxFloat64, Items: byItems, Enforced: true},
edytagarbarz marked this conversation as resolved.
Show resolved Hide resolved
)
physicalWindow.SetSchema(lw.Schema())
edytagarbarz marked this conversation as resolved.
Show resolved Hide resolved
return impl.NewWindowImpl(physicalWindow), nil
}
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
}