Skip to content

Commit

Permalink
refactor: move more code from logical plans to ops (#14287)
Browse files Browse the repository at this point in the history
  • Loading branch information
systay authored Oct 18, 2023
1 parent a5e819d commit 705deee
Show file tree
Hide file tree
Showing 34 changed files with 174 additions and 774 deletions.
43 changes: 0 additions & 43 deletions go/vt/vtgate/planbuilder/concatenate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ limitations under the License.
package planbuilder

import (
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/semantics"
)

type concatenate struct {
Expand All @@ -34,17 +30,6 @@ type concatenate struct {

var _ logicalPlan = (*concatenate)(nil)

// Wireup implements the logicalPlan interface
func (c *concatenate) Wireup(ctx *plancontext.PlanningContext) error {
for _, source := range c.sources {
err := source.Wireup(ctx)
if err != nil {
return err
}
}
return nil
}

// Primitive implements the logicalPlan interface
func (c *concatenate) Primitive() engine.Primitive {
var sources []engine.Primitive
Expand All @@ -54,31 +39,3 @@ func (c *concatenate) Primitive() engine.Primitive {

return engine.NewConcatenate(sources, c.noNeedToTypeCheck)
}

// Rewrite implements the logicalPlan interface
func (c *concatenate) Rewrite(inputs ...logicalPlan) error {
if len(inputs) != len(c.sources) {
return vterrors.VT13001("concatenate: wrong number of inputs")
}
c.sources = inputs
return nil
}

// ContainsTables implements the logicalPlan interface
func (c *concatenate) ContainsTables() semantics.TableSet {
var tableSet semantics.TableSet
for _, source := range c.sources {
tableSet = tableSet.Merge(source.ContainsTables())
}
return tableSet
}

// Inputs implements the logicalPlan interface
func (c *concatenate) Inputs() []logicalPlan {
return c.sources
}

// OutputColumns implements the logicalPlan interface
func (c *concatenate) OutputColumns() []sqlparser.SelectExpr {
return c.sources[0].OutputColumns()
}
8 changes: 0 additions & 8 deletions go/vt/vtgate/planbuilder/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,6 @@ func gen4DeleteStmtPlanner(
return nil, err
}

plan = pushCommentDirectivesOnPlan(plan, deleteStmt)

setLockOnAllSelect(plan)

if err := plan.Wireup(ctx); err != nil {
return nil, err
}

return newPlanResult(plan.Primitive(), operators.TablesUsed(op)...), nil
}

Expand Down
15 changes: 0 additions & 15 deletions go/vt/vtgate/planbuilder/distinct.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package planbuilder

import (
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
)

Expand Down Expand Up @@ -61,17 +60,3 @@ func (d *distinct) Primitive() engine.Primitive {
Truncate: truncate,
}
}

// Rewrite implements the logicalPlan interface
func (d *distinct) Rewrite(inputs ...logicalPlan) error {
if len(inputs) != 1 {
return vterrors.VT13001("distinct: wrong number of inputs")
}
d.input = inputs[0]
return nil
}

// Inputs implements the logicalPlan interface
func (d *distinct) Inputs() []logicalPlan {
return []logicalPlan{d.input}
}
13 changes: 0 additions & 13 deletions go/vt/vtgate/planbuilder/dml_planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,6 @@ func rewriteRoutedTables(stmt sqlparser.Statement, vschema plancontext.VSchema)
}, stmt)
}

func setLockOnAllSelect(plan logicalPlan) {
_, _ = visit(plan, func(plan logicalPlan) (bool, logicalPlan, error) {
switch node := plan.(type) {
case *route:
if node.Select.GetLock() == sqlparser.NoLock {
node.Select.SetLock(sqlparser.ShareModeLock)
}
return true, node, nil
}
return true, plan, nil
})
}

func generateQuery(statement sqlparser.Statement) string {
buf := sqlparser.NewTrackedBuffer(dmlFormatter)
statement.Format(buf)
Expand Down
37 changes: 0 additions & 37 deletions go/vt/vtgate/planbuilder/fk_cascade.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ limitations under the License.
package planbuilder

import (
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/semantics"
)

var _ logicalPlan = (*fkCascade)(nil)
Expand Down Expand Up @@ -50,36 +46,3 @@ func (fkc *fkCascade) Primitive() engine.Primitive {
Children: fkc.children,
}
}

// Wireup implements the logicalPlan interface
func (fkc *fkCascade) Wireup(ctx *plancontext.PlanningContext) error {
if err := fkc.parent.Wireup(ctx); err != nil {
return err
}
return fkc.selection.Wireup(ctx)
}

// Rewrite implements the logicalPlan interface
func (fkc *fkCascade) Rewrite(inputs ...logicalPlan) error {
if len(inputs) != 2 {
return vterrors.VT13001("fkCascade: wrong number of inputs")
}
fkc.parent = inputs[0]
fkc.selection = inputs[1]
return nil
}

// ContainsTables implements the logicalPlan interface
func (fkc *fkCascade) ContainsTables() semantics.TableSet {
return fkc.parent.ContainsTables()
}

// Inputs implements the logicalPlan interface
func (fkc *fkCascade) Inputs() []logicalPlan {
return []logicalPlan{fkc.parent, fkc.selection}
}

// OutputColumns implements the logicalPlan interface
func (fkc *fkCascade) OutputColumns() []sqlparser.SelectExpr {
return nil
}
46 changes: 0 additions & 46 deletions go/vt/vtgate/planbuilder/fk_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ limitations under the License.
package planbuilder

import (
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/semantics"
)

