Skip to content

Commit 902823e

Browse files
kyleconroyclaude
andauthored
Support row alias on INSERT ... VALUES/SET (#22) (#34)
* Support row alias on INSERT ... VALUES/SET (#22) MySQL 8.0.19 added a row alias on the inserted row (`INSERT ... VALUES (...) AS new ON DUPLICATE KEY UPDATE col = new.col`) to replace the deprecated `VALUES(col)` function inside the update list. Extend `InsertStmt` with `RowAlias` and `ColumnAliases`, and add the optional `AS row_alias [(col_alias_list)]` slot to the `VALUES` and `SET` forms of `InsertValues`. The `SELECT` form is intentionally left out: attaching the alias there propagates `AS` into the column-alias FOLLOW set inside `SELECT` and yields a shift/reduce conflict that goyacc rejects. REPLACE is left unchanged; it has no ON DUPLICATE KEY UPDATE, so a row alias is meaningless there. * Ignore parser/genkeyword build artifact The Makefile's genkeyword target produces a Go binary at parser/genkeyword that's used by `go generate`. It's a build output, not source. --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 845ca69 commit 902823e

5 files changed

Lines changed: 7716 additions & 7582 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ y.go
44
.idea/
55
.vscode/
66
coverage.txt
7+
parser/genkeyword

ast/dml.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,6 +2399,12 @@ type InsertStmt struct {
23992399
Priority mysql.PriorityEnum
24002400
OnDuplicate []*Assignment
24012401
Select ResultSetNode
2402+
// RowAlias is the alias for the inserted row, set by `INSERT ... AS row_alias`.
2403+
// MySQL 8.0.19+ syntax for use inside ON DUPLICATE KEY UPDATE.
2404+
RowAlias *CIStr
2405+
// ColumnAliases are the optional column aliases that follow RowAlias,
2406+
// set by `INSERT ... AS row_alias (col_alias_list)`.
2407+
ColumnAliases []*CIStr
24022408
// TableHints represents the table level Optimizer Hint for join type.
24032409
TableHints []*TableOptimizerHint
24042410
PartitionNames []CIStr
@@ -2516,6 +2522,20 @@ func (n *InsertStmt) Restore(ctx *format.RestoreCtx) error {
25162522
return fmt.Errorf("Incorrect type for InsertStmt.Select: %T", v)
25172523
}
25182524
}
2525+
if n.RowAlias != nil {
2526+
ctx.WriteKeyWord(" AS ")
2527+
ctx.WriteName(n.RowAlias.O)
2528+
if len(n.ColumnAliases) != 0 {
2529+
ctx.WritePlain("(")
2530+
for i, c := range n.ColumnAliases {
2531+
if i != 0 {
2532+
ctx.WritePlain(",")
2533+
}
2534+
ctx.WriteName(c.O)
2535+
}
2536+
ctx.WritePlain(")")
2537+
}
2538+
}
25192539
if n.OnDuplicate != nil {
25202540
ctx.WriteKeyWord(" ON DUPLICATE KEY UPDATE ")
25212541
for i, v := range n.OnDuplicate {

0 commit comments

Comments
 (0)