Skip to content

Commit

Permalink
planbuilder: clean up code (#14657)
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay authored Dec 4, 2023
1 parent cd9693c commit 27f0009
Show file tree
Hide file tree
Showing 56 changed files with 1,345 additions and 1,948 deletions.
33 changes: 11 additions & 22 deletions go/vt/vtgate/planbuilder/operator_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ import (
"vitess.io/vitess/go/vt/vtgate/engine/opcode"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/vt/vtgate/planbuilder/operators"
"vitess.io/vitess/go/vt/vtgate/planbuilder/operators/ops"
"vitess.io/vitess/go/vt/vtgate/planbuilder/operators/rewrite"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)

func transformToLogicalPlan(ctx *plancontext.PlanningContext, op ops.Operator) (logicalPlan, error) {
func transformToLogicalPlan(ctx *plancontext.PlanningContext, op operators.Operator) (logicalPlan, error) {
switch op := op.(type) {
case *operators.Route:
return transformRoutePlan(ctx, op)
Expand Down Expand Up @@ -190,10 +188,7 @@ func transformSubQuery(ctx *plancontext.PlanningContext, op *operators.SubQuery)
return newUncorrelatedSubquery(op.FilterType, op.SubqueryValueName, op.HasValuesName, inner, outer), nil
}

lhsCols, err := op.OuterExpressionsNeeded(ctx, op.Outer)
if err != nil {
return nil, err
}
lhsCols := op.OuterExpressionsNeeded(ctx, op.Outer)
return newSemiJoin(outer, inner, op.Vars, lhsCols), nil
}

Expand Down Expand Up @@ -251,7 +246,7 @@ func transformAggregator(ctx *plancontext.PlanningContext, op *operators.Aggrega
oa.groupByKeys = append(oa.groupByKeys, &engine.GroupByParams{
KeyCol: groupBy.ColOffset,
WeightStringCol: groupBy.WSOffset,
Expr: groupBy.AsAliasedExpr().Expr,
Expr: groupBy.SimplifiedExpr,
Type: typ,
})
}
Expand Down Expand Up @@ -435,7 +430,7 @@ func routeToEngineRoute(ctx *plancontext.PlanningContext, op *operators.Route, h
}

rp := newRoutingParams(ctx, op.Routing.OpCode())
err = op.Routing.UpdateRoutingParams(ctx, rp)
op.Routing.UpdateRoutingParams(ctx, rp)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -544,7 +539,7 @@ func buildRouteLogicalPlan(ctx *plancontext.PlanningContext, op *operators.Route
}

func buildInsertLogicalPlan(
rb *operators.Route, op ops.Operator, stmt *sqlparser.Insert,
rb *operators.Route, op operators.Operator, stmt *sqlparser.Insert,
hints *queryHints,
) (logicalPlan, error) {
ins := op.(*operators.Insert)
Expand Down Expand Up @@ -635,16 +630,13 @@ func dmlFormatter(buf *sqlparser.TrackedBuffer, node sqlparser.SQLNode) {
func buildUpdateLogicalPlan(
ctx *plancontext.PlanningContext,
rb *operators.Route,
dmlOp ops.Operator,
dmlOp operators.Operator,
stmt *sqlparser.Update,
hints *queryHints,
) (logicalPlan, error) {
upd := dmlOp.(*operators.Update)
rp := newRoutingParams(ctx, rb.Routing.OpCode())
err := rb.Routing.UpdateRoutingParams(ctx, rp)
if err != nil {
return nil, err
}
rb.Routing.UpdateRoutingParams(ctx, rp)
edml := &engine.DML{
Query: generateQuery(stmt),
TableNames: []string{upd.VTable.Name.String()},
Expand All @@ -670,15 +662,12 @@ func buildUpdateLogicalPlan(
func buildDeleteLogicalPlan(
ctx *plancontext.PlanningContext,
rb *operators.Route,
dmlOp ops.Operator,
dmlOp operators.Operator,
hints *queryHints,
) (logicalPlan, error) {
del := dmlOp.(*operators.Delete)
rp := newRoutingParams(ctx, rb.Routing.OpCode())
err := rb.Routing.UpdateRoutingParams(ctx, rp)
if err != nil {
return nil, err
}
rb.Routing.UpdateRoutingParams(ctx, rp)
edml := &engine.DML{
Query: generateQuery(del.AST),
TableNames: []string{del.VTable.Name.String()},
Expand Down Expand Up @@ -739,7 +728,7 @@ func updateSelectedVindexPredicate(op *operators.Route) sqlparser.Expr {

func getAllTableNames(op *operators.Route) ([]string, error) {
tableNameMap := map[string]any{}
err := rewrite.Visit(op, func(op ops.Operator) error {
err := operators.Visit(op, func(op operators.Operator) error {
tbl, isTbl := op.(*operators.Table)
var name string
if isTbl {
Expand All @@ -764,7 +753,7 @@ func getAllTableNames(op *operators.Route) ([]string, error) {
}

func transformUnionPlan(ctx *plancontext.PlanningContext, op *operators.Union) (logicalPlan, error) {
sources, err := slice.MapWithError(op.Sources, func(src ops.Operator) (logicalPlan, error) {
sources, err := slice.MapWithError(op.Sources, func(src operators.Operator) (logicalPlan, error) {
plan, err := transformToLogicalPlan(ctx, src)
if err != nil {
return nil, err
Expand Down
9 changes: 4 additions & 5 deletions go/vt/vtgate/planbuilder/operators/SQL_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/planbuilder/operators/ops"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/semantics"
)
Expand All @@ -33,15 +32,15 @@ type (
ctx *plancontext.PlanningContext
stmt sqlparser.Statement
tableNames []string
dmlOperator ops.Operator
dmlOperator Operator
}
)

func (qb *queryBuilder) asSelectStatement() sqlparser.SelectStatement {
return qb.stmt.(sqlparser.SelectStatement)
}

func ToSQL(ctx *plancontext.PlanningContext, op ops.Operator) (_ sqlparser.Statement, _ ops.Operator, err error) {
func ToSQL(ctx *plancontext.PlanningContext, op Operator) (_ sqlparser.Statement, _ Operator, err error) {
defer PanicHandler(&err)

q := &queryBuilder{ctx: ctx}
Expand Down Expand Up @@ -347,7 +346,7 @@ func stripDownQuery(from, to sqlparser.SelectStatement) {
}

// buildQuery recursively builds the query into an AST, from an operator tree
func buildQuery(op ops.Operator, qb *queryBuilder) {
func buildQuery(op Operator, qb *queryBuilder) {
switch op := op.(type) {
case *Table:
buildTable(op, qb)
Expand Down Expand Up @@ -415,7 +414,7 @@ func buildUpdate(op *Update, qb *queryBuilder) {
}

type OpWithAST interface {
ops.Operator
Operator
Statement() sqlparser.Statement
}

Expand Down
Loading

0 comments on commit 27f0009

Please sign in to comment.