Skip to content

Commit ac0ab09

Browse files
authored
Fix SELECT keyword inside functions (#200)
1 parent c884bd5 commit ac0ab09

File tree

4 files changed

+239
-0
lines changed

4 files changed

+239
-0
lines changed

parser/parser_column.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ func (p *Parser) parseColumnExpr(pos Pos) (Expr, error) { //nolint:funlen
366366
return p.parseColumnCastExpr(pos)
367367
case p.matchKeyword(KeywordCase):
368368
return p.parseColumnCaseExpr(pos)
369+
case p.matchKeyword(KeywordSelect):
370+
return p.parseSelectQuery(pos)
369371
case p.matchKeyword(KeywordExtract):
370372
return p.parseColumnExtractExpr(pos)
371373
case p.matchTokenKind(TokenKindIdent):
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Origin SQL:
2+
SELECT
3+
*,
4+
CASE
5+
WHEN EXISTS(SELECT 1
6+
FROM table_name
7+
WHERE col1 = '999999999')
8+
THEN 'then'
9+
ELSE 'else'
10+
END as check_result
11+
FROM table_name
12+
WHERE col1 = '123456789'
13+
14+
15+
-- Format SQL:
16+
SELECT *, CASE WHEN EXISTS(SELECT 1 FROM table_name WHERE col1 = '999999999') THEN 'then' ELSE 'else' END AS check_result FROM table_name WHERE col1 = '123456789';
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
[
2+
{
3+
"SelectPos": 0,
4+
"StatementEnd": 205,
5+
"With": null,
6+
"Top": null,
7+
"HasDistinct": false,
8+
"DistinctOn": null,
9+
"SelectItems": [
10+
{
11+
"Expr": {
12+
"Name": "*",
13+
"QuoteType": 0,
14+
"NamePos": 11,
15+
"NameEnd": 11
16+
},
17+
"Modifiers": [],
18+
"Alias": null
19+
},
20+
{
21+
"Expr": {
22+
"CasePos": 18,
23+
"EndPos": 0,
24+
"Expr": null,
25+
"Whens": [
26+
{
27+
"WhenPos": 31,
28+
"ThenPos": 110,
29+
"When": {
30+
"Name": {
31+
"Name": "EXISTS",
32+
"QuoteType": 1,
33+
"NamePos": 36,
34+
"NameEnd": 42
35+
},
36+
"Params": {
37+
"LeftParenPos": 42,
38+
"RightParenPos": 100,
39+
"Items": {
40+
"ListPos": 43,
41+
"ListEnd": 99,
42+
"HasDistinct": false,
43+
"Items": [
44+
{
45+
"Expr": {
46+
"SelectPos": 43,
47+
"StatementEnd": 99,
48+
"With": null,
49+
"Top": null,
50+
"HasDistinct": false,
51+
"DistinctOn": null,
52+
"SelectItems": [
53+
{
54+
"Expr": {
55+
"NumPos": 50,
56+
"NumEnd": 51,
57+
"Literal": "1",
58+
"Base": 10
59+
},
60+
"Modifiers": [],
61+
"Alias": null
62+
}
63+
],
64+
"From": {
65+
"FromPos": 56,
66+
"Expr": {
67+
"Table": {
68+
"TablePos": 61,
69+
"TableEnd": 71,
70+
"Alias": null,
71+
"Expr": {
72+
"Database": null,
73+
"Table": {
74+
"Name": "table_name",
75+
"QuoteType": 1,
76+
"NamePos": 61,
77+
"NameEnd": 71
78+
}
79+
},
80+
"HasFinal": false
81+
},
82+
"StatementEnd": 71,
83+
"SampleRatio": null,
84+
"HasFinal": false
85+
}
86+
},
87+
"ArrayJoin": null,
88+
"Window": null,
89+
"Prewhere": null,
90+
"Where": {
91+
"WherePos": 76,
92+
"Expr": {
93+
"LeftExpr": {
94+
"Name": "col1",
95+
"QuoteType": 1,
96+
"NamePos": 82,
97+
"NameEnd": 86
98+
},
99+
"Operation": "=",
100+
"RightExpr": {
101+
"LiteralPos": 90,
102+
"LiteralEnd": 99,
103+
"Literal": "999999999"
104+
},
105+
"HasGlobal": false,
106+
"HasNot": false
107+
}
108+
},
109+
"GroupBy": null,
110+
"WithTotal": false,
111+
"Having": null,
112+
"OrderBy": null,
113+
"LimitBy": null,
114+
"Limit": null,
115+
"Settings": null,
116+
"Format": null,
117+
"UnionAll": null,
118+
"UnionDistinct": null,
119+
"Except": null
120+
},
121+
"Alias": null
122+
}
123+
]
124+
},
125+
"ColumnArgList": null
126+
}
127+
},
128+
"Then": {
129+
"LiteralPos": 116,
130+
"LiteralEnd": 120,
131+
"Literal": "then"
132+
},
133+
"ElsePos": 0,
134+
"Else": null
135+
}
136+
],
137+
"ElsePos": 130,
138+
"Else": {
139+
"LiteralPos": 136,
140+
"LiteralEnd": 140,
141+
"Literal": "else"
142+
}
143+
},
144+
"Modifiers": [],
145+
"Alias": {
146+
"Name": "check_result",
147+
"QuoteType": 1,
148+
"NamePos": 153,
149+
"NameEnd": 165
150+
}
151+
}
152+
],
153+
"From": {
154+
"FromPos": 166,
155+
"Expr": {
156+
"Table": {
157+
"TablePos": 171,
158+
"TableEnd": 181,
159+
"Alias": null,
160+
"Expr": {
161+
"Database": null,
162+
"Table": {
163+
"Name": "table_name",
164+
"QuoteType": 1,
165+
"NamePos": 171,
166+
"NameEnd": 181
167+
}
168+
},
169+
"HasFinal": false
170+
},
171+
"StatementEnd": 181,
172+
"SampleRatio": null,
173+
"HasFinal": false
174+
}
175+
},
176+
"ArrayJoin": null,
177+
"Window": null,
178+
"Prewhere": null,
179+
"Where": {
180+
"WherePos": 182,
181+
"Expr": {
182+
"LeftExpr": {
183+
"Name": "col1",
184+
"QuoteType": 1,
185+
"NamePos": 188,
186+
"NameEnd": 192
187+
},
188+
"Operation": "=",
189+
"RightExpr": {
190+
"LiteralPos": 196,
191+
"LiteralEnd": 205,
192+
"Literal": "123456789"
193+
},
194+
"HasGlobal": false,
195+
"HasNot": false
196+
}
197+
},
198+
"GroupBy": null,
199+
"WithTotal": false,
200+
"Having": null,
201+
"OrderBy": null,
202+
"LimitBy": null,
203+
"Limit": null,
204+
"Settings": null,
205+
"Format": null,
206+
"UnionAll": null,
207+
"UnionDistinct": null,
208+
"Except": null
209+
}
210+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
SELECT
2+
*,
3+
CASE
4+
WHEN EXISTS(SELECT 1
5+
FROM table_name
6+
WHERE col1 = '999999999')
7+
THEN 'then'
8+
ELSE 'else'
9+
END as check_result
10+
FROM table_name
11+
WHERE col1 = '123456789'

0 commit comments

Comments
 (0)