Skip to content

Commit

Permalink
Add support for row_alias syntax added in MySQL 8.0.19. (#15510)
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Manan Gupta <manan@planetscale.com>
Co-authored-by: Manan Gupta <manan@planetscale.com>
  • Loading branch information
arthurschreiber and GuptaManan100 authored Apr 4, 2024
1 parent 6c2c895 commit 97890a7
Show file tree
Hide file tree
Showing 12 changed files with 7,377 additions and 7,185 deletions.
6 changes: 6 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ type (
Columns Columns
Rows InsertRows
OnDup OnDup
RowAlias *RowAlias
}

// Ignore represents whether ignore was specified or not
Expand Down Expand Up @@ -3492,6 +3493,11 @@ type SetExpr struct {
// OnDup represents an ON DUPLICATE KEY clause.
type OnDup UpdateExprs

type RowAlias struct {
TableName IdentifierCS
Columns Columns
}

// IdentifierCI is a case insensitive SQL identifier. It will be escaped with
// backquotes if necessary.
type IdentifierCI struct {
Expand Down
14 changes: 14 additions & 0 deletions go/vt/sqlparser/ast_clone.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion go/vt/sqlparser/ast_copy_on_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion go/vt/sqlparser/ast_equals.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,20 @@ func (node *Stream) Format(buf *TrackedBuffer) {
func (node *Insert) Format(buf *TrackedBuffer) {
switch node.Action {
case InsertAct:
buf.astPrintf(node, "%s %v%sinto %v%v%v %v%v",
buf.astPrintf(node, "%s %v%sinto %v%v%v %v%v%v",
InsertStr,
node.Comments, node.Ignore.ToString(),
node.Table.Expr, node.Partitions, node.Columns, node.Rows, node.OnDup)
node.Table.Expr, node.Partitions, node.Columns, node.Rows, node.RowAlias, node.OnDup)
case ReplaceAct:
buf.astPrintf(node, "%s %v%sinto %v%v%v %v%v",
buf.astPrintf(node, "%s %v%sinto %v%v%v %v%v%v",
ReplaceStr,
node.Comments, node.Ignore.ToString(),
node.Table.Expr, node.Partitions, node.Columns, node.Rows, node.OnDup)
node.Table.Expr, node.Partitions, node.Columns, node.Rows, node.RowAlias, node.OnDup)
default:
buf.astPrintf(node, "%s %v%sinto %v%v%v %v%v",
buf.astPrintf(node, "%s %v%sinto %v%v%v %v%v%v",
"Unkown Insert Action",
node.Comments, node.Ignore.ToString(),
node.Table.Expr, node.Partitions, node.Columns, node.Rows, node.OnDup)
node.Table.Expr, node.Partitions, node.Columns, node.Rows, node.RowAlias, node.OnDup)
}

}
Expand Down Expand Up @@ -2010,6 +2010,18 @@ func (node OnDup) Format(buf *TrackedBuffer) {
buf.astPrintf(node, " on duplicate key update %v", UpdateExprs(node))
}

func (node *RowAlias) Format(buf *TrackedBuffer) {
if node == nil {
return
}

buf.astPrintf(node, " as %v", node.TableName)

if node.Columns != nil {
buf.astPrintf(node, " %v", node.Columns)
}
}

// Format formats the node.
func (node IdentifierCI) Format(buf *TrackedBuffer) {
if node.IsEmpty() {
Expand Down
20 changes: 20 additions & 0 deletions go/vt/sqlparser/ast_format_fast.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions go/vt/sqlparser/ast_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions go/vt/sqlparser/ast_visit.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions go/vt/sqlparser/cached_size.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,12 @@ var (
input: "insert /* bool in on duplicate */ into a values (1, 2, 3) on duplicate key update b = values(b), c = d",
}, {
input: "insert /* bool in on duplicate */ into a values (1, 2, 3) on duplicate key update b = values(a.b), c = d",
}, {
input: "insert into a values (1, 2, 3) as `a_values`",
output: "insert into a values (1, 2, 3) as a_values",
}, {
input: "insert into a values (1, 2, 3) as `a_values` (`foo`, bar, baz)",
output: "insert into a values (1, 2, 3) as a_values (foo, bar, baz)",
}, {
input: "insert /* bool expression on duplicate */ into a values (1, 2) on duplicate key update b = func(a), c = a > d",
}, {
Expand Down
Loading

0 comments on commit 97890a7

Please sign in to comment.