Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Update Multi Table #15211

Merged
merged 12 commits into from
Feb 14, 2024
Prev Previous commit
Next Next commit
build update using interfaces
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Feb 12, 2024
commit 0775b6a6c5edc6e40f4531df51ad523d8d76ec4f
4 changes: 4 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,10 @@ type (
IndexType int8
)

var _ OrderAndLimit = (*Select)(nil)
var _ OrderAndLimit = (*Update)(nil)
var _ OrderAndLimit = (*Delete)(nil)

func (*Union) iStatement() {}
func (*Select) iStatement() {}
func (*Stream) iStatement() {}
Expand Down
40 changes: 40 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2622,3 +2622,43 @@ func (node *Update) AddFrom(tbl TableExpr) {
func (node *Delete) AddFrom(tbl TableExpr) {
node.TableExprs = append(node.TableExprs, tbl)
}

func (node *Update) AddOrder(order *Order) {
node.OrderBy = append(node.OrderBy, order)
}

func (node *Update) SetOrderBy(by OrderBy) {
node.OrderBy = by
}

func (node *Update) GetOrderBy() OrderBy {
return node.OrderBy
}

func (node *Update) GetLimit() *Limit {
return node.Limit
}

func (node *Update) SetLimit(limit *Limit) {
node.Limit = limit
}

func (node *Delete) AddOrder(order *Order) {
node.OrderBy = append(node.OrderBy, order)
}

func (node *Delete) SetOrderBy(by OrderBy) {
node.OrderBy = by
}

func (node *Delete) GetOrderBy() OrderBy {
return node.OrderBy
}

func (node *Delete) GetLimit() *Limit {
return node.Limit
}

func (node *Delete) SetLimit(limit *Limit) {
node.Limit = limit
}
23 changes: 5 additions & 18 deletions go/vt/vtgate/planbuilder/operators/SQL_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,26 +411,13 @@ func buildDelete(op *Delete, qb *queryBuilder) {

func buildUpdate(op *Update, qb *queryBuilder) {
updExprs := getUpdateExprs(op)

buildQuery(op.Source, qb)
// currently the qb builds a select query underneath.
// Will take the `From` and `Where` from this select
// and create a update statement.
// TODO: change it to directly produce `update` statement.
sel, ok := qb.stmt.(*sqlparser.Select)
if !ok {
panic(vterrors.VT13001("expected a select here"))
upd := &sqlparser.Update{
Ignore: op.Ignore,
Exprs: updExprs,
}

qb.stmt = upd
qb.dmlOperator = op
qb.stmt = &sqlparser.Update{
Ignore: op.Ignore,
TableExprs: sel.From,
Exprs: updExprs,
Where: sel.Where,
Limit: sel.Limit,
OrderBy: sel.OrderBy,
}
buildQuery(op.Source, qb)
}

func getUpdateExprs(op *Update) sqlparser.UpdateExprs {
Expand Down