Skip to content

Commit

Permalink
refactor: make delete work well with SQL builder
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Feb 12, 2024
1 parent 0775b6a commit 3e91727
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 26 deletions.
65 changes: 65 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2662,3 +2662,68 @@ func (node *Delete) GetLimit() *Limit {
func (node *Delete) SetLimit(limit *Limit) {
node.Limit = limit
}

func (node *Select) GetFrom() []TableExpr {
return node.From
}

func (node *Select) SetFrom(exprs []TableExpr) {
node.From = exprs
}

func (node *Select) GetWherePredicate() Expr {
if node.Where == nil {
return nil
}
return node.Where.Expr
}

func (node *Select) SetWherePredicate(expr Expr) {
node.Where = &Where{
Type: WhereClause,
Expr: expr,
}
}
func (node *Delete) GetFrom() []TableExpr {
return node.TableExprs
}

func (node *Delete) SetFrom(exprs []TableExpr) {
node.TableExprs = exprs
}

func (node *Delete) GetWherePredicate() Expr {
if node.Where == nil {
return nil
}
return node.Where.Expr
}

func (node *Delete) SetWherePredicate(expr Expr) {
node.Where = &Where{
Type: WhereClause,
Expr: expr,
}
}

func (node *Update) GetFrom() []TableExpr {
return node.TableExprs
}

func (node *Update) SetFrom(exprs []TableExpr) {
node.TableExprs = exprs
}

func (node *Update) GetWherePredicate() Expr {
if node.Where == nil {
return nil
}
return node.Where.Expr
}

func (node *Update) SetWherePredicate(expr Expr) {
node.Where = &Where{
Type: WhereClause,
Expr: expr,
}
}
52 changes: 26 additions & 26 deletions go/vt/vtgate/planbuilder/operators/SQL_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,23 +203,35 @@ func (qb *queryBuilder) unionWith(other *queryBuilder, distinct bool) {
}
}

type FromStatement interface {
GetFrom() []sqlparser.TableExpr
SetFrom([]sqlparser.TableExpr)
GetWherePredicate() sqlparser.Expr
SetWherePredicate(sqlparser.Expr)
}

var _ FromStatement = (*sqlparser.Select)(nil)
var _ FromStatement = (*sqlparser.Update)(nil)
var _ FromStatement = (*sqlparser.Delete)(nil)

func (qb *queryBuilder) joinInnerWith(other *queryBuilder, onCondition sqlparser.Expr) {
sel := qb.stmt.(*sqlparser.Select)
otherSel := other.stmt.(*sqlparser.Select)
sel.From = append(sel.From, otherSel.From...)
sel.SelectExprs = append(sel.SelectExprs, otherSel.SelectExprs...)
stmt := qb.stmt.(FromStatement)
otherStmt := other.stmt.(FromStatement)
stmt.SetFrom(append(stmt.GetFrom(), otherStmt.GetFrom()...))

var predicate sqlparser.Expr
if sel.Where != nil {
predicate = sel.Where.Expr
if sel, isSel := stmt.(*sqlparser.Select); isSel {
otherSel := otherStmt.(*sqlparser.Select)
sel.SelectExprs = append(sel.SelectExprs, otherSel.SelectExprs...)
}
if otherSel.Where != nil {

predicate := stmt.GetWherePredicate()
if otherPredicate := otherStmt.GetWherePredicate(); otherPredicate != nil {
predExprs := sqlparser.SplitAndExpression(nil, predicate)
otherExprs := sqlparser.SplitAndExpression(nil, otherSel.Where.Expr)
otherExprs := sqlparser.SplitAndExpression(nil, otherPredicate)
predicate = qb.ctx.SemTable.AndExpressions(append(predExprs, otherExprs...)...)
}
if predicate != nil {
sel.Where = &sqlparser.Where{Type: sqlparser.WhereClause, Expr: predicate}
stmt.SetWherePredicate(predicate)
}

qb.addPredicate(onCondition)
Expand Down Expand Up @@ -388,25 +400,13 @@ func buildQuery(op Operator, qb *queryBuilder) {
}

func buildDelete(op *Delete, qb *queryBuilder) {
buildQuery(op.Source, qb)
// currently the qb builds a select query underneath.
// Will take the `From` and `Where` from this select
// and create a delete statement.
// TODO: change it to directly produce `delete` statement.
sel, ok := qb.stmt.(*sqlparser.Select)
if !ok {
panic(vterrors.VT13001("expected a select here"))
qb.stmt = &sqlparser.Delete{
Ignore: op.Ignore,
Targets: sqlparser.TableNames{op.Target.Name},
}
buildQuery(op.Source, qb)

qb.dmlOperator = op
qb.stmt = &sqlparser.Delete{
Ignore: op.Ignore,
Targets: sqlparser.TableNames{op.Target.Name},
TableExprs: sel.From,
Where: sel.Where,
Limit: sel.Limit,
OrderBy: sel.OrderBy,
}
}

func buildUpdate(op *Update, qb *queryBuilder) {
Expand Down

0 comments on commit 3e91727

Please sign in to comment.