Skip to content

Commit

Permalink
Add support for Update Multi Table (#15211)
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Co-authored-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
harshit-gangal and systay authored Feb 14, 2024
1 parent fc5b6d0 commit a0ce8bc
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 165 deletions.
23 changes: 21 additions & 2 deletions changelog/20.0/20.0.0/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
### Table of Contents

- **[Major Changes](#major-changes)**
- **[Query Serving](#query-serving)**
- **[Query Compatibility](#query-compatibility)**
- [Vindex Hints](#vindex-hints)
- [Update with Limit Support](#update-limit)
- [Update with Multi Table Support](#multi-table-update)
- **[Minor Changes](#minor-changes)**


## <a id="major-changes"/>Major Changes


### <a id="query-serving"/>Query Serving
### <a id="query-compatibility"/>Query Compatibility

#### <a id="vindex-hints"/> Vindex Hints

Expand All @@ -25,5 +27,22 @@ Example:

For more information about Vindex hints and its usage, please consult the documentation.

#### <a id="update-limit"/> Update with Limit Support

Support is added for sharded update with limit.

Example: `update t1 set t1.foo = 'abc', t1.bar = 23 where t1.baz > 5 limit 1`

More details about how it works is available in [MySQL Docs](https://dev.mysql.com/doc/refman/8.0/en/update.html)

#### <a id="multi-table-update"/> Update with Multi Table Support

Support is added for sharded multi-table update with column update on single target table using multiple table join.

Example: `update t1 join t2 on t1.id = t2.id join t3 on t1.col = t3.col set t1.baz = 'abc', t1.apa = 23 where t3.foo = 5 and t2.bar = 7`

More details about how it works is available in [MySQL Docs](https://dev.mysql.com/doc/refman/8.0/en/update.html)


## <a id="minor-changes"/>Minor Changes

18 changes: 13 additions & 5 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ type (
Commented
}

OrderAndLimit interface {
AddOrder(*Order)
SetLimit(*Limit)
}

// SelectStatement any SELECT statement.
SelectStatement interface {
Statement
InsertRows
OrderAndLimit
iSelectStatement()
AddOrder(*Order)
SetOrderBy(OrderBy)
GetOrderBy() OrderBy
GetLimit() *Limit
SetLimit(*Limit)
GetLock() Lock
SetLock(lock Lock)
SetInto(into *SelectInto)
Expand All @@ -72,6 +73,9 @@ type (
GetColumns() SelectExprs
Commented
IsDistinct() bool
GetOrderBy() OrderBy
SetOrderBy(OrderBy)
GetLimit() *Limit
}

// DDLStatement represents any DDL Statement
Expand Down Expand Up @@ -712,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
81 changes: 81 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2605,3 +2605,84 @@ func MultiTable(node []TableExpr) bool {
_, singleTbl := node[0].(*AliasedTableExpr)
return !singleTbl
}

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

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

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

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,
}
}
2 changes: 2 additions & 0 deletions go/vt/vterrors/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var (
VT03029 = errorWithState("VT03029", vtrpcpb.Code_INVALID_ARGUMENT, WrongValueCountOnRow, "column count does not match value count with the row for vindex '%s'", "The number of columns you want to insert do not match the number of columns of your SELECT query.")
VT03030 = errorWithState("VT03030", vtrpcpb.Code_INVALID_ARGUMENT, WrongValueCountOnRow, "lookup column count does not match value count with the row (columns, count): (%v, %d)", "The number of columns you want to insert do not match the number of columns of your SELECT query.")
VT03031 = errorWithoutState("VT03031", vtrpcpb.Code_INVALID_ARGUMENT, "EXPLAIN is only supported for single keyspace", "EXPLAIN has to be sent down as a single query to the underlying MySQL, and this is not possible if it uses tables from multiple keyspaces")
VT03032 = errorWithState("VT03031", vtrpcpb.Code_INVALID_ARGUMENT, NonUpdateableTable, "the target table %s of the UPDATE is not updatable", "You cannot update a table that is not a real MySQL table.")

VT05001 = errorWithState("VT05001", vtrpcpb.Code_NOT_FOUND, DbDropExists, "cannot drop database '%s'; database does not exists", "The given database does not exist; Vitess cannot drop it.")
VT05002 = errorWithState("VT05002", vtrpcpb.Code_NOT_FOUND, BadDb, "cannot alter database '%s'; unknown database", "The given database does not exist; Vitess cannot alter it.")
Expand Down Expand Up @@ -141,6 +142,7 @@ var (
VT03029,
VT03030,
VT03031,
VT03032,
VT05001,
VT05002,
VT05003,
Expand Down
Loading

0 comments on commit a0ce8bc

Please sign in to comment.