Skip to content

Commit a0ea6a5

Browse files
authored
Support DMLs with named schema (#262)
* Support FQNs in DMLs * Update testdata
1 parent 7a98aad commit a0ea6a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+811
-150
lines changed

ast/ast.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3656,7 +3656,7 @@ type Insert struct {
36563656

36573657
InsertOrType InsertOrType
36583658

3659-
TableName *Ident
3659+
TableName *Path
36603660
Columns []*Ident
36613661
Input InsertInput
36623662
ThenReturn *ThenReturn // optional
@@ -3719,7 +3719,7 @@ type Delete struct {
37193719

37203720
Delete token.Pos // position of "DELETE" keyword
37213721

3722-
TableName *Ident
3722+
TableName *Path
37233723
As *AsAlias // optional
37243724
Where *Where
37253725
ThenReturn *ThenReturn // optional
@@ -3736,7 +3736,7 @@ type Update struct {
37363736

37373737
Update token.Pos // position of "UPDATE" keyword
37383738

3739-
TableName *Ident
3739+
TableName *Path
37403740
As *AsAlias // optional
37413741
Updates []*UpdateItem // len(Updates) > 0
37423742
Where *Where

parser.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -4754,7 +4754,7 @@ func (p *Parser) parseInsert(pos token.Pos) *ast.Insert {
47544754
p.nextToken()
47554755
}
47564756

4757-
name := p.parseIdent()
4757+
name := p.parsePath()
47584758

47594759
p.expect("(")
47604760
var columns []*ast.Ident
@@ -4849,7 +4849,7 @@ func (p *Parser) parseDelete(pos token.Pos) *ast.Delete {
48494849
p.nextToken()
48504850
}
48514851

4852-
name := p.parseIdent()
4852+
name := p.parsePath()
48534853
as := p.tryParseAsAlias(withOptionalAs)
48544854
where := p.parseWhere()
48554855
thenReturn := p.tryParseThenReturn()
@@ -4864,7 +4864,7 @@ func (p *Parser) parseDelete(pos token.Pos) *ast.Delete {
48644864
}
48654865

48664866
func (p *Parser) parseUpdate(pos token.Pos) *ast.Update {
4867-
name := p.parseIdent()
4867+
name := p.parsePath()
48684868
as := p.tryParseAsAlias(withOptionalAs)
48694869

48704870
p.expect("SET")

testdata/input/dml/delete_fqn.sql

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
delete sch1.foo where foo = 1 and bar = 2
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
insert sch1.foo (foo, bar, baz)
2+
values (1, 2, 3),
3+
(4, 5, 6)

testdata/input/dml/update_fqn.sql

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
update sch1.foo set foo = bar, bar = foo, baz = DEFAULT where foo = 1

testdata/result/dml/!bad_insert.sql.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ syntax error: testdata/input/dml/!bad_insert.sql:2:1: expected beginning of simp
1010

1111
--- AST
1212
&ast.Insert{
13-
TableName: &ast.Ident{
14-
NamePos: 7,
15-
NameEnd: 10,
16-
Name: "foo",
13+
TableName: &ast.Path{
14+
Idents: []*ast.Ident{
15+
&ast.Ident{
16+
NamePos: 7,
17+
NameEnd: 10,
18+
Name: "foo",
19+
},
20+
},
1721
},
1822
Columns: []*ast.Ident{
1923
&ast.Ident{

testdata/result/dml/delete _from.sql.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
delete from foo where foo = 1 and bar = 2
33
--- AST
44
&ast.Delete{
5-
TableName: &ast.Ident{
6-
NamePos: 12,
7-
NameEnd: 15,
8-
Name: "foo",
5+
TableName: &ast.Path{
6+
Idents: []*ast.Ident{
7+
&ast.Ident{
8+
NamePos: 12,
9+
NameEnd: 15,
10+
Name: "foo",
11+
},
12+
},
913
},
1014
Where: &ast.Where{
1115
Where: 16,

testdata/result/dml/delete.sql.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
delete foo where foo = 1 and bar = 2
33
--- AST
44
&ast.Delete{
5-
TableName: &ast.Ident{
6-
NamePos: 7,
7-
NameEnd: 10,
8-
Name: "foo",
5+
TableName: &ast.Path{
6+
Idents: []*ast.Ident{
7+
&ast.Ident{
8+
NamePos: 7,
9+
NameEnd: 10,
10+
Name: "foo",
11+
},
12+
},
913
},
1014
Where: &ast.Where{
1115
Where: 11,

testdata/result/dml/delete_as.sql.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
delete foo as F where F.foo = 1
33
--- AST
44
&ast.Delete{
5-
TableName: &ast.Ident{
6-
NamePos: 7,
7-
NameEnd: 10,
8-
Name: "foo",
5+
TableName: &ast.Path{
6+
Idents: []*ast.Ident{
7+
&ast.Ident{
8+
NamePos: 7,
9+
NameEnd: 10,
10+
Name: "foo",
11+
},
12+
},
913
},
1014
As: &ast.AsAlias{
1115
As: 11,
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--- delete_fqn.sql
2+
delete sch1.foo where foo = 1 and bar = 2
3+
--- AST
4+
&ast.Delete{
5+
TableName: &ast.Path{
6+
Idents: []*ast.Ident{
7+
&ast.Ident{
8+
NamePos: 7,
9+
NameEnd: 11,
10+
Name: "sch1",
11+
},
12+
&ast.Ident{
13+
NamePos: 12,
14+
NameEnd: 15,
15+
Name: "foo",
16+
},
17+
},
18+
},
19+
Where: &ast.Where{
20+
Where: 16,
21+
Expr: &ast.BinaryExpr{
22+
Op: "AND",
23+
Left: &ast.BinaryExpr{
24+
Op: "=",
25+
Left: &ast.Ident{
26+
NamePos: 22,
27+
NameEnd: 25,
28+
Name: "foo",
29+
},
30+
Right: &ast.IntLiteral{
31+
ValuePos: 28,
32+
ValueEnd: 29,
33+
Base: 10,
34+
Value: "1",
35+
},
36+
},
37+
Right: &ast.BinaryExpr{
38+
Op: "=",
39+
Left: &ast.Ident{
40+
NamePos: 34,
41+
NameEnd: 37,
42+
Name: "bar",
43+
},
44+
Right: &ast.IntLiteral{
45+
ValuePos: 40,
46+
ValueEnd: 41,
47+
Base: 10,
48+
Value: "2",
49+
},
50+
},
51+
},
52+
},
53+
}
54+
55+
--- SQL
56+
DELETE FROM sch1.foo WHERE foo = 1 AND bar = 2

testdata/result/dml/delete_then_return.sql.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
delete foo where foo = 1 and bar = 2 then return *
33
--- AST
44
&ast.Delete{
5-
TableName: &ast.Ident{
6-
NamePos: 7,
7-
NameEnd: 10,
8-
Name: "foo",
5+
TableName: &ast.Path{
6+
Idents: []*ast.Ident{
7+
&ast.Ident{
8+
NamePos: 7,
9+
NameEnd: 10,
10+
Name: "foo",
11+
},
12+
},
913
},
1014
Where: &ast.Where{
1115
Where: 11,

testdata/result/dml/insert_cte_select.sql.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ with cte AS (select 1 as foo, 2 as bar)
44
select *
55
--- AST
66
&ast.Insert{
7-
TableName: &ast.Ident{
8-
NamePos: 7,
9-
NameEnd: 10,
10-
Name: "foo",
7+
TableName: &ast.Path{
8+
Idents: []*ast.Ident{
9+
&ast.Ident{
10+
NamePos: 7,
11+
NameEnd: 10,
12+
Name: "foo",
13+
},
14+
},
1115
},
1216
Columns: []*ast.Ident{
1317
&ast.Ident{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
--- insert_fqn_values.sql
2+
insert sch1.foo (foo, bar, baz)
3+
values (1, 2, 3),
4+
(4, 5, 6)
5+
--- AST
6+
&ast.Insert{
7+
TableName: &ast.Path{
8+
Idents: []*ast.Ident{
9+
&ast.Ident{
10+
NamePos: 7,
11+
NameEnd: 11,
12+
Name: "sch1",
13+
},
14+
&ast.Ident{
15+
NamePos: 12,
16+
NameEnd: 15,
17+
Name: "foo",
18+
},
19+
},
20+
},
21+
Columns: []*ast.Ident{
22+
&ast.Ident{
23+
NamePos: 17,
24+
NameEnd: 20,
25+
Name: "foo",
26+
},
27+
&ast.Ident{
28+
NamePos: 22,
29+
NameEnd: 25,
30+
Name: "bar",
31+
},
32+
&ast.Ident{
33+
NamePos: 27,
34+
NameEnd: 30,
35+
Name: "baz",
36+
},
37+
},
38+
Input: &ast.ValuesInput{
39+
Values: 32,
40+
Rows: []*ast.ValuesRow{
41+
&ast.ValuesRow{
42+
Lparen: 39,
43+
Rparen: 47,
44+
Exprs: []*ast.DefaultExpr{
45+
&ast.DefaultExpr{
46+
DefaultPos: -1,
47+
Expr: &ast.IntLiteral{
48+
ValuePos: 40,
49+
ValueEnd: 41,
50+
Base: 10,
51+
Value: "1",
52+
},
53+
},
54+
&ast.DefaultExpr{
55+
DefaultPos: -1,
56+
Expr: &ast.IntLiteral{
57+
ValuePos: 43,
58+
ValueEnd: 44,
59+
Base: 10,
60+
Value: "2",
61+
},
62+
},
63+
&ast.DefaultExpr{
64+
DefaultPos: -1,
65+
Expr: &ast.IntLiteral{
66+
ValuePos: 46,
67+
ValueEnd: 47,
68+
Base: 10,
69+
Value: "3",
70+
},
71+
},
72+
},
73+
},
74+
&ast.ValuesRow{
75+
Lparen: 57,
76+
Rparen: 65,
77+
Exprs: []*ast.DefaultExpr{
78+
&ast.DefaultExpr{
79+
DefaultPos: -1,
80+
Expr: &ast.IntLiteral{
81+
ValuePos: 58,
82+
ValueEnd: 59,
83+
Base: 10,
84+
Value: "4",
85+
},
86+
},
87+
&ast.DefaultExpr{
88+
DefaultPos: -1,
89+
Expr: &ast.IntLiteral{
90+
ValuePos: 61,
91+
ValueEnd: 62,
92+
Base: 10,
93+
Value: "5",
94+
},
95+
},
96+
&ast.DefaultExpr{
97+
DefaultPos: -1,
98+
Expr: &ast.IntLiteral{
99+
ValuePos: 64,
100+
ValueEnd: 65,
101+
Base: 10,
102+
Value: "6",
103+
},
104+
},
105+
},
106+
},
107+
},
108+
},
109+
}
110+
111+
--- SQL
112+
INSERT INTO sch1.foo (foo, bar, baz) VALUES (1, 2, 3), (4, 5, 6)

testdata/result/dml/insert_into_values.sql.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ values (1, 2, 3),
44
(4, 5, 6)
55
--- AST
66
&ast.Insert{
7-
TableName: &ast.Ident{
8-
NamePos: 12,
9-
NameEnd: 15,
10-
Name: "foo",
7+
TableName: &ast.Path{
8+
Idents: []*ast.Ident{
9+
&ast.Ident{
10+
NamePos: 12,
11+
NameEnd: 15,
12+
Name: "foo",
13+
},
14+
},
1115
},
1216
Columns: []*ast.Ident{
1317
&ast.Ident{

testdata/result/dml/insert_or_ignore.sql.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ INSERT OR IGNORE INTO foo
44
--- AST
55
&ast.Insert{
66
InsertOrType: "IGNORE",
7-
TableName: &ast.Ident{
8-
NamePos: 22,
9-
NameEnd: 25,
10-
Name: "foo",
7+
TableName: &ast.Path{
8+
Idents: []*ast.Ident{
9+
&ast.Ident{
10+
NamePos: 22,
11+
NameEnd: 25,
12+
Name: "foo",
13+
},
14+
},
1115
},
1216
Columns: []*ast.Ident{
1317
&ast.Ident{

0 commit comments

Comments
 (0)