Skip to content

Commit

Permalink
refactor: cleaner cloning API
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Jun 7, 2024
1 parent 87ecae7 commit 26e2359
Show file tree
Hide file tree
Showing 17 changed files with 27 additions and 22 deletions.
5 changes: 5 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2776,3 +2776,8 @@ func (lock Lock) GetHighestOrderLock(newLock Lock) Lock {
}
return lock
}

// Clone returns a deep copy of the SQLNode, typed as the original type
func Clone[K SQLNode](x K) K {
return CloneSQLNode(x).(K)
}
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/aggregation_pushing.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ func splitAvgAggregations(ctx *plancontext.PlanningContext, aggr *Aggregator) (O

outputColumn := aeWrap(col.Expr)
outputColumn.As = sqlparser.NewIdentifierCI(col.ColumnName())
proj.addUnexploredExpr(sqlparser.CloneRefOfAliasedExpr(col), calcExpr)
proj.addUnexploredExpr(sqlparser.Clone(col), calcExpr)
col.Expr = sumExpr
found := false
for aggrOffset, aggregation := range aggr.Aggregations {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (p *joinPusher) countStar(ctx *plancontext.PlanningContext) (*sqlparser.Ali
// It returns the expression of the aggregation as it should be used in the parent Aggregator.
func (p *joinPusher) addAggr(ctx *plancontext.PlanningContext, aggr Aggr) sqlparser.Expr {
copyAggr := aggr
expr := sqlparser.CloneExpr(aggr.Original.Expr)
expr := sqlparser.Clone(aggr.Original.Expr)
copyAggr.Original = aeWrap(expr)
// copy dependencies so we can keep track of which side expressions need to be pushed to
ctx.SemTable.Direct[expr] = p.tableID
Expand All @@ -291,7 +291,7 @@ func (p *joinPusher) pushThroughAggr(aggr Aggr) {
// It returns the expression of the GroupBy as it should be used in the parent Aggregator.
func (p *joinPusher) addGrouping(ctx *plancontext.PlanningContext, gb GroupBy) sqlparser.Expr {
copyGB := gb
expr := sqlparser.CloneExpr(gb.Inner)
expr := sqlparser.Clone(gb.Inner)
// copy dependencies so we can keep track of which side expressions need to be pushed to
ctx.SemTable.CopyDependencies(gb.Inner, expr)
// if the column exists in the selection then copy it down to the pushed aggregator operator.
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func createOperatorFromDelete(ctx *plancontext.PlanningContext, deleteStmt *sqlp
return createDeleteWithInputOp(ctx, deleteStmt)
}

delClone := sqlparser.CloneRefOfDelete(deleteStmt)
delClone := sqlparser.Clone(deleteStmt)
var vTbl *vindexes.Table
op, vTbl = createDeleteOperator(ctx, deleteStmt)

Expand Down Expand Up @@ -315,7 +315,7 @@ func addOrdering(ctx *plancontext.PlanningContext, orderBy sqlparser.OrderBy, op
continue
}
ordering.Order = append(ordering.Order, OrderBy{
Inner: sqlparser.CloneRefOfOrder(order),
Inner: sqlparser.Clone(order),
SimplifiedExpr: order.Expr,
})
}
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/horizon.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func newHorizon(src Operator, query sqlparser.SelectStatement) *Horizon {
func (h *Horizon) Clone(inputs []Operator) Operator {
klone := *h
klone.Source = inputs[0]
klone.ColumnAliases = sqlparser.CloneColumns(h.ColumnAliases)
klone.ColumnAliases = sqlparser.Clone(h.ColumnAliases)
klone.Columns = slices.Clone(h.Columns)
klone.ColumnsOffset = slices.Clone(h.ColumnsOffset)
klone.QP = h.QP
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func createOperatorFromInsert(ctx *plancontext.PlanningContext, ins *sqlparser.I

delStmt := &sqlparser.Delete{
Comments: ins.Comments,
TableExprs: sqlparser.TableExprs{sqlparser.CloneRefOfAliasedTableExpr(ins.Table)},
TableExprs: sqlparser.TableExprs{sqlparser.Clone(ins.Table)},
Where: sqlparser.NewWhere(sqlparser.WhereClause, whereExpr),
}
delOp := createOpFromStmt(ctx, delStmt, false, "")
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Limit struct {
func (l *Limit) Clone(inputs []Operator) Operator {
return &Limit{
Source: inputs[0],
AST: sqlparser.CloneRefOfLimit(l.AST),
AST: sqlparser.Clone(l.AST),
Top: l.Top,
Pushed: l.Pushed,
}
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ func createDMLWithInput(ctx *plancontext.PlanningContext, op, src Operator, in *
targetQT := targetTable.QTable
qt := &QueryTable{
ID: targetQT.ID,
Alias: sqlparser.CloneRefOfAliasedTableExpr(targetQT.Alias),
Table: sqlparser.CloneTableName(targetQT.Table),
Alias: sqlparser.Clone(targetQT.Alias),
Table: sqlparser.Clone(targetQT.Table),
Predicates: []sqlparser.Expr{compExpr},
}

Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type (

func newProjExpr(ae *sqlparser.AliasedExpr) *ProjExpr {
return &ProjExpr{
Original: sqlparser.CloneRefOfAliasedExpr(ae),
Original: sqlparser.Clone(ae),
EvalExpr: ae.Expr,
ColExpr: ae.Expr,
}
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/projection_pushing.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func splitUnexploredExpression(
alias string,
dt *DerivedTable,
) applyJoinColumn {
original := sqlparser.CloneRefOfAliasedExpr(pe.Original)
original := sqlparser.Clone(pe.Original)
expr := pe.ColExpr

var colName *sqlparser.ColName
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/query_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func planQuery(ctx *plancontext.PlanningContext, root Operator) Operator {
var selExpr sqlparser.SelectExprs
if horizon, isHorizon := root.(*Horizon); isHorizon {
sel := sqlparser.GetFirstSelect(horizon.Query)
selExpr = sqlparser.CloneSelectExprs(sel.SelectExprs)
selExpr = sqlparser.Clone(sel.SelectExprs)
}

output := runPhases(ctx, root)
Expand Down Expand Up @@ -252,7 +252,7 @@ func tryPushLimit(ctx *plancontext.PlanningContext, in *Limit) (Operator, *Apply
}

func createPushedLimit(ctx *plancontext.PlanningContext, src Operator, orig *Limit) Operator {
pushedLimit := sqlparser.CloneRefOfLimit(orig.AST)
pushedLimit := sqlparser.Clone(orig.AST)
if pushedLimit.Offset != nil {
// we can't push down an offset, so we need to convert it to a rowcount
// by adding it to the already existing rowcount, and then let the LIMIT running on the vtgate do the rest
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/querygraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ func (qg *QueryGraph) AddPredicate(ctx *plancontext.PlanningContext, expr sqlpar
func (qt *QueryTable) Clone() *QueryTable {
return &QueryTable{
ID: qt.ID,
Alias: sqlparser.CloneRefOfAliasedTableExpr(qt.Alias),
Table: sqlparser.CloneTableName(qt.Table),
Alias: sqlparser.Clone(qt.Alias),
Table: sqlparser.Clone(qt.Table),
Predicates: qt.Predicates,
IsInfSchema: qt.IsInfSchema,
}
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/subquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (sq *SubQuery) Clone(inputs []Operator) Operator {
}
klone.JoinColumns = slices.Clone(sq.JoinColumns)
klone.Vars = maps.Clone(sq.Vars)
klone.Predicates = sqlparser.CloneExprs(sq.Predicates)
klone.Predicates = sqlparser.Clone(sq.Predicates)
return &klone
}

Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/subquery_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (sqb *SubQueryBuilder) pullOutValueSubqueries(
outerID semantics.TableSet,
isDML bool,
) (sqlparser.Expr, []*SubQuery) {
original := sqlparser.CloneExpr(expr)
original := sqlparser.Clone(expr)
sqe := extractSubQueries(ctx, expr, isDML)
if sqe == nil {
return nil, nil
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type (
func (to *Table) Clone([]Operator) Operator {
var columns []*sqlparser.ColName
for _, name := range to.Columns {
columns = append(columns, sqlparser.CloneRefOfColName(name))
columns = append(columns, sqlparser.Clone(name))
}
return &Table{
QTable: to.QTable,
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,15 @@ func createUpdateOperator(ctx *plancontext.PlanningContext, updStmt *sqlparser.U
// updClone is used in foreign key planning to create the selection statements to be used for verification and selection.
// If we encounter subqueries, we want to fix the updClone to use the replaced expression, so that the pulled out subquery's
// result is used everywhere instead of running the subquery multiple times, which is wasteful.
updClone := sqlparser.CloneRefOfUpdate(updStmt)
updClone := sqlparser.Clone(updStmt)
var tblInfo semantics.TableInfo
var err error
for idx, updExpr := range updStmt.Exprs {
expr, subqs := sqc.pullOutValueSubqueries(ctx, updExpr.Expr, outerID, true)
if len(subqs) == 0 {
expr = updExpr.Expr
} else {
updClone.Exprs[idx].Expr = sqlparser.CloneExpr(expr)
updClone.Exprs[idx].Expr = sqlparser.Clone(expr)
ctx.SemTable.UpdateChildFKExpr(updExpr, expr)
}
proj := newProjExpr(aeWrap(expr))
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func createUpsertOperator(ctx *plancontext.PlanningContext, ins *sqlparser.Inser
updOp := createOpFromStmt(ctx, upd, false, "")

// replan insert statement without on duplicate key update.
newInsert := sqlparser.CloneRefOfInsert(ins)
newInsert := sqlparser.Clone(ins)
newInsert.OnDup = nil
newInsert.Rows = sqlparser.Values{row}
insOp = createOpFromStmt(ctx, newInsert, false, "")
Expand Down

0 comments on commit 26e2359

Please sign in to comment.