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 9802e374d87..5201818951d 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,14 @@ 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 - } + cols := op.GetColumns(qb.ctx) for _, column := range cols { - err := qb.addProjection(column) - if err != nil { - return err - } + qb.addProjection(column) } for _, by := range op.Grouping { @@ -462,29 +441,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 +467,19 @@ 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 - } + cols := op.GetSelectExprs(qb.ctx) for _, column := range cols { - err := qb.addProjection(column) - if err != nil { - return err - } + qb.addProjection(column) } } @@ -537,26 +494,15 @@ func buildProjection(op *Projection, qb *queryBuilder) error { } if !isSel { - cols, err := op.GetSelectExprs(qb.ctx) - if err != nil { - return err - } + cols := op.GetSelectExprs(qb.ctx) 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 +510,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 +530,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 +574,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 +590,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 { 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 b572e05fb45..562a477c9d4 100644 --- a/go/vt/vtgate/planbuilder/operators/aggregator.go +++ b/go/vt/vtgate/planbuilder/operators/aggregator.go @@ -80,14 +80,14 @@ 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(_ *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) { +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) { +func (a *Aggregator) addColumnsWithoutPushing(ctx *plancontext.PlanningContext, reuse bool, groupby []bool, exprs []*sqlparser.AliasedExpr) (offsets []int) { 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 @@ -124,22 +121,16 @@ func (a *Aggregator) isDerived() bool { return a.DT != nil } -func (a *Aggregator) FindCol(ctx *plancontext.PlanningContext, in sqlparser.Expr, _ bool) (int, error) { - expr, err := a.DT.RewriteExpression(ctx, in) - if err != nil { - return 0, err - } +func (a *Aggregator) FindCol(ctx *plancontext.PlanningContext, in sqlparser.Expr, _ bool) int { + expr := a.DT.RewriteExpression(ctx, in) 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, error) { - rewritten, err := a.DT.RewriteExpression(ctx, ae.Expr) - if err != nil { - return 0, err - } +func (a *Aggregator) AddColumn(ctx *plancontext.PlanningContext, reuse bool, groupBy bool, ae *sqlparser.AliasedExpr) int { + rewritten := a.DT.RewriteExpression(ctx, ae.Expr) ae = &sqlparser.AliasedExpr{ Expr: rewritten, @@ -147,12 +138,9 @@ 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 - } + offset := a.findColInternal(ctx, ae, groupBy) if offset >= 0 { - return offset, nil + return offset } } @@ -177,78 +165,62 @@ 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) { +func (a *Aggregator) findColInternal(ctx *plancontext.PlanningContext, ae *sqlparser.AliasedExpr, addToGroupBy bool) int { 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 - } - expr, err = a.DT.RewriteExpression(ctx, expr) - if err != nil { - return 0, err + return offset } + 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. - 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 + 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, 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) { +func (a *Aggregator) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return transformColumnsToSelectExprs(ctx, a) } @@ -277,37 +249,32 @@ 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() } -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 - } + 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 { - return err - } + offset := a.internalAddColumn(ctx, aeWrap(weightStringFor(gb.SimplifiedExpr)), true) a.Grouping[idx].WSOffset = offset } @@ -315,14 +282,9 @@ func (a *Aggregator) planOffsets(ctx *plancontext.PlanningContext) error { if !aggr.NeedsWeightString(ctx) { continue } - offset, err := a.internalAddColumn(ctx, aeWrap(weightStringFor(aggr.Func.GetArg())), true) - if err != nil { - return err - } + offset := a.internalAddColumn(ctx, aeWrap(weightStringFor(aggr.Func.GetArg())), true) a.Aggregations[idx].WSOffset = offset } - - return nil } func (aggr Aggr) getPushColumn() sqlparser.Expr { @@ -341,84 +303,68 @@ 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 - } + idx := a.addIfGroupingColumn(ctx, colIdx) if idx >= 0 { continue } - idx, err = a.addIfAggregationColumn(ctx, colIdx) - if err != nil { - return err - } + idx = a.addIfAggregationColumn(ctx, colIdx) 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) { +func (a *Aggregator) addIfAggregationColumn(ctx *plancontext.PlanningContext, colIdx int) int { for _, aggr := range a.Aggregations { if aggr.ColOffset != colIdx { continue } 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) + 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 } 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) + 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) 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 - } + offset := a.internalAddColumn(ctx, aeWrap(gb.Inner), false) a.Grouping[idx].ColOffset = offset } @@ -426,10 +372,7 @@ func (a *Aggregator) pushRemainingGroupingColumnsAndWeightStrings(ctx *planconte continue } - offset, err := a.internalAddColumn(ctx, aeWrap(weightStringFor(gb.SimplifiedExpr)), false) - if err != nil { - return err - } + offset := a.internalAddColumn(ctx, aeWrap(weightStringFor(gb.SimplifiedExpr)), false) a.Grouping[idx].WSOffset = offset } for idx, aggr := range a.Aggregations { @@ -437,30 +380,23 @@ func (a *Aggregator) pushRemainingGroupingColumnsAndWeightStrings(ctx *planconte continue } - offset, err := a.internalAddColumn(ctx, aeWrap(weightStringFor(aggr.Func.GetArg())), false) - if err != nil { - return err - } + offset := a.internalAddColumn(ctx, aeWrap(weightStringFor(aggr.Func.GetArg())), false) a.Aggregations[idx].WSOffset = offset } - return nil } func (a *Aggregator) setTruncateColumnCount(offset int) { a.ResultColumns = offset } -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 - } +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. diff --git a/go/vt/vtgate/planbuilder/operators/apply_join.go b/go/vt/vtgate/planbuilder/operators/apply_join.go index 5e48fb4d5e3..af8be7adf32 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,40 +156,26 @@ 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 } -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 } -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) { +func (aj *ApplyJoin) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { return transformColumnsToSelectExprs(ctx, aj) } -func (aj *ApplyJoin) GetOrdering() ([]ops.OrderBy, error) { +func (aj *ApplyJoin) GetOrdering() []ops.OrderBy { return aj.LHS.GetOrdering() } @@ -228,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( @@ -241,33 +227,27 @@ 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 - } + offset := aj.FindCol(ctx, expr.Expr, false) 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) { +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, err := aj.pushColLeft(ctx, aeWrap(lhsExpr.Expr), col.GroupBy) - if err != nil { - return err - } + offset := aj.LHS.AddColumn(ctx, true, col.GroupBy, aeWrap(lhsExpr.Expr)) 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) @@ -276,35 +256,22 @@ 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 { - return err - } } 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 } func (aj *ApplyJoin) addOffset(offset int) { 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 46f9e8c7462..b480df9ee66 100644 --- a/go/vt/vtgate/planbuilder/operators/comments.go +++ b/go/vt/vtgate/planbuilder/operators/comments.go @@ -46,28 +46,24 @@ 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, error) { +func (l *LockAndComment) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, expr *sqlparser.AliasedExpr) int { 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) } -func (l *LockAndComment) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (l *LockAndComment) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { 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) } @@ -80,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 f7f4b350fc7..b141706e847 100644 --- a/go/vt/vtgate/planbuilder/operators/distinct.go +++ b/go/vt/vtgate/planbuilder/operators/distinct.go @@ -46,21 +46,15 @@ type ( } ) -func (d *Distinct) planOffsets(ctx *plancontext.PlanningContext) error { - columns, err := d.GetColumns(ctx) - if err != nil { - return err - } +func (d *Distinct) planOffsets(ctx *plancontext.PlanningContext) { + columns := d.GetColumns(ctx) for idx, col := range columns { e := d.QP.GetSimplifiedExpr(col.Expr) var wsCol *int 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 } @@ -71,7 +65,6 @@ func (d *Distinct) planOffsets(ctx *plancontext.PlanningContext) error { Collation: coll, }) } - return nil } func (d *Distinct) Clone(inputs []ops.Operator) ops.Operator { @@ -93,28 +86,24 @@ 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, error) { +func (d *Distinct) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { 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) } -func (d *Distinct) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (d *Distinct) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { 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) } @@ -125,7 +114,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 874e799cf43..6e531693752 100644 --- a/go/vt/vtgate/planbuilder/operators/filter.go +++ b/go/vt/vtgate/planbuilder/operators/filter.go @@ -80,32 +80,28 @@ 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, error) { +func (f *Filter) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { 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) } -func (f *Filter) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (f *Filter) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { 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) } -func (f *Filter) GetOrdering() ([]ops.OrderBy, error) { +func (f *Filter) GetOrdering() []ops.OrderBy { return f.Source.GetOrdering() } @@ -123,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/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 2f9d574d99d..3057553984b 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" @@ -85,51 +86,44 @@ 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 { - if err == semantics.ErrNotSingleTable { + if errors.Is(err, semantics.ErrNotSingleTable) { 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, 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") - } - offset, err := h.FindCol(ctx, col, false) - if err != nil { - return 0, err + panic(vterrors.VT13001("cannot push non-ColName expression to horizon")) } + offset := h.FindCol(ctx, col, false) if offset < 0 { - return 0, errNoNewColumns + panic(errNoNewColumns) } - return offset, nil + return offset } var errNoNewColumns = vterrors.VT13001("can't add new columns to Horizon") @@ -151,41 +145,41 @@ 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) { +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) { - 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, 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/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 + } } 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/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 693b7a75d8e..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 @@ -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/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/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 79a6980b937..12929c69e7d 100644 --- a/go/vt/vtgate/planbuilder/operators/limit.go +++ b/go/vt/vtgate/planbuilder/operators/limit.go @@ -47,32 +47,28 @@ 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, error) { +func (l *Limit) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { 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) } -func (l *Limit) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (l *Limit) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { 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) } -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/offset_planning.go b/go/vt/vtgate/planbuilder/operators/offset_planning.go index 502df5e9c82..7e7be49874a 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] @@ -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 } @@ -88,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. @@ -109,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 } @@ -155,7 +149,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 @@ -170,11 +164,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 a1cfbfd0cc0..4e58fca9214 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,25 +116,25 @@ 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) { - 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, 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) { - 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 -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 @@ -151,13 +164,10 @@ 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 - } +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 87bf9d9e12f..379011d99d9 100644 --- a/go/vt/vtgate/planbuilder/operators/ops/op.go +++ b/go/vt/vtgate/planbuilder/operators/ops/op.go @@ -42,18 +42,18 @@ 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, error) + 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) + GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr + GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs 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 07f82239728..813f091acbe 100644 --- a/go/vt/vtgate/planbuilder/operators/ordering.go +++ b/go/vt/vtgate/planbuilder/operators/ordering.go @@ -53,41 +53,34 @@ 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, error) { +func (o *Ordering) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int { 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) } -func (o *Ordering) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (o *Ordering) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { 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) } -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 { +func (o *Ordering) planOffsets(ctx *plancontext.PlanningContext) { 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,14 +89,9 @@ 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) } - - return nil } func (o *Ordering) ShortDescription() string { diff --git a/go/vt/vtgate/planbuilder/operators/phases.go b/go/vt/vtgate/planbuilder/operators/phases.go index 1eecc595c8f..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 @@ -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 686950ba56d..027c95a6e10 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 { @@ -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 { @@ -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) @@ -247,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) { @@ -291,24 +288,32 @@ 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) { +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, 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( @@ -318,16 +323,10 @@ 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, err := p.FindCol(ctx, expr, false) - if err != nil { - return 0, err - } + offset := p.FindCol(ctx, expr, false) if offset >= 0 { return offset, nil } @@ -355,10 +354,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) @@ -385,24 +381,20 @@ 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) { +func (p *Projection) GetColumns(*plancontext.PlanningContext) []*sqlparser.AliasedExpr { 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 { @@ -414,13 +406,13 @@ func (p *Projection) GetSelectExprs(*plancontext.PlanningContext) (sqlparser.Sel } output = append(output, ae) } - return output, nil + return output default: panic("unknown type") } } -func (p *Projection) GetOrdering() ([]ops.OrderBy, error) { +func (p *Projection) GetOrdering() []ops.OrderBy { return p.Source.GetOrdering() } @@ -544,7 +536,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 } @@ -573,10 +565,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 { @@ -586,10 +578,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 @@ -602,15 +591,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/query_planning.go b/go/vt/vtgate/planbuilder/operators/query_planning.go index b4ecbdb4a7f..4554b09fcb7 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 } @@ -861,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/querygraph.go b/go/vt/vtgate/planbuilder/operators/querygraph.go index f384607fe10..c30bfb8fd21 100644 --- a/go/vt/vtgate/planbuilder/operators/querygraph.go +++ b/go/vt/vtgate/planbuilder/operators/querygraph.go @@ -176,15 +176,15 @@ 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, 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 799b101892b..47b42f124d6 100644 --- a/go/vt/vtgate/planbuilder/operators/route.go +++ b/go/vt/vtgate/planbuilder/operators/route.go @@ -518,29 +518,22 @@ 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) { 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 { @@ -550,16 +543,13 @@ 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 - } + offset := r.FindCol(ctx, expr.Expr, true) if offset != -1 { - return offset, nil + return offset } } @@ -568,24 +558,24 @@ 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 + 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, error) - addColumnsWithoutPushing(ctx *plancontext.PlanningContext, reuse bool, addToGroupBy []bool, exprs []*sqlparser.AliasedExpr) ([]int, error) + addColumnWithoutPushing(ctx *plancontext.PlanningContext, expr *sqlparser.AliasedExpr, addToGroupBy bool) int + addColumnsWithoutPushing(ctx *plancontext.PlanningContext, reuse bool, addToGroupBy []bool, exprs []*sqlparser.AliasedExpr) []int isDerived() bool } @@ -635,16 +625,13 @@ 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: 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)), @@ -659,19 +646,19 @@ 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) } -func (r *Route) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (r *Route) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { 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) } -func (r *Route) GetOrdering() ([]ops.OrderBy, error) { +func (r *Route) GetOrdering() []ops.OrderBy { return r.Source.GetOrdering() } @@ -695,31 +682,25 @@ 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, err := r.Source.GetOrdering() - if err != nil || len(ordering) == 0 { - return err + ordering := r.Source.GetOrdering() + if len(ordering) == 0 { + return } for _, order := range ordering { 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,16 +709,11 @@ 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) } - - return nil } func weightStringFor(expr sqlparser.Expr) sqlparser.Expr { @@ -759,11 +735,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/route_planning.go b/go/vt/vtgate/planbuilder/operators/route_planning.go index 720f2f56480..6ecfce5bf07 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 } @@ -365,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 } @@ -621,10 +615,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/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.go b/go/vt/vtgate/planbuilder/operators/subquery.go index 55fcba6cd3b..958b873e9e9 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery.go +++ b/go/vt/vtgate/planbuilder/operators/subquery.go @@ -54,22 +54,18 @@ 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 { - 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 } } - return nil } func (sq *SubQuery) OuterExpressionsNeeded(ctx *plancontext.PlanningContext, outer ops.Operator) (result []*sqlparser.ColName, err error) { @@ -129,7 +125,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() } @@ -171,28 +167,24 @@ 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, error) { +func (sq *SubQuery) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, exprs *sqlparser.AliasedExpr) int { 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) } -func (sq *SubQuery) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (sq *SubQuery) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { 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 a2fba977436..fc2fc823fb4 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() } @@ -71,24 +71,23 @@ 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, error) { +func (sqc *SubQueryContainer) AddColumn(ctx *plancontext.PlanningContext, reuseExisting bool, addToGroupBy bool, exprs *sqlparser.AliasedExpr) int { 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) } -func (sqc *SubQueryContainer) GetColumns(ctx *plancontext.PlanningContext) ([]*sqlparser.AliasedExpr, error) { +func (sqc *SubQueryContainer) GetColumns(ctx *plancontext.PlanningContext) []*sqlparser.AliasedExpr { 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/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 } } diff --git a/go/vt/vtgate/planbuilder/operators/table.go b/go/vt/vtgate/planbuilder/operators/table.go index 226cbc6dea7..4809a60c988 100644 --- a/go/vt/vtgate/planbuilder/operators/table.go +++ b/go/vt/vtgate/planbuilder/operators/table.go @@ -61,39 +61,39 @@ 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, 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) { +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) { - 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) { +func (to *Table) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { 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 { @@ -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..b926aefdd04 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 @@ -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) { @@ -183,21 +180,14 @@ 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 - } - + offset := u.FindCol(ctx, expr.Expr, false) if offset >= 0 { - return offset, nil + return offset } } - cols, err := u.GetColumns(ctx) - if err != nil { - return 0, err - } + cols := u.GetColumns(ctx) switch e := expr.Expr.(type) { case *sqlparser.ColName: @@ -206,9 +196,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 +206,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 +228,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 { @@ -255,22 +242,19 @@ func (u *Union) addWeightStringToOffset(ctx *plancontext.PlanningContext, argIdx return } -func (u *Union) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) (int, error) { - columns, err := u.GetColumns(ctx) - if err != nil { - return 0, err - } +func (u *Union) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int { + columns := u.GetColumns(ctx) 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) { +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 { @@ -279,41 +263,33 @@ 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) { +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/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 b0317ab332c..49a0ffd1409 100644 --- a/go/vt/vtgate/planbuilder/operators/vindex.go +++ b/go/vt/vtgate/planbuilder/operators/vindex.go @@ -62,17 +62,14 @@ 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 - } + offset := v.FindCol(ctx, ae.Expr, true) if offset > -1 { - return offset, nil + return offset } } @@ -86,26 +83,26 @@ 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) { - 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) { +func (v *Vindex) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs { 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 { @@ -124,31 +121,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 +153,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.