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
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 Serving

#### <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,20 @@ type (
Commented
}

// SelectStatement any SELECT statement.
SelectStatement interface {
Statement
InsertRows
iSelectStatement()
OrderAndLimit interface {
AddOrder(*Order)
SetOrderBy(OrderBy)
GetOrderBy() OrderBy
GetLimit() *Limit
SetLimit(*Limit)
}

// SelectStatement any SELECT statement.
SelectStatement interface {
Statement
InsertRows
OrderAndLimit
iSelectStatement()
GetLock() Lock
SetLock(lock Lock)
SetInto(into *SelectInto)
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
117 changes: 117 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2605,3 +2605,120 @@
_, singleTbl := node[0].(*AliasedTableExpr)
return !singleTbl
}

func (node *Select) AddFrom(tbl TableExpr) {
node.From = append(node.From, tbl)

Check warning on line 2610 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2609-L2610

Added lines #L2609 - L2610 were not covered by tests
}

func (node *Update) AddFrom(tbl TableExpr) {
node.TableExprs = append(node.TableExprs, tbl)

Check warning on line 2614 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2613-L2614

Added lines #L2613 - L2614 were not covered by tests
}

func (node *Delete) AddFrom(tbl TableExpr) {
node.TableExprs = append(node.TableExprs, tbl)

Check warning on line 2618 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2617-L2618

Added lines #L2617 - L2618 were not covered by tests
}

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

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

Check warning on line 2626 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2625-L2626

Added lines #L2625 - L2626 were not covered by tests
}

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

Check warning on line 2630 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2629-L2630

Added lines #L2629 - L2630 were not covered by tests
}

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

Check warning on line 2634 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2633-L2634

Added lines #L2633 - L2634 were not covered by tests
}

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

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

Check warning on line 2642 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2641-L2642

Added lines #L2641 - L2642 were not covered by tests
}

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

Check warning on line 2646 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2645-L2646

Added lines #L2645 - L2646 were not covered by tests
}

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

Check warning on line 2650 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2649-L2650

Added lines #L2649 - L2650 were not covered by tests
}

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

Check warning on line 2654 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2653-L2654

Added lines #L2653 - L2654 were not covered by tests
}

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

Check warning on line 2714 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2712-L2714

Added lines #L2712 - L2714 were not covered by tests
}
return node.Where.Expr

Check warning on line 2716 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2716

Added line #L2716 was not covered by tests
}

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

Check warning on line 2722 in go/vt/sqlparser/ast_funcs.go

View check run for this annotation

Codecov / codecov/patch

go/vt/sqlparser/ast_funcs.go#L2719-L2722

Added lines #L2719 - L2722 were not covered by tests
}
}
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
Loading