Skip to content

Commit a0eef0f

Browse files
authored
Fix SubQuery was being recognized as an ident (#132)
1 parent d03ad5b commit a0eef0f

File tree

4 files changed

+316
-1
lines changed

4 files changed

+316
-1
lines changed

parser/parser_table.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,16 @@ func (p *Parser) parseOrderExpr(pos Pos) (*OrderExpr, error) {
669669
}
670670

671671
var alias *Ident
672-
if p.tryConsumeKeyword(KeywordAs) != nil {
672+
if p.matchKeyword(KeywordAs) {
673+
// It should be a subquery instead of an order by alias if the `AS` is followed by `SELECT` keyword.
674+
if nextToken, err := p.lexer.peekToken(); err == nil && nextToken.ToString() == KeywordSelect {
675+
return &OrderExpr{
676+
OrderPos: pos,
677+
Expr: columnExpr,
678+
}, nil
679+
}
680+
// consume the `AS` keyword
681+
_ = p.lexer.consumeToken()
673682
alias, err = p.parseIdent()
674683
if err != nil {
675684
return nil, err
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE MATERIALIZED VIEW IF NOT EXISTS test_mv
2+
ENGINE = ReplacingMergeTree()
3+
PRIMARY KEY (id)
4+
ORDER BY (id)
5+
AS
6+
SELECT * FROM test_table;
7+
8+
CREATE MATERIALIZED VIEW IF NOT EXISTS test_mv
9+
ENGINE = ReplacingMergeTree()
10+
PRIMARY KEY (id)
11+
AS
12+
SELECT * FROM test_table;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- Origin SQL:
2+
CREATE MATERIALIZED VIEW IF NOT EXISTS test_mv
3+
ENGINE = ReplacingMergeTree()
4+
PRIMARY KEY (id)
5+
ORDER BY (id)
6+
AS
7+
SELECT * FROM test_table;
8+
9+
CREATE MATERIALIZED VIEW IF NOT EXISTS test_mv
10+
ENGINE = ReplacingMergeTree()
11+
PRIMARY KEY (id)
12+
AS
13+
SELECT * FROM test_table;
14+
15+
-- Format SQL:
16+
CREATE MATERIALIZED VIEW IF NOT EXISTS test_mv ENGINE = ReplacingMergeTree() PRIMARY KEY (id) ORDER BY (id) AS SELECT * FROM test_table;
17+
CREATE MATERIALIZED VIEW IF NOT EXISTS test_mv ENGINE = ReplacingMergeTree() PRIMARY KEY (id) AS SELECT * FROM test_table;
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
[
2+
{
3+
"CreatePos": 0,
4+
"StatementEnd": 135,
5+
"Name": {
6+
"Database": null,
7+
"Table": {
8+
"Name": "test_mv",
9+
"QuoteType": 1,
10+
"NamePos": 39,
11+
"NameEnd": 46
12+
}
13+
},
14+
"IfNotExists": true,
15+
"OnCluster": null,
16+
"Engine": {
17+
"EnginePos": 47,
18+
"EngineEnd": 106,
19+
"Name": "ReplacingMergeTree",
20+
"Params": {
21+
"LeftParenPos": 74,
22+
"RightParenPos": 75,
23+
"Items": {
24+
"ListPos": 75,
25+
"ListEnd": 75,
26+
"HasDistinct": false,
27+
"Items": []
28+
},
29+
"ColumnArgList": null
30+
},
31+
"PrimaryKey": {
32+
"PrimaryPos": 77,
33+
"Expr": {
34+
"LeftParenPos": 89,
35+
"RightParenPos": 92,
36+
"Items": {
37+
"ListPos": 90,
38+
"ListEnd": 92,
39+
"HasDistinct": false,
40+
"Items": [
41+
{
42+
"Expr": {
43+
"Name": "id",
44+
"QuoteType": 1,
45+
"NamePos": 90,
46+
"NameEnd": 92
47+
},
48+
"Alias": null
49+
}
50+
]
51+
},
52+
"ColumnArgList": null
53+
}
54+
},
55+
"PartitionBy": null,
56+
"SampleBy": null,
57+
"TTL": null,
58+
"Settings": null,
59+
"OrderBy": {
60+
"OrderPos": 94,
61+
"ListEnd": 106,
62+
"Items": [
63+
{
64+
"OrderPos": 94,
65+
"Expr": {
66+
"LeftParenPos": 103,
67+
"RightParenPos": 106,
68+
"Items": {
69+
"ListPos": 104,
70+
"ListEnd": 106,
71+
"HasDistinct": false,
72+
"Items": [
73+
{
74+
"Expr": {
75+
"Name": "id",
76+
"QuoteType": 1,
77+
"NamePos": 104,
78+
"NameEnd": 106
79+
},
80+
"Alias": null
81+
}
82+
]
83+
},
84+
"ColumnArgList": null
85+
},
86+
"Alias": null,
87+
"Direction": ""
88+
}
89+
]
90+
}
91+
},
92+
"Destination": null,
93+
"SubQuery": {
94+
"HasParen": false,
95+
"Select": {
96+
"SelectPos": 111,
97+
"StatementEnd": 135,
98+
"With": null,
99+
"Top": null,
100+
"SelectItems": [
101+
{
102+
"Expr": {
103+
"Name": "*",
104+
"QuoteType": 0,
105+
"NamePos": 118,
106+
"NameEnd": 118
107+
},
108+
"Modifiers": [],
109+
"Alias": null
110+
}
111+
],
112+
"From": {
113+
"FromPos": 120,
114+
"Expr": {
115+
"Table": {
116+
"TablePos": 125,
117+
"TableEnd": 135,
118+
"Alias": null,
119+
"Expr": {
120+
"Database": null,
121+
"Table": {
122+
"Name": "test_table",
123+
"QuoteType": 1,
124+
"NamePos": 125,
125+
"NameEnd": 135
126+
}
127+
},
128+
"HasFinal": false
129+
},
130+
"StatementEnd": 135,
131+
"SampleRatio": null,
132+
"HasFinal": false
133+
}
134+
},
135+
"ArrayJoin": null,
136+
"Window": null,
137+
"Prewhere": null,
138+
"Where": null,
139+
"GroupBy": null,
140+
"WithTotal": false,
141+
"Having": null,
142+
"OrderBy": null,
143+
"LimitBy": null,
144+
"Limit": null,
145+
"Settings": null,
146+
"Format": null,
147+
"UnionAll": null,
148+
"UnionDistinct": null,
149+
"Except": null
150+
}
151+
},
152+
"Populate": false,
153+
"Comment": null
154+
},
155+
{
156+
"CreatePos": 138,
157+
"StatementEnd": 259,
158+
"Name": {
159+
"Database": null,
160+
"Table": {
161+
"Name": "test_mv",
162+
"QuoteType": 1,
163+
"NamePos": 177,
164+
"NameEnd": 184
165+
}
166+
},
167+
"IfNotExists": true,
168+
"OnCluster": null,
169+
"Engine": {
170+
"EnginePos": 185,
171+
"EngineEnd": 230,
172+
"Name": "ReplacingMergeTree",
173+
"Params": {
174+
"LeftParenPos": 212,
175+
"RightParenPos": 213,
176+
"Items": {
177+
"ListPos": 213,
178+
"ListEnd": 213,
179+
"HasDistinct": false,
180+
"Items": []
181+
},
182+
"ColumnArgList": null
183+
},
184+
"PrimaryKey": {
185+
"PrimaryPos": 215,
186+
"Expr": {
187+
"LeftParenPos": 227,
188+
"RightParenPos": 230,
189+
"Items": {
190+
"ListPos": 228,
191+
"ListEnd": 230,
192+
"HasDistinct": false,
193+
"Items": [
194+
{
195+
"Expr": {
196+
"Name": "id",
197+
"QuoteType": 1,
198+
"NamePos": 228,
199+
"NameEnd": 230
200+
},
201+
"Alias": null
202+
}
203+
]
204+
},
205+
"ColumnArgList": null
206+
}
207+
},
208+
"PartitionBy": null,
209+
"SampleBy": null,
210+
"TTL": null,
211+
"Settings": null,
212+
"OrderBy": null
213+
},
214+
"Destination": null,
215+
"SubQuery": {
216+
"HasParen": false,
217+
"Select": {
218+
"SelectPos": 235,
219+
"StatementEnd": 259,
220+
"With": null,
221+
"Top": null,
222+
"SelectItems": [
223+
{
224+
"Expr": {
225+
"Name": "*",
226+
"QuoteType": 0,
227+
"NamePos": 242,
228+
"NameEnd": 242
229+
},
230+
"Modifiers": [],
231+
"Alias": null
232+
}
233+
],
234+
"From": {
235+
"FromPos": 244,
236+
"Expr": {
237+
"Table": {
238+
"TablePos": 249,
239+
"TableEnd": 259,
240+
"Alias": null,
241+
"Expr": {
242+
"Database": null,
243+
"Table": {
244+
"Name": "test_table",
245+
"QuoteType": 1,
246+
"NamePos": 249,
247+
"NameEnd": 259
248+
}
249+
},
250+
"HasFinal": false
251+
},
252+
"StatementEnd": 259,
253+
"SampleRatio": null,
254+
"HasFinal": false
255+
}
256+
},
257+
"ArrayJoin": null,
258+
"Window": null,
259+
"Prewhere": null,
260+
"Where": null,
261+
"GroupBy": null,
262+
"WithTotal": false,
263+
"Having": null,
264+
"OrderBy": null,
265+
"LimitBy": null,
266+
"Limit": null,
267+
"Settings": null,
268+
"Format": null,
269+
"UnionAll": null,
270+
"UnionDistinct": null,
271+
"Except": null
272+
}
273+
},
274+
"Populate": false,
275+
"Comment": null
276+
}
277+
]

0 commit comments

Comments
 (0)