Skip to content

Commit

Permalink
Support UPDATE SET DEFAULT (#157)
Browse files Browse the repository at this point in the history
* Implement DEFAULT in UPDATE

* Update testdata

* Remove unused implementation

* Fix doc comment
  • Loading branch information
apstndb authored Oct 24, 2024
1 parent a61894d commit 2fef70c
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 83 deletions.
8 changes: 4 additions & 4 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2608,11 +2608,11 @@ type Update struct {

// UpdateItem is SET clause items in UPDATE.
//
// {{.Path | sqlJoin "."}} = {{.Expr | sql}}
// {{.Path | sqlJoin "."}} = {{.DefaultExpr | sql}}
type UpdateItem struct {
// pos = Path[0].pos
// end = Expr.end
// end = DefaultExpr.end

Path []*Ident // len(Path) > 0
Expr Expr
Path []*Ident // len(Path) > 0
DefaultExpr *DefaultExpr
}
2 changes: 1 addition & 1 deletion ast/pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,4 +929,4 @@ func (u *Update) Pos() token.Pos { return u.Update }
func (u *Update) End() token.Pos { return u.Where.End() }

func (u *UpdateItem) Pos() token.Pos { return u.Path[0].Pos() }
func (u *UpdateItem) End() token.Pos { return u.Expr.End() }
func (u *UpdateItem) End() token.Pos { return u.DefaultExpr.End() }
7 changes: 1 addition & 6 deletions ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1343,10 +1343,5 @@ func (u *Update) SQL() string {
}

func (u *UpdateItem) SQL() string {
sql := u.Path[0].SQL()
for _, id := range u.Path[1:] {
sql += "." + id.SQL()
}
sql += " = " + u.Expr.SQL()
return sql
return sqlJoin(u.Path, ".") + " = " + u.DefaultExpr.SQL()
}
6 changes: 3 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3542,11 +3542,11 @@ func (p *Parser) parseUpdate(pos token.Pos) *ast.Update {
func (p *Parser) parseUpdateItem() *ast.UpdateItem {
path := p.parseIdentOrPath()
p.expect("=")
expr := p.parseExpr()
defaultExpr := p.parseDefaultExpr()

return &ast.UpdateItem{
Path: path,
Expr: expr,
Path: path,
DefaultExpr: defaultExpr,
}
}

Expand Down
2 changes: 1 addition & 1 deletion testdata/input/dml/update.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
update foo set foo = bar, bar = foo where foo = 1
update foo set foo = bar, bar = foo, baz = DEFAULT where foo = 1
52 changes: 37 additions & 15 deletions testdata/result/dml/update.sql.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- update.sql
update foo set foo = bar, bar = foo where foo = 1
update foo set foo = bar, bar = foo, baz = DEFAULT where foo = 1
--- AST
&ast.Update{
Update: 0,
Expand All @@ -18,10 +18,14 @@ update foo set foo = bar, bar = foo where foo = 1
Name: "foo",
},
},
Expr: &ast.Ident{
NamePos: 21,
NameEnd: 24,
Name: "bar",
DefaultExpr: &ast.DefaultExpr{
DefaultPos: -1,
Default: false,
Expr: &ast.Ident{
NamePos: 21,
NameEnd: 24,
Name: "bar",
},
},
},
&ast.UpdateItem{
Expand All @@ -32,25 +36,43 @@ update foo set foo = bar, bar = foo where foo = 1
Name: "bar",
},
},
Expr: &ast.Ident{
NamePos: 32,
NameEnd: 35,
Name: "foo",
DefaultExpr: &ast.DefaultExpr{
DefaultPos: -1,
Default: false,
Expr: &ast.Ident{
NamePos: 32,
NameEnd: 35,
Name: "foo",
},
},
},
&ast.UpdateItem{
Path: []*ast.Ident{
&ast.Ident{
NamePos: 37,
NameEnd: 40,
Name: "baz",
},
},
DefaultExpr: &ast.DefaultExpr{
DefaultPos: 43,
Default: true,
Expr: nil,
},
},
},
Where: &ast.Where{
Where: 36,
Where: 51,
Expr: &ast.BinaryExpr{
Op: "=",
Left: &ast.Ident{
NamePos: 42,
NameEnd: 45,
NamePos: 57,
NameEnd: 60,
Name: "foo",
},
Right: &ast.IntLiteral{
ValuePos: 48,
ValueEnd: 49,
ValuePos: 63,
ValueEnd: 64,
Base: 10,
Value: "1",
},
Expand All @@ -59,4 +81,4 @@ update foo set foo = bar, bar = foo where foo = 1
}

--- SQL
UPDATE foo SET foo = bar, bar = foo WHERE foo = 1
UPDATE foo SET foo = bar, bar = foo, baz = DEFAULT WHERE foo = 1
42 changes: 23 additions & 19 deletions testdata/result/dml/update_as.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,31 @@ update foo as F set F.foo = F.bar + 1 where foo = F.bar
Name: "foo",
},
},
Expr: &ast.BinaryExpr{
Op: "+",
Left: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 28,
NameEnd: 29,
Name: "F",
},
&ast.Ident{
NamePos: 30,
NameEnd: 33,
Name: "bar",
DefaultExpr: &ast.DefaultExpr{
DefaultPos: -1,
Default: false,
Expr: &ast.BinaryExpr{
Op: "+",
Left: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 28,
NameEnd: 29,
Name: "F",
},
&ast.Ident{
NamePos: 30,
NameEnd: 33,
Name: "bar",
},
},
},
},
Right: &ast.IntLiteral{
ValuePos: 36,
ValueEnd: 37,
Base: 10,
Value: "1",
Right: &ast.IntLiteral{
ValuePos: 36,
ValueEnd: 37,
Base: 10,
Value: "1",
},
},
},
},
Expand Down
52 changes: 37 additions & 15 deletions testdata/result/statement/update.sql.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- update.sql
update foo set foo = bar, bar = foo where foo = 1
update foo set foo = bar, bar = foo, baz = DEFAULT where foo = 1
--- AST
&ast.Update{
Update: 0,
Expand All @@ -18,10 +18,14 @@ update foo set foo = bar, bar = foo where foo = 1
Name: "foo",
},
},
Expr: &ast.Ident{
NamePos: 21,
NameEnd: 24,
Name: "bar",
DefaultExpr: &ast.DefaultExpr{
DefaultPos: -1,
Default: false,
Expr: &ast.Ident{
NamePos: 21,
NameEnd: 24,
Name: "bar",
},
},
},
&ast.UpdateItem{
Expand All @@ -32,25 +36,43 @@ update foo set foo = bar, bar = foo where foo = 1
Name: "bar",
},
},
Expr: &ast.Ident{
NamePos: 32,
NameEnd: 35,
Name: "foo",
DefaultExpr: &ast.DefaultExpr{
DefaultPos: -1,
Default: false,
Expr: &ast.Ident{
NamePos: 32,
NameEnd: 35,
Name: "foo",
},
},
},
&ast.UpdateItem{
Path: []*ast.Ident{
&ast.Ident{
NamePos: 37,
NameEnd: 40,
Name: "baz",
},
},
DefaultExpr: &ast.DefaultExpr{
DefaultPos: 43,
Default: true,
Expr: nil,
},
},
},
Where: &ast.Where{
Where: 36,
Where: 51,
Expr: &ast.BinaryExpr{
Op: "=",
Left: &ast.Ident{
NamePos: 42,
NameEnd: 45,
NamePos: 57,
NameEnd: 60,
Name: "foo",
},
Right: &ast.IntLiteral{
ValuePos: 48,
ValueEnd: 49,
ValuePos: 63,
ValueEnd: 64,
Base: 10,
Value: "1",
},
Expand All @@ -59,4 +81,4 @@ update foo set foo = bar, bar = foo where foo = 1
}

--- SQL
UPDATE foo SET foo = bar, bar = foo WHERE foo = 1
UPDATE foo SET foo = bar, bar = foo, baz = DEFAULT WHERE foo = 1
42 changes: 23 additions & 19 deletions testdata/result/statement/update_as.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,31 @@ update foo as F set F.foo = F.bar + 1 where foo = F.bar
Name: "foo",
},
},
Expr: &ast.BinaryExpr{
Op: "+",
Left: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 28,
NameEnd: 29,
Name: "F",
},
&ast.Ident{
NamePos: 30,
NameEnd: 33,
Name: "bar",
DefaultExpr: &ast.DefaultExpr{
DefaultPos: -1,
Default: false,
Expr: &ast.BinaryExpr{
Op: "+",
Left: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 28,
NameEnd: 29,
Name: "F",
},
&ast.Ident{
NamePos: 30,
NameEnd: 33,
Name: "bar",
},
},
},
},
Right: &ast.IntLiteral{
ValuePos: 36,
ValueEnd: 37,
Base: 10,
Value: "1",
Right: &ast.IntLiteral{
ValuePos: 36,
ValueEnd: 37,
Base: 10,
Value: "1",
},
},
},
},
Expand Down

0 comments on commit 2fef70c

Please sign in to comment.