Skip to content

Commit

Permalink
fail aggregation inside of derived tables
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Jul 28, 2021
1 parent 20cb949 commit 7d879df
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
8 changes: 5 additions & 3 deletions go/vt/vtgate/planbuilder/jointree_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"vitess.io/vitess/go/vt/vterrors"
)

func transformToLogicalPlan(tree joinTree, semTable *semantics.SemTable, processing func(plan logicalPlan, sel *sqlparser.Select) (logicalPlan, error)) (logicalPlan, error) {
func transformToLogicalPlan(tree joinTree, semTable *semantics.SemTable, processing *postProcessor) (logicalPlan, error) {
switch n := tree.(type) {
case *routePlan:
return transformRoutePlan(n)
Expand All @@ -42,10 +42,12 @@ func transformToLogicalPlan(tree joinTree, semTable *semantics.SemTable, process
if err != nil {
return nil, err
}
plan, err = processing(plan, n.query)
processing.inDerived = true
plan, err = processing.planHorizon(plan, n.query)
if err != nil {
return nil, err
}
processing.inDerived = false

rb, isRoute := plan.(*route)
if !isRoute {
Expand All @@ -66,7 +68,7 @@ func transformToLogicalPlan(tree joinTree, semTable *semantics.SemTable, process
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG] unknown type encountered: %T", tree)
}

func transformJoinPlan(n *joinPlan, semTable *semantics.SemTable, processing func(plan logicalPlan, sel *sqlparser.Select) (logicalPlan, error)) (logicalPlan, error) {
func transformJoinPlan(n *joinPlan, semTable *semantics.SemTable, processing *postProcessor) (logicalPlan, error) {
lhs, err := transformToLogicalPlan(n.lhs, semTable, processing)
if err != nil {
return nil, err
Expand Down
61 changes: 41 additions & 20 deletions go/vt/vtgate/planbuilder/route_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,34 @@ func gen4Planner(_ string) func(sqlparser.Statement, *sqlparser.ReservedVars, Co
}
}

type postProcessor struct {
inDerived bool
semTable *semantics.SemTable
vschema ContextVSchema
}

func (pp *postProcessor) planHorizon(plan logicalPlan, sel *sqlparser.Select) (logicalPlan, error) {
hp := horizonPlanning{
sel: sel,
plan: plan,
semTable: pp.semTable,
vschema: pp.vschema,
inDerived: pp.inDerived,
}

plan, err := hp.planHorizon()
if err != nil {
return nil, err
}

plan, err = planLimit(sel.Limit, plan)
if err != nil {
return nil, err
}
return plan, nil

}

func newBuildSelectPlan(sel *sqlparser.Select, reservedVars *sqlparser.ReservedVars, vschema ContextVSchema) (logicalPlan, error) {
directives := sqlparser.ExtractCommentDirectives(sel.Comments)
if len(directives) > 0 {
Expand Down Expand Up @@ -91,32 +119,16 @@ func newBuildSelectPlan(sel *sqlparser.Select, reservedVars *sqlparser.ReservedV
return nil, err
}

postProcessing := func(plan logicalPlan, sel *sqlparser.Select) (logicalPlan, error) {
hp := horizonPlanning{
sel: sel,
plan: plan,
semTable: semTable,
vschema: vschema,
}

plan, err = hp.planHorizon()
if err != nil {
return nil, err
}

plan, err = planLimit(sel.Limit, plan)
if err != nil {
return nil, err
}
return plan, nil
postProcessing := &postProcessor{
semTable: semTable,
vschema: vschema,
}

plan, err := transformToLogicalPlan(tree, semTable, postProcessing)
if err != nil {
return nil, err
}

plan, err = postProcessing(plan, sel)
plan, err = postProcessing.planHorizon(plan, sel)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -201,6 +213,7 @@ type horizonPlanning struct {
semTable *semantics.SemTable
vschema ContextVSchema
qp *abstract.QueryProjection
inDerived bool
needsTruncation bool
vtgateGrouping bool
}
Expand All @@ -211,6 +224,14 @@ func (hp *horizonPlanning) planHorizon() (logicalPlan, error) {
return nil, hp.semTable.ProjectionErr
}

if hp.inDerived {
for _, expr := range hp.sel.SelectExprs {
if sqlparser.ContainsAggregation(expr) {
return nil, semantics.Gen4NotSupportedF("aggregation inside of derived table")
}
}
}

if ok && rb.isSingleShard() {
createSingleShardRoutePlan(hp.sel, rb)
return hp.plan, nil
Expand Down

0 comments on commit 7d879df

Please sign in to comment.