Skip to content

Commit b083fa0

Browse files
authored
Add support of using the SELECT query as table function argument (#128)
1 parent 8bea76f commit b083fa0

File tree

4 files changed

+228
-1
lines changed

4 files changed

+228
-1
lines changed

parser/parser_table.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,9 @@ func (p *Parser) parseTableArgExpr(pos Pos) (Expr, error) {
521521
default:
522522
return ident, nil
523523
}
524-
case p.matchTokenKind(TokenKindInt), p.matchTokenKind(TokenKindString), p.matchKeyword("NULL"):
524+
case p.matchTokenKind(TokenKindLParen):
525+
return p.parseSubQuery(p.Pos())
526+
case p.matchTokenKind(TokenKindInt), p.matchTokenKind(TokenKindString), p.matchKeyword(KeywordNull):
525527
return p.parseLiteral(p.Pos())
526528
default:
527529
return nil, fmt.Errorf("unexpected token: %q, expected <Name>, <literal>", p.last().String)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Origin SQL:
2+
SELECT 1, (SELECT 70) AS `power`, number
3+
FROM
4+
numbers(
5+
plus(
6+
ifNull((SELECT 1 AS bin_count, 1),
7+
1)
8+
)
9+
)
10+
11+
-- Format SQL:
12+
SELECT 1, (SELECT 70) AS `power`, number FROM numbers(plus(ifNull((SELECT 1 AS bin_count, 1), 1)));
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
[
2+
{
3+
"SelectPos": 0,
4+
"StatementEnd": 125,
5+
"With": null,
6+
"Top": null,
7+
"SelectItems": [
8+
{
9+
"Expr": {
10+
"NumPos": 7,
11+
"NumEnd": 8,
12+
"Literal": "1",
13+
"Base": 10
14+
},
15+
"Modifiers": [],
16+
"Alias": null
17+
},
18+
{
19+
"Expr": {
20+
"HasParen": true,
21+
"Select": {
22+
"SelectPos": 11,
23+
"StatementEnd": 20,
24+
"With": null,
25+
"Top": null,
26+
"SelectItems": [
27+
{
28+
"Expr": {
29+
"NumPos": 18,
30+
"NumEnd": 20,
31+
"Literal": "70",
32+
"Base": 10
33+
},
34+
"Modifiers": [],
35+
"Alias": null
36+
}
37+
],
38+
"From": null,
39+
"ArrayJoin": null,
40+
"Window": null,
41+
"Prewhere": null,
42+
"Where": null,
43+
"GroupBy": null,
44+
"WithTotal": false,
45+
"Having": null,
46+
"OrderBy": null,
47+
"LimitBy": null,
48+
"Limit": null,
49+
"Settings": null,
50+
"Format": null,
51+
"UnionAll": null,
52+
"UnionDistinct": null,
53+
"Except": null
54+
}
55+
},
56+
"Modifiers": [],
57+
"Alias": {
58+
"Name": "power",
59+
"QuoteType": 3,
60+
"NamePos": 26,
61+
"NameEnd": 31
62+
}
63+
},
64+
{
65+
"Expr": {
66+
"Name": "number",
67+
"QuoteType": 1,
68+
"NamePos": 34,
69+
"NameEnd": 40
70+
},
71+
"Modifiers": [],
72+
"Alias": null
73+
}
74+
],
75+
"From": {
76+
"FromPos": 41,
77+
"Expr": {
78+
"Table": {
79+
"TablePos": 46,
80+
"TableEnd": 125,
81+
"Alias": null,
82+
"Expr": {
83+
"Name": {
84+
"Name": "numbers",
85+
"QuoteType": 1,
86+
"NamePos": 46,
87+
"NameEnd": 53
88+
},
89+
"Args": {
90+
"LeftParenPos": 53,
91+
"RightParenPos": 125,
92+
"Args": [
93+
{
94+
"Name": {
95+
"Name": "plus",
96+
"QuoteType": 1,
97+
"NamePos": 59,
98+
"NameEnd": 63
99+
},
100+
"Args": {
101+
"LeftParenPos": 59,
102+
"RightParenPos": 123,
103+
"Args": [
104+
{
105+
"Name": {
106+
"Name": "ifNull",
107+
"QuoteType": 1,
108+
"NamePos": 73,
109+
"NameEnd": 79
110+
},
111+
"Args": {
112+
"LeftParenPos": 73,
113+
"RightParenPos": 117,
114+
"Args": [
115+
{
116+
"HasParen": true,
117+
"Select": {
118+
"SelectPos": 81,
119+
"StatementEnd": 105,
120+
"With": null,
121+
"Top": null,
122+
"SelectItems": [
123+
{
124+
"Expr": {
125+
"NumPos": 88,
126+
"NumEnd": 89,
127+
"Literal": "1",
128+
"Base": 10
129+
},
130+
"Modifiers": [],
131+
"Alias": {
132+
"Name": "bin_count",
133+
"QuoteType": 1,
134+
"NamePos": 93,
135+
"NameEnd": 102
136+
}
137+
},
138+
{
139+
"Expr": {
140+
"NumPos": 104,
141+
"NumEnd": 105,
142+
"Literal": "1",
143+
"Base": 10
144+
},
145+
"Modifiers": [],
146+
"Alias": null
147+
}
148+
],
149+
"From": null,
150+
"ArrayJoin": null,
151+
"Window": null,
152+
"Prewhere": null,
153+
"Where": null,
154+
"GroupBy": null,
155+
"WithTotal": false,
156+
"Having": null,
157+
"OrderBy": null,
158+
"LimitBy": null,
159+
"Limit": null,
160+
"Settings": null,
161+
"Format": null,
162+
"UnionAll": null,
163+
"UnionDistinct": null,
164+
"Except": null
165+
}
166+
},
167+
{
168+
"NumPos": 116,
169+
"NumEnd": 117,
170+
"Literal": "1",
171+
"Base": 10
172+
}
173+
]
174+
}
175+
}
176+
]
177+
}
178+
}
179+
]
180+
}
181+
},
182+
"HasFinal": false
183+
},
184+
"StatementEnd": 125,
185+
"SampleRatio": null,
186+
"HasFinal": false
187+
}
188+
},
189+
"ArrayJoin": null,
190+
"Window": null,
191+
"Prewhere": null,
192+
"Where": null,
193+
"GroupBy": null,
194+
"WithTotal": false,
195+
"Having": null,
196+
"OrderBy": null,
197+
"LimitBy": null,
198+
"Limit": null,
199+
"Settings": null,
200+
"Format": null,
201+
"UnionAll": null,
202+
"UnionDistinct": null,
203+
"Except": null
204+
}
205+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SELECT 1, (SELECT 70) AS `power`, number
2+
FROM
3+
numbers(
4+
plus(
5+
ifNull((SELECT 1 AS bin_count, 1),
6+
1)
7+
)
8+
)

0 commit comments

Comments
 (0)