var _ logicalPlan = (*fkVerify)(nil)
Expand Down Expand Up @@ -59,45 +55,3 @@ func (fkc *fkVerify) Primitive() engine.Primitive {
Verify: verify,
}
}

// Wireup implements the logicalPlan interface
func (fkc *fkVerify) Wireup(ctx *plancontext.PlanningContext) error {
for _, v := range fkc.verify {
err := v.verify.Wireup(ctx)
if err != nil {
return err
}
}
return fkc.input.Wireup(ctx)
}

// Rewrite implements the logicalPlan interface
func (fkc *fkVerify) Rewrite(inputs ...logicalPlan) error {
if len(fkc.verify) != len(inputs)-1 {
return vterrors.VT13001("fkVerify: wrong number of inputs")
}
fkc.input = inputs[0]
for i := 1; i < len(inputs); i++ {
fkc.verify[i-1].verify = inputs[i]
}
return nil
}

// ContainsTables implements the logicalPlan interface
func (fkc *fkVerify) ContainsTables() semantics.TableSet {
return fkc.input.ContainsTables()
}

// Inputs implements the logicalPlan interface
func (fkc *fkVerify) Inputs() []logicalPlan {
inputs := []logicalPlan{fkc.input}
for _, v := range fkc.verify {
inputs = append(inputs, v.verify)
}
return inputs
}

// OutputColumns implements the logicalPlan interface
func (fkc *fkVerify) OutputColumns() []sqlparser.SelectExpr {
return nil
}
32 changes: 1 addition & 31 deletions go/vt/vtgate/planbuilder/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func gen4InsertStmtPlanner(version querypb.ExecuteOptions_PlannerVersion, insStm
if ks != nil {
if tables[0].AutoIncrement == nil && !ctx.SemTable.ForeignKeysPresent() {
plan := insertUnshardedShortcut(insStmt, ks, tables)
plan = pushCommentDirectivesOnPlan(plan, insStmt)
setCommentDirectivesOnPlan(plan, insStmt)
return newPlanResult(plan.Primitive(), operators.QualifiedTables(ks, tables)...), nil
}
}
Expand Down Expand Up @@ -81,14 +81,6 @@ func gen4InsertStmtPlanner(version querypb.ExecuteOptions_PlannerVersion, insStm
return nil, err
}

plan = pushCommentDirectivesOnPlan(plan, insStmt)

setLockOnAllSelect(plan)

if err := plan.Wireup(ctx); err != nil {
return nil, err
}

return newPlanResult(plan.Primitive(), operators.TablesUsed(op)...), nil
}

Expand Down Expand Up @@ -121,35 +113,13 @@ type insert struct {

var _ logicalPlan = (*insert)(nil)

func (i *insert) Wireup(ctx *plancontext.PlanningContext) error {
if i.source == nil {
return nil
}
return i.source.Wireup(ctx)
}

func (i *insert) Primitive() engine.Primitive {
if i.source != nil {
i.eInsert.Input = i.source.Primitive()
}
return i.eInsert
}

func (i *insert) Inputs() []logicalPlan {
if i.source == nil {
return nil
}
return []logicalPlan{i.source}
}

func (i *insert) Rewrite(inputs ...logicalPlan) error {
panic("does not expect insert to get rewrite call")
}

func (i *insert) ContainsTables() semantics.TableSet {
panic("does not expect insert to get contains tables call")
}

func (i *insert) OutputColumns() []sqlparser.SelectExpr {
panic("does not expect insert to get output columns call")
}
51 changes: 0 additions & 51 deletions go/vt/vtgate/planbuilder/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ limitations under the License.
package planbuilder

import (
"fmt"

"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/semantics"
)

var _ logicalPlan = (*join)(nil)
Expand All @@ -50,15 +45,6 @@ type join struct {
LHSColumns []*sqlparser.ColName
}

// Wireup implements the logicalPlan interface
func (j *join) Wireup(ctx *plancontext.PlanningContext) error {
err := j.Left.Wireup(ctx)
if err != nil {
return err
}
return j.Right.Wireup(ctx)
}

// Primitive implements the logicalPlan interface
func (j *join) Primitive() engine.Primitive {
return &engine.Join{
Expand All @@ -69,40 +55,3 @@ func (j *join) Primitive() engine.Primitive {
Opcode: j.Opcode,
}
}

// Inputs implements the logicalPlan interface
func (j *join) Inputs() []logicalPlan {
return []logicalPlan{j.Left, j.Right}
}

// Rewrite implements the logicalPlan interface
func (j *join) Rewrite(inputs ...logicalPlan) error {
if len(inputs) != 2 {
return vterrors.VT13001(fmt.Sprintf("wrong number of children in join rewrite, got: %d, expect: 2", len(inputs)))
}
j.Left = inputs[0]
j.Right = inputs[1]
return nil
}

// ContainsTables implements the logicalPlan interface
func (j *join) ContainsTables() semantics.TableSet {
return j.Left.ContainsTables().Merge(j.Right.ContainsTables())
}

// OutputColumns implements the logicalPlan interface
func (j *join) OutputColumns() []sqlparser.SelectExpr {
return getOutputColumnsFromJoin(j.Cols, j.Left.OutputColumns(), j.Right.OutputColumns())
}

func getOutputColumnsFromJoin(ints []int, lhs []sqlparser.SelectExpr, rhs []sqlparser.SelectExpr) (cols []sqlparser.SelectExpr) {
for _, col := range ints {
if col < 0 {
col *= -1
cols = append(cols, lhs[col-1])
} else {
cols = append(cols, rhs[col-1])
}
}
return
}
Loading

0 comments on commit 705deee

Please sign in to comment.