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 row_alias syntax added in MySQL 8.0.19. #15510

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading