From a120ee410232eaf028efdac01e76bb6829628001 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Mon, 9 Oct 2023 08:13:34 +0200 Subject: [PATCH 01/15] make AddColumn panic instead of returning an error Signed-off-by: Andres Taylor --- .../planbuilder/operators/aggregator.go | 33 +++++---------- .../planbuilder/operators/apply_join.go | 40 +++++-------------- .../vtgate/planbuilder/operators/comments.go | 2 +- .../vtgate/planbuilder/operators/distinct.go | 7 +--- go/vt/vtgate/planbuilder/operators/filter.go | 2 +- go/vt/vtgate/planbuilder/operators/horizon.go | 12 +++--- go/vt/vtgate/planbuilder/operators/limit.go | 2 +- .../planbuilder/operators/offset_planning.go | 5 +-- .../vtgate/planbuilder/operators/operator.go | 19 +++++++-- go/vt/vtgate/planbuilder/operators/ops/op.go | 2 +- .../vtgate/planbuilder/operators/ordering.go | 12 ++---- .../planbuilder/operators/projection.go | 18 ++++----- .../planbuilder/operators/query_planning.go | 5 +-- go/vt/vtgate/planbuilder/operators/route.go | 25 ++++-------- .../vtgate/planbuilder/operators/subquery.go | 7 +--- .../operators/subquery_container.go | 2 +- go/vt/vtgate/planbuilder/operators/table.go | 12 +++--- go/vt/vtgate/planbuilder/operators/union.go | 25 +++++------- go/vt/vtgate/planbuilder/operators/vindex.go | 8 ++-- 19 files changed, 93 insertions(+), 145 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/aggregator.go b/go/vt/vtgate/planbuilder/operators/aggregator.go index b572e05fb45..a7d62d7c2ae 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -135,10 +135,10 @@ func (a *Aggregator) FindCol(ctx *plancontext.PlanningContext, in sqlparser.Expr return -1, nil } -func (a *Aggregator) AddColumn(ctx *plancontext.PlanningContext, reuse bool, groupBy bool, ae *sqlparser.AliasedExpr) (int, error) { +func (a *Aggregator) AddColumn(ctx *plancontext.PlanningContext, reuse bool, groupBy bool, ae *sqlparser.AliasedExpr) int { rewritten, err := a.DT.RewriteExpression(ctx, ae.Expr) if err != nil { - return 0, err + panic(err) } ae = &sqlparser.AliasedExpr{ @@ -149,10 +149,10 @@ func (a *Aggregator) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gro if reuse { offset, err := a.findColInternal(ctx, ae, groupBy) if err != nil { - return 0, err + panic(err) } if offset >= 0 { - return offset, nil + return offset } } @@ -177,16 +177,13 @@ func (a *Aggregator) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gro offset := len(a.Columns) a.Columns = append(a.Columns, ae) - incomingOffset, err := a.Source.AddColumn(ctx, false, groupBy, ae) - if err != nil { - return 0, err - } + incomingOffset := a.Source.AddColumn(ctx, false, groupBy, ae) if offset != incomingOffset { - return 0, errFailedToPlan(ae) + panic(errFailedToPlan(ae)) } - return offset, nil + return offset } func (a *Aggregator) findColInternal(ctx *plancontext.PlanningContext, ae *sqlparser.AliasedExpr, addToGroupBy bool) (int, error) { @@ -373,10 +370,7 @@ func (a *Aggregator) addIfAggregationColumn(ctx *plancontext.PlanningContext, co } wrap := aeWrap(aggr.getPushColumn()) - offset, err := a.Source.AddColumn(ctx, false, false, wrap) - if err != nil { - return 0, err - } + offset := a.Source.AddColumn(ctx, false, false, wrap) if aggr.ColOffset != offset { return -1, errFailedToPlan(aggr.Original) } @@ -397,11 +391,7 @@ func (a *Aggregator) addIfGroupingColumn(ctx *plancontext.PlanningContext, colId } expr := a.Columns[colIdx] - offset, err := a.Source.AddColumn(ctx, false, true, expr) - if err != nil { - return -1, err - } - + offset := a.Source.AddColumn(ctx, false, true, expr) if gb.ColOffset != offset { return -1, errFailedToPlan(expr) } @@ -451,10 +441,7 @@ func (a *Aggregator) setTruncateColumnCount(offset int) { } func (a *Aggregator) internalAddColumn(ctx *plancontext.PlanningContext, aliasedExpr *sqlparser.AliasedExpr, addToGroupBy bool) (int, error) { - offset, err := a.Source.AddColumn(ctx, true, addToGroupBy, aliasedExpr) - if err != nil { - return 0, err - } + offset := a.Source.AddColumn(ctx, true, addToGroupBy, aliasedExpr) if offset == len(a.Columns) { // if we get an offset at the end of our current column list, it means we added a new column diff --git a/go/vt/vtgate/planbuilder/operators/apply_join.go b/go/vt/vtgate/planbuilder/operators/apply_join.go index 5e48fb4d5e3..cb329953586 100644 --- a/go/vt/vtgate/planbuilder/operators/apply_join.go +++ b/go/vt/vtgate/planbuilder/operators/apply_join.go @@ -165,19 +165,8 @@ func (aj *ApplyJoin) AddJoinPredicate(ctx *plancontext.PlanningContext, expr sql return nil } -func (aj *ApplyJoin) pushColLeft(ctx *plancontext.PlanningContext, e *sqlparser.AliasedExpr, addToGroupBy bool) (int, error) { - offset, err := aj.LHS.AddColumn(ctx, true, addToGroupBy, e) - if err != nil { - return 0, err - } - return offset, nil -} - func (aj *ApplyJoin) pushColRight(ctx *plancontext.PlanningContext, e *sqlparser.AliasedExpr, addToGroupBy bool) (int, error) { - offset, err := aj.RHS.AddColumn(ctx, true, addToGroupBy, e) - if err != nil { - return 0, err - } + offset := aj.RHS.AddColumn(ctx, true, addToGroupBy, e) return offset, nil } @@ -241,30 +230,30 @@ func (aj *ApplyJoin) AddColumn( reuse bool, groupBy bool, expr *sqlparser.AliasedExpr, -) (int, error) { +) int { if reuse { offset, err := aj.FindCol(ctx, expr.Expr, false) if err != nil { - return 0, err + panic(err) } if offset != -1 { - return offset, nil + return offset } } col, err := aj.getJoinColumnFor(ctx, expr, expr.Expr, groupBy) if err != nil { - return 0, err + panic(err) } offset := len(aj.JoinColumns) aj.JoinColumns = append(aj.JoinColumns, col) - return offset, nil + return offset } func (aj *ApplyJoin) planOffsets(ctx *plancontext.PlanningContext) (err error) { for _, col := range aj.JoinColumns { // Read the type description for JoinColumn to understand the following code for _, lhsExpr := range col.LHSExprs { - offset, err := aj.pushColLeft(ctx, aeWrap(lhsExpr.Expr), col.GroupBy) + offset := aj.LHS.AddColumn(ctx, true, col.GroupBy, aeWrap(lhsExpr.Expr)) if err != nil { return err } @@ -276,20 +265,14 @@ func (aj *ApplyJoin) planOffsets(ctx *plancontext.PlanningContext) (err error) { } } if col.RHSExpr != nil { - offset, err := aj.pushColRight(ctx, aeWrap(col.RHSExpr), col.GroupBy) - if err != nil { - return err - } + offset := aj.RHS.AddColumn(ctx, true, col.GroupBy, aeWrap(col.RHSExpr)) aj.addOffset(offset + 1) } } for _, col := range aj.JoinPredicates { for _, lhsExpr := range col.LHSExprs { - offset, err := aj.pushColLeft(ctx, aeWrap(lhsExpr.Expr), false) - if err != nil { - return err - } + offset := aj.LHS.AddColumn(ctx, true, false, aeWrap(lhsExpr.Expr)) aj.Vars[lhsExpr.Name] = offset } if err != nil { @@ -298,10 +281,7 @@ func (aj *ApplyJoin) planOffsets(ctx *plancontext.PlanningContext) (err error) { } for _, lhsExpr := range aj.ExtraLHSVars { - offset, err := aj.pushColLeft(ctx, aeWrap(lhsExpr.Expr), false) - if err != nil { - return err - } + offset := aj.LHS.AddColumn(ctx, true, false, aeWrap(lhsExpr.Expr)) aj.Vars[lhsExpr.Name] = offset } return nil diff --git a/go/vt/vtgate/planbuilder/operators/comments.go b/go/vt/vtgate/planbuilder/operators/comments.go index 46f9e8c7462..de737d965c1 100644 --- a/go/vt/vtgate/planbuilder/operators/comments.go +++ b/go/vt/vtgate/planbuilder/operators/comments.go @@ -55,7 +55,7 @@ func (l *LockAndComment) AddPredicate(ctx *plancontext.PlanningContext, expr sql return l, nil } -func (l *LockAndComment) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, expr *sqlparser.AliasedExpr) (int, error) { +func (l *LockAndComment) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, expr *sqlparser.AliasedExpr) int { return l.Source.AddColumn(ctx, reuseExisting, addToGroupBy, expr) } diff --git a/go/vt/vtgate/planbuilder/operators/distinct.go b/go/vt/vtgate/planbuilder/operators/distinct.go index f7f4b350fc7..63c24dc58b1 100644 --- a/go/vt/vtgate/planbuilder/operators/distinct.go +++ b/go/vt/vtgate/planbuilder/operators/distinct.go @@ -57,10 +57,7 @@ func (d *Distinct) planOffsets(ctx *plancontext.PlanningContext) error { typ, coll, _ := ctx.SemTable.TypeForExpr(e) if ctx.SemTable.NeedsWeightString(e) { - offset, err := d.Source.AddColumn(ctx, true, false, aeWrap(weightStringFor(e))) - if err != nil { - return err - } + offset := d.Source.AddColumn(ctx, true, false, aeWrap(weightStringFor(e))) wsCol = &offset } @@ -102,7 +99,7 @@ func (d *Distinct) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser return d, nil } -func (d *Distinct) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) (int, error) { +func (d *Distinct) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { return d.Source.AddColumn(ctx, reuse, gb, expr) } diff --git a/go/vt/vtgate/planbuilder/operators/filter.go b/go/vt/vtgate/planbuilder/operators/filter.go index 874e799cf43..f6770f03c0d 100644 --- a/go/vt/vtgate/planbuilder/operators/filter.go +++ b/go/vt/vtgate/planbuilder/operators/filter.go @@ -89,7 +89,7 @@ func (f *Filter) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.E return f, nil } -func (f *Filter) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) (int, error) { +func (f *Filter) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { return f.Source.AddColumn(ctx, reuse, gb, expr) } diff --git a/go/vt/vtgate/planbuilder/operators/horizon.go b/go/vt/vtgate/planbuilder/operators/horizon.go index 2f9d574d99d..0a73e841916 100644 --- a/go/vt/vtgate/planbuilder/operators/horizon.go +++ b/go/vt/vtgate/planbuilder/operators/horizon.go @@ -114,22 +114,22 @@ func (h *Horizon) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser. return h, nil } -func (h *Horizon) AddColumn(ctx *plancontext.PlanningContext, reuse bool, _ bool, expr *sqlparser.AliasedExpr) (int, error) { +func (h *Horizon) AddColumn(ctx *plancontext.PlanningContext, reuse bool, _ bool, expr *sqlparser.AliasedExpr) int { if !reuse { - return 0, errNoNewColumns + panic(errNoNewColumns) } col, ok := expr.Expr.(*sqlparser.ColName) if !ok { - return 0, vterrors.VT13001("cannot push non-ColName expression to horizon") + panic(vterrors.VT13001("cannot push non-ColName expression to horizon")) } offset, err := h.FindCol(ctx, col, false) if err != nil { - return 0, err + panic(err) } if offset < 0 { - return 0, errNoNewColumns + panic(errNoNewColumns) } - return offset, nil + return offset } var errNoNewColumns = vterrors.VT13001("can't add new columns to Horizon") diff --git a/go/vt/vtgate/planbuilder/operators/limit.go b/go/vt/vtgate/planbuilder/operators/limit.go index 79a6980b937..ee4d1a7c10b 100644 --- a/go/vt/vtgate/planbuilder/operators/limit.go +++ b/go/vt/vtgate/planbuilder/operators/limit.go @@ -56,7 +56,7 @@ func (l *Limit) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Ex return l, nil } -func (l *Limit) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) (int, error) { +func (l *Limit) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { return l.Source.AddColumn(ctx, reuse, gb, expr) } diff --git a/go/vt/vtgate/planbuilder/operators/offset_planning.go b/go/vt/vtgate/planbuilder/operators/offset_planning.go index 502df5e9c82..d5d57dc69fc 100644 --- a/go/vt/vtgate/planbuilder/operators/offset_planning.go +++ b/go/vt/vtgate/planbuilder/operators/offset_planning.go @@ -68,10 +68,7 @@ func useOffsets(ctx *plancontext.PlanningContext, expr sqlparser.Expr, op ops.Op notFound := func(e sqlparser.Expr) error { _, addToGroupBy := e.(*sqlparser.ColName) - offset, err := in.AddColumn(ctx, true, addToGroupBy, aeWrap(e)) - if err != nil { - return err - } + offset := in.AddColumn(ctx, true, addToGroupBy, aeWrap(e)) exprOffset = sqlparser.NewOffset(offset, e) return nil } diff --git a/go/vt/vtgate/planbuilder/operators/operator.go b/go/vt/vtgate/planbuilder/operators/operator.go index a1cfbfd0cc0..b373e1f7a1b 100644 --- a/go/vt/vtgate/planbuilder/operators/operator.go +++ b/go/vt/vtgate/planbuilder/operators/operator.go @@ -58,7 +58,9 @@ type ( ) // PlanQuery creates a query plan for a given SQL statement -func PlanQuery(ctx *plancontext.PlanningContext, stmt sqlparser.Statement) (ops.Operator, error) { +func PlanQuery(ctx *plancontext.PlanningContext, stmt sqlparser.Statement) (result ops.Operator, err error) { + defer PanicHandler(&err) + op, err := translateQueryToOp(ctx, stmt) if err != nil { return nil, err @@ -90,6 +92,17 @@ func PlanQuery(ctx *plancontext.PlanningContext, stmt sqlparser.Statement) (ops. return op, err } +func PanicHandler(err *error) { + if r := recover(); r != nil { + badness, ok := r.(error) + if !ok { + panic(r) + } + + *err = badness + } +} + // Inputs implements the Operator interface func (noInputs) Inputs() []ops.Operator { return nil @@ -103,8 +116,8 @@ func (noInputs) SetInputs(ops []ops.Operator) { } // AddColumn implements the Operator interface -func (noColumns) AddColumn(*plancontext.PlanningContext, bool, bool, *sqlparser.AliasedExpr) (int, error) { - return 0, vterrors.VT13001("noColumns operators have no column") +func (noColumns) AddColumn(*plancontext.PlanningContext, bool, bool, *sqlparser.AliasedExpr) int { + panic(vterrors.VT13001("noColumns operators have no column")) } func (noColumns) GetColumns(*plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { diff --git a/go/vt/vtgate/planbuilder/operators/ops/op.go b/go/vt/vtgate/planbuilder/operators/ops/op.go index 87bf9d9e12f..ed2d1a8dc67 100644 --- a/go/vt/vtgate/planbuilder/operators/ops/op.go +++ b/go/vt/vtgate/planbuilder/operators/ops/op.go @@ -44,7 +44,7 @@ type ( // TODO: we should remove this and replace it with rewriters AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (Operator, error) - AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, expr *sqlparser.AliasedExpr) (int, error) + AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, expr *sqlparser.AliasedExpr) int FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) diff --git a/go/vt/vtgate/planbuilder/operators/ordering.go b/go/vt/vtgate/planbuilder/operators/ordering.go index 07f82239728..f507ed9d1cd 100644 --- a/go/vt/vtgate/planbuilder/operators/ordering.go +++ b/go/vt/vtgate/planbuilder/operators/ordering.go @@ -62,7 +62,7 @@ func (o *Ordering) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser return o, nil } -func (o *Ordering) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) (int, error) { +func (o *Ordering) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { return o.Source.AddColumn(ctx, reuse, gb, expr) } @@ -84,10 +84,7 @@ func (o *Ordering) GetOrdering() ([]ops.OrderBy, error) { func (o *Ordering) planOffsets(ctx *plancontext.PlanningContext) error { for _, order := range o.Order { - offset, err := o.Source.AddColumn(ctx, true, false, aeWrap(order.SimplifiedExpr)) - if err != nil { - return err - } + offset := o.Source.AddColumn(ctx, true, false, aeWrap(order.SimplifiedExpr)) o.Offset = append(o.Offset, offset) if !ctx.SemTable.NeedsWeightString(order.SimplifiedExpr) { @@ -96,10 +93,7 @@ func (o *Ordering) planOffsets(ctx *plancontext.PlanningContext) error { } wsExpr := &sqlparser.WeightStringFuncExpr{Expr: order.SimplifiedExpr} - offset, err = o.Source.AddColumn(ctx, true, false, aeWrap(wsExpr)) - if err != nil { - return err - } + offset = o.Source.AddColumn(ctx, true, false, aeWrap(wsExpr)) o.WOffset = append(o.WOffset, offset) } diff --git a/go/vt/vtgate/planbuilder/operators/projection.go b/go/vt/vtgate/planbuilder/operators/projection.go index 686950ba56d..afb7b03a342 100644 --- a/go/vt/vtgate/planbuilder/operators/projection.go +++ b/go/vt/vtgate/planbuilder/operators/projection.go @@ -201,10 +201,7 @@ func createSimpleProjection(ctx *plancontext.PlanningContext, qp *QueryProjectio if err != nil { return nil, err } - offset, err := p.Source.AddColumn(ctx, true, false, ae) - if err != nil { - return nil, err - } + offset := p.Source.AddColumn(ctx, true, false, ae) expr := newProjExpr(ae) expr.Info = Offset(offset) _, err = p.addProjExpr(expr) @@ -307,8 +304,12 @@ func (p *Projection) addColumnsWithoutPushing(ctx *plancontext.PlanningContext, return offsets, nil } -func (p *Projection) AddColumn(ctx *plancontext.PlanningContext, reuse bool, addToGroupBy bool, ae *sqlparser.AliasedExpr) (int, error) { - return p.addColumn(ctx, reuse, addToGroupBy, ae, true) +func (p *Projection) AddColumn(ctx *plancontext.PlanningContext, reuse bool, addToGroupBy bool, ae *sqlparser.AliasedExpr) int { + column, err := p.addColumn(ctx, reuse, addToGroupBy, ae, true) + if err != nil { + panic(err) + } + return column } func (p *Projection) addColumn( @@ -355,10 +356,7 @@ func (p *Projection) addColumn( } // we need to push down this column to our input - inputOffset, err := p.Source.AddColumn(ctx, true, addToGroupBy, ae) - if err != nil { - return 0, err - } + inputOffset := p.Source.AddColumn(ctx, true, addToGroupBy, ae) pe.Info = Offset(inputOffset) // since we already know the offset, let's save the information return p.addProjExpr(pe) diff --git a/go/vt/vtgate/planbuilder/operators/query_planning.go b/go/vt/vtgate/planbuilder/operators/query_planning.go index b4ecbdb4a7f..ec5aa7f4e26 100644 --- a/go/vt/vtgate/planbuilder/operators/query_planning.go +++ b/go/vt/vtgate/planbuilder/operators/query_planning.go @@ -269,10 +269,7 @@ func pushProjectionInVindex( return nil, nil, err } for _, pe := range ap { - _, err = src.AddColumn(ctx, true, false, aeWrap(pe.EvalExpr)) - if err != nil { - return nil, nil, err - } + src.AddColumn(ctx, true, false, aeWrap(pe.EvalExpr)) } return src, rewrite.NewTree("push projection into vindex", p), nil } diff --git a/go/vt/vtgate/planbuilder/operators/route.go b/go/vt/vtgate/planbuilder/operators/route.go index 799b101892b..10704765de9 100644 --- a/go/vt/vtgate/planbuilder/operators/route.go +++ b/go/vt/vtgate/planbuilder/operators/route.go @@ -550,16 +550,16 @@ func createProjection(ctx *plancontext.PlanningContext, src ops.Operator) (*Proj return proj, nil } -func (r *Route) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) (int, error) { +func (r *Route) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { removeKeyspaceFromSelectExpr(expr) if reuse { offset, err := r.FindCol(ctx, expr.Expr, true) if err != nil { - return 0, err + panic(err) } if offset != -1 { - return offset, nil + return offset } } @@ -568,18 +568,18 @@ func (r *Route) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, op, ok, offsets := addMultipleColumnsToInput(ctx, r.Source, reuse, []bool{gb}, []*sqlparser.AliasedExpr{expr}) r.Source = op if ok { - return offsets[0], nil + return offsets[0] } // If no-one could be found, we probably don't have one yet, so we add one here src, err := createProjection(ctx, r.Source) if err != nil { - return 0, err + panic(err) } r.Source = src offsets, _ = src.addColumnsWithoutPushing(ctx, reuse, []bool{gb}, []*sqlparser.AliasedExpr{expr}) - return offsets[0], nil + return offsets[0] } type selectExpressions interface { @@ -712,14 +712,8 @@ func (r *Route) planOffsets(ctx *plancontext.PlanningContext) (err error) { if isSpecialOrderBy(order) { continue } - offset, err := r.AddColumn(ctx, true, false, aeWrap(order.SimplifiedExpr)) - if err != nil { - return err - } + offset := r.AddColumn(ctx, true, false, aeWrap(order.SimplifiedExpr)) - if err != nil { - return err - } o := RouteOrdering{ AST: order.Inner.Expr, Offset: offset, @@ -728,10 +722,7 @@ func (r *Route) planOffsets(ctx *plancontext.PlanningContext) (err error) { } if ctx.SemTable.NeedsWeightString(order.SimplifiedExpr) { ws := weightStringFor(order.SimplifiedExpr) - offset, err := r.AddColumn(ctx, true, false, aeWrap(ws)) - if err != nil { - return err - } + offset := r.AddColumn(ctx, true, false, aeWrap(ws)) o.WOffset = offset } r.Ordering = append(r.Ordering, o) diff --git a/go/vt/vtgate/planbuilder/operators/subquery.go b/go/vt/vtgate/planbuilder/operators/subquery.go index 55fcba6cd3b..a589c98fdb2 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery.go +++ b/go/vt/vtgate/planbuilder/operators/subquery.go @@ -62,10 +62,7 @@ func (sq *SubQuery) planOffsets(ctx *plancontext.PlanningContext) error { } for _, jc := range columns { for _, lhsExpr := range jc.LHSExprs { - offset, err := sq.Outer.AddColumn(ctx, true, false, aeWrap(lhsExpr.Expr)) - if err != nil { - return err - } + offset := sq.Outer.AddColumn(ctx, true, false, aeWrap(lhsExpr.Expr)) sq.Vars[lhsExpr.Name] = offset } } @@ -180,7 +177,7 @@ func (sq *SubQuery) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparse return sq, nil } -func (sq *SubQuery) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, exprs *sqlparser.AliasedExpr) (int, error) { +func (sq *SubQuery) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, exprs *sqlparser.AliasedExpr) int { return sq.Outer.AddColumn(ctx, reuseExisting, addToGroupBy, exprs) } diff --git a/go/vt/vtgate/planbuilder/operators/subquery_container.go b/go/vt/vtgate/planbuilder/operators/subquery_container.go index a2fba977436..3ccc2c81ec7 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery_container.go +++ b/go/vt/vtgate/planbuilder/operators/subquery_container.go @@ -77,7 +77,7 @@ func (sqc *SubQueryContainer) AddPredicate(ctx *plancontext.PlanningContext, exp return sqc, err } -func (sqc *SubQueryContainer) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, exprs *sqlparser.AliasedExpr) (int, error) { +func (sqc *SubQueryContainer) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, exprs *sqlparser.AliasedExpr) int { return sqc.Outer.AddColumn(ctx, reuseExisting, addToGroupBy, exprs) } diff --git a/go/vt/vtgate/planbuilder/operators/table.go b/go/vt/vtgate/planbuilder/operators/table.go index 226cbc6dea7..ac34bf6c6f9 100644 --- a/go/vt/vtgate/planbuilder/operators/table.go +++ b/go/vt/vtgate/planbuilder/operators/table.go @@ -65,8 +65,8 @@ func (to *Table) AddPredicate(_ *plancontext.PlanningContext, expr sqlparser.Exp return newFilter(to, expr), nil } -func (to *Table) AddColumn(*plancontext.PlanningContext, bool, bool, *sqlparser.AliasedExpr) (int, error) { - return 0, vterrors.VT13001("did not expect this method to be called") +func (to *Table) AddColumn(*plancontext.PlanningContext, bool, bool, *sqlparser.AliasedExpr) int { + panic(vterrors.VT13001("did not expect this method to be called")) } func (to *Table) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { @@ -111,20 +111,20 @@ func (to *Table) TablesUsed() []string { return SingleQualifiedIdentifier(to.VTable.Keyspace, to.VTable.Name) } -func addColumn(ctx *plancontext.PlanningContext, op ColNameColumns, e sqlparser.Expr) (int, error) { +func addColumn(ctx *plancontext.PlanningContext, op ColNameColumns, e sqlparser.Expr) int { col, ok := e.(*sqlparser.ColName) if !ok { - return 0, vterrors.VT09018(fmt.Sprintf("cannot add '%s' expression to a table/vindex", sqlparser.String(e))) + panic(vterrors.VT09018(fmt.Sprintf("cannot add '%s' expression to a table/vindex", sqlparser.String(e)))) } sqlparser.RemoveKeyspaceFromColName(col) cols := op.GetColNames() colAsExpr := func(c *sqlparser.ColName) sqlparser.Expr { return c } if offset, found := canReuseColumn(ctx, cols, e, colAsExpr); found { - return offset, nil + return offset } offset := len(cols) op.AddCol(col) - return offset, nil + return offset } func (to *Table) ShortDescription() string { diff --git a/go/vt/vtgate/planbuilder/operators/union.go b/go/vt/vtgate/planbuilder/operators/union.go index 54740e70a29..bda2f16a6ee 100644 --- a/go/vt/vtgate/planbuilder/operators/union.go +++ b/go/vt/vtgate/planbuilder/operators/union.go @@ -183,20 +183,20 @@ func (u *Union) GetSelectFor(source int) (*sqlparser.Select, error) { } } -func (u *Union) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) (int, error) { +func (u *Union) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { if reuse { offset, err := u.FindCol(ctx, expr.Expr, false) if err != nil { - return 0, err + panic(err) } if offset >= 0 { - return offset, nil + return offset } } cols, err := u.GetColumns(ctx) if err != nil { - return 0, err + panic(err) } switch e := expr.Expr.(type) { @@ -206,9 +206,9 @@ func (u *Union) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, return e.Name.EqualString(expr.ColumnName()) }) if offset == -1 { - return 0, vterrors.VT13001(fmt.Sprintf("could not find the column '%s' on the UNION", sqlparser.String(e))) + panic(vterrors.VT13001(fmt.Sprintf("could not find the column '%s' on the UNION", sqlparser.String(e)))) } - return offset, nil + return offset case *sqlparser.WeightStringFuncExpr: wsArg := e.Expr argIdx := slices.IndexFunc(cols, func(expr *sqlparser.AliasedExpr) bool { @@ -216,17 +216,17 @@ func (u *Union) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, }) if argIdx == -1 { - return 0, vterrors.VT13001(fmt.Sprintf("could not find the argument to the weight_string function: %s", sqlparser.String(wsArg))) + panic(vterrors.VT13001(fmt.Sprintf("could not find the argument to the weight_string function: %s", sqlparser.String(wsArg)))) } outputOffset, err := u.addWeightStringToOffset(ctx, argIdx, gb) if err != nil { - return 0, err + panic(err) } - return outputOffset, nil + return outputOffset default: - return 0, vterrors.VT13001(fmt.Sprintf("only weight_string function is expected - got %s", sqlparser.String(expr))) + panic(vterrors.VT13001(fmt.Sprintf("only weight_string function is expected - got %s", sqlparser.String(expr)))) } } @@ -238,10 +238,7 @@ func (u *Union) addWeightStringToOffset(ctx *plancontext.PlanningContext, argIdx if !ok { return 0, vterrors.VT09015() } - thisOffset, err := src.AddColumn(ctx, false, addToGroupBy, aeWrap(weightStringFor(ae.Expr))) - if err != nil { - return 0, err - } + thisOffset := src.AddColumn(ctx, false, addToGroupBy, aeWrap(weightStringFor(ae.Expr))) // all offsets for the newly added ws need to line up if i == 0 { diff --git a/go/vt/vtgate/planbuilder/operators/vindex.go b/go/vt/vtgate/planbuilder/operators/vindex.go index b0317ab332c..6a89ff48c7c 100644 --- a/go/vt/vtgate/planbuilder/operators/vindex.go +++ b/go/vt/vtgate/planbuilder/operators/vindex.go @@ -62,17 +62,17 @@ func (v *Vindex) Clone([]ops.Operator) ops.Operator { return &clone } -func (v *Vindex) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, ae *sqlparser.AliasedExpr) (int, error) { +func (v *Vindex) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, ae *sqlparser.AliasedExpr) int { if gb { - return 0, vterrors.VT13001("tried to add group by to a table") + panic(vterrors.VT13001("tried to add group by to a table")) } if reuse { offset, err := v.FindCol(ctx, ae.Expr, true) if err != nil { - return 0, err + panic(err) } if offset > -1 { - return offset, nil + return offset } } From 809ac6e9eb6d6de7ce9f979270c133af023192d1 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Mon, 9 Oct 2023 08:24:41 +0200 Subject: [PATCH 02/15] refactor: make AddPredicate panic instead of return error Signed-off-by: Andres Taylor --- .../planbuilder/operators/aggregator.go | 4 +-- .../planbuilder/operators/apply_join.go | 7 ++--- .../vtgate/planbuilder/operators/ast_to_op.go | 5 +--- .../vtgate/planbuilder/operators/comments.go | 10 +++---- .../vtgate/planbuilder/operators/distinct.go | 10 +++---- go/vt/vtgate/planbuilder/operators/filter.go | 10 +++---- go/vt/vtgate/planbuilder/operators/horizon.go | 20 ++++++-------- go/vt/vtgate/planbuilder/operators/join.go | 7 ++--- go/vt/vtgate/planbuilder/operators/joins.go | 26 +++++++------------ go/vt/vtgate/planbuilder/operators/limit.go | 10 +++---- .../vtgate/planbuilder/operators/operator.go | 4 +-- go/vt/vtgate/planbuilder/operators/ops/op.go | 2 +- .../vtgate/planbuilder/operators/ordering.go | 10 +++---- .../planbuilder/operators/projection.go | 10 +++---- .../planbuilder/operators/querygraph.go | 4 +-- go/vt/vtgate/planbuilder/operators/route.go | 12 +++------ .../planbuilder/operators/route_planning.go | 10 ++----- .../vtgate/planbuilder/operators/subquery.go | 10 +++---- .../operators/subquery_container.go | 7 +++-- go/vt/vtgate/planbuilder/operators/table.go | 4 +-- go/vt/vtgate/planbuilder/operators/union.go | 17 +++++------- go/vt/vtgate/planbuilder/operators/vindex.go | 20 +++++++------- 22 files changed, 79 insertions(+), 140 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/aggregator.go b/go/vt/vtgate/planbuilder/operators/aggregator.go index a7d62d7c2ae..ad6f62d8475 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -80,11 +80,11 @@ func (a *Aggregator) SetInputs(operators []ops.Operator) { a.Source = operators[0] } -func (a *Aggregator) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { +func (a *Aggregator) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { return &Filter{ Source: a, Predicates: []sqlparser.Expr{expr}, - }, nil + } } func (a *Aggregator) addColumnWithoutPushing(ctx *plancontext.PlanningContext, expr *sqlparser.AliasedExpr, addToGroupBy bool) (int, error) { diff --git a/go/vt/vtgate/planbuilder/operators/apply_join.go b/go/vt/vtgate/planbuilder/operators/apply_join.go index cb329953586..caf4aa65bfb 100644 --- a/go/vt/vtgate/planbuilder/operators/apply_join.go +++ b/go/vt/vtgate/planbuilder/operators/apply_join.go @@ -110,7 +110,7 @@ func (aj *ApplyJoin) Clone(inputs []ops.Operator) ops.Operator { return &kopy } -func (aj *ApplyJoin) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { +func (aj *ApplyJoin) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { return AddPredicate(ctx, aj, expr, false, newFilter) } @@ -156,10 +156,7 @@ func (aj *ApplyJoin) AddJoinPredicate(ctx *plancontext.PlanningContext, expr sql return err } aj.JoinPredicates = append(aj.JoinPredicates, col) - rhs, err := aj.RHS.AddPredicate(ctx, col.RHSExpr) - if err != nil { - return err - } + rhs := aj.RHS.AddPredicate(ctx, col.RHSExpr) aj.RHS = rhs return nil diff --git a/go/vt/vtgate/planbuilder/operators/ast_to_op.go b/go/vt/vtgate/planbuilder/operators/ast_to_op.go index e7628edacc5..af546bc8b85 100644 --- a/go/vt/vtgate/planbuilder/operators/ast_to_op.go +++ b/go/vt/vtgate/planbuilder/operators/ast_to_op.go @@ -91,10 +91,7 @@ func addWherePredicates(ctx *plancontext.PlanningContext, expr sqlparser.Expr, o if subq != nil { continue } - op, err = op.AddPredicate(ctx, expr) - if err != nil { - return nil, err - } + op = op.AddPredicate(ctx, expr) addColumnEquality(ctx, expr) } return sqc.getRootOperator(op), nil diff --git a/go/vt/vtgate/planbuilder/operators/comments.go b/go/vt/vtgate/planbuilder/operators/comments.go index de737d965c1..d8dc5a93fa8 100644 --- a/go/vt/vtgate/planbuilder/operators/comments.go +++ b/go/vt/vtgate/planbuilder/operators/comments.go @@ -46,13 +46,9 @@ func (l *LockAndComment) SetInputs(operators []ops.Operator) { l.Source = operators[0] } -func (l *LockAndComment) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { - newSrc, err := l.Source.AddPredicate(ctx, expr) - if err != nil { - return nil, err - } - l.Source = newSrc - return l, nil +func (l *LockAndComment) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { + l.Source = l.Source.AddPredicate(ctx, expr) + return l } func (l *LockAndComment) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, expr *sqlparser.AliasedExpr) int { diff --git a/go/vt/vtgate/planbuilder/operators/distinct.go b/go/vt/vtgate/planbuilder/operators/distinct.go index 63c24dc58b1..3ce832667c9 100644 --- a/go/vt/vtgate/planbuilder/operators/distinct.go +++ b/go/vt/vtgate/planbuilder/operators/distinct.go @@ -90,13 +90,9 @@ func (d *Distinct) SetInputs(operators []ops.Operator) { d.Source = operators[0] } -func (d *Distinct) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { - newSrc, err := d.Source.AddPredicate(ctx, expr) - if err != nil { - return nil, err - } - d.Source = newSrc - return d, nil +func (d *Distinct) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { + d.Source = d.Source.AddPredicate(ctx, expr) + return d } func (d *Distinct) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { diff --git a/go/vt/vtgate/planbuilder/operators/filter.go b/go/vt/vtgate/planbuilder/operators/filter.go index f6770f03c0d..858877108f5 100644 --- a/go/vt/vtgate/planbuilder/operators/filter.go +++ b/go/vt/vtgate/planbuilder/operators/filter.go @@ -80,13 +80,9 @@ func (f *Filter) UnsolvedPredicates(st *semantics.SemTable) []sqlparser.Expr { return result } -func (f *Filter) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { - newSrc, err := f.Source.AddPredicate(ctx, expr) - if err != nil { - return nil, err - } - f.Source = newSrc - return f, nil +func (f *Filter) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { + f.Source = f.Source.AddPredicate(ctx, expr) + return f } func (f *Filter) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { diff --git a/go/vt/vtgate/planbuilder/operators/horizon.go b/go/vt/vtgate/planbuilder/operators/horizon.go index 0a73e841916..1587b96afc4 100644 --- a/go/vt/vtgate/planbuilder/operators/horizon.go +++ b/go/vt/vtgate/planbuilder/operators/horizon.go @@ -85,12 +85,11 @@ func (h *Horizon) SetInputs(ops []ops.Operator) { h.Source = ops[0] } -func (h *Horizon) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { +func (h *Horizon) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { if _, isUNion := h.Source.(*Union); isUNion { // If we have a derived table on top of a UNION, we can let the UNION do the expression rewriting - var err error - h.Source, err = h.Source.AddPredicate(ctx, expr) - return h, err + h.Source = h.Source.AddPredicate(ctx, expr) + return h } tableInfo, err := ctx.SemTable.TableInfoForExpr(expr) if err != nil { @@ -98,20 +97,17 @@ func (h *Horizon) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser. return &Filter{ Source: h, Predicates: []sqlparser.Expr{expr}, - }, nil + } } - return nil, err + panic(err) } newExpr := semantics.RewriteDerivedTableExpression(expr, tableInfo) if sqlparser.ContainsAggregation(newExpr) { - return &Filter{Source: h, Predicates: []sqlparser.Expr{expr}}, nil - } - h.Source, err = h.Source.AddPredicate(ctx, newExpr) - if err != nil { - return nil, err + return &Filter{Source: h, Predicates: []sqlparser.Expr{expr}} } - return h, nil + h.Source = h.Source.AddPredicate(ctx, newExpr) + return h } func (h *Horizon) AddColumn(ctx *plancontext.PlanningContext, reuse bool, _ bool, expr *sqlparser.AliasedExpr) int { diff --git a/go/vt/vtgate/planbuilder/operators/join.go b/go/vt/vtgate/planbuilder/operators/join.go index 693b7a75d8e..dc5d4153d54 100644 --- a/go/vt/vtgate/planbuilder/operators/join.go +++ b/go/vt/vtgate/planbuilder/operators/join.go @@ -127,15 +127,12 @@ func createInnerJoin(ctx *plancontext.PlanningContext, tableExpr *sqlparser.Join if subq != nil { continue } - op, err = op.AddPredicate(ctx, pred) - if err != nil { - return nil, err - } + op = op.AddPredicate(ctx, pred) } return sqc.getRootOperator(op), nil } -func (j *Join) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { +func (j *Join) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { return AddPredicate(ctx, j, expr, false, newFilter) } diff --git a/go/vt/vtgate/planbuilder/operators/joins.go b/go/vt/vtgate/planbuilder/operators/joins.go index 2d7451b83d4..3b5c31c5dce 100644 --- a/go/vt/vtgate/planbuilder/operators/joins.go +++ b/go/vt/vtgate/planbuilder/operators/joins.go @@ -40,17 +40,14 @@ func AddPredicate( expr sqlparser.Expr, joinPredicates bool, newFilter func(ops.Operator, sqlparser.Expr) ops.Operator, -) (ops.Operator, error) { +) ops.Operator { deps := ctx.SemTable.RecursiveDeps(expr) switch { case deps.IsSolvedBy(TableID(join.GetLHS())): // predicates can always safely be pushed down to the lhs if that is all they depend on - lhs, err := join.GetLHS().AddPredicate(ctx, expr) - if err != nil { - return nil, err - } + lhs := join.GetLHS().AddPredicate(ctx, expr) join.SetLHS(lhs) - return join, err + return join case deps.IsSolvedBy(TableID(join.GetRHS())): // if we are dealing with an outer join, always start by checking if this predicate can turn // the join into an inner join @@ -61,16 +58,13 @@ func AddPredicate( if !joinPredicates && !join.IsInner() { // if we still are dealing with an outer join // we need to filter after the join has been evaluated - return newFilter(join, expr), nil + return newFilter(join, expr) } // For inner joins, we can just push the filtering on the RHS - rhs, err := join.GetRHS().AddPredicate(ctx, expr) - if err != nil { - return nil, err - } + rhs := join.GetRHS().AddPredicate(ctx, expr) join.SetRHS(rhs) - return join, err + return join case deps.IsSolvedBy(TableID(join)): // if we are dealing with an outer join, always start by checking if this predicate can turn @@ -82,17 +76,17 @@ func AddPredicate( if !joinPredicates && !join.IsInner() { // if we still are dealing with an outer join // we need to filter after the join has been evaluated - return newFilter(join, expr), nil + return newFilter(join, expr) } err := join.AddJoinPredicate(ctx, expr) if err != nil { - return nil, err + panic(err) } - return join, nil + return join } - return nil, nil + return nil } // we are looking for predicates like `tbl.col = <>` or `<> = tbl.col`, diff --git a/go/vt/vtgate/planbuilder/operators/limit.go b/go/vt/vtgate/planbuilder/operators/limit.go index ee4d1a7c10b..221e3e4789f 100644 --- a/go/vt/vtgate/planbuilder/operators/limit.go +++ b/go/vt/vtgate/planbuilder/operators/limit.go @@ -47,13 +47,9 @@ func (l *Limit) SetInputs(operators []ops.Operator) { l.Source = operators[0] } -func (l *Limit) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { - newSrc, err := l.Source.AddPredicate(ctx, expr) - if err != nil { - return nil, err - } - l.Source = newSrc - return l, nil +func (l *Limit) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { + l.Source = l.Source.AddPredicate(ctx, expr) + return l } func (l *Limit) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { diff --git a/go/vt/vtgate/planbuilder/operators/operator.go b/go/vt/vtgate/planbuilder/operators/operator.go index b373e1f7a1b..e44ea0195fa 100644 --- a/go/vt/vtgate/planbuilder/operators/operator.go +++ b/go/vt/vtgate/planbuilder/operators/operator.go @@ -133,8 +133,8 @@ func (noColumns) GetSelectExprs(*plancontext.PlanningContext) (sqlparser.SelectE } // AddPredicate implements the Operator interface -func (noPredicates) AddPredicate(*plancontext.PlanningContext, sqlparser.Expr) (ops.Operator, error) { - return nil, vterrors.VT13001("the noColumns operator cannot accept predicates") +func (noPredicates) AddPredicate(*plancontext.PlanningContext, sqlparser.Expr) ops.Operator { + panic(vterrors.VT13001("the noColumns operator cannot accept predicates")) } // tryTruncateColumnsAt will see if we can truncate the columns by just asking the operator to do it for us diff --git a/go/vt/vtgate/planbuilder/operators/ops/op.go b/go/vt/vtgate/planbuilder/operators/ops/op.go index ed2d1a8dc67..d3f89fa6e42 100644 --- a/go/vt/vtgate/planbuilder/operators/ops/op.go +++ b/go/vt/vtgate/planbuilder/operators/ops/op.go @@ -42,7 +42,7 @@ type ( // If we encounter a join and the predicate depends on both sides of the join, the predicate will be split into two parts, // where data is fetched from the LHS of the join to be used in the evaluation on the RHS // TODO: we should remove this and replace it with rewriters - AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (Operator, error) + AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) Operator AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, expr *sqlparser.AliasedExpr) int diff --git a/go/vt/vtgate/planbuilder/operators/ordering.go b/go/vt/vtgate/planbuilder/operators/ordering.go index f507ed9d1cd..3130ba98e89 100644 --- a/go/vt/vtgate/planbuilder/operators/ordering.go +++ b/go/vt/vtgate/planbuilder/operators/ordering.go @@ -53,13 +53,9 @@ func (o *Ordering) SetInputs(operators []ops.Operator) { o.Source = operators[0] } -func (o *Ordering) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { - newSrc, err := o.Source.AddPredicate(ctx, expr) - if err != nil { - return nil, err - } - o.Source = newSrc - return o, nil +func (o *Ordering) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { + o.Source = o.Source.AddPredicate(ctx, expr) + return o } func (o *Ordering) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { diff --git a/go/vt/vtgate/planbuilder/operators/projection.go b/go/vt/vtgate/planbuilder/operators/projection.go index afb7b03a342..b84fd41a978 100644 --- a/go/vt/vtgate/planbuilder/operators/projection.go +++ b/go/vt/vtgate/planbuilder/operators/projection.go @@ -383,14 +383,10 @@ func (p *Projection) SetInputs(operators []ops.Operator) { p.Source = operators[0] } -func (p *Projection) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { +func (p *Projection) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { // we just pass through the predicate to our source - src, err := p.Source.AddPredicate(ctx, expr) - if err != nil { - return nil, err - } - p.Source = src - return p, nil + p.Source = p.Source.AddPredicate(ctx, expr) + return p } func (p *Projection) GetColumns(*plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { diff --git a/go/vt/vtgate/planbuilder/operators/querygraph.go b/go/vt/vtgate/planbuilder/operators/querygraph.go index f384607fe10..7195f22567a 100644 --- a/go/vt/vtgate/planbuilder/operators/querygraph.go +++ b/go/vt/vtgate/planbuilder/operators/querygraph.go @@ -180,11 +180,11 @@ func (qg *QueryGraph) GetOrdering() ([]ops.OrderBy, error) { return nil, nil } -func (qg *QueryGraph) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { +func (qg *QueryGraph) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { for _, e := range sqlparser.SplitAndExpression(nil, expr) { qg.collectPredicate(ctx, e) } - return qg, nil + return qg } // Clone implements the Operator interface diff --git a/go/vt/vtgate/planbuilder/operators/route.go b/go/vt/vtgate/planbuilder/operators/route.go index 10704765de9..b6d7135080f 100644 --- a/go/vt/vtgate/planbuilder/operators/route.go +++ b/go/vt/vtgate/planbuilder/operators/route.go @@ -518,21 +518,17 @@ func createAlternateRoutesFromVSchemaTable( return routes, nil } -func (r *Route) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { +func (r *Route) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { // first we see if the predicate changes how we route newRouting, err := UpdateRoutingLogic(ctx, expr, r.Routing) if err != nil { - return nil, err + panic(err) } r.Routing = newRouting // we also need to push the predicate down into the query - newSrc, err := r.Source.AddPredicate(ctx, expr) - if err != nil { - return nil, err - } - r.Source = newSrc - return r, err + r.Source = r.Source.AddPredicate(ctx, expr) + return r } func createProjection(ctx *plancontext.PlanningContext, src ops.Operator) (*Projection, error) { diff --git a/go/vt/vtgate/planbuilder/operators/route_planning.go b/go/vt/vtgate/planbuilder/operators/route_planning.go index 720f2f56480..96220f89ea2 100644 --- a/go/vt/vtgate/planbuilder/operators/route_planning.go +++ b/go/vt/vtgate/planbuilder/operators/route_planning.go @@ -224,10 +224,7 @@ func seedOperatorList(ctx *plancontext.PlanningContext, qg *QueryGraph) ([]ops.O return nil, err } if qg.NoDeps != nil { - plan, err = plan.AddPredicate(ctx, qg.NoDeps) - if err != nil { - return nil, err - } + plan = plan.AddPredicate(ctx, qg.NoDeps) } plans[i] = plan } @@ -621,10 +618,7 @@ func pushJoinPredicates(ctx *plancontext.PlanningContext, exprs []sqlparser.Expr } for _, expr := range exprs { - _, err := AddPredicate(ctx, op, expr, true, newFilter) - if err != nil { - return nil, err - } + AddPredicate(ctx, op, expr, true, newFilter) } return op, nil diff --git a/go/vt/vtgate/planbuilder/operators/subquery.go b/go/vt/vtgate/planbuilder/operators/subquery.go index a589c98fdb2..076e18ac561 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery.go +++ b/go/vt/vtgate/planbuilder/operators/subquery.go @@ -168,13 +168,9 @@ func (sq *SubQuery) ShortDescription() string { return fmt.Sprintf("%s %v%s", typ, sq.FilterType.String(), pred) } -func (sq *SubQuery) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { - newOuter, err := sq.Outer.AddPredicate(ctx, expr) - if err != nil { - return nil, err - } - sq.Outer = newOuter - return sq, nil +func (sq *SubQuery) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { + sq.Outer = sq.Outer.AddPredicate(ctx, expr) + return sq } func (sq *SubQuery) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, exprs *sqlparser.AliasedExpr) int { diff --git a/go/vt/vtgate/planbuilder/operators/subquery_container.go b/go/vt/vtgate/planbuilder/operators/subquery_container.go index 3ccc2c81ec7..1eed3bed746 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery_container.go +++ b/go/vt/vtgate/planbuilder/operators/subquery_container.go @@ -71,10 +71,9 @@ func (sqc *SubQueryContainer) ShortDescription() string { return "" } -func (sqc *SubQueryContainer) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { - newSrc, err := sqc.Outer.AddPredicate(ctx, expr) - sqc.Outer = newSrc - return sqc, err +func (sqc *SubQueryContainer) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { + sqc.Outer = sqc.Outer.AddPredicate(ctx, expr) + return sqc } func (sqc *SubQueryContainer) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, exprs *sqlparser.AliasedExpr) int { diff --git a/go/vt/vtgate/planbuilder/operators/table.go b/go/vt/vtgate/planbuilder/operators/table.go index ac34bf6c6f9..2fba8a363c0 100644 --- a/go/vt/vtgate/planbuilder/operators/table.go +++ b/go/vt/vtgate/planbuilder/operators/table.go @@ -61,8 +61,8 @@ func (to *Table) introducesTableID() semantics.TableSet { } // AddPredicate implements the PhysicalOperator interface -func (to *Table) AddPredicate(_ *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { - return newFilter(to, expr), nil +func (to *Table) AddPredicate(_ *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { + return newFilter(to, expr) } func (to *Table) AddColumn(*plancontext.PlanningContext, bool, bool, *sqlparser.AliasedExpr) int { diff --git a/go/vt/vtgate/planbuilder/operators/union.go b/go/vt/vtgate/planbuilder/operators/union.go index bda2f16a6ee..e51084e9b6e 100644 --- a/go/vt/vtgate/planbuilder/operators/union.go +++ b/go/vt/vtgate/planbuilder/operators/union.go @@ -93,39 +93,36 @@ Notice how `X.col = 42` has been translated to `foo = 42` and `id = 42` on respe The first SELECT of the union dictates the column names, and the second is whatever expression can be found on the same offset. The names of the RHS are discarded. */ -func (u *Union) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { +func (u *Union) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { offsets := make(map[string]int) sel, err := u.GetSelectFor(0) if err != nil { - return nil, err + panic(err) } for i, selectExpr := range sel.SelectExprs { ae, ok := selectExpr.(*sqlparser.AliasedExpr) if !ok { - return nil, vterrors.VT12001("pushing predicates on UNION where the first SELECT contains * or NEXT") + panic(vterrors.VT12001("pushing predicates on UNION where the first SELECT contains * or NEXT")) } offsets[ae.ColumnName()] = i } needsFilter, exprPerSource, err := u.predicatePerSource(expr, offsets) if err != nil { - return nil, err + panic(err) } if needsFilter { return &Filter{ Source: u, Predicates: []sqlparser.Expr{expr}, - }, nil + } } for i, src := range u.Sources { - u.Sources[i], err = src.AddPredicate(ctx, exprPerSource[i]) - if err != nil { - return nil, err - } + u.Sources[i] = src.AddPredicate(ctx, exprPerSource[i]) } - return u, nil + return u } func (u *Union) predicatePerSource(expr sqlparser.Expr, offsets map[string]int) (bool, []sqlparser.Expr, error) { diff --git a/go/vt/vtgate/planbuilder/operators/vindex.go b/go/vt/vtgate/planbuilder/operators/vindex.go index 6a89ff48c7c..fc92187589c 100644 --- a/go/vt/vtgate/planbuilder/operators/vindex.go +++ b/go/vt/vtgate/planbuilder/operators/vindex.go @@ -124,31 +124,31 @@ func (v *Vindex) CheckValid() error { return nil } -func (v *Vindex) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { +func (v *Vindex) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { for _, e := range sqlparser.SplitAndExpression(nil, expr) { deps := ctx.SemTable.RecursiveDeps(e) if deps.NumberOfTables() > 1 { - return nil, vterrors.VT09018(wrongWhereCond + " (multiple tables involved)") + panic(vterrors.VT09018(wrongWhereCond + " (multiple tables involved)")) } // check if we already have a predicate if v.OpCode != engine.VindexNone { - return nil, vterrors.VT09018(wrongWhereCond + " (multiple filters)") + panic(vterrors.VT09018(wrongWhereCond + " (multiple filters)")) } // check LHS comparison, ok := e.(*sqlparser.ComparisonExpr) if !ok { - return nil, vterrors.VT09018(wrongWhereCond + " (not a comparison)") + panic(vterrors.VT09018(wrongWhereCond + " (not a comparison)")) } if comparison.Operator != sqlparser.EqualOp && comparison.Operator != sqlparser.InOp { - return nil, vterrors.VT09018(wrongWhereCond + " (not equality)") + panic(vterrors.VT09018(wrongWhereCond + " (not equality)")) } colname, ok := comparison.Left.(*sqlparser.ColName) if !ok { - return nil, vterrors.VT09018(wrongWhereCond + " (lhs is not a column)") + panic(vterrors.VT09018(wrongWhereCond + " (lhs is not a column)")) } if !colname.Name.EqualString("id") { - return nil, vterrors.VT09018(wrongWhereCond + " (lhs is not id)") + panic(vterrors.VT09018(wrongWhereCond + " (lhs is not id)")) } // check RHS @@ -156,15 +156,15 @@ func (v *Vindex) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.E if sqlparser.IsValue(comparison.Right) || sqlparser.IsSimpleTuple(comparison.Right) { v.Value = comparison.Right } else { - return nil, vterrors.VT09018(wrongWhereCond + " (rhs is not a value)") + panic(vterrors.VT09018(wrongWhereCond + " (rhs is not a value)")) } if err != nil { - return nil, vterrors.VT09018(wrongWhereCond+": %v", err) + panic(vterrors.VT09018(wrongWhereCond+": %v", err)) } v.OpCode = engine.VindexMap v.Table.Predicates = append(v.Table.Predicates, e) } - return v, nil + return v } // TablesUsed implements the Operator interface. From 6ba7dd0da100a5ce6eb9a2eff5eda5aa300d7395 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Mon, 9 Oct 2023 12:12:43 +0200 Subject: [PATCH 03/15] refactor: use panic instead of errors for SQL_builder Signed-off-by: Andres Taylor --- .../planbuilder/operators/SQL_builder.go | 218 ++++++------------ 1 file changed, 73 insertions(+), 145 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/SQL_builder.go b/go/vt/vtgate/planbuilder/operators/SQL_builder.go index 9802e374d87..eff070aa6d2 100644 --- a/go/vt/vtgate/planbuilder/operators/SQL_builder.go +++ b/go/vt/vtgate/planbuilder/operators/SQL_builder.go @@ -41,12 +41,11 @@ func (qb *queryBuilder) asSelectStatement() sqlparser.SelectStatement { return qb.stmt.(sqlparser.SelectStatement) } -func ToSQL(ctx *plancontext.PlanningContext, op ops.Operator) (sqlparser.Statement, ops.Operator, error) { +func ToSQL(ctx *plancontext.PlanningContext, op ops.Operator) (_ sqlparser.Statement, _ ops.Operator, err error) { + defer PanicHandler(&err) + q := &queryBuilder{ctx: ctx} - err := buildQuery(op, q) - if err != nil { - return nil, nil, err - } + buildQuery(op, q) if ctx.SemTable != nil { q.sortTables() } @@ -119,22 +118,24 @@ func (qb *queryBuilder) addGroupBy(original sqlparser.Expr) { sel.GroupBy = append(sel.GroupBy, original) } -func (qb *queryBuilder) addProjection(projection sqlparser.SelectExpr) error { +func (qb *queryBuilder) addProjection(projection sqlparser.SelectExpr) { switch stmt := qb.stmt.(type) { case *sqlparser.Select: stmt.SelectExprs = append(stmt.SelectExprs, projection) - return nil + return case *sqlparser.Union: if ae, ok := projection.(*sqlparser.AliasedExpr); ok { if col, ok := ae.Expr.(*sqlparser.ColName); ok { - return checkUnionColumnByName(col, stmt) + checkUnionColumnByName(col, stmt) + return } } qb.pushUnionInsideDerived() - return qb.addProjection(projection) + qb.addProjection(projection) + return } - return vterrors.VT13001(fmt.Sprintf("unknown select statement type: %T", qb.stmt)) + panic(vterrors.VT13001(fmt.Sprintf("unknown select statement type: %T", qb.stmt))) } func (qb *queryBuilder) pushUnionInsideDerived() { @@ -166,7 +167,7 @@ func unionSelects(exprs sqlparser.SelectExprs) (selectExprs sqlparser.SelectExpr return } -func checkUnionColumnByName(column *sqlparser.ColName, sel sqlparser.SelectStatement) error { +func checkUnionColumnByName(column *sqlparser.ColName, sel sqlparser.SelectStatement) { colName := column.Name.String() exprs := sqlparser.GetFirstSelect(sel).SelectExprs offset := slices.IndexFunc(exprs, func(expr sqlparser.SelectExpr) bool { @@ -180,9 +181,8 @@ func checkUnionColumnByName(column *sqlparser.ColName, sel sqlparser.SelectState return false }) if offset == -1 { - return vterrors.VT12001(fmt.Sprintf("did not find column [%s] on UNION", sqlparser.String(column))) + panic(vterrors.VT12001(fmt.Sprintf("did not find column [%s] on UNION", sqlparser.String(column)))) } - return nil } func (qb *queryBuilder) clearProjections() { @@ -316,14 +316,12 @@ func removeKeyspaceFromSelectExpr(expr sqlparser.SelectExpr) { } } -func stripDownQuery(from, to sqlparser.SelectStatement) error { - var err error - +func stripDownQuery(from, to sqlparser.SelectStatement) { switch node := from.(type) { case *sqlparser.Select: toNode, ok := to.(*sqlparser.Select) if !ok { - return vterrors.VT13001("AST did not match") + panic(vterrors.VT13001("AST did not match")) } toNode.Distinct = node.Distinct toNode.GroupBy = node.GroupBy @@ -338,52 +336,43 @@ func stripDownQuery(from, to sqlparser.SelectStatement) error { case *sqlparser.Union: toNode, ok := to.(*sqlparser.Union) if !ok { - return vterrors.VT13001("AST did not match") - } - err = stripDownQuery(node.Left, toNode.Left) - if err != nil { - return err - } - err = stripDownQuery(node.Right, toNode.Right) - if err != nil { - return err + panic(vterrors.VT13001("AST did not match")) } + stripDownQuery(node.Left, toNode.Left) + stripDownQuery(node.Right, toNode.Right) toNode.OrderBy = node.OrderBy default: - return vterrors.VT13001(fmt.Sprintf("this should not happen - we have covered all implementations of SelectStatement %T", from)) + panic(vterrors.VT13001(fmt.Sprintf("this should not happen - we have covered all implementations of SelectStatement %T", from))) } - return nil } // buildQuery recursively builds the query into an AST, from an operator tree -func buildQuery(op ops.Operator, qb *queryBuilder) error { +func buildQuery(op ops.Operator, qb *queryBuilder) { switch op := op.(type) { case *Table: buildTable(op, qb) case *Projection: - return buildProjection(op, qb) + buildProjection(op, qb) case *ApplyJoin: - return buildApplyJoin(op, qb) + buildApplyJoin(op, qb) case *Filter: - return buildFilter(op, qb) + buildFilter(op, qb) case *Horizon: if op.TableId != nil { - return buildDerived(op, qb) + buildDerived(op, qb) + return } - return buildHorizon(op, qb) + buildHorizon(op, qb) case *Limit: - return buildLimit(op, qb) + buildLimit(op, qb) case *Ordering: - return buildOrdering(op, qb) + buildOrdering(op, qb) case *Aggregator: - return buildAggregation(op, qb) + buildAggregation(op, qb) case *Union: - return buildUnion(op, qb) + buildUnion(op, qb) case *Distinct: - err := buildQuery(op.Source, qb) - if err != nil { - return err - } + buildQuery(op.Source, qb) qb.asSelectStatement().MakeDistinct() case *Update: buildUpdate(op, qb) @@ -392,9 +381,8 @@ func buildQuery(op ops.Operator, qb *queryBuilder) error { case *Insert: buildDML(op, qb) default: - return vterrors.VT13001(fmt.Sprintf("unknown operator to convert to SQL: %T", op)) + panic(vterrors.VT13001(fmt.Sprintf("unknown operator to convert to SQL: %T", op))) } - return nil } func buildUpdate(op *Update, qb *queryBuilder) { @@ -436,23 +424,17 @@ func buildDML(op OpWithAST, qb *queryBuilder) { qb.dmlOperator = op } -func buildAggregation(op *Aggregator, qb *queryBuilder) error { - err := buildQuery(op.Source, qb) - if err != nil { - return err - } +func buildAggregation(op *Aggregator, qb *queryBuilder) { + buildQuery(op.Source, qb) qb.clearProjections() cols, err := op.GetColumns(qb.ctx) if err != nil { - return err + panic(err) } for _, column := range cols { - err := qb.addProjection(column) - if err != nil { - return err - } + qb.addProjection(column) } for _, by := range op.Grouping { @@ -462,29 +444,19 @@ func buildAggregation(op *Aggregator, qb *queryBuilder) error { qb.addGroupBy(weightStringFor(simplified)) } } - - return nil } -func buildOrdering(op *Ordering, qb *queryBuilder) error { - err := buildQuery(op.Source, qb) - if err != nil { - return err - } +func buildOrdering(op *Ordering, qb *queryBuilder) { + buildQuery(op.Source, qb) for _, order := range op.Order { qb.asSelectStatement().AddOrder(order.Inner) } - return nil } -func buildLimit(op *Limit, qb *queryBuilder) error { - err := buildQuery(op.Source, qb) - if err != nil { - return err - } +func buildLimit(op *Limit, qb *queryBuilder) { + buildQuery(op.Source, qb) qb.asSelectStatement().SetLimit(op.AST) - return nil } func buildTable(op *Table, qb *queryBuilder) { @@ -498,31 +470,22 @@ func buildTable(op *Table, qb *queryBuilder) { qb.addPredicate(pred) } for _, name := range op.Columns { - err := qb.addProjection(&sqlparser.AliasedExpr{Expr: name}) - if err != nil { - return - } + qb.addProjection(&sqlparser.AliasedExpr{Expr: name}) } } -func buildProjection(op *Projection, qb *queryBuilder) error { - err := buildQuery(op.Source, qb) - if err != nil { - return err - } +func buildProjection(op *Projection, qb *queryBuilder) { + buildQuery(op.Source, qb) _, isSel := qb.stmt.(*sqlparser.Select) if isSel { qb.clearProjections() cols, err := op.GetSelectExprs(qb.ctx) if err != nil { - return err + panic(err) } for _, column := range cols { - err := qb.addProjection(column) - if err != nil { - return err - } + qb.addProjection(column) } } @@ -539,24 +502,16 @@ func buildProjection(op *Projection, qb *queryBuilder) error { if !isSel { cols, err := op.GetSelectExprs(qb.ctx) if err != nil { - return err + panic(err) } for _, column := range cols { - err := qb.addProjection(column) - if err != nil { - return err - } + qb.addProjection(column) } } - - return nil } -func buildApplyJoin(op *ApplyJoin, qb *queryBuilder) error { - err := buildQuery(op.LHS, qb) - if err != nil { - return err - } +func buildApplyJoin(op *ApplyJoin, qb *queryBuilder) { + buildQuery(op.LHS, qb) // If we are going to add the predicate used in join here // We should not add the predicate's copy of when it was split into // two parts. To avoid this, we use the SkipPredicates map. @@ -564,24 +519,18 @@ func buildApplyJoin(op *ApplyJoin, qb *queryBuilder) error { qb.ctx.SkipPredicates[expr] = nil } qbR := &queryBuilder{ctx: qb.ctx} - err = buildQuery(op.RHS, qbR) - if err != nil { - return err - } + buildQuery(op.RHS, qbR) + if op.LeftJoin { qb.joinOuterWith(qbR, op.Predicate) } else { qb.joinInnerWith(qbR, op.Predicate) } - return nil } -func buildUnion(op *Union, qb *queryBuilder) error { +func buildUnion(op *Union, qb *queryBuilder) { // the first input is built first - err := buildQuery(op.Sources[0], qb) - if err != nil { - return err - } + buildQuery(op.Sources[0], qb) for i, src := range op.Sources { if i == 0 { @@ -590,49 +539,41 @@ func buildUnion(op *Union, qb *queryBuilder) error { // now we can go over the remaining inputs and UNION them together qbOther := &queryBuilder{ctx: qb.ctx} - err = buildQuery(src, qbOther) - if err != nil { - return err - } + buildQuery(src, qbOther) qb.unionWith(qbOther, op.distinct) } - - return nil } -func buildFilter(op *Filter, qb *queryBuilder) error { - err := buildQuery(op.Source, qb) - if err != nil { - return err - } +func buildFilter(op *Filter, qb *queryBuilder) { + buildQuery(op.Source, qb) + for _, pred := range op.Predicates { qb.addPredicate(pred) } - return nil } -func buildDerived(op *Horizon, qb *queryBuilder) error { - err := buildQuery(op.Source, qb) - if err != nil { - return err - } +func buildDerived(op *Horizon, qb *queryBuilder) { + buildQuery(op.Source, qb) + sqlparser.RemoveKeyspace(op.Query) stmt := qb.stmt qb.stmt = nil switch sel := stmt.(type) { case *sqlparser.Select: - return buildDerivedSelect(op, qb, sel) + buildDerivedSelect(op, qb, sel) + return case *sqlparser.Union: - return buildDerivedUnion(op, qb, sel) + buildDerivedUnion(op, qb, sel) + return } panic(fmt.Sprintf("unknown select statement type: %T", stmt)) } -func buildDerivedUnion(op *Horizon, qb *queryBuilder, union *sqlparser.Union) error { +func buildDerivedUnion(op *Horizon, qb *queryBuilder, union *sqlparser.Union) { opQuery, ok := op.Query.(*sqlparser.Union) if !ok { - return vterrors.VT12001("Horizon contained SELECT but statement was UNION") + panic(vterrors.VT12001("Horizon contained SELECT but statement was UNION")) } union.Limit = opQuery.Limit @@ -642,14 +583,12 @@ func buildDerivedUnion(op *Horizon, qb *queryBuilder, union *sqlparser.Union) er qb.addTableExpr(op.Alias, op.Alias, TableID(op), &sqlparser.DerivedTable{ Select: union, }, nil, op.ColumnAliases) - - return nil } -func buildDerivedSelect(op *Horizon, qb *queryBuilder, sel *sqlparser.Select) error { +func buildDerivedSelect(op *Horizon, qb *queryBuilder, sel *sqlparser.Select) { opQuery, ok := op.Query.(*sqlparser.Select) if !ok { - return vterrors.VT12001("Horizon contained UNION but statement was SELECT") + panic(vterrors.VT12001("Horizon contained UNION but statement was SELECT")) } sel.Limit = opQuery.Limit sel.OrderBy = opQuery.OrderBy @@ -660,32 +599,21 @@ func buildDerivedSelect(op *Horizon, qb *queryBuilder, sel *sqlparser.Select) er Select: sel, }, nil, op.ColumnAliases) for _, col := range op.Columns { - err := qb.addProjection(&sqlparser.AliasedExpr{Expr: col}) - if err != nil { - return err - } + qb.addProjection(&sqlparser.AliasedExpr{Expr: col}) } - return nil - } -func buildHorizon(op *Horizon, qb *queryBuilder) error { - err := buildQuery(op.Source, qb) - if err != nil { - return err - } +func buildHorizon(op *Horizon, qb *queryBuilder) { + buildQuery(op.Source, qb) + + stripDownQuery(op.Query, qb.asSelectStatement()) - err = stripDownQuery(op.Query, qb.asSelectStatement()) - if err != nil { - return err - } _ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { if aliasedExpr, ok := node.(sqlparser.SelectExpr); ok { removeKeyspaceFromSelectExpr(aliasedExpr) } return true, nil }, qb.stmt) - return nil } func mergeHaving(h1, h2 *sqlparser.Where) *sqlparser.Where { From ed2a589ad410f5a5b7c2b8696ea3cf34efc78be1 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Mon, 9 Oct 2023 12:19:24 +0200 Subject: [PATCH 04/15] refactor: panic instead of error for FindCol Signed-off-by: Andres Taylor --- .../vtgate/planbuilder/operators/aggregator.go | 17 +++++++---------- .../vtgate/planbuilder/operators/apply_join.go | 11 ++++------- go/vt/vtgate/planbuilder/operators/comments.go | 2 +- go/vt/vtgate/planbuilder/operators/distinct.go | 2 +- go/vt/vtgate/planbuilder/operators/filter.go | 2 +- go/vt/vtgate/planbuilder/operators/horizon.go | 13 +++++-------- go/vt/vtgate/planbuilder/operators/limit.go | 2 +- .../planbuilder/operators/offset_planning.go | 8 ++------ go/vt/vtgate/planbuilder/operators/operator.go | 4 ++-- go/vt/vtgate/planbuilder/operators/ops/op.go | 2 +- go/vt/vtgate/planbuilder/operators/ordering.go | 2 +- .../vtgate/planbuilder/operators/projection.go | 15 ++++++--------- go/vt/vtgate/planbuilder/operators/route.go | 7 ++----- go/vt/vtgate/planbuilder/operators/subquery.go | 2 +- .../planbuilder/operators/subquery_container.go | 2 +- go/vt/vtgate/planbuilder/operators/table.go | 8 ++++---- go/vt/vtgate/planbuilder/operators/union.go | 14 +++++--------- go/vt/vtgate/planbuilder/operators/vindex.go | 11 ++++------- 18 files changed, 49 insertions(+), 75 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/aggregator.go b/go/vt/vtgate/planbuilder/operators/aggregator.go index ad6f62d8475..345484e89ac 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -124,15 +124,15 @@ func (a *Aggregator) isDerived() bool { return a.DT != nil } -func (a *Aggregator) FindCol(ctx *plancontext.PlanningContext, in sqlparser.Expr, _ bool) (int, error) { +func (a *Aggregator) FindCol(ctx *plancontext.PlanningContext, in sqlparser.Expr, _ bool) int { expr, err := a.DT.RewriteExpression(ctx, in) if err != nil { - return 0, err + panic(err) } if offset, found := canReuseColumn(ctx, a.Columns, expr, extractExpr); found { - return offset, nil + return offset } - return -1, nil + return -1 } func (a *Aggregator) AddColumn(ctx *plancontext.PlanningContext, reuse bool, groupBy bool, ae *sqlparser.AliasedExpr) int { @@ -188,14 +188,11 @@ func (a *Aggregator) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gro func (a *Aggregator) findColInternal(ctx *plancontext.PlanningContext, ae *sqlparser.AliasedExpr, addToGroupBy bool) (int, error) { expr := ae.Expr - offset, err := a.FindCol(ctx, expr, false) - if err != nil { - return 0, err - } + offset := a.FindCol(ctx, expr, false) if offset >= 0 { - return offset, err + return offset, nil } - expr, err = a.DT.RewriteExpression(ctx, expr) + expr, err := a.DT.RewriteExpression(ctx, expr) if err != nil { return 0, err } diff --git a/go/vt/vtgate/planbuilder/operators/apply_join.go b/go/vt/vtgate/planbuilder/operators/apply_join.go index caf4aa65bfb..bf8ee70d5f1 100644 --- a/go/vt/vtgate/planbuilder/operators/apply_join.go +++ b/go/vt/vtgate/planbuilder/operators/apply_join.go @@ -214,12 +214,12 @@ func (aj *ApplyJoin) getJoinColumnFor(ctx *plancontext.PlanningContext, orig *sq return } -func (aj *ApplyJoin) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, _ bool) (int, error) { +func (aj *ApplyJoin) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, _ bool) int { offset, found := canReuseColumn(ctx, aj.JoinColumns, expr, joinColumnToExpr) if !found { - return -1, nil + return -1 } - return offset, nil + return offset } func (aj *ApplyJoin) AddColumn( @@ -229,10 +229,7 @@ func (aj *ApplyJoin) AddColumn( expr *sqlparser.AliasedExpr, ) int { if reuse { - offset, err := aj.FindCol(ctx, expr.Expr, false) - if err != nil { - panic(err) - } + offset := aj.FindCol(ctx, expr.Expr, false) if offset != -1 { return offset } diff --git a/go/vt/vtgate/planbuilder/operators/comments.go b/go/vt/vtgate/planbuilder/operators/comments.go index d8dc5a93fa8..696ae066972 100644 --- a/go/vt/vtgate/planbuilder/operators/comments.go +++ b/go/vt/vtgate/planbuilder/operators/comments.go @@ -55,7 +55,7 @@ func (l *LockAndComment) AddColumn(ctx *plancontext.PlanningContext, reuseExisti return l.Source.AddColumn(ctx, reuseExisting, addToGroupBy, expr) } -func (l *LockAndComment) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { +func (l *LockAndComment) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { return l.Source.FindCol(ctx, expr, underRoute) } diff --git a/go/vt/vtgate/planbuilder/operators/distinct.go b/go/vt/vtgate/planbuilder/operators/distinct.go index 3ce832667c9..f99fb2dc3ef 100644 --- a/go/vt/vtgate/planbuilder/operators/distinct.go +++ b/go/vt/vtgate/planbuilder/operators/distinct.go @@ -99,7 +99,7 @@ func (d *Distinct) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bo return d.Source.AddColumn(ctx, reuse, gb, expr) } -func (d *Distinct) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { +func (d *Distinct) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { return d.Source.FindCol(ctx, expr, underRoute) } diff --git a/go/vt/vtgate/planbuilder/operators/filter.go b/go/vt/vtgate/planbuilder/operators/filter.go index 858877108f5..5277a2a7de6 100644 --- a/go/vt/vtgate/planbuilder/operators/filter.go +++ b/go/vt/vtgate/planbuilder/operators/filter.go @@ -89,7 +89,7 @@ func (f *Filter) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool return f.Source.AddColumn(ctx, reuse, gb, expr) } -func (f *Filter) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { +func (f *Filter) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { return f.Source.FindCol(ctx, expr, underRoute) } diff --git a/go/vt/vtgate/planbuilder/operators/horizon.go b/go/vt/vtgate/planbuilder/operators/horizon.go index 1587b96afc4..76fb47ce8ed 100644 --- a/go/vt/vtgate/planbuilder/operators/horizon.go +++ b/go/vt/vtgate/planbuilder/operators/horizon.go @@ -118,10 +118,7 @@ func (h *Horizon) AddColumn(ctx *plancontext.PlanningContext, reuse bool, _ bool if !ok { panic(vterrors.VT13001("cannot push non-ColName expression to horizon")) } - offset, err := h.FindCol(ctx, col, false) - if err != nil { - panic(err) - } + offset := h.FindCol(ctx, col, false) if offset < 0 { panic(errNoNewColumns) } @@ -147,18 +144,18 @@ func canReuseColumn[T any]( return } -func (h *Horizon) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, _ bool) (int, error) { +func (h *Horizon) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, _ bool) int { for idx, se := range sqlparser.GetFirstSelect(h.Query).SelectExprs { ae, ok := se.(*sqlparser.AliasedExpr) if !ok { - return 0, vterrors.VT09015() + panic(vterrors.VT09015()) } if ctx.SemTable.EqualsExprWithDeps(ae.Expr, expr) { - return idx, nil + return idx } } - return -1, nil + return -1 } func (h *Horizon) GetColumns(ctx *plancontext.PlanningContext) (exprs []*sqlparser.AliasedExpr, err error) { diff --git a/go/vt/vtgate/planbuilder/operators/limit.go b/go/vt/vtgate/planbuilder/operators/limit.go index 221e3e4789f..d18de526849 100644 --- a/go/vt/vtgate/planbuilder/operators/limit.go +++ b/go/vt/vtgate/planbuilder/operators/limit.go @@ -56,7 +56,7 @@ func (l *Limit) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, return l.Source.AddColumn(ctx, reuse, gb, expr) } -func (l *Limit) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { +func (l *Limit) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { return l.Source.FindCol(ctx, expr, underRoute) } diff --git a/go/vt/vtgate/planbuilder/operators/offset_planning.go b/go/vt/vtgate/planbuilder/operators/offset_planning.go index d5d57dc69fc..6e0a3ba36c8 100644 --- a/go/vt/vtgate/planbuilder/operators/offset_planning.go +++ b/go/vt/vtgate/planbuilder/operators/offset_planning.go @@ -152,7 +152,7 @@ func pullDistinctFromUNION(_ *plancontext.PlanningContext, root ops.Operator) (o func getOffsetRewritingVisitor( ctx *plancontext.PlanningContext, // this is the function that will be called to try to find the offset for an expression - findCol func(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error), + findCol func(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int, // this function will be called when an expression has been found on the input found func(sqlparser.Expr, int), // if we have an expression that mush be fetched, this method will be called @@ -167,11 +167,7 @@ func getOffsetRewritingVisitor( if !ok { return true } - var offset int - offset, err = findCol(ctx, e, false) - if err != nil { - return false - } + offset := findCol(ctx, e, false) if offset >= 0 { found(e, offset) return false diff --git a/go/vt/vtgate/planbuilder/operators/operator.go b/go/vt/vtgate/planbuilder/operators/operator.go index e44ea0195fa..e7e8de88982 100644 --- a/go/vt/vtgate/planbuilder/operators/operator.go +++ b/go/vt/vtgate/planbuilder/operators/operator.go @@ -124,8 +124,8 @@ func (noColumns) GetColumns(*plancontext.PlanningContext) ([]*sqlparser.AliasedE return nil, vterrors.VT13001("noColumns operators have no column") } -func (noColumns) FindCol(*plancontext.PlanningContext, sqlparser.Expr, bool) (int, error) { - return 0, vterrors.VT13001("noColumns operators have no column") +func (noColumns) FindCol(*plancontext.PlanningContext, sqlparser.Expr, bool) int { + panic(vterrors.VT13001("noColumns operators have no column")) } func (noColumns) GetSelectExprs(*plancontext.PlanningContext) (sqlparser.SelectExprs, error) { diff --git a/go/vt/vtgate/planbuilder/operators/ops/op.go b/go/vt/vtgate/planbuilder/operators/ops/op.go index d3f89fa6e42..0d4a7e17510 100644 --- a/go/vt/vtgate/planbuilder/operators/ops/op.go +++ b/go/vt/vtgate/planbuilder/operators/ops/op.go @@ -46,7 +46,7 @@ type ( AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, expr *sqlparser.AliasedExpr) int - FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) + FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) diff --git a/go/vt/vtgate/planbuilder/operators/ordering.go b/go/vt/vtgate/planbuilder/operators/ordering.go index 3130ba98e89..2fc8e06f1ed 100644 --- a/go/vt/vtgate/planbuilder/operators/ordering.go +++ b/go/vt/vtgate/planbuilder/operators/ordering.go @@ -62,7 +62,7 @@ func (o *Ordering) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bo return o.Source.AddColumn(ctx, reuse, gb, expr) } -func (o *Ordering) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { +func (o *Ordering) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { return o.Source.FindCol(ctx, expr, underRoute) } diff --git a/go/vt/vtgate/planbuilder/operators/projection.go b/go/vt/vtgate/planbuilder/operators/projection.go index b84fd41a978..3acc728104a 100644 --- a/go/vt/vtgate/planbuilder/operators/projection.go +++ b/go/vt/vtgate/planbuilder/operators/projection.go @@ -244,23 +244,23 @@ func (p *Projection) isDerived() bool { return p.DT != nil } -func (p *Projection) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { +func (p *Projection) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { ap, err := p.GetAliasedProjections() if err != nil { - return 0, err + panic(err) } if underRoute && p.isDerived() { - return -1, nil + return -1 } for offset, pe := range ap { if ctx.SemTable.EqualsExprWithDeps(pe.ColExpr, expr) { - return offset, nil + return offset } } - return -1, nil + return -1 } func (p *Projection) addProjExpr(pe *ProjExpr) (int, error) { @@ -325,10 +325,7 @@ func (p *Projection) addColumn( } if reuse { - offset, err := p.FindCol(ctx, expr, false) - if err != nil { - return 0, err - } + offset := p.FindCol(ctx, expr, false) if offset >= 0 { return offset, nil } diff --git a/go/vt/vtgate/planbuilder/operators/route.go b/go/vt/vtgate/planbuilder/operators/route.go index b6d7135080f..ac5027c4076 100644 --- a/go/vt/vtgate/planbuilder/operators/route.go +++ b/go/vt/vtgate/planbuilder/operators/route.go @@ -550,10 +550,7 @@ func (r *Route) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, removeKeyspaceFromSelectExpr(expr) if reuse { - offset, err := r.FindCol(ctx, expr.Expr, true) - if err != nil { - panic(err) - } + offset := r.FindCol(ctx, expr.Expr, true) if offset != -1 { return offset } @@ -655,7 +652,7 @@ func addMultipleColumnsToInput(ctx *plancontext.PlanningContext, operator ops.Op } } -func (r *Route) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, _ bool) (int, error) { +func (r *Route) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, _ bool) int { return r.Source.FindCol(ctx, expr, true) } diff --git a/go/vt/vtgate/planbuilder/operators/subquery.go b/go/vt/vtgate/planbuilder/operators/subquery.go index 076e18ac561..f0dcfb15a1f 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery.go +++ b/go/vt/vtgate/planbuilder/operators/subquery.go @@ -177,7 +177,7 @@ func (sq *SubQuery) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bo return sq.Outer.AddColumn(ctx, reuseExisting, addToGroupBy, exprs) } -func (sq *SubQuery) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { +func (sq *SubQuery) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { return sq.Outer.FindCol(ctx, expr, underRoute) } diff --git a/go/vt/vtgate/planbuilder/operators/subquery_container.go b/go/vt/vtgate/planbuilder/operators/subquery_container.go index 1eed3bed746..e3e43b6330a 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery_container.go +++ b/go/vt/vtgate/planbuilder/operators/subquery_container.go @@ -80,7 +80,7 @@ func (sqc *SubQueryContainer) AddColumn(ctx *plancontext.PlanningContext, reuseE return sqc.Outer.AddColumn(ctx, reuseExisting, addToGroupBy, exprs) } -func (sqc *SubQueryContainer) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { +func (sqc *SubQueryContainer) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { return sqc.Outer.FindCol(ctx, expr, underRoute) } diff --git a/go/vt/vtgate/planbuilder/operators/table.go b/go/vt/vtgate/planbuilder/operators/table.go index 2fba8a363c0..a219e1f6cb1 100644 --- a/go/vt/vtgate/planbuilder/operators/table.go +++ b/go/vt/vtgate/planbuilder/operators/table.go @@ -69,19 +69,19 @@ func (to *Table) AddColumn(*plancontext.PlanningContext, bool, bool, *sqlparser. panic(vterrors.VT13001("did not expect this method to be called")) } -func (to *Table) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { +func (to *Table) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { colToFind, ok := expr.(*sqlparser.ColName) if !ok { - return -1, nil + return -1 } for idx, colName := range to.Columns { if colName.Name.Equal(colToFind.Name) { - return idx, nil + return idx } } - return -1, nil + return -1 } func (to *Table) GetColumns(*plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { diff --git a/go/vt/vtgate/planbuilder/operators/union.go b/go/vt/vtgate/planbuilder/operators/union.go index e51084e9b6e..406e3c04725 100644 --- a/go/vt/vtgate/planbuilder/operators/union.go +++ b/go/vt/vtgate/planbuilder/operators/union.go @@ -182,11 +182,7 @@ func (u *Union) GetSelectFor(source int) (*sqlparser.Select, error) { func (u *Union) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { if reuse { - offset, err := u.FindCol(ctx, expr.Expr, false) - if err != nil { - panic(err) - } - + offset := u.FindCol(ctx, expr.Expr, false) if offset >= 0 { return offset } @@ -249,19 +245,19 @@ func (u *Union) addWeightStringToOffset(ctx *plancontext.PlanningContext, argIdx return } -func (u *Union) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { +func (u *Union) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { columns, err := u.GetColumns(ctx) if err != nil { - return 0, err + panic(err) } for idx, col := range columns { if ctx.SemTable.EqualsExprWithDeps(expr, col.Expr) { - return idx, nil + return idx } } - return -1, nil + return -1 } func (u *Union) GetColumns(ctx *plancontext.PlanningContext) (result []*sqlparser.AliasedExpr, err error) { diff --git a/go/vt/vtgate/planbuilder/operators/vindex.go b/go/vt/vtgate/planbuilder/operators/vindex.go index fc92187589c..29c167137f6 100644 --- a/go/vt/vtgate/planbuilder/operators/vindex.go +++ b/go/vt/vtgate/planbuilder/operators/vindex.go @@ -67,10 +67,7 @@ func (v *Vindex) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool panic(vterrors.VT13001("tried to add group by to a table")) } if reuse { - offset, err := v.FindCol(ctx, ae.Expr, true) - if err != nil { - panic(err) - } + offset := v.FindCol(ctx, ae.Expr, true) if offset > -1 { return offset } @@ -86,14 +83,14 @@ func colNameToExpr(c *sqlparser.ColName) *sqlparser.AliasedExpr { } } -func (v *Vindex) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { +func (v *Vindex) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { for idx, col := range v.Columns { if ctx.SemTable.EqualsExprWithDeps(expr, col) { - return idx, nil + return idx } } - return -1, nil + return -1 } func (v *Vindex) GetColumns(*plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { From eca1841973ca61a941244e8782905eff4285d1b2 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Mon, 9 Oct 2023 12:24:22 +0200 Subject: [PATCH 05/15] refactor: panic instead of error for GetOrdering Signed-off-by: Andres Taylor --- go/vt/vtgate/planbuilder/operators/aggregator.go | 2 +- go/vt/vtgate/planbuilder/operators/apply_join.go | 2 +- go/vt/vtgate/planbuilder/operators/comments.go | 2 +- go/vt/vtgate/planbuilder/operators/delete.go | 4 ++-- go/vt/vtgate/planbuilder/operators/distinct.go | 2 +- go/vt/vtgate/planbuilder/operators/filter.go | 2 +- go/vt/vtgate/planbuilder/operators/fk_cascade.go | 4 ++-- go/vt/vtgate/planbuilder/operators/fk_verify.go | 4 ++-- go/vt/vtgate/planbuilder/operators/horizon.go | 6 +++--- go/vt/vtgate/planbuilder/operators/insert.go | 4 ++-- go/vt/vtgate/planbuilder/operators/join.go | 4 ++-- go/vt/vtgate/planbuilder/operators/limit.go | 2 +- go/vt/vtgate/planbuilder/operators/ops/op.go | 2 +- go/vt/vtgate/planbuilder/operators/ordering.go | 4 ++-- go/vt/vtgate/planbuilder/operators/phases.go | 5 +---- go/vt/vtgate/planbuilder/operators/projection.go | 2 +- go/vt/vtgate/planbuilder/operators/querygraph.go | 4 ++-- go/vt/vtgate/planbuilder/operators/route.go | 14 +++++--------- go/vt/vtgate/planbuilder/operators/subquery.go | 2 +- .../planbuilder/operators/subquery_container.go | 2 +- go/vt/vtgate/planbuilder/operators/table.go | 4 ++-- go/vt/vtgate/planbuilder/operators/union.go | 4 ++-- go/vt/vtgate/planbuilder/operators/update.go | 4 ++-- go/vt/vtgate/planbuilder/operators/vindex.go | 4 ++-- 24 files changed, 41 insertions(+), 48 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/aggregator.go b/go/vt/vtgate/planbuilder/operators/aggregator.go index 345484e89ac..0d52c2274e9 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -271,7 +271,7 @@ func (a *Aggregator) ShortDescription() string { return fmt.Sprintf("%s%s group by %s", org, strings.Join(columns, ", "), strings.Join(grouping, ",")) } -func (a *Aggregator) GetOrdering() ([]ops.OrderBy, error) { +func (a *Aggregator) GetOrdering() []ops.OrderBy { return a.Source.GetOrdering() } diff --git a/go/vt/vtgate/planbuilder/operators/apply_join.go b/go/vt/vtgate/planbuilder/operators/apply_join.go index bf8ee70d5f1..a33af34f401 100644 --- a/go/vt/vtgate/planbuilder/operators/apply_join.go +++ b/go/vt/vtgate/planbuilder/operators/apply_join.go @@ -175,7 +175,7 @@ func (aj *ApplyJoin) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser return transformColumnsToSelectExprs(ctx, aj) } -func (aj *ApplyJoin) GetOrdering() ([]ops.OrderBy, error) { +func (aj *ApplyJoin) GetOrdering() []ops.OrderBy { return aj.LHS.GetOrdering() } diff --git a/go/vt/vtgate/planbuilder/operators/comments.go b/go/vt/vtgate/planbuilder/operators/comments.go index 696ae066972..1bce49e1670 100644 --- a/go/vt/vtgate/planbuilder/operators/comments.go +++ b/go/vt/vtgate/planbuilder/operators/comments.go @@ -76,6 +76,6 @@ func (l *LockAndComment) ShortDescription() string { return strings.Join(s, " ") } -func (l *LockAndComment) GetOrdering() ([]ops.OrderBy, error) { +func (l *LockAndComment) GetOrdering() []ops.OrderBy { return l.Source.GetOrdering() } diff --git a/go/vt/vtgate/planbuilder/operators/delete.go b/go/vt/vtgate/planbuilder/operators/delete.go index 3d4d5e40357..2bf5a65a893 100644 --- a/go/vt/vtgate/planbuilder/operators/delete.go +++ b/go/vt/vtgate/planbuilder/operators/delete.go @@ -62,8 +62,8 @@ func (d *Delete) TablesUsed() []string { return nil } -func (d *Delete) GetOrdering() ([]ops.OrderBy, error) { - return nil, nil +func (d *Delete) GetOrdering() []ops.OrderBy { + return nil } func (d *Delete) ShortDescription() string { diff --git a/go/vt/vtgate/planbuilder/operators/distinct.go b/go/vt/vtgate/planbuilder/operators/distinct.go index f99fb2dc3ef..7fe48db4399 100644 --- a/go/vt/vtgate/planbuilder/operators/distinct.go +++ b/go/vt/vtgate/planbuilder/operators/distinct.go @@ -118,7 +118,7 @@ func (d *Distinct) ShortDescription() string { return "Performance" } -func (d *Distinct) GetOrdering() ([]ops.OrderBy, error) { +func (d *Distinct) GetOrdering() []ops.OrderBy { return d.Source.GetOrdering() } diff --git a/go/vt/vtgate/planbuilder/operators/filter.go b/go/vt/vtgate/planbuilder/operators/filter.go index 5277a2a7de6..6b4692aa89c 100644 --- a/go/vt/vtgate/planbuilder/operators/filter.go +++ b/go/vt/vtgate/planbuilder/operators/filter.go @@ -101,7 +101,7 @@ func (f *Filter) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.Sel return f.Source.GetSelectExprs(ctx) } -func (f *Filter) GetOrdering() ([]ops.OrderBy, error) { +func (f *Filter) GetOrdering() []ops.OrderBy { return f.Source.GetOrdering() } diff --git a/go/vt/vtgate/planbuilder/operators/fk_cascade.go b/go/vt/vtgate/planbuilder/operators/fk_cascade.go index a9afbde0a7c..e37280b0d08 100644 --- a/go/vt/vtgate/planbuilder/operators/fk_cascade.go +++ b/go/vt/vtgate/planbuilder/operators/fk_cascade.go @@ -96,8 +96,8 @@ func (fkc *FkCascade) Clone(inputs []ops.Operator) ops.Operator { } // GetOrdering implements the Operator interface -func (fkc *FkCascade) GetOrdering() ([]ops.OrderBy, error) { - return nil, nil +func (fkc *FkCascade) GetOrdering() []ops.OrderBy { + return nil } // ShortDescription implements the Operator interface diff --git a/go/vt/vtgate/planbuilder/operators/fk_verify.go b/go/vt/vtgate/planbuilder/operators/fk_verify.go index 8c2431d26fc..1bac01ca7d0 100644 --- a/go/vt/vtgate/planbuilder/operators/fk_verify.go +++ b/go/vt/vtgate/planbuilder/operators/fk_verify.go @@ -70,8 +70,8 @@ func (fkv *FkVerify) Clone(inputs []ops.Operator) ops.Operator { } // GetOrdering implements the Operator interface -func (fkv *FkVerify) GetOrdering() ([]ops.OrderBy, error) { - return nil, nil +func (fkv *FkVerify) GetOrdering() []ops.OrderBy { + return nil } // ShortDescription implements the Operator interface diff --git a/go/vt/vtgate/planbuilder/operators/horizon.go b/go/vt/vtgate/planbuilder/operators/horizon.go index 76fb47ce8ed..7deb5b1408d 100644 --- a/go/vt/vtgate/planbuilder/operators/horizon.go +++ b/go/vt/vtgate/planbuilder/operators/horizon.go @@ -174,11 +174,11 @@ func (h *Horizon) GetSelectExprs(*plancontext.PlanningContext) (sqlparser.Select return sqlparser.GetFirstSelect(h.Query).SelectExprs, nil } -func (h *Horizon) GetOrdering() ([]ops.OrderBy, error) { +func (h *Horizon) GetOrdering() []ops.OrderBy { if h.QP == nil { - return nil, vterrors.VT13001("QP should already be here") + panic(vterrors.VT13001("QP should already be here")) } - return h.QP.OrderExprs, nil + return h.QP.OrderExprs } // TODO: REMOVE diff --git a/go/vt/vtgate/planbuilder/operators/insert.go b/go/vt/vtgate/planbuilder/operators/insert.go index 37f1d439bbd..3afeb79d88a 100644 --- a/go/vt/vtgate/planbuilder/operators/insert.go +++ b/go/vt/vtgate/planbuilder/operators/insert.go @@ -96,8 +96,8 @@ func (i *Insert) ShortDescription() string { return i.VTable.String() } -func (i *Insert) GetOrdering() ([]ops.OrderBy, error) { - return nil, nil +func (i *Insert) GetOrdering() []ops.OrderBy { + return nil } var _ ops.Operator = (*Insert)(nil) diff --git a/go/vt/vtgate/planbuilder/operators/join.go b/go/vt/vtgate/planbuilder/operators/join.go index dc5d4153d54..5574e859953 100644 --- a/go/vt/vtgate/planbuilder/operators/join.go +++ b/go/vt/vtgate/planbuilder/operators/join.go @@ -48,8 +48,8 @@ func (j *Join) Clone(inputs []ops.Operator) ops.Operator { } } -func (j *Join) GetOrdering() ([]ops.OrderBy, error) { - return nil, nil +func (j *Join) GetOrdering() []ops.OrderBy { + return nil } // Inputs implements the Operator interface diff --git a/go/vt/vtgate/planbuilder/operators/limit.go b/go/vt/vtgate/planbuilder/operators/limit.go index d18de526849..8fb40cd0bd6 100644 --- a/go/vt/vtgate/planbuilder/operators/limit.go +++ b/go/vt/vtgate/planbuilder/operators/limit.go @@ -68,7 +68,7 @@ func (l *Limit) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.Sele return l.Source.GetSelectExprs(ctx) } -func (l *Limit) GetOrdering() ([]ops.OrderBy, error) { +func (l *Limit) GetOrdering() []ops.OrderBy { return l.Source.GetOrdering() } diff --git a/go/vt/vtgate/planbuilder/operators/ops/op.go b/go/vt/vtgate/planbuilder/operators/ops/op.go index 0d4a7e17510..189550bc958 100644 --- a/go/vt/vtgate/planbuilder/operators/ops/op.go +++ b/go/vt/vtgate/planbuilder/operators/ops/op.go @@ -53,7 +53,7 @@ type ( ShortDescription() string - GetOrdering() ([]OrderBy, error) + GetOrdering() []OrderBy } // OrderBy contains the expression to used in order by and also if ordering is needed at VTGate level then what the weight_string function expression to be sent down for evaluation. diff --git a/go/vt/vtgate/planbuilder/operators/ordering.go b/go/vt/vtgate/planbuilder/operators/ordering.go index 2fc8e06f1ed..605c296678a 100644 --- a/go/vt/vtgate/planbuilder/operators/ordering.go +++ b/go/vt/vtgate/planbuilder/operators/ordering.go @@ -74,8 +74,8 @@ func (o *Ordering) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.S return o.Source.GetSelectExprs(ctx) } -func (o *Ordering) GetOrdering() ([]ops.OrderBy, error) { - return o.Order, nil +func (o *Ordering) GetOrdering() []ops.OrderBy { + return o.Order } func (o *Ordering) planOffsets(ctx *plancontext.PlanningContext) error { diff --git a/go/vt/vtgate/planbuilder/operators/phases.go b/go/vt/vtgate/planbuilder/operators/phases.go index 1eecc595c8f..d5c02490fce 100644 --- a/go/vt/vtgate/planbuilder/operators/phases.go +++ b/go/vt/vtgate/planbuilder/operators/phases.go @@ -165,10 +165,7 @@ func needsOrdering(ctx *plancontext.PlanningContext, in *Aggregator) (bool, erro if len(requiredOrder) == 0 { return false, nil } - srcOrdering, err := in.Source.GetOrdering() - if err != nil { - return false, err - } + srcOrdering := in.Source.GetOrdering() if len(srcOrdering) < len(requiredOrder) { return true, nil } diff --git a/go/vt/vtgate/planbuilder/operators/projection.go b/go/vt/vtgate/planbuilder/operators/projection.go index 3acc728104a..6ba45ea88f4 100644 --- a/go/vt/vtgate/planbuilder/operators/projection.go +++ b/go/vt/vtgate/planbuilder/operators/projection.go @@ -411,7 +411,7 @@ func (p *Projection) GetSelectExprs(*plancontext.PlanningContext) (sqlparser.Sel } } -func (p *Projection) GetOrdering() ([]ops.OrderBy, error) { +func (p *Projection) GetOrdering() []ops.OrderBy { return p.Source.GetOrdering() } diff --git a/go/vt/vtgate/planbuilder/operators/querygraph.go b/go/vt/vtgate/planbuilder/operators/querygraph.go index 7195f22567a..c30bfb8fd21 100644 --- a/go/vt/vtgate/planbuilder/operators/querygraph.go +++ b/go/vt/vtgate/planbuilder/operators/querygraph.go @@ -176,8 +176,8 @@ func (qg *QueryGraph) Clone([]ops.Operator) ops.Operator { return result } -func (qg *QueryGraph) GetOrdering() ([]ops.OrderBy, error) { - return nil, nil +func (qg *QueryGraph) GetOrdering() []ops.OrderBy { + return nil } func (qg *QueryGraph) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { diff --git a/go/vt/vtgate/planbuilder/operators/route.go b/go/vt/vtgate/planbuilder/operators/route.go index ac5027c4076..b6f70764562 100644 --- a/go/vt/vtgate/planbuilder/operators/route.go +++ b/go/vt/vtgate/planbuilder/operators/route.go @@ -664,7 +664,7 @@ func (r *Route) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.Sele return r.Source.GetSelectExprs(ctx) } -func (r *Route) GetOrdering() ([]ops.OrderBy, error) { +func (r *Route) GetOrdering() []ops.OrderBy { return r.Source.GetOrdering() } @@ -696,9 +696,9 @@ func (r *Route) planOffsets(ctx *plancontext.PlanningContext) (err error) { // if we are getting results from multiple shards, we need to do a merge-sort // between them to get the final output correctly sorted - ordering, err := r.Source.GetOrdering() - if err != nil || len(ordering) == 0 { - return err + ordering := r.Source.GetOrdering() + if len(ordering) == 0 { + return nil } for _, order := range ordering { @@ -743,11 +743,7 @@ func (r *Route) ShortDescription() string { first += " " + info.extraInfo() } - orderBy, err := r.Source.GetOrdering() - if err != nil { - return first - } - + orderBy := r.Source.GetOrdering() ordering := "" if len(orderBy) > 0 { var oo []string diff --git a/go/vt/vtgate/planbuilder/operators/subquery.go b/go/vt/vtgate/planbuilder/operators/subquery.go index f0dcfb15a1f..cbe03d563c1 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery.go +++ b/go/vt/vtgate/planbuilder/operators/subquery.go @@ -126,7 +126,7 @@ func (sq *SubQuery) Clone(inputs []ops.Operator) ops.Operator { return &klone } -func (sq *SubQuery) GetOrdering() ([]ops.OrderBy, error) { +func (sq *SubQuery) GetOrdering() []ops.OrderBy { return sq.Outer.GetOrdering() } diff --git a/go/vt/vtgate/planbuilder/operators/subquery_container.go b/go/vt/vtgate/planbuilder/operators/subquery_container.go index e3e43b6330a..a8a195b15b2 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery_container.go +++ b/go/vt/vtgate/planbuilder/operators/subquery_container.go @@ -49,7 +49,7 @@ func (sqc *SubQueryContainer) Clone(inputs []ops.Operator) ops.Operator { return result } -func (sqc *SubQueryContainer) GetOrdering() ([]ops.OrderBy, error) { +func (sqc *SubQueryContainer) GetOrdering() []ops.OrderBy { return sqc.Outer.GetOrdering() } diff --git a/go/vt/vtgate/planbuilder/operators/table.go b/go/vt/vtgate/planbuilder/operators/table.go index a219e1f6cb1..93325d0ddea 100644 --- a/go/vt/vtgate/planbuilder/operators/table.go +++ b/go/vt/vtgate/planbuilder/operators/table.go @@ -92,8 +92,8 @@ func (to *Table) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.Sel return transformColumnsToSelectExprs(ctx, to) } -func (to *Table) GetOrdering() ([]ops.OrderBy, error) { - return nil, nil +func (to *Table) GetOrdering() []ops.OrderBy { + return nil } func (to *Table) GetColNames() []*sqlparser.ColName { diff --git a/go/vt/vtgate/planbuilder/operators/union.go b/go/vt/vtgate/planbuilder/operators/union.go index 406e3c04725..59e362931e6 100644 --- a/go/vt/vtgate/planbuilder/operators/union.go +++ b/go/vt/vtgate/planbuilder/operators/union.go @@ -58,8 +58,8 @@ func (u *Union) Clone(inputs []ops.Operator) ops.Operator { return &newOp } -func (u *Union) GetOrdering() ([]ops.OrderBy, error) { - return nil, nil +func (u *Union) GetOrdering() []ops.OrderBy { + return nil } // Inputs implements the Operator interface diff --git a/go/vt/vtgate/planbuilder/operators/update.go b/go/vt/vtgate/planbuilder/operators/update.go index c20ce9fa020..3f049fd6257 100644 --- a/go/vt/vtgate/planbuilder/operators/update.go +++ b/go/vt/vtgate/planbuilder/operators/update.go @@ -76,8 +76,8 @@ func (u *Update) Clone([]ops.Operator) ops.Operator { return &upd } -func (u *Update) GetOrdering() ([]ops.OrderBy, error) { - return nil, nil +func (u *Update) GetOrdering() []ops.OrderBy { + return nil } func (u *Update) TablesUsed() []string { diff --git a/go/vt/vtgate/planbuilder/operators/vindex.go b/go/vt/vtgate/planbuilder/operators/vindex.go index 29c167137f6..76f7348b508 100644 --- a/go/vt/vtgate/planbuilder/operators/vindex.go +++ b/go/vt/vtgate/planbuilder/operators/vindex.go @@ -101,8 +101,8 @@ func (v *Vindex) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.Sel return transformColumnsToSelectExprs(ctx, v) } -func (v *Vindex) GetOrdering() ([]ops.OrderBy, error) { - return nil, nil +func (v *Vindex) GetOrdering() []ops.OrderBy { + return nil } func (v *Vindex) GetColNames() []*sqlparser.ColName { From 42074b342a88237d99113daea741f6a2c272ab4b Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Mon, 9 Oct 2023 12:33:38 +0200 Subject: [PATCH 06/15] refactor: panic instead of error for GetColumns Signed-off-by: Andres Taylor --- .../planbuilder/operator_transformers.go | 5 +---- .../planbuilder/operators/SQL_builder.go | 5 +---- .../planbuilder/operators/aggregator.go | 11 ++++------ .../planbuilder/operators/apply_join.go | 4 ++-- .../vtgate/planbuilder/operators/comments.go | 2 +- .../vtgate/planbuilder/operators/distinct.go | 7 ++---- go/vt/vtgate/planbuilder/operators/filter.go | 2 +- go/vt/vtgate/planbuilder/operators/horizon.go | 9 ++++---- go/vt/vtgate/planbuilder/operators/limit.go | 2 +- .../vtgate/planbuilder/operators/operator.go | 9 +++----- go/vt/vtgate/planbuilder/operators/ops/op.go | 2 +- .../vtgate/planbuilder/operators/ordering.go | 2 +- .../planbuilder/operators/projection.go | 14 ++++++------ go/vt/vtgate/planbuilder/operators/route.go | 12 +++------- .../vtgate/planbuilder/operators/subquery.go | 2 +- .../operators/subquery_container.go | 2 +- go/vt/vtgate/planbuilder/operators/table.go | 4 ++-- go/vt/vtgate/planbuilder/operators/union.go | 22 +++++-------------- go/vt/vtgate/planbuilder/operators/vindex.go | 4 ++-- 19 files changed, 45 insertions(+), 75 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operator_transformers.go b/go/vt/vtgate/planbuilder/operator_transformers.go index 6140b1b9b5d..8104a20bc6c 100644 --- a/go/vt/vtgate/planbuilder/operator_transformers.go +++ b/go/vt/vtgate/planbuilder/operator_transformers.go @@ -303,10 +303,7 @@ func getEvalEngingeExpr(ctx *plancontext.PlanningContext, pe *operators.ProjExpr // useSimpleProjection uses nothing at all if the output is already correct, // or SimpleProjection when we have to reorder or truncate the columns func useSimpleProjection(ctx *plancontext.PlanningContext, op *operators.Projection, cols []int, src logicalPlan) (logicalPlan, error) { - columns, err := op.Source.GetColumns(ctx) - if err != nil { - return nil, err - } + columns := op.Source.GetColumns(ctx) if len(columns) == len(cols) && elementsMatchIndices(cols) { // the columns are already in the right order. we don't need anything at all here return src, nil diff --git a/go/vt/vtgate/planbuilder/operators/SQL_builder.go b/go/vt/vtgate/planbuilder/operators/SQL_builder.go index eff070aa6d2..0a458a5b191 100644 --- a/go/vt/vtgate/planbuilder/operators/SQL_builder.go +++ b/go/vt/vtgate/planbuilder/operators/SQL_builder.go @@ -429,10 +429,7 @@ func buildAggregation(op *Aggregator, qb *queryBuilder) { qb.clearProjections() - cols, err := op.GetColumns(qb.ctx) - if err != nil { - panic(err) - } + cols := op.GetColumns(qb.ctx) for _, column := range cols { qb.addProjection(column) } diff --git a/go/vt/vtgate/planbuilder/operators/aggregator.go b/go/vt/vtgate/planbuilder/operators/aggregator.go index 0d52c2274e9..e434a4a895f 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -221,25 +221,22 @@ func (a *Aggregator) findColInternal(ctx *plancontext.PlanningContext, ae *sqlpa return -1, nil } -func (a *Aggregator) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (a *Aggregator) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { if _, isSourceDerived := a.Source.(*Horizon); isSourceDerived { - return a.Columns, nil + return a.Columns } // we update the incoming columns, so we know about any new columns that have been added // in the optimization phase, other operators could be pushed down resulting in additional columns for aggregator. // Aggregator should be made aware of these to truncate them in final result. - columns, err := a.Source.GetColumns(ctx) - if err != nil { - return nil, err - } + columns := a.Source.GetColumns(ctx) // if this operator is producing more columns than expected, we want to know about it if len(columns) > len(a.Columns) { a.Columns = append(a.Columns, columns[len(a.Columns):]...) } - return a.Columns, nil + return a.Columns } func (a *Aggregator) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { diff --git a/go/vt/vtgate/planbuilder/operators/apply_join.go b/go/vt/vtgate/planbuilder/operators/apply_join.go index a33af34f401..706039cfd8a 100644 --- a/go/vt/vtgate/planbuilder/operators/apply_join.go +++ b/go/vt/vtgate/planbuilder/operators/apply_join.go @@ -167,8 +167,8 @@ func (aj *ApplyJoin) pushColRight(ctx *plancontext.PlanningContext, e *sqlparser return offset, nil } -func (aj *ApplyJoin) GetColumns(*plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { - return slice.Map(aj.JoinColumns, joinColumnToAliasedExpr), nil +func (aj *ApplyJoin) GetColumns(*plancontext.PlanningContext) []*sqlparser.AliasedExpr { + return slice.Map(aj.JoinColumns, joinColumnToAliasedExpr) } func (aj *ApplyJoin) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { diff --git a/go/vt/vtgate/planbuilder/operators/comments.go b/go/vt/vtgate/planbuilder/operators/comments.go index 1bce49e1670..5db439507a1 100644 --- a/go/vt/vtgate/planbuilder/operators/comments.go +++ b/go/vt/vtgate/planbuilder/operators/comments.go @@ -59,7 +59,7 @@ func (l *LockAndComment) FindCol(ctx *plancontext.PlanningContext, expr sqlparse return l.Source.FindCol(ctx, expr, underRoute) } -func (l *LockAndComment) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (l *LockAndComment) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { return l.Source.GetColumns(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/distinct.go b/go/vt/vtgate/planbuilder/operators/distinct.go index 7fe48db4399..a2a119dc16c 100644 --- a/go/vt/vtgate/planbuilder/operators/distinct.go +++ b/go/vt/vtgate/planbuilder/operators/distinct.go @@ -47,10 +47,7 @@ type ( ) func (d *Distinct) planOffsets(ctx *plancontext.PlanningContext) error { - columns, err := d.GetColumns(ctx) - if err != nil { - return err - } + columns := d.GetColumns(ctx) for idx, col := range columns { e := d.QP.GetSimplifiedExpr(col.Expr) var wsCol *int @@ -103,7 +100,7 @@ func (d *Distinct) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr return d.Source.FindCol(ctx, expr, underRoute) } -func (d *Distinct) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (d *Distinct) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { return d.Source.GetColumns(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/filter.go b/go/vt/vtgate/planbuilder/operators/filter.go index 6b4692aa89c..d69928f8d1d 100644 --- a/go/vt/vtgate/planbuilder/operators/filter.go +++ b/go/vt/vtgate/planbuilder/operators/filter.go @@ -93,7 +93,7 @@ func (f *Filter) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, return f.Source.FindCol(ctx, expr, underRoute) } -func (f *Filter) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (f *Filter) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { return f.Source.GetColumns(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/horizon.go b/go/vt/vtgate/planbuilder/operators/horizon.go index 7deb5b1408d..2ff17a7cd65 100644 --- a/go/vt/vtgate/planbuilder/operators/horizon.go +++ b/go/vt/vtgate/planbuilder/operators/horizon.go @@ -17,6 +17,7 @@ limitations under the License. package operators import ( + "errors" "slices" "vitess.io/vitess/go/vt/sqlparser" @@ -93,7 +94,7 @@ func (h *Horizon) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser. } tableInfo, err := ctx.SemTable.TableInfoForExpr(expr) if err != nil { - if err == semantics.ErrNotSingleTable { + if errors.Is(err, semantics.ErrNotSingleTable) { return &Filter{ Source: h, Predicates: []sqlparser.Expr{expr}, @@ -158,16 +159,16 @@ func (h *Horizon) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, return -1 } -func (h *Horizon) GetColumns(ctx *plancontext.PlanningContext) (exprs []*sqlparser.AliasedExpr, err error) { +func (h *Horizon) GetColumns(ctx *plancontext.PlanningContext) (exprs []*sqlparser.AliasedExpr) { for _, expr := range ctx.SemTable.SelectExprs(h.Query) { ae, ok := expr.(*sqlparser.AliasedExpr) if !ok { - return nil, vterrors.VT09015() + panic(vterrors.VT09015()) } exprs = append(exprs, ae) } - return exprs, nil + return exprs } func (h *Horizon) GetSelectExprs(*plancontext.PlanningContext) (sqlparser.SelectExprs, error) { diff --git a/go/vt/vtgate/planbuilder/operators/limit.go b/go/vt/vtgate/planbuilder/operators/limit.go index 8fb40cd0bd6..1d9803e4bd3 100644 --- a/go/vt/vtgate/planbuilder/operators/limit.go +++ b/go/vt/vtgate/planbuilder/operators/limit.go @@ -60,7 +60,7 @@ func (l *Limit) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, u return l.Source.FindCol(ctx, expr, underRoute) } -func (l *Limit) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (l *Limit) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { return l.Source.GetColumns(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/operator.go b/go/vt/vtgate/planbuilder/operators/operator.go index e7e8de88982..d97da16c132 100644 --- a/go/vt/vtgate/planbuilder/operators/operator.go +++ b/go/vt/vtgate/planbuilder/operators/operator.go @@ -120,8 +120,8 @@ func (noColumns) AddColumn(*plancontext.PlanningContext, bool, bool, *sqlparser. panic(vterrors.VT13001("noColumns operators have no column")) } -func (noColumns) GetColumns(*plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { - return nil, vterrors.VT13001("noColumns operators have no column") +func (noColumns) GetColumns(*plancontext.PlanningContext) []*sqlparser.AliasedExpr { + panic(vterrors.VT13001("noColumns operators have no column")) } func (noColumns) FindCol(*plancontext.PlanningContext, sqlparser.Expr, bool) int { @@ -165,10 +165,7 @@ func tryTruncateColumnsAt(op ops.Operator, truncateAt int) bool { } func transformColumnsToSelectExprs(ctx *plancontext.PlanningContext, op ops.Operator) (sqlparser.SelectExprs, error) { - columns, err := op.GetColumns(ctx) - if err != nil { - return nil, err - } + columns := op.GetColumns(ctx) selExprs := slice.Map(columns, func(from *sqlparser.AliasedExpr) sqlparser.SelectExpr { return from }) diff --git a/go/vt/vtgate/planbuilder/operators/ops/op.go b/go/vt/vtgate/planbuilder/operators/ops/op.go index 189550bc958..e291f528e50 100644 --- a/go/vt/vtgate/planbuilder/operators/ops/op.go +++ b/go/vt/vtgate/planbuilder/operators/ops/op.go @@ -48,7 +48,7 @@ type ( FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int - GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) + GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) ShortDescription() string diff --git a/go/vt/vtgate/planbuilder/operators/ordering.go b/go/vt/vtgate/planbuilder/operators/ordering.go index 605c296678a..07b3577d52c 100644 --- a/go/vt/vtgate/planbuilder/operators/ordering.go +++ b/go/vt/vtgate/planbuilder/operators/ordering.go @@ -66,7 +66,7 @@ func (o *Ordering) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr return o.Source.FindCol(ctx, expr, underRoute) } -func (o *Ordering) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (o *Ordering) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { return o.Source.GetColumns(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/projection.go b/go/vt/vtgate/planbuilder/operators/projection.go index 6ba45ea88f4..00c82cab127 100644 --- a/go/vt/vtgate/planbuilder/operators/projection.go +++ b/go/vt/vtgate/planbuilder/operators/projection.go @@ -77,7 +77,7 @@ func (dt *DerivedTable) introducesTableID() semantics.TableSet { type ( // ProjCols is used to enable projections that are only valid if we can push them into a route, and we never need to ask it about offsets ProjCols interface { - GetColumns() ([]*sqlparser.AliasedExpr, error) + GetColumns() []*sqlparser.AliasedExpr GetSelectExprs() sqlparser.SelectExprs AddColumn(*sqlparser.AliasedExpr) (ProjCols, int, error) } @@ -135,8 +135,8 @@ func newAliasedProjection(src ops.Operator) *Projection { } } -func (sp StarProjections) GetColumns() ([]*sqlparser.AliasedExpr, error) { - return nil, vterrors.VT09015() +func (sp StarProjections) GetColumns() []*sqlparser.AliasedExpr { + panic(vterrors.VT09015()) } func (sp StarProjections) AddColumn(*sqlparser.AliasedExpr) (ProjCols, int, error) { @@ -147,10 +147,10 @@ func (sp StarProjections) GetSelectExprs() sqlparser.SelectExprs { return sqlparser.SelectExprs(sp) } -func (ap AliasedProjections) GetColumns() ([]*sqlparser.AliasedExpr, error) { +func (ap AliasedProjections) GetColumns() []*sqlparser.AliasedExpr { return slice.Map(ap, func(from *ProjExpr) *sqlparser.AliasedExpr { return aeWrap(from.ColExpr) - }), nil + }) } func (ap AliasedProjections) GetSelectExprs() sqlparser.SelectExprs { @@ -386,7 +386,7 @@ func (p *Projection) AddPredicate(ctx *plancontext.PlanningContext, expr sqlpars return p } -func (p *Projection) GetColumns(*plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (p *Projection) GetColumns(*plancontext.PlanningContext) []*sqlparser.AliasedExpr { return p.Columns.GetColumns() } @@ -535,7 +535,7 @@ func (p *Projection) compactWithRoute(ctx *plancontext.PlanningContext, rb *Rout return p, rewrite.SameTree, nil } } - columns, err := rb.GetColumns(ctx) + columns := rb.GetColumns(ctx) if err != nil { return nil, nil, err } diff --git a/go/vt/vtgate/planbuilder/operators/route.go b/go/vt/vtgate/planbuilder/operators/route.go index b6f70764562..3e394580f0a 100644 --- a/go/vt/vtgate/planbuilder/operators/route.go +++ b/go/vt/vtgate/planbuilder/operators/route.go @@ -533,10 +533,7 @@ func (r *Route) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Ex func createProjection(ctx *plancontext.PlanningContext, src ops.Operator) (*Projection, error) { proj := newAliasedProjection(src) - cols, err := src.GetColumns(ctx) - if err != nil { - return nil, err - } + cols := src.GetColumns(ctx) for _, col := range cols { _, err := proj.addUnexploredExpr(col, col.Expr) if err != nil { @@ -634,10 +631,7 @@ func addMultipleColumnsToInput(ctx *plancontext.PlanningContext, operator ops.Op case *Union: tableID := semantics.SingleTableSet(len(ctx.SemTable.Tables)) ctx.SemTable.Tables = append(ctx.SemTable.Tables, nil) - unionColumns, err := op.GetColumns(ctx) - if err != nil { - return op, false, nil - } + unionColumns := op.GetColumns(ctx) proj := &Projection{ Source: op, Columns: AliasedProjections(slice.Map(unionColumns, newProjExpr)), @@ -656,7 +650,7 @@ func (r *Route) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, _ return r.Source.FindCol(ctx, expr, true) } -func (r *Route) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (r *Route) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { return r.Source.GetColumns(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/subquery.go b/go/vt/vtgate/planbuilder/operators/subquery.go index cbe03d563c1..2aee4903831 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery.go +++ b/go/vt/vtgate/planbuilder/operators/subquery.go @@ -181,7 +181,7 @@ func (sq *SubQuery) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Exp return sq.Outer.FindCol(ctx, expr, underRoute) } -func (sq *SubQuery) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (sq *SubQuery) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { return sq.Outer.GetColumns(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/subquery_container.go b/go/vt/vtgate/planbuilder/operators/subquery_container.go index a8a195b15b2..1ea54b18acd 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery_container.go +++ b/go/vt/vtgate/planbuilder/operators/subquery_container.go @@ -84,7 +84,7 @@ func (sqc *SubQueryContainer) FindCol(ctx *plancontext.PlanningContext, expr sql return sqc.Outer.FindCol(ctx, expr, underRoute) } -func (sqc *SubQueryContainer) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (sqc *SubQueryContainer) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { return sqc.Outer.GetColumns(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/table.go b/go/vt/vtgate/planbuilder/operators/table.go index 93325d0ddea..08ca5bb0c09 100644 --- a/go/vt/vtgate/planbuilder/operators/table.go +++ b/go/vt/vtgate/planbuilder/operators/table.go @@ -84,8 +84,8 @@ func (to *Table) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, return -1 } -func (to *Table) GetColumns(*plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { - return slice.Map(to.Columns, colNameToExpr), nil +func (to *Table) GetColumns(*plancontext.PlanningContext) []*sqlparser.AliasedExpr { + return slice.Map(to.Columns, colNameToExpr) } func (to *Table) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { diff --git a/go/vt/vtgate/planbuilder/operators/union.go b/go/vt/vtgate/planbuilder/operators/union.go index 59e362931e6..8a2843fdea8 100644 --- a/go/vt/vtgate/planbuilder/operators/union.go +++ b/go/vt/vtgate/planbuilder/operators/union.go @@ -187,10 +187,7 @@ func (u *Union) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, return offset } } - cols, err := u.GetColumns(ctx) - if err != nil { - panic(err) - } + cols := u.GetColumns(ctx) switch e := expr.Expr.(type) { case *sqlparser.ColName: @@ -246,10 +243,7 @@ func (u *Union) addWeightStringToOffset(ctx *plancontext.PlanningContext, argIdx } func (u *Union) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { - columns, err := u.GetColumns(ctx) - if err != nil { - panic(err) - } + columns := u.GetColumns(ctx) for idx, col := range columns { if ctx.SemTable.EqualsExprWithDeps(expr, col.Expr) { @@ -260,7 +254,7 @@ func (u *Union) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, u return -1 } -func (u *Union) GetColumns(ctx *plancontext.PlanningContext) (result []*sqlparser.AliasedExpr, err error) { +func (u *Union) GetColumns(ctx *plancontext.PlanningContext) (result []*sqlparser.AliasedExpr) { if u.unionColumnsAsAlisedExprs == nil { allOk := true u.unionColumnsAsAlisedExprs = slice.Map(u.unionColumns, func(from sqlparser.SelectExpr) *sqlparser.AliasedExpr { @@ -269,24 +263,20 @@ func (u *Union) GetColumns(ctx *plancontext.PlanningContext) (result []*sqlparse return expr }) if !allOk { - return nil, vterrors.VT09015() + panic(vterrors.VT09015()) } } // if any of the inputs has more columns that we expect, we want to show on top of UNION, so the results can // be truncated to the expected result columns and nothing else for _, src := range u.Sources { - columns, err := src.GetColumns(ctx) - if err != nil { - return nil, err - } - + columns := src.GetColumns(ctx) for len(columns) > len(u.unionColumnsAsAlisedExprs) { u.unionColumnsAsAlisedExprs = append(u.unionColumnsAsAlisedExprs, aeWrap(sqlparser.NewIntLiteral("0"))) } } - return u.unionColumnsAsAlisedExprs, nil + return u.unionColumnsAsAlisedExprs } func (u *Union) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { diff --git a/go/vt/vtgate/planbuilder/operators/vindex.go b/go/vt/vtgate/planbuilder/operators/vindex.go index 76f7348b508..0ca359e9264 100644 --- a/go/vt/vtgate/planbuilder/operators/vindex.go +++ b/go/vt/vtgate/planbuilder/operators/vindex.go @@ -93,8 +93,8 @@ func (v *Vindex) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, return -1 } -func (v *Vindex) GetColumns(*plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { - return slice.Map(v.Columns, colNameToExpr), nil +func (v *Vindex) GetColumns(*plancontext.PlanningContext) []*sqlparser.AliasedExpr { + return slice.Map(v.Columns, colNameToExpr) } func (v *Vindex) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { From d8fc33c89d7b6006bdded7aeba9c8ef885bb5dfc Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Mon, 9 Oct 2023 12:37:58 +0200 Subject: [PATCH 07/15] refactor: panic instead of error for GetSelectExprs Signed-off-by: Andres Taylor --- go/vt/vtgate/planbuilder/operators/SQL_builder.go | 10 ++-------- go/vt/vtgate/planbuilder/operators/aggregator.go | 2 +- go/vt/vtgate/planbuilder/operators/apply_join.go | 2 +- go/vt/vtgate/planbuilder/operators/comments.go | 2 +- go/vt/vtgate/planbuilder/operators/distinct.go | 2 +- go/vt/vtgate/planbuilder/operators/filter.go | 2 +- go/vt/vtgate/planbuilder/operators/horizon.go | 4 ++-- go/vt/vtgate/planbuilder/operators/limit.go | 2 +- go/vt/vtgate/planbuilder/operators/operator.go | 8 ++++---- go/vt/vtgate/planbuilder/operators/ops/op.go | 2 +- go/vt/vtgate/planbuilder/operators/ordering.go | 2 +- go/vt/vtgate/planbuilder/operators/projection.go | 6 +++--- go/vt/vtgate/planbuilder/operators/query_planning.go | 6 +----- go/vt/vtgate/planbuilder/operators/route.go | 2 +- go/vt/vtgate/planbuilder/operators/subquery.go | 2 +- .../vtgate/planbuilder/operators/subquery_container.go | 2 +- go/vt/vtgate/planbuilder/operators/table.go | 2 +- go/vt/vtgate/planbuilder/operators/union.go | 10 +++------- go/vt/vtgate/planbuilder/operators/vindex.go | 2 +- 19 files changed, 28 insertions(+), 42 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/SQL_builder.go b/go/vt/vtgate/planbuilder/operators/SQL_builder.go index 0a458a5b191..5201818951d 100644 --- a/go/vt/vtgate/planbuilder/operators/SQL_builder.go +++ b/go/vt/vtgate/planbuilder/operators/SQL_builder.go @@ -477,10 +477,7 @@ func buildProjection(op *Projection, qb *queryBuilder) { _, isSel := qb.stmt.(*sqlparser.Select) if isSel { qb.clearProjections() - cols, err := op.GetSelectExprs(qb.ctx) - if err != nil { - panic(err) - } + cols := op.GetSelectExprs(qb.ctx) for _, column := range cols { qb.addProjection(column) } @@ -497,10 +494,7 @@ func buildProjection(op *Projection, qb *queryBuilder) { } if !isSel { - cols, err := op.GetSelectExprs(qb.ctx) - if err != nil { - panic(err) - } + cols := op.GetSelectExprs(qb.ctx) for _, column := range cols { qb.addProjection(column) } diff --git a/go/vt/vtgate/planbuilder/operators/aggregator.go b/go/vt/vtgate/planbuilder/operators/aggregator.go index e434a4a895f..14f2d50ca87 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -239,7 +239,7 @@ func (a *Aggregator) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.A return a.Columns } -func (a *Aggregator) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (a *Aggregator) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return transformColumnsToSelectExprs(ctx, a) } diff --git a/go/vt/vtgate/planbuilder/operators/apply_join.go b/go/vt/vtgate/planbuilder/operators/apply_join.go index 706039cfd8a..43f8d9abb06 100644 --- a/go/vt/vtgate/planbuilder/operators/apply_join.go +++ b/go/vt/vtgate/planbuilder/operators/apply_join.go @@ -171,7 +171,7 @@ func (aj *ApplyJoin) GetColumns(*plancontext.PlanningContext) []*sqlparser.Alias return slice.Map(aj.JoinColumns, joinColumnToAliasedExpr) } -func (aj *ApplyJoin) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (aj *ApplyJoin) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return transformColumnsToSelectExprs(ctx, aj) } diff --git a/go/vt/vtgate/planbuilder/operators/comments.go b/go/vt/vtgate/planbuilder/operators/comments.go index 5db439507a1..b480df9ee66 100644 --- a/go/vt/vtgate/planbuilder/operators/comments.go +++ b/go/vt/vtgate/planbuilder/operators/comments.go @@ -63,7 +63,7 @@ func (l *LockAndComment) GetColumns(ctx *plancontext.PlanningContext) []*sqlpars return l.Source.GetColumns(ctx) } -func (l *LockAndComment) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (l *LockAndComment) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return l.Source.GetSelectExprs(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/distinct.go b/go/vt/vtgate/planbuilder/operators/distinct.go index a2a119dc16c..08b2a9d9f8f 100644 --- a/go/vt/vtgate/planbuilder/operators/distinct.go +++ b/go/vt/vtgate/planbuilder/operators/distinct.go @@ -104,7 +104,7 @@ func (d *Distinct) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.Ali return d.Source.GetColumns(ctx) } -func (d *Distinct) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (d *Distinct) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return d.Source.GetSelectExprs(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/filter.go b/go/vt/vtgate/planbuilder/operators/filter.go index d69928f8d1d..45f894fccac 100644 --- a/go/vt/vtgate/planbuilder/operators/filter.go +++ b/go/vt/vtgate/planbuilder/operators/filter.go @@ -97,7 +97,7 @@ func (f *Filter) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.Alias return f.Source.GetColumns(ctx) } -func (f *Filter) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (f *Filter) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return f.Source.GetSelectExprs(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/horizon.go b/go/vt/vtgate/planbuilder/operators/horizon.go index 2ff17a7cd65..3057553984b 100644 --- a/go/vt/vtgate/planbuilder/operators/horizon.go +++ b/go/vt/vtgate/planbuilder/operators/horizon.go @@ -171,8 +171,8 @@ func (h *Horizon) GetColumns(ctx *plancontext.PlanningContext) (exprs []*sqlpars return exprs } -func (h *Horizon) GetSelectExprs(*plancontext.PlanningContext) (sqlparser.SelectExprs, error) { - return sqlparser.GetFirstSelect(h.Query).SelectExprs, nil +func (h *Horizon) GetSelectExprs(*plancontext.PlanningContext) sqlparser.SelectExprs { + return sqlparser.GetFirstSelect(h.Query).SelectExprs } func (h *Horizon) GetOrdering() []ops.OrderBy { diff --git a/go/vt/vtgate/planbuilder/operators/limit.go b/go/vt/vtgate/planbuilder/operators/limit.go index 1d9803e4bd3..12929c69e7d 100644 --- a/go/vt/vtgate/planbuilder/operators/limit.go +++ b/go/vt/vtgate/planbuilder/operators/limit.go @@ -64,7 +64,7 @@ func (l *Limit) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.Aliase return l.Source.GetColumns(ctx) } -func (l *Limit) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (l *Limit) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return l.Source.GetSelectExprs(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/operator.go b/go/vt/vtgate/planbuilder/operators/operator.go index d97da16c132..4e58fca9214 100644 --- a/go/vt/vtgate/planbuilder/operators/operator.go +++ b/go/vt/vtgate/planbuilder/operators/operator.go @@ -128,8 +128,8 @@ func (noColumns) FindCol(*plancontext.PlanningContext, sqlparser.Expr, bool) int panic(vterrors.VT13001("noColumns operators have no column")) } -func (noColumns) GetSelectExprs(*plancontext.PlanningContext) (sqlparser.SelectExprs, error) { - return nil, vterrors.VT13001("noColumns operators have no column") +func (noColumns) GetSelectExprs(*plancontext.PlanningContext) sqlparser.SelectExprs { + panic(vterrors.VT13001("noColumns operators have no column")) } // AddPredicate implements the Operator interface @@ -164,10 +164,10 @@ func tryTruncateColumnsAt(op ops.Operator, truncateAt int) bool { } } -func transformColumnsToSelectExprs(ctx *plancontext.PlanningContext, op ops.Operator) (sqlparser.SelectExprs, error) { +func transformColumnsToSelectExprs(ctx *plancontext.PlanningContext, op ops.Operator) sqlparser.SelectExprs { columns := op.GetColumns(ctx) selExprs := slice.Map(columns, func(from *sqlparser.AliasedExpr) sqlparser.SelectExpr { return from }) - return selExprs, nil + return selExprs } diff --git a/go/vt/vtgate/planbuilder/operators/ops/op.go b/go/vt/vtgate/planbuilder/operators/ops/op.go index e291f528e50..379011d99d9 100644 --- a/go/vt/vtgate/planbuilder/operators/ops/op.go +++ b/go/vt/vtgate/planbuilder/operators/ops/op.go @@ -49,7 +49,7 @@ type ( FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr - GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) + GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs ShortDescription() string diff --git a/go/vt/vtgate/planbuilder/operators/ordering.go b/go/vt/vtgate/planbuilder/operators/ordering.go index 07b3577d52c..f57dd38937d 100644 --- a/go/vt/vtgate/planbuilder/operators/ordering.go +++ b/go/vt/vtgate/planbuilder/operators/ordering.go @@ -70,7 +70,7 @@ func (o *Ordering) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.Ali return o.Source.GetColumns(ctx) } -func (o *Ordering) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (o *Ordering) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return o.Source.GetSelectExprs(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/projection.go b/go/vt/vtgate/planbuilder/operators/projection.go index 00c82cab127..e352d6bcae6 100644 --- a/go/vt/vtgate/planbuilder/operators/projection.go +++ b/go/vt/vtgate/planbuilder/operators/projection.go @@ -390,10 +390,10 @@ func (p *Projection) GetColumns(*plancontext.PlanningContext) []*sqlparser.Alias return p.Columns.GetColumns() } -func (p *Projection) GetSelectExprs(*plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (p *Projection) GetSelectExprs(*plancontext.PlanningContext) sqlparser.SelectExprs { switch cols := p.Columns.(type) { case StarProjections: - return sqlparser.SelectExprs(cols), nil + return sqlparser.SelectExprs(cols) case AliasedProjections: var output sqlparser.SelectExprs for _, pe := range cols { @@ -405,7 +405,7 @@ func (p *Projection) GetSelectExprs(*plancontext.PlanningContext) (sqlparser.Sel } output = append(output, ae) } - return output, nil + return output default: panic("unknown type") } diff --git a/go/vt/vtgate/planbuilder/operators/query_planning.go b/go/vt/vtgate/planbuilder/operators/query_planning.go index ec5aa7f4e26..4554b09fcb7 100644 --- a/go/vt/vtgate/planbuilder/operators/query_planning.go +++ b/go/vt/vtgate/planbuilder/operators/query_planning.go @@ -858,11 +858,7 @@ func addTruncationOrProjectionToReturnOutput(ctx *plancontext.PlanningContext, o return output, nil } - cols, err := output.GetSelectExprs(ctx) - if err != nil { - return nil, err - } - + cols := output.GetSelectExprs(ctx) sel := sqlparser.GetFirstSelect(horizon.Query) if len(sel.SelectExprs) == len(cols) { return output, nil diff --git a/go/vt/vtgate/planbuilder/operators/route.go b/go/vt/vtgate/planbuilder/operators/route.go index 3e394580f0a..38d04eb2270 100644 --- a/go/vt/vtgate/planbuilder/operators/route.go +++ b/go/vt/vtgate/planbuilder/operators/route.go @@ -654,7 +654,7 @@ func (r *Route) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.Aliase return r.Source.GetColumns(ctx) } -func (r *Route) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (r *Route) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return r.Source.GetSelectExprs(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/subquery.go b/go/vt/vtgate/planbuilder/operators/subquery.go index 2aee4903831..b25d598a980 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery.go +++ b/go/vt/vtgate/planbuilder/operators/subquery.go @@ -185,7 +185,7 @@ func (sq *SubQuery) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.Al return sq.Outer.GetColumns(ctx) } -func (sq *SubQuery) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (sq *SubQuery) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return sq.Outer.GetSelectExprs(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/subquery_container.go b/go/vt/vtgate/planbuilder/operators/subquery_container.go index 1ea54b18acd..fc2fc823fb4 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery_container.go +++ b/go/vt/vtgate/planbuilder/operators/subquery_container.go @@ -88,6 +88,6 @@ func (sqc *SubQueryContainer) GetColumns(ctx *plancontext.PlanningContext) []*sq return sqc.Outer.GetColumns(ctx) } -func (sqc *SubQueryContainer) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (sqc *SubQueryContainer) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return sqc.Outer.GetSelectExprs(ctx) } diff --git a/go/vt/vtgate/planbuilder/operators/table.go b/go/vt/vtgate/planbuilder/operators/table.go index 08ca5bb0c09..4809a60c988 100644 --- a/go/vt/vtgate/planbuilder/operators/table.go +++ b/go/vt/vtgate/planbuilder/operators/table.go @@ -88,7 +88,7 @@ func (to *Table) GetColumns(*plancontext.PlanningContext) []*sqlparser.AliasedEx return slice.Map(to.Columns, colNameToExpr) } -func (to *Table) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (to *Table) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return transformColumnsToSelectExprs(ctx, to) } diff --git a/go/vt/vtgate/planbuilder/operators/union.go b/go/vt/vtgate/planbuilder/operators/union.go index 8a2843fdea8..b926aefdd04 100644 --- a/go/vt/vtgate/planbuilder/operators/union.go +++ b/go/vt/vtgate/planbuilder/operators/union.go @@ -279,21 +279,17 @@ func (u *Union) GetColumns(ctx *plancontext.PlanningContext) (result []*sqlparse return u.unionColumnsAsAlisedExprs } -func (u *Union) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (u *Union) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { // if any of the inputs has more columns that we expect, we want to show on top of UNION, so the results can // be truncated to the expected result columns and nothing else for _, src := range u.Sources { - columns, err := src.GetSelectExprs(ctx) - if err != nil { - return nil, err - } - + columns := src.GetSelectExprs(ctx) for len(columns) > len(u.unionColumns) { u.unionColumns = append(u.unionColumns, aeWrap(sqlparser.NewIntLiteral("0"))) } } - return u.unionColumns, nil + return u.unionColumns } func (u *Union) NoLHSTableSet() {} diff --git a/go/vt/vtgate/planbuilder/operators/vindex.go b/go/vt/vtgate/planbuilder/operators/vindex.go index 0ca359e9264..49a0ffd1409 100644 --- a/go/vt/vtgate/planbuilder/operators/vindex.go +++ b/go/vt/vtgate/planbuilder/operators/vindex.go @@ -97,7 +97,7 @@ func (v *Vindex) GetColumns(*plancontext.PlanningContext) []*sqlparser.AliasedEx return slice.Map(v.Columns, colNameToExpr) } -func (v *Vindex) GetSelectExprs(ctx *plancontext.PlanningContext) (sqlparser.SelectExprs, error) { +func (v *Vindex) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return transformColumnsToSelectExprs(ctx, v) } From 435cf2e410bdbd466e7b040773e17ef5b6c6e082 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Mon, 9 Oct 2023 12:57:15 +0200 Subject: [PATCH 08/15] refactor: panic instead of error for planOffsets Signed-off-by: Andres Taylor --- .../planbuilder/operators/aggregator.go | 40 ++++++++----------- .../planbuilder/operators/apply_join.go | 9 +---- .../vtgate/planbuilder/operators/distinct.go | 3 +- go/vt/vtgate/planbuilder/operators/filter.go | 12 ++---- .../planbuilder/operators/offset_planning.go | 8 ++-- .../vtgate/planbuilder/operators/ordering.go | 4 +- .../planbuilder/operators/projection.go | 13 ++---- go/vt/vtgate/planbuilder/operators/route.go | 8 ++-- .../vtgate/planbuilder/operators/subquery.go | 5 +-- 9 files changed, 37 insertions(+), 65 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/aggregator.go b/go/vt/vtgate/planbuilder/operators/aggregator.go index 14f2d50ca87..563a67c967a 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -199,11 +199,7 @@ func (a *Aggregator) findColInternal(ctx *plancontext.PlanningContext, ae *sqlpa // Aggregator is little special and cannot work if the input offset are not matched with the aggregation columns. // So, before pushing anything from above the aggregator offset planning needs to be completed. - err = a.planOffsets(ctx) - if err != nil { - return 0, err - } - + a.planOffsets(ctx) if offset, found := canReuseColumn(ctx, a.Columns, expr, extractExpr); found { return offset, nil } @@ -272,22 +268,23 @@ func (a *Aggregator) GetOrdering() []ops.OrderBy { return a.Source.GetOrdering() } -func (a *Aggregator) planOffsets(ctx *plancontext.PlanningContext) error { +func (a *Aggregator) planOffsets(ctx *plancontext.PlanningContext) { if a.offsetPlanned { - return nil + return } defer func() { a.offsetPlanned = true }() if !a.Pushed { - return a.planOffsetsNotPushed(ctx) + a.planOffsetsNotPushed(ctx) + return } for idx, gb := range a.Grouping { if gb.ColOffset == -1 { offset, err := a.internalAddColumn(ctx, aeWrap(gb.Inner), false) if err != nil { - return err + panic(err) } a.Grouping[idx].ColOffset = offset } @@ -297,7 +294,7 @@ func (a *Aggregator) planOffsets(ctx *plancontext.PlanningContext) error { offset, err := a.internalAddColumn(ctx, aeWrap(weightStringFor(gb.SimplifiedExpr)), true) if err != nil { - return err + panic(err) } a.Grouping[idx].WSOffset = offset } @@ -308,12 +305,10 @@ func (a *Aggregator) planOffsets(ctx *plancontext.PlanningContext) error { } offset, err := a.internalAddColumn(ctx, aeWrap(weightStringFor(aggr.Func.GetArg())), true) if err != nil { - return err + panic(err) } a.Aggregations[idx].WSOffset = offset } - - return nil } func (aggr Aggr) getPushColumn() sqlparser.Expr { @@ -332,13 +327,13 @@ func (aggr Aggr) getPushColumn() sqlparser.Expr { } } -func (a *Aggregator) planOffsetsNotPushed(ctx *plancontext.PlanningContext) error { +func (a *Aggregator) planOffsetsNotPushed(ctx *plancontext.PlanningContext) { a.Source = newAliasedProjection(a.Source) // we need to keep things in the column order, so we can't iterate over the aggregations or groupings for colIdx := range a.Columns { idx, err := a.addIfGroupingColumn(ctx, colIdx) if err != nil { - return err + panic(err) } if idx >= 0 { continue @@ -346,15 +341,15 @@ func (a *Aggregator) planOffsetsNotPushed(ctx *plancontext.PlanningContext) erro idx, err = a.addIfAggregationColumn(ctx, colIdx) if err != nil { - return err + panic(err) } if idx < 0 { - return vterrors.VT13001("failed to find the corresponding column") + panic(vterrors.VT13001("failed to find the corresponding column")) } } - return a.pushRemainingGroupingColumnsAndWeightStrings(ctx) + a.pushRemainingGroupingColumnsAndWeightStrings(ctx) } func (a *Aggregator) addIfAggregationColumn(ctx *plancontext.PlanningContext, colIdx int) (int, error) { @@ -396,12 +391,12 @@ func (a *Aggregator) addIfGroupingColumn(ctx *plancontext.PlanningContext, colId } // pushRemainingGroupingColumnsAndWeightStrings pushes any grouping column that is not part of the columns list and weight strings needed for performing grouping aggregations. -func (a *Aggregator) pushRemainingGroupingColumnsAndWeightStrings(ctx *plancontext.PlanningContext) error { +func (a *Aggregator) pushRemainingGroupingColumnsAndWeightStrings(ctx *plancontext.PlanningContext) { for idx, gb := range a.Grouping { if gb.ColOffset == -1 { offset, err := a.internalAddColumn(ctx, aeWrap(gb.Inner), false) if err != nil { - return err + panic(err) } a.Grouping[idx].ColOffset = offset } @@ -412,7 +407,7 @@ func (a *Aggregator) pushRemainingGroupingColumnsAndWeightStrings(ctx *planconte offset, err := a.internalAddColumn(ctx, aeWrap(weightStringFor(gb.SimplifiedExpr)), false) if err != nil { - return err + panic(err) } a.Grouping[idx].WSOffset = offset } @@ -423,11 +418,10 @@ func (a *Aggregator) pushRemainingGroupingColumnsAndWeightStrings(ctx *planconte offset, err := a.internalAddColumn(ctx, aeWrap(weightStringFor(aggr.Func.GetArg())), false) if err != nil { - return err + panic(err) } a.Aggregations[idx].WSOffset = offset } - return nil } func (a *Aggregator) setTruncateColumnCount(offset int) { diff --git a/go/vt/vtgate/planbuilder/operators/apply_join.go b/go/vt/vtgate/planbuilder/operators/apply_join.go index 43f8d9abb06..af8be7adf32 100644 --- a/go/vt/vtgate/planbuilder/operators/apply_join.go +++ b/go/vt/vtgate/planbuilder/operators/apply_join.go @@ -243,14 +243,11 @@ func (aj *ApplyJoin) AddColumn( return offset } -func (aj *ApplyJoin) planOffsets(ctx *plancontext.PlanningContext) (err error) { +func (aj *ApplyJoin) planOffsets(ctx *plancontext.PlanningContext) { for _, col := range aj.JoinColumns { // Read the type description for JoinColumn to understand the following code for _, lhsExpr := range col.LHSExprs { offset := aj.LHS.AddColumn(ctx, true, col.GroupBy, aeWrap(lhsExpr.Expr)) - if err != nil { - return err - } if col.RHSExpr == nil { // if we don't have an RHS expr, it means that this is a pure LHS expression aj.addOffset(-offset - 1) @@ -269,16 +266,12 @@ func (aj *ApplyJoin) planOffsets(ctx *plancontext.PlanningContext) (err error) { offset := aj.LHS.AddColumn(ctx, true, false, aeWrap(lhsExpr.Expr)) aj.Vars[lhsExpr.Name] = offset } - if err != nil { - return err - } } for _, lhsExpr := range aj.ExtraLHSVars { offset := aj.LHS.AddColumn(ctx, true, false, aeWrap(lhsExpr.Expr)) aj.Vars[lhsExpr.Name] = offset } - return nil } func (aj *ApplyJoin) addOffset(offset int) { diff --git a/go/vt/vtgate/planbuilder/operators/distinct.go b/go/vt/vtgate/planbuilder/operators/distinct.go index 08b2a9d9f8f..b141706e847 100644 --- a/go/vt/vtgate/planbuilder/operators/distinct.go +++ b/go/vt/vtgate/planbuilder/operators/distinct.go @@ -46,7 +46,7 @@ type ( } ) -func (d *Distinct) planOffsets(ctx *plancontext.PlanningContext) error { +func (d *Distinct) planOffsets(ctx *plancontext.PlanningContext) { columns := d.GetColumns(ctx) for idx, col := range columns { e := d.QP.GetSimplifiedExpr(col.Expr) @@ -65,7 +65,6 @@ func (d *Distinct) planOffsets(ctx *plancontext.PlanningContext) error { Collation: coll, }) } - return nil } func (d *Distinct) Clone(inputs []ops.Operator) ops.Operator { diff --git a/go/vt/vtgate/planbuilder/operators/filter.go b/go/vt/vtgate/planbuilder/operators/filter.go index 45f894fccac..6e531693752 100644 --- a/go/vt/vtgate/planbuilder/operators/filter.go +++ b/go/vt/vtgate/planbuilder/operators/filter.go @@ -119,27 +119,23 @@ func (f *Filter) Compact(*plancontext.PlanningContext) (ops.Operator, *rewrite.A return f, rewrite.NewTree("two filters merged into one", f), nil } -func (f *Filter) planOffsets(ctx *plancontext.PlanningContext) error { +func (f *Filter) planOffsets(ctx *plancontext.PlanningContext) { cfg := &evalengine.Config{ ResolveType: ctx.SemTable.TypeForExpr, Collation: ctx.SemTable.Collation, } predicate := sqlparser.AndExpressions(f.Predicates...) - rewritten, err := useOffsets(ctx, predicate, f) - if err != nil { - return err - } + rewritten := useOffsets(ctx, predicate, f) eexpr, err := evalengine.Translate(rewritten, cfg) if err != nil { if strings.HasPrefix(err.Error(), evalengine.ErrTranslateExprNotSupported) { - return vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "%s: %s", evalengine.ErrTranslateExprNotSupported, sqlparser.String(predicate)) + panic(vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "%s: %s", evalengine.ErrTranslateExprNotSupported, sqlparser.String(predicate))) } - return err + panic(err) } f.PredicateWithOffsets = eexpr - return nil } func (f *Filter) ShortDescription() string { diff --git a/go/vt/vtgate/planbuilder/operators/offset_planning.go b/go/vt/vtgate/planbuilder/operators/offset_planning.go index 6e0a3ba36c8..e063e7635c7 100644 --- a/go/vt/vtgate/planbuilder/operators/offset_planning.go +++ b/go/vt/vtgate/planbuilder/operators/offset_planning.go @@ -30,7 +30,7 @@ import ( // planOffsets will walk the tree top down, adding offset information to columns in the tree for use in further optimization, func planOffsets(ctx *plancontext.PlanningContext, root ops.Operator) (ops.Operator, error) { type offsettable interface { - planOffsets(ctx *plancontext.PlanningContext) error + planOffsets(ctx *plancontext.PlanningContext) } visitor := func(in ops.Operator, _ semantics.TableSet, _ bool) (ops.Operator, *rewrite.ApplyResult, error) { @@ -39,7 +39,7 @@ func planOffsets(ctx *plancontext.PlanningContext, root ops.Operator) (ops.Opera case *Horizon: return nil, nil, vterrors.VT13001(fmt.Sprintf("should not see %T here", in)) case offsettable: - err = op.planOffsets(ctx) + op.planOffsets(ctx) } if err != nil { return nil, nil, err @@ -60,7 +60,7 @@ func fetchByOffset(e sqlparser.SQLNode) bool { } // useOffsets rewrites an expression to use values from the input -func useOffsets(ctx *plancontext.PlanningContext, expr sqlparser.Expr, op ops.Operator) (sqlparser.Expr, error) { +func useOffsets(ctx *plancontext.PlanningContext, expr sqlparser.Expr, op ops.Operator) sqlparser.Expr { var exprOffset *sqlparser.Offset in := op.Inputs()[0] @@ -85,7 +85,7 @@ func useOffsets(ctx *plancontext.PlanningContext, expr sqlparser.Expr, op ops.Op rewritten := sqlparser.CopyOnRewrite(expr, visitor, up, ctx.SemTable.CopySemanticInfo) - return rewritten.(sqlparser.Expr), nil + return rewritten.(sqlparser.Expr) } // addColumnsToInput adds columns needed by an operator to its input. diff --git a/go/vt/vtgate/planbuilder/operators/ordering.go b/go/vt/vtgate/planbuilder/operators/ordering.go index f57dd38937d..813f091acbe 100644 --- a/go/vt/vtgate/planbuilder/operators/ordering.go +++ b/go/vt/vtgate/planbuilder/operators/ordering.go @@ -78,7 +78,7 @@ func (o *Ordering) GetOrdering() []ops.OrderBy { return o.Order } -func (o *Ordering) planOffsets(ctx *plancontext.PlanningContext) error { +func (o *Ordering) planOffsets(ctx *plancontext.PlanningContext) { for _, order := range o.Order { offset := o.Source.AddColumn(ctx, true, false, aeWrap(order.SimplifiedExpr)) o.Offset = append(o.Offset, offset) @@ -92,8 +92,6 @@ func (o *Ordering) planOffsets(ctx *plancontext.PlanningContext) error { offset = o.Source.AddColumn(ctx, true, false, aeWrap(wsExpr)) o.WOffset = append(o.WOffset, offset) } - - return nil } func (o *Ordering) ShortDescription() string { diff --git a/go/vt/vtgate/planbuilder/operators/projection.go b/go/vt/vtgate/planbuilder/operators/projection.go index e352d6bcae6..cbad45abfd7 100644 --- a/go/vt/vtgate/planbuilder/operators/projection.go +++ b/go/vt/vtgate/planbuilder/operators/projection.go @@ -564,10 +564,10 @@ func (p *Projection) needsEvaluation(ctx *plancontext.PlanningContext, e sqlpars return false } -func (p *Projection) planOffsets(ctx *plancontext.PlanningContext) error { +func (p *Projection) planOffsets(ctx *plancontext.PlanningContext) { ap, err := p.GetAliasedProjections() if err != nil { - return err + panic(err) } for _, pe := range ap { @@ -577,10 +577,7 @@ func (p *Projection) planOffsets(ctx *plancontext.PlanningContext) error { } // first step is to replace the expressions we expect to get from our input with the offsets for these - rewritten, err := useOffsets(ctx, pe.EvalExpr, p) - if err != nil { - return err - } + rewritten := useOffsets(ctx, pe.EvalExpr, p) pe.EvalExpr = rewritten // if we get a pure offset back. No need to do anything else @@ -593,15 +590,13 @@ func (p *Projection) planOffsets(ctx *plancontext.PlanningContext) error { // for everything else, we'll turn to the evalengine eexpr, err := evalengine.Translate(rewritten, nil) if err != nil { - return err + panic(err) } pe.Info = &EvalEngine{ EExpr: eexpr, } } - - return nil } func (p *Projection) introducesTableID() semantics.TableSet { diff --git a/go/vt/vtgate/planbuilder/operators/route.go b/go/vt/vtgate/planbuilder/operators/route.go index 38d04eb2270..e68f945b829 100644 --- a/go/vt/vtgate/planbuilder/operators/route.go +++ b/go/vt/vtgate/planbuilder/operators/route.go @@ -682,17 +682,17 @@ func isSpecialOrderBy(o ops.OrderBy) bool { return isFunction && f.Name.Lowered() == "rand" } -func (r *Route) planOffsets(ctx *plancontext.PlanningContext) (err error) { +func (r *Route) planOffsets(ctx *plancontext.PlanningContext) { // if operator is returning data from a single shard, we don't need to do anything more if r.IsSingleShard() { - return nil + return } // if we are getting results from multiple shards, we need to do a merge-sort // between them to get the final output correctly sorted ordering := r.Source.GetOrdering() if len(ordering) == 0 { - return nil + return } for _, order := range ordering { @@ -714,8 +714,6 @@ func (r *Route) planOffsets(ctx *plancontext.PlanningContext) (err error) { } r.Ordering = append(r.Ordering, o) } - - return nil } func weightStringFor(expr sqlparser.Expr) sqlparser.Expr { diff --git a/go/vt/vtgate/planbuilder/operators/subquery.go b/go/vt/vtgate/planbuilder/operators/subquery.go index b25d598a980..958b873e9e9 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery.go +++ b/go/vt/vtgate/planbuilder/operators/subquery.go @@ -54,11 +54,11 @@ type SubQuery struct { IsProjection bool } -func (sq *SubQuery) planOffsets(ctx *plancontext.PlanningContext) error { +func (sq *SubQuery) planOffsets(ctx *plancontext.PlanningContext) { sq.Vars = make(map[string]int) columns, err := sq.GetJoinColumns(ctx, sq.Outer) if err != nil { - return err + panic(err) } for _, jc := range columns { for _, lhsExpr := range jc.LHSExprs { @@ -66,7 +66,6 @@ func (sq *SubQuery) planOffsets(ctx *plancontext.PlanningContext) error { sq.Vars[lhsExpr.Name] = offset } } - return nil } func (sq *SubQuery) OuterExpressionsNeeded(ctx *plancontext.PlanningContext, outer ops.Operator) (result []*sqlparser.ColName, err error) { From d3d8ba927c796df53ee8a0f8c2b8eb620b45378f Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Tue, 10 Oct 2023 07:18:42 +0200 Subject: [PATCH 09/15] refactor: panic instead of error for RewriteExpression Signed-off-by: Andres Taylor --- .../vtgate/planbuilder/operators/aggregator.go | 17 ++++------------- .../vtgate/planbuilder/operators/projection.go | 13 +++++-------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/aggregator.go b/go/vt/vtgate/planbuilder/operators/aggregator.go index 563a67c967a..939122b1ad4 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -80,7 +80,7 @@ func (a *Aggregator) SetInputs(operators []ops.Operator) { a.Source = operators[0] } -func (a *Aggregator) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { +func (a *Aggregator) AddPredicate(_ *plancontext.PlanningContext, expr sqlparser.Expr) ops.Operator { return &Filter{ Source: a, Predicates: []sqlparser.Expr{expr}, @@ -125,10 +125,7 @@ func (a *Aggregator) isDerived() bool { } func (a *Aggregator) FindCol(ctx *plancontext.PlanningContext, in sqlparser.Expr, _ bool) int { - expr, err := a.DT.RewriteExpression(ctx, in) - if err != nil { - panic(err) - } + expr := a.DT.RewriteExpression(ctx, in) if offset, found := canReuseColumn(ctx, a.Columns, expr, extractExpr); found { return offset } @@ -136,10 +133,7 @@ func (a *Aggregator) FindCol(ctx *plancontext.PlanningContext, in sqlparser.Expr } func (a *Aggregator) AddColumn(ctx *plancontext.PlanningContext, reuse bool, groupBy bool, ae *sqlparser.AliasedExpr) int { - rewritten, err := a.DT.RewriteExpression(ctx, ae.Expr) - if err != nil { - panic(err) - } + rewritten := a.DT.RewriteExpression(ctx, ae.Expr) ae = &sqlparser.AliasedExpr{ Expr: rewritten, @@ -192,10 +186,7 @@ func (a *Aggregator) findColInternal(ctx *plancontext.PlanningContext, ae *sqlpa if offset >= 0 { return offset, nil } - expr, err := a.DT.RewriteExpression(ctx, expr) - if err != nil { - return 0, err - } + expr = a.DT.RewriteExpression(ctx, expr) // Aggregator is little special and cannot work if the input offset are not matched with the aggregation columns. // So, before pushing anything from above the aggregator offset planning needs to be completed. diff --git a/go/vt/vtgate/planbuilder/operators/projection.go b/go/vt/vtgate/planbuilder/operators/projection.go index cbad45abfd7..5a30b53531d 100644 --- a/go/vt/vtgate/planbuilder/operators/projection.go +++ b/go/vt/vtgate/planbuilder/operators/projection.go @@ -56,15 +56,15 @@ func (dt *DerivedTable) String() string { return fmt.Sprintf("DERIVED %s(%s)", dt.Alias, sqlparser.String(dt.Columns)) } -func (dt *DerivedTable) RewriteExpression(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (sqlparser.Expr, error) { +func (dt *DerivedTable) RewriteExpression(ctx *plancontext.PlanningContext, expr sqlparser.Expr) sqlparser.Expr { if dt == nil { - return expr, nil + return expr } tableInfo, err := ctx.SemTable.TableInfoFor(dt.TableID) if err != nil { - return nil, err + panic(err) } - return semantics.RewriteDerivedTableExpression(expr, tableInfo), nil + return semantics.RewriteDerivedTableExpression(expr, tableInfo) } func (dt *DerivedTable) introducesTableID() semantics.TableSet { @@ -319,10 +319,7 @@ func (p *Projection) addColumn( ae *sqlparser.AliasedExpr, push bool, ) (int, error) { - expr, err := p.DT.RewriteExpression(ctx, ae.Expr) - if err != nil { - return 0, err - } + expr := p.DT.RewriteExpression(ctx, ae.Expr) if reuse { offset := p.FindCol(ctx, expr, false) From 5a98e3ab529f1d10f23d9cf0f439f419bc28d20b Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Tue, 10 Oct 2023 07:21:08 +0200 Subject: [PATCH 10/15] refactor: panic instead of error for addColumnWithoutPushing Signed-off-by: Andres Taylor --- .../planbuilder/operators/aggregation_pushing.go | 10 ++-------- go/vt/vtgate/planbuilder/operators/aggregator.go | 9 +++------ go/vt/vtgate/planbuilder/operators/offset_planning.go | 5 +---- go/vt/vtgate/planbuilder/operators/projection.go | 8 ++++++-- go/vt/vtgate/planbuilder/operators/route.go | 2 +- 5 files changed, 13 insertions(+), 21 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/aggregation_pushing.go b/go/vt/vtgate/planbuilder/operators/aggregation_pushing.go index c7fc8c790a7..deb1a8df8c5 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregation_pushing.go +++ b/go/vt/vtgate/planbuilder/operators/aggregation_pushing.go @@ -98,10 +98,7 @@ func pushAggregationThroughSubquery( if idx >= 0 { continue } - _, err := pushedAggr.addColumnWithoutPushing(ctx, aeWrap(colName), true) - if err != nil { - return nil, nil, err - } + pushedAggr.addColumnWithoutPushing(ctx, aeWrap(colName), true) } } @@ -256,10 +253,7 @@ withNextColumn: continue withNextColumn } } - _, err := pushedAggr.addColumnWithoutPushing(ctx, aeWrap(col), true) - if err != nil { - return nil, nil, err - } + pushedAggr.addColumnWithoutPushing(ctx, aeWrap(col), true) } // Set the source of the filter to the new aggregator placed below the route. diff --git a/go/vt/vtgate/planbuilder/operators/aggregator.go b/go/vt/vtgate/planbuilder/operators/aggregator.go index 939122b1ad4..2505adf17f6 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -87,7 +87,7 @@ func (a *Aggregator) AddPredicate(_ *plancontext.PlanningContext, expr sqlparser } } -func (a *Aggregator) addColumnWithoutPushing(ctx *plancontext.PlanningContext, expr *sqlparser.AliasedExpr, addToGroupBy bool) (int, error) { +func (a *Aggregator) addColumnWithoutPushing(ctx *plancontext.PlanningContext, expr *sqlparser.AliasedExpr, addToGroupBy bool) int { offset := len(a.Columns) a.Columns = append(a.Columns, expr) @@ -106,15 +106,12 @@ func (a *Aggregator) addColumnWithoutPushing(ctx *plancontext.PlanningContext, e aggr.ColOffset = offset a.Aggregations = append(a.Aggregations, aggr) } - return offset, nil + return offset } func (a *Aggregator) addColumnsWithoutPushing(ctx *plancontext.PlanningContext, reuse bool, groupby []bool, exprs []*sqlparser.AliasedExpr) (offsets []int, err error) { for i, ae := range exprs { - offset, err := a.addColumnWithoutPushing(ctx, ae, groupby[i]) - if err != nil { - return nil, err - } + offset := a.addColumnWithoutPushing(ctx, ae, groupby[i]) offsets = append(offsets, offset) } return diff --git a/go/vt/vtgate/planbuilder/operators/offset_planning.go b/go/vt/vtgate/planbuilder/operators/offset_planning.go index e063e7635c7..7e7be49874a 100644 --- a/go/vt/vtgate/planbuilder/operators/offset_planning.go +++ b/go/vt/vtgate/planbuilder/operators/offset_planning.go @@ -106,10 +106,7 @@ func addColumnsToInput(ctx *plancontext.PlanningContext, root ops.Operator) (ops found := func(expr sqlparser.Expr, i int) {} notFound := func(e sqlparser.Expr) error { _, addToGroupBy := e.(*sqlparser.ColName) - _, err := proj.addColumnWithoutPushing(ctx, aeWrap(e), addToGroupBy) - if err != nil { - return err - } + proj.addColumnWithoutPushing(ctx, aeWrap(e), addToGroupBy) addedColumns = true return nil } diff --git a/go/vt/vtgate/planbuilder/operators/projection.go b/go/vt/vtgate/planbuilder/operators/projection.go index 5a30b53531d..541e9590e01 100644 --- a/go/vt/vtgate/planbuilder/operators/projection.go +++ b/go/vt/vtgate/planbuilder/operators/projection.go @@ -288,8 +288,12 @@ func (p *Projection) addSubqueryExpr(ae *sqlparser.AliasedExpr, expr sqlparser.E return err } -func (p *Projection) addColumnWithoutPushing(ctx *plancontext.PlanningContext, expr *sqlparser.AliasedExpr, _ bool) (int, error) { - return p.addColumn(ctx, true, false, expr, false) +func (p *Projection) addColumnWithoutPushing(ctx *plancontext.PlanningContext, expr *sqlparser.AliasedExpr, _ bool) int { + column, err := p.addColumn(ctx, true, false, expr, false) + if err != nil { + panic(err) + } + return column } func (p *Projection) addColumnsWithoutPushing(ctx *plancontext.PlanningContext, reuse bool, _ []bool, exprs []*sqlparser.AliasedExpr) ([]int, error) { diff --git a/go/vt/vtgate/planbuilder/operators/route.go b/go/vt/vtgate/planbuilder/operators/route.go index e68f945b829..dc321c61739 100644 --- a/go/vt/vtgate/planbuilder/operators/route.go +++ b/go/vt/vtgate/planbuilder/operators/route.go @@ -574,7 +574,7 @@ func (r *Route) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, type selectExpressions interface { ops.Operator - addColumnWithoutPushing(ctx *plancontext.PlanningContext, expr *sqlparser.AliasedExpr, addToGroupBy bool) (int, error) + addColumnWithoutPushing(ctx *plancontext.PlanningContext, expr *sqlparser.AliasedExpr, addToGroupBy bool) int addColumnsWithoutPushing(ctx *plancontext.PlanningContext, reuse bool, addToGroupBy []bool, exprs []*sqlparser.AliasedExpr) ([]int, error) isDerived() bool } From 8a9260d4735d96e2cc3765a8cb1a82b08fafda1d Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Tue, 10 Oct 2023 07:24:22 +0200 Subject: [PATCH 11/15] refactor: panic instead of error for addColumnsWithoutPushing Signed-off-by: Andres Taylor --- go/vt/vtgate/planbuilder/operators/aggregator.go | 2 +- go/vt/vtgate/planbuilder/operators/projection.go | 6 +++--- go/vt/vtgate/planbuilder/operators/route.go | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/aggregator.go b/go/vt/vtgate/planbuilder/operators/aggregator.go index 2505adf17f6..91510c5f4a8 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -109,7 +109,7 @@ func (a *Aggregator) addColumnWithoutPushing(ctx *plancontext.PlanningContext, e return offset } -func (a *Aggregator) addColumnsWithoutPushing(ctx *plancontext.PlanningContext, reuse bool, groupby []bool, exprs []*sqlparser.AliasedExpr) (offsets []int, err error) { +func (a *Aggregator) addColumnsWithoutPushing(ctx *plancontext.PlanningContext, reuse bool, groupby []bool, exprs []*sqlparser.AliasedExpr) (offsets []int) { for i, ae := range exprs { offset := a.addColumnWithoutPushing(ctx, ae, groupby[i]) offsets = append(offsets, offset) diff --git a/go/vt/vtgate/planbuilder/operators/projection.go b/go/vt/vtgate/planbuilder/operators/projection.go index 541e9590e01..027c95a6e10 100644 --- a/go/vt/vtgate/planbuilder/operators/projection.go +++ b/go/vt/vtgate/planbuilder/operators/projection.go @@ -296,16 +296,16 @@ func (p *Projection) addColumnWithoutPushing(ctx *plancontext.PlanningContext, e return column } -func (p *Projection) addColumnsWithoutPushing(ctx *plancontext.PlanningContext, reuse bool, _ []bool, exprs []*sqlparser.AliasedExpr) ([]int, error) { +func (p *Projection) addColumnsWithoutPushing(ctx *plancontext.PlanningContext, reuse bool, _ []bool, exprs []*sqlparser.AliasedExpr) []int { offsets := make([]int, len(exprs)) for idx, expr := range exprs { offset, err := p.addColumn(ctx, reuse, false, expr, false) if err != nil { - return nil, err + panic(err) } offsets[idx] = offset } - return offsets, nil + return offsets } func (p *Projection) AddColumn(ctx *plancontext.PlanningContext, reuse bool, addToGroupBy bool, ae *sqlparser.AliasedExpr) int { diff --git a/go/vt/vtgate/planbuilder/operators/route.go b/go/vt/vtgate/planbuilder/operators/route.go index dc321c61739..47b42f124d6 100644 --- a/go/vt/vtgate/planbuilder/operators/route.go +++ b/go/vt/vtgate/planbuilder/operators/route.go @@ -568,14 +568,14 @@ func (r *Route) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, } r.Source = src - offsets, _ = src.addColumnsWithoutPushing(ctx, reuse, []bool{gb}, []*sqlparser.AliasedExpr{expr}) + offsets = src.addColumnsWithoutPushing(ctx, reuse, []bool{gb}, []*sqlparser.AliasedExpr{expr}) return offsets[0] } type selectExpressions interface { ops.Operator addColumnWithoutPushing(ctx *plancontext.PlanningContext, expr *sqlparser.AliasedExpr, addToGroupBy bool) int - addColumnsWithoutPushing(ctx *plancontext.PlanningContext, reuse bool, addToGroupBy []bool, exprs []*sqlparser.AliasedExpr) ([]int, error) + addColumnsWithoutPushing(ctx *plancontext.PlanningContext, reuse bool, addToGroupBy []bool, exprs []*sqlparser.AliasedExpr) []int isDerived() bool } @@ -625,7 +625,7 @@ func addMultipleColumnsToInput(ctx *plancontext.PlanningContext, operator ops.Op // we have to add a new projection and can't build on this one return op, false, nil } - offset, _ := op.addColumnsWithoutPushing(ctx, reuse, addToGroupBy, exprs) + offset := op.addColumnsWithoutPushing(ctx, reuse, addToGroupBy, exprs) return op, true, offset case *Union: From ed46feb72832827f29a638f4f2ad1d008d588109 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Tue, 10 Oct 2023 07:26:11 +0200 Subject: [PATCH 12/15] refactor: panic instead of error for findColInternal Signed-off-by: Andres Taylor --- .../vtgate/planbuilder/operators/aggregator.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/aggregator.go b/go/vt/vtgate/planbuilder/operators/aggregator.go index 91510c5f4a8..210b021e327 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -138,10 +138,7 @@ func (a *Aggregator) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gro } if reuse { - offset, err := a.findColInternal(ctx, ae, groupBy) - if err != nil { - panic(err) - } + offset := a.findColInternal(ctx, ae, groupBy) if offset >= 0 { return offset } @@ -177,11 +174,11 @@ func (a *Aggregator) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gro return offset } -func (a *Aggregator) findColInternal(ctx *plancontext.PlanningContext, ae *sqlparser.AliasedExpr, addToGroupBy bool) (int, error) { +func (a *Aggregator) findColInternal(ctx *plancontext.PlanningContext, ae *sqlparser.AliasedExpr, addToGroupBy bool) int { expr := ae.Expr offset := a.FindCol(ctx, expr, false) if offset >= 0 { - return offset, nil + return offset } expr = a.DT.RewriteExpression(ctx, expr) @@ -189,20 +186,20 @@ func (a *Aggregator) findColInternal(ctx *plancontext.PlanningContext, ae *sqlpa // So, before pushing anything from above the aggregator offset planning needs to be completed. a.planOffsets(ctx) if offset, found := canReuseColumn(ctx, a.Columns, expr, extractExpr); found { - return offset, nil + return offset } colName, isColName := expr.(*sqlparser.ColName) for i, col := range a.Columns { if isColName && colName.Name.EqualString(col.As.String()) { - return i, nil + return i } } if addToGroupBy { - return 0, vterrors.VT13001("did not expect to add group by here") + panic(vterrors.VT13001("did not expect to add group by here")) } - return -1, nil + return -1 } func (a *Aggregator) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { From 8f35800beeb224af934c193f0580bfd8a0b52f67 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Tue, 10 Oct 2023 07:28:23 +0200 Subject: [PATCH 13/15] refactor: panic instead of error for aggregator.go Signed-off-by: Andres Taylor --- .../planbuilder/operators/aggregator.go | 60 ++++++------------- 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/aggregator.go b/go/vt/vtgate/planbuilder/operators/aggregator.go index 210b021e327..562a477c9d4 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -267,20 +267,14 @@ func (a *Aggregator) planOffsets(ctx *plancontext.PlanningContext) { for idx, gb := range a.Grouping { if gb.ColOffset == -1 { - offset, err := a.internalAddColumn(ctx, aeWrap(gb.Inner), false) - if err != nil { - panic(err) - } + offset := a.internalAddColumn(ctx, aeWrap(gb.Inner), false) a.Grouping[idx].ColOffset = offset } if gb.WSOffset != -1 || !ctx.SemTable.NeedsWeightString(gb.SimplifiedExpr) { continue } - offset, err := a.internalAddColumn(ctx, aeWrap(weightStringFor(gb.SimplifiedExpr)), true) - if err != nil { - panic(err) - } + offset := a.internalAddColumn(ctx, aeWrap(weightStringFor(gb.SimplifiedExpr)), true) a.Grouping[idx].WSOffset = offset } @@ -288,10 +282,7 @@ func (a *Aggregator) planOffsets(ctx *plancontext.PlanningContext) { if !aggr.NeedsWeightString(ctx) { continue } - offset, err := a.internalAddColumn(ctx, aeWrap(weightStringFor(aggr.Func.GetArg())), true) - if err != nil { - panic(err) - } + offset := a.internalAddColumn(ctx, aeWrap(weightStringFor(aggr.Func.GetArg())), true) a.Aggregations[idx].WSOffset = offset } } @@ -316,18 +307,12 @@ func (a *Aggregator) planOffsetsNotPushed(ctx *plancontext.PlanningContext) { a.Source = newAliasedProjection(a.Source) // we need to keep things in the column order, so we can't iterate over the aggregations or groupings for colIdx := range a.Columns { - idx, err := a.addIfGroupingColumn(ctx, colIdx) - if err != nil { - panic(err) - } + idx := a.addIfGroupingColumn(ctx, colIdx) if idx >= 0 { continue } - idx, err = a.addIfAggregationColumn(ctx, colIdx) - if err != nil { - panic(err) - } + idx = a.addIfAggregationColumn(ctx, colIdx) if idx < 0 { panic(vterrors.VT13001("failed to find the corresponding column")) @@ -337,7 +322,7 @@ func (a *Aggregator) planOffsetsNotPushed(ctx *plancontext.PlanningContext) { a.pushRemainingGroupingColumnsAndWeightStrings(ctx) } -func (a *Aggregator) addIfAggregationColumn(ctx *plancontext.PlanningContext, colIdx int) (int, error) { +func (a *Aggregator) addIfAggregationColumn(ctx *plancontext.PlanningContext, colIdx int) int { for _, aggr := range a.Aggregations { if aggr.ColOffset != colIdx { continue @@ -346,19 +331,19 @@ func (a *Aggregator) addIfAggregationColumn(ctx *plancontext.PlanningContext, co wrap := aeWrap(aggr.getPushColumn()) offset := a.Source.AddColumn(ctx, false, false, wrap) if aggr.ColOffset != offset { - return -1, errFailedToPlan(aggr.Original) + panic(errFailedToPlan(aggr.Original)) } - return offset, nil + return offset } - return -1, nil + return -1 } func errFailedToPlan(original *sqlparser.AliasedExpr) *vterrors.VitessError { return vterrors.VT12001(fmt.Sprintf("failed to plan aggregation on: %s", sqlparser.String(original))) } -func (a *Aggregator) addIfGroupingColumn(ctx *plancontext.PlanningContext, colIdx int) (int, error) { +func (a *Aggregator) addIfGroupingColumn(ctx *plancontext.PlanningContext, colIdx int) int { for _, gb := range a.Grouping { if gb.ColOffset != colIdx { continue @@ -367,22 +352,19 @@ func (a *Aggregator) addIfGroupingColumn(ctx *plancontext.PlanningContext, colId expr := a.Columns[colIdx] offset := a.Source.AddColumn(ctx, false, true, expr) if gb.ColOffset != offset { - return -1, errFailedToPlan(expr) + panic(errFailedToPlan(expr)) } - return offset, nil + return offset } - return -1, nil + return -1 } // pushRemainingGroupingColumnsAndWeightStrings pushes any grouping column that is not part of the columns list and weight strings needed for performing grouping aggregations. func (a *Aggregator) pushRemainingGroupingColumnsAndWeightStrings(ctx *plancontext.PlanningContext) { for idx, gb := range a.Grouping { if gb.ColOffset == -1 { - offset, err := a.internalAddColumn(ctx, aeWrap(gb.Inner), false) - if err != nil { - panic(err) - } + offset := a.internalAddColumn(ctx, aeWrap(gb.Inner), false) a.Grouping[idx].ColOffset = offset } @@ -390,10 +372,7 @@ func (a *Aggregator) pushRemainingGroupingColumnsAndWeightStrings(ctx *planconte continue } - offset, err := a.internalAddColumn(ctx, aeWrap(weightStringFor(gb.SimplifiedExpr)), false) - if err != nil { - panic(err) - } + offset := a.internalAddColumn(ctx, aeWrap(weightStringFor(gb.SimplifiedExpr)), false) a.Grouping[idx].WSOffset = offset } for idx, aggr := range a.Aggregations { @@ -401,10 +380,7 @@ func (a *Aggregator) pushRemainingGroupingColumnsAndWeightStrings(ctx *planconte continue } - offset, err := a.internalAddColumn(ctx, aeWrap(weightStringFor(aggr.Func.GetArg())), false) - if err != nil { - panic(err) - } + offset := a.internalAddColumn(ctx, aeWrap(weightStringFor(aggr.Func.GetArg())), false) a.Aggregations[idx].WSOffset = offset } } @@ -413,14 +389,14 @@ func (a *Aggregator) setTruncateColumnCount(offset int) { a.ResultColumns = offset } -func (a *Aggregator) internalAddColumn(ctx *plancontext.PlanningContext, aliasedExpr *sqlparser.AliasedExpr, addToGroupBy bool) (int, error) { +func (a *Aggregator) internalAddColumn(ctx *plancontext.PlanningContext, aliasedExpr *sqlparser.AliasedExpr, addToGroupBy bool) int { offset := a.Source.AddColumn(ctx, true, addToGroupBy, aliasedExpr) if offset == len(a.Columns) { // if we get an offset at the end of our current column list, it means we added a new column a.Columns = append(a.Columns, aliasedExpr) } - return offset, nil + return offset } // SplitAggregatorBelowRoute returns the aggregator that will live under the Route. From 54763310e81db61ad2544e370e2ecbf9ef1c368c Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Tue, 10 Oct 2023 08:21:06 +0200 Subject: [PATCH 14/15] refactor: panic instead of error for horizon_expanding Signed-off-by: Andres Taylor --- .../operators/horizon_expanding.go | 50 ++++++++----------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/horizon_expanding.go b/go/vt/vtgate/planbuilder/operators/horizon_expanding.go index 2714cb73ff1..0616ec60c32 100644 --- a/go/vt/vtgate/planbuilder/operators/horizon_expanding.go +++ b/go/vt/vtgate/planbuilder/operators/horizon_expanding.go @@ -79,10 +79,7 @@ func expandUnionHorizon(ctx *plancontext.PlanningContext, horizon *Horizon, unio } func expandSelectHorizon(ctx *plancontext.PlanningContext, horizon *Horizon, sel *sqlparser.Select) (ops.Operator, *rewrite.ApplyResult, error) { - op, err := createProjectionFromSelect(ctx, horizon) - if err != nil { - return nil, nil, err - } + op := createProjectionFromSelect(ctx, horizon) qp, err := horizon.getQP(ctx) if err != nil { @@ -132,10 +129,10 @@ func expandSelectHorizon(ctx *plancontext.PlanningContext, horizon *Horizon, sel return op, rewrite.NewTree(fmt.Sprintf("expand SELECT horizon into (%s)", strings.Join(extracted, ", ")), op), nil } -func createProjectionFromSelect(ctx *plancontext.PlanningContext, horizon *Horizon) (out ops.Operator, err error) { +func createProjectionFromSelect(ctx *plancontext.PlanningContext, horizon *Horizon) (out ops.Operator) { qp, err := horizon.getQP(ctx) if err != nil { - return nil, err + panic(err) } var dt *DerivedTable @@ -148,19 +145,16 @@ func createProjectionFromSelect(ctx *plancontext.PlanningContext, horizon *Horiz } if !qp.NeedsAggregation() { - projX, err := createProjectionWithoutAggr(ctx, qp, horizon.src()) - if err != nil { - return nil, err - } + projX := createProjectionWithoutAggr(ctx, qp, horizon.src()) projX.DT = dt out = projX - return out, nil + return out } aggregations, complexAggr, err := qp.AggregationExpressions(ctx, true) if err != nil { - return nil, err + panic(err) } a := &Aggregator{ @@ -178,12 +172,12 @@ func createProjectionFromSelect(ctx *plancontext.PlanningContext, horizon *Horiz return createProjectionForSimpleAggregation(ctx, a, qp) } -func createProjectionForSimpleAggregation(ctx *plancontext.PlanningContext, a *Aggregator, qp *QueryProjection) (ops.Operator, error) { +func createProjectionForSimpleAggregation(ctx *plancontext.PlanningContext, a *Aggregator, qp *QueryProjection) ops.Operator { outer: for colIdx, expr := range qp.SelectExprs { ae, err := expr.GetAliasedExpr() if err != nil { - return nil, err + panic(err) } addedToCol := false for idx, groupBy := range a.Grouping { @@ -207,23 +201,23 @@ outer: continue outer } } - return nil, vterrors.VT13001(fmt.Sprintf("Could not find the %s in aggregation in the original query", sqlparser.String(ae))) + panic(vterrors.VT13001(fmt.Sprintf("Could not find the %s in aggregation in the original query", sqlparser.String(ae)))) } - return a, nil + return a } -func createProjectionForComplexAggregation(a *Aggregator, qp *QueryProjection) (ops.Operator, error) { +func createProjectionForComplexAggregation(a *Aggregator, qp *QueryProjection) ops.Operator { p := newAliasedProjection(a) p.DT = a.DT for _, expr := range qp.SelectExprs { ae, err := expr.GetAliasedExpr() if err != nil { - return nil, err + panic(err) } _, err = p.addProjExpr(newProjExpr(ae)) if err != nil { - return nil, err + panic(err) } } for i, by := range a.Grouping { @@ -234,10 +228,10 @@ func createProjectionForComplexAggregation(a *Aggregator, qp *QueryProjection) ( a.Aggregations[i].ColOffset = len(a.Columns) a.Columns = append(a.Columns, aggregation.Original) } - return p, nil + return p } -func createProjectionWithoutAggr(ctx *plancontext.PlanningContext, qp *QueryProjection, src ops.Operator) (*Projection, error) { +func createProjectionWithoutAggr(ctx *plancontext.PlanningContext, qp *QueryProjection, src ops.Operator) *Projection { // first we need to check if we have all columns or there are still unexpanded stars aes, err := slice.MapWithError(qp.SelectExprs, func(from SelectExpr) (*sqlparser.AliasedExpr, error) { ae, ok := from.Col.(*sqlparser.AliasedExpr) @@ -260,26 +254,26 @@ func createProjectionWithoutAggr(ctx *plancontext.PlanningContext, qp *QueryProj expr := ae.Expr newExpr, subqs, err := sqc.pullOutValueSubqueries(ctx, expr, outerID, false) if err != nil { - return nil, err + panic(err) } if newExpr == nil { // there was no subquery in this expression _, err := proj.addUnexploredExpr(org, expr) if err != nil { - return nil, err + panic(err) } } else { err := proj.addSubqueryExpr(org, newExpr, subqs...) if err != nil { - return nil, err + panic(err) } } } proj.Source = sqc.getRootOperator(src) - return proj, nil + return proj } -func newStarProjection(src ops.Operator, qp *QueryProjection) (*Projection, error) { +func newStarProjection(src ops.Operator, qp *QueryProjection) *Projection { cols := sqlparser.SelectExprs{} for _, expr := range qp.SelectExprs { @@ -291,7 +285,7 @@ func newStarProjection(src ops.Operator, qp *QueryProjection) (*Projection, erro return false, vterrors.VT09015() }, expr.Col) if err != nil { - return nil, err + panic(err) } cols = append(cols, expr.Col) } @@ -299,5 +293,5 @@ func newStarProjection(src ops.Operator, qp *QueryProjection) (*Projection, erro return &Projection{ Source: src, Columns: StarProjections(cols), - }, nil + } } From ac9d992a3c0cd215a94a9817032a7eac3b586515 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Tue, 10 Oct 2023 08:27:11 +0200 Subject: [PATCH 15/15] refactor: panic instead of error for merging logic Signed-off-by: Andres Taylor --- .../operators/info_schema_planning.go | 7 +- .../planbuilder/operators/join_merging.go | 16 ++--- go/vt/vtgate/planbuilder/operators/phases.go | 2 +- .../planbuilder/operators/route_planning.go | 5 +- .../planbuilder/operators/sharded_routing.go | 10 +-- .../operators/subquery_planning.go | 68 ++++++++----------- 6 files changed, 47 insertions(+), 61 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/info_schema_planning.go b/go/vt/vtgate/planbuilder/operators/info_schema_planning.go index c10303e61f5..4f096e1ac65 100644 --- a/go/vt/vtgate/planbuilder/operators/info_schema_planning.go +++ b/go/vt/vtgate/planbuilder/operators/info_schema_planning.go @@ -169,7 +169,7 @@ func isTableOrSchemaRoutable(cmp *sqlparser.ComparisonExpr) ( return false, nil } -func tryMergeInfoSchemaRoutings(ctx *plancontext.PlanningContext, routingA, routingB Routing, m merger, lhsRoute, rhsRoute *Route) (*Route, error) { +func tryMergeInfoSchemaRoutings(ctx *plancontext.PlanningContext, routingA, routingB Routing, m merger, lhsRoute, rhsRoute *Route) *Route { // we have already checked type earlier, so this should always be safe isrA := routingA.(*InfoSchemaRouting) isrB := routingB.(*InfoSchemaRouting) @@ -188,7 +188,7 @@ func tryMergeInfoSchemaRoutings(ctx *plancontext.PlanningContext, routingA, rout for k, expr := range isrB.SysTableTableName { if e, found := isrA.SysTableTableName[k]; found && !sqlparser.Equals.Expr(expr, e) { // schema names are the same, but we have contradicting table names, so we give up - return nil, nil + return nil } isrA.SysTableTableName[k] = expr } @@ -203,9 +203,8 @@ func tryMergeInfoSchemaRoutings(ctx *plancontext.PlanningContext, routingA, rout // give up default: - return nil, nil + return nil } - } var ( diff --git a/go/vt/vtgate/planbuilder/operators/join_merging.go b/go/vt/vtgate/planbuilder/operators/join_merging.go index f31259fddc5..52c9c4e5837 100644 --- a/go/vt/vtgate/planbuilder/operators/join_merging.go +++ b/go/vt/vtgate/planbuilder/operators/join_merging.go @@ -28,10 +28,10 @@ import ( // mergeJoinInputs checks whether two operators can be merged into a single one. // If they can be merged, a new operator with the merged routing is returned // If they cannot be merged, nil is returned. -func mergeJoinInputs(ctx *plancontext.PlanningContext, lhs, rhs ops.Operator, joinPredicates []sqlparser.Expr, m merger) (*Route, error) { +func mergeJoinInputs(ctx *plancontext.PlanningContext, lhs, rhs ops.Operator, joinPredicates []sqlparser.Expr, m merger) *Route { lhsRoute, rhsRoute, routingA, routingB, a, b, sameKeyspace := prepareInputRoutes(lhs, rhs) if lhsRoute == nil { - return nil, nil + return nil } switch { @@ -62,7 +62,7 @@ func mergeJoinInputs(ctx *plancontext.PlanningContext, lhs, rhs ops.Operator, jo return tryMergeJoinShardedRouting(ctx, lhsRoute, rhsRoute, m, joinPredicates) default: - return nil, nil + return nil } } @@ -87,8 +87,8 @@ func prepareInputRoutes(lhs ops.Operator, rhs ops.Operator) (*Route, *Route, Rou type ( merger interface { - mergeShardedRouting(ctx *plancontext.PlanningContext, r1, r2 *ShardedRouting, op1, op2 *Route) (*Route, error) - merge(ctx *plancontext.PlanningContext, op1, op2 *Route, r Routing) (*Route, error) + mergeShardedRouting(ctx *plancontext.PlanningContext, r1, r2 *ShardedRouting, op1, op2 *Route) *Route + merge(ctx *plancontext.PlanningContext, op1, op2 *Route, r Routing) *Route } joinMerger struct { @@ -184,7 +184,7 @@ func newJoinMerge(predicates []sqlparser.Expr, innerJoin bool) merger { } } -func (jm *joinMerger) mergeShardedRouting(ctx *plancontext.PlanningContext, r1, r2 *ShardedRouting, op1, op2 *Route) (*Route, error) { +func (jm *joinMerger) mergeShardedRouting(ctx *plancontext.PlanningContext, r1, r2 *ShardedRouting, op1, op2 *Route) *Route { return jm.merge(ctx, op1, op2, mergeShardedRouting(r1, r2)) } @@ -207,10 +207,10 @@ func (jm *joinMerger) getApplyJoin(ctx *plancontext.PlanningContext, op1, op2 *R return NewApplyJoin(op1.Source, op2.Source, ctx.SemTable.AndExpressions(jm.predicates...), !jm.innerJoin) } -func (jm *joinMerger) merge(ctx *plancontext.PlanningContext, op1, op2 *Route, r Routing) (*Route, error) { +func (jm *joinMerger) merge(ctx *plancontext.PlanningContext, op1, op2 *Route, r Routing) *Route { return &Route{ Source: jm.getApplyJoin(ctx, op1, op2), MergedWith: []*Route{op2}, Routing: r, - }, nil + } } diff --git a/go/vt/vtgate/planbuilder/operators/phases.go b/go/vt/vtgate/planbuilder/operators/phases.go index d5c02490fce..0dcc859055b 100644 --- a/go/vt/vtgate/planbuilder/operators/phases.go +++ b/go/vt/vtgate/planbuilder/operators/phases.go @@ -88,7 +88,7 @@ func (p Phase) act(ctx *plancontext.PlanningContext, op ops.Operator) (ops.Opera case cleanOutPerfDistinct: return removePerformanceDistinctAboveRoute(ctx, op) case subquerySettling: - return settleSubqueries(ctx, op) + return settleSubqueries(ctx, op), nil } return op, nil diff --git a/go/vt/vtgate/planbuilder/operators/route_planning.go b/go/vt/vtgate/planbuilder/operators/route_planning.go index 96220f89ea2..6ecfce5bf07 100644 --- a/go/vt/vtgate/planbuilder/operators/route_planning.go +++ b/go/vt/vtgate/planbuilder/operators/route_planning.go @@ -362,10 +362,7 @@ func requiresSwitchingSides(ctx *plancontext.PlanningContext, op ops.Operator) b } func mergeOrJoin(ctx *plancontext.PlanningContext, lhs, rhs ops.Operator, joinPredicates []sqlparser.Expr, inner bool) (ops.Operator, *rewrite.ApplyResult, error) { - newPlan, err := mergeJoinInputs(ctx, lhs, rhs, joinPredicates, newJoinMerge(joinPredicates, inner)) - if err != nil { - return nil, nil, err - } + newPlan := mergeJoinInputs(ctx, lhs, rhs, joinPredicates, newJoinMerge(joinPredicates, inner)) if newPlan != nil { return newPlan, rewrite.NewTree("merge routes into single operator", newPlan), nil } diff --git a/go/vt/vtgate/planbuilder/operators/sharded_routing.go b/go/vt/vtgate/planbuilder/operators/sharded_routing.go index 4965c5d18b5..d54db071d46 100644 --- a/go/vt/vtgate/planbuilder/operators/sharded_routing.go +++ b/go/vt/vtgate/planbuilder/operators/sharded_routing.go @@ -578,7 +578,7 @@ func tryMergeJoinShardedRouting( routeA, routeB *Route, m merger, joinPredicates []sqlparser.Expr, -) (*Route, error) { +) *Route { sameKeyspace := routeA.Routing.Keyspace() == routeB.Routing.Keyspace() tblA := routeA.Routing.(*ShardedRouting) tblB := routeB.Routing.(*ShardedRouting) @@ -605,20 +605,20 @@ func tryMergeJoinShardedRouting( // If we are doing two Scatters, we have to make sure that the // joins are on the correct vindex to allow them to be merged // no join predicates - no vindex - return nil, nil + return nil } if !sameKeyspace { - return nil, vterrors.VT12001("cross-shard correlated subquery") + panic(vterrors.VT12001("cross-shard correlated subquery")) } canMerge := canMergeOnFilters(ctx, routeA, routeB, joinPredicates) if !canMerge { - return nil, nil + return nil } return m.mergeShardedRouting(ctx, tblA, tblB, routeA, routeB) } - return nil, nil + return nil } // makeEvalEngineExpr transforms the given sqlparser.Expr into an evalengine expression diff --git a/go/vt/vtgate/planbuilder/operators/subquery_planning.go b/go/vt/vtgate/planbuilder/operators/subquery_planning.go index 44bce0e0f2e..92f411da48b 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery_planning.go +++ b/go/vt/vtgate/planbuilder/operators/subquery_planning.go @@ -71,7 +71,7 @@ func isMergeable(ctx *plancontext.PlanningContext, query sqlparser.SelectStateme return true } -func settleSubqueries(ctx *plancontext.PlanningContext, op ops.Operator) (ops.Operator, error) { +func settleSubqueries(ctx *plancontext.PlanningContext, op ops.Operator) ops.Operator { visit := func(op ops.Operator, lhsTables semantics.TableSet, isRoot bool) (ops.Operator, *rewrite.ApplyResult, error) { switch op := op.(type) { case *SubQueryContainer: @@ -101,7 +101,11 @@ func settleSubqueries(ctx *plancontext.PlanningContext, op ops.Operator) (ops.Op } return op, rewrite.SameTree, nil } - return rewrite.BottomUp(op, TableID, visit, nil) + op, err := rewrite.BottomUp(op, TableID, visit, nil) + if err != nil { + panic(err) + } + return op } func mergeSubqueryExpr(ctx *plancontext.PlanningContext, pe *ProjExpr) { @@ -296,18 +300,15 @@ func tryMergeWithRHS(ctx *plancontext.PlanningContext, inner *SubQuery, outer *A return nil, nil, nil } - newExpr, err := rewriteOriginalPushedToRHS(ctx, inner.Original, outer) - if err != nil { - return nil, nil, err - } + newExpr := rewriteOriginalPushedToRHS(ctx, inner.Original, outer) sqm := &subqueryRouteMerger{ outer: outerRoute, original: newExpr, subq: inner, } - newOp, err := mergeSubqueryInputs(ctx, innerRoute, outerRoute, inner.GetMergePredicates(), sqm) - if err != nil || newOp == nil { - return nil, nil, err + newOp := mergeSubqueryInputs(ctx, innerRoute, outerRoute, inner.GetMergePredicates(), sqm) + if newOp == nil { + return nil, nil, nil } outer.RHS = newOp @@ -334,7 +335,7 @@ func addSubQuery(in ops.Operator, inner *SubQuery) ops.Operator { // rewriteOriginalPushedToRHS rewrites the original expression to use the argument names instead of the column names // this is necessary because we are pushing the subquery into the RHS of the join, and we need to use the argument names // instead of the column names -func rewriteOriginalPushedToRHS(ctx *plancontext.PlanningContext, expression sqlparser.Expr, outer *ApplyJoin) (sqlparser.Expr, error) { +func rewriteOriginalPushedToRHS(ctx *plancontext.PlanningContext, expression sqlparser.Expr, outer *ApplyJoin) sqlparser.Expr { var err error outerID := TableID(outer.LHS) result := sqlparser.CopyOnRewrite(expression, nil, func(cursor *sqlparser.CopyOnWriteCursor) { @@ -355,9 +356,9 @@ func rewriteOriginalPushedToRHS(ctx *plancontext.PlanningContext, expression sql cursor.Replace(sqlparser.NewArgument(name)) }, nil) if err != nil { - return nil, err + panic(err) } - return result.(sqlparser.Expr), nil + return result.(sqlparser.Expr) } func pushProjectionToOuterContainer(ctx *plancontext.PlanningContext, p *Projection, src *SubQueryContainer) (ops.Operator, *rewrite.ApplyResult, error) { @@ -486,10 +487,7 @@ func tryMergeSubqueriesRecursively( original: subQuery.Original, subq: subQuery, } - op, err := mergeSubqueryInputs(ctx, inner.Outer, outer, exprs, merger) - if err != nil { - return nil, nil, err - } + op := mergeSubqueryInputs(ctx, inner.Outer, outer, exprs, merger) if op == nil { return outer, rewrite.SameTree, nil } @@ -524,10 +522,7 @@ func tryMergeSubqueryWithOuter(ctx *plancontext.PlanningContext, subQuery *SubQu original: subQuery.Original, subq: subQuery, } - op, err := mergeSubqueryInputs(ctx, inner, outer, exprs, merger) - if err != nil { - return nil, nil, err - } + op := mergeSubqueryInputs(ctx, inner, outer, exprs, merger) if op == nil { return outer, rewrite.SameTree, nil } @@ -572,7 +567,7 @@ type subqueryRouteMerger struct { subq *SubQuery } -func (s *subqueryRouteMerger) mergeShardedRouting(ctx *plancontext.PlanningContext, r1, r2 *ShardedRouting, old1, old2 *Route) (*Route, error) { +func (s *subqueryRouteMerger) mergeShardedRouting(ctx *plancontext.PlanningContext, r1, r2 *ShardedRouting, old1, old2 *Route) *Route { tr := &ShardedRouting{ VindexPreds: append(r1.VindexPreds, r2.VindexPreds...), keyspace: r1.keyspace, @@ -622,12 +617,12 @@ func (s *subqueryRouteMerger) mergeShardedRouting(ctx *plancontext.PlanningConte routing, err := tr.resetRoutingLogic(ctx) if err != nil { - return nil, err + panic(err) } return s.merge(ctx, old1, old2, routing) } -func (s *subqueryRouteMerger) merge(ctx *plancontext.PlanningContext, inner, outer *Route, r Routing) (*Route, error) { +func (s *subqueryRouteMerger) merge(ctx *plancontext.PlanningContext, inner, outer *Route, r Routing) *Route { if !s.subq.TopLevel { // if the subquery we are merging isn't a top level predicate, we can't use it for routing return &Route{ @@ -636,12 +631,10 @@ func (s *subqueryRouteMerger) merge(ctx *plancontext.PlanningContext, inner, out Routing: outer.Routing, Ordering: outer.Ordering, ResultColumns: outer.ResultColumns, - }, nil - + } } _, isSharded := r.(*ShardedRouting) var src ops.Operator - var err error if isSharded { src = s.outer.Source if !s.subq.IsProjection { @@ -651,10 +644,7 @@ func (s *subqueryRouteMerger) merge(ctx *plancontext.PlanningContext, inner, out } } } else { - src, err = s.rewriteASTExpression(ctx, inner) - if err != nil { - return nil, err - } + src = s.rewriteASTExpression(ctx, inner) } return &Route{ Source: src, @@ -662,7 +652,7 @@ func (s *subqueryRouteMerger) merge(ctx *plancontext.PlanningContext, inner, out Routing: r, Ordering: s.outer.Ordering, ResultColumns: s.outer.ResultColumns, - }, nil + } } // rewriteASTExpression rewrites the subquery expression that is used in the merged output @@ -672,15 +662,15 @@ func (s *subqueryRouteMerger) merge(ctx *plancontext.PlanningContext, inner, out // we should be able to use this method for all plan types, // but using this method for sharded queries introduces bugs // We really need to figure out why this is not working as expected -func (s *subqueryRouteMerger) rewriteASTExpression(ctx *plancontext.PlanningContext, inner *Route) (ops.Operator, error) { +func (s *subqueryRouteMerger) rewriteASTExpression(ctx *plancontext.PlanningContext, inner *Route) ops.Operator { src := s.outer.Source stmt, _, err := ToSQL(ctx, inner.Source) if err != nil { - return nil, err + panic(err) } subqStmt, ok := stmt.(sqlparser.SelectStatement) if !ok { - return nil, vterrors.VT13001("subqueries should only be select statement") + panic(vterrors.VT13001("subqueries should only be select statement")) } subqID := TableID(s.subq.Subquery) subqStmt = sqlparser.CopyOnRewrite(subqStmt, nil, func(cursor *sqlparser.CopyOnWriteCursor) { @@ -708,7 +698,7 @@ func (s *subqueryRouteMerger) rewriteASTExpression(ctx *plancontext.PlanningCont } }, nil).(sqlparser.SelectStatement) if err != nil { - return nil, err + panic(err) } if s.subq.IsProjection { @@ -726,17 +716,17 @@ func (s *subqueryRouteMerger) rewriteASTExpression(ctx *plancontext.PlanningCont Predicates: []sqlparser.Expr{sQuery}, } } - return src, nil + return src } // mergeSubqueryInputs checks whether two operators can be merged into a single one. // If they can be merged, a new operator with the merged routing is returned // If they cannot be merged, nil is returned. // These rules are similar but different from join merging -func mergeSubqueryInputs(ctx *plancontext.PlanningContext, in, out ops.Operator, joinPredicates []sqlparser.Expr, m *subqueryRouteMerger) (*Route, error) { +func mergeSubqueryInputs(ctx *plancontext.PlanningContext, in, out ops.Operator, joinPredicates []sqlparser.Expr, m *subqueryRouteMerger) *Route { inRoute, outRoute := operatorsToRoutes(in, out) if inRoute == nil || outRoute == nil { - return nil, nil + return nil } inRoute, outRoute, inRouting, outRouting, sameKeyspace := getRoutesOrAlternates(inRoute, outRoute) @@ -770,7 +760,7 @@ func mergeSubqueryInputs(ctx *plancontext.PlanningContext, in, out ops.Operator, return tryMergeJoinShardedRouting(ctx, inRoute, outRoute, m, joinPredicates) default: - return nil, nil + return nil } }