This repository was archived by the owner on Apr 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathexpr_base.py
More file actions
342 lines (281 loc) · 9.65 KB
/
expr_base.py
File metadata and controls
342 lines (281 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from dataclasses import dataclass
from typing import List
from typing import Optional
from rfmt.blocks import ChoiceBlock as CB
from rfmt.blocks import IndentBlock as IB
from rfmt.blocks import LineBlock as LB
from rfmt.blocks import StackBlock as SB
from rfmt.blocks import TextBlock as TB
from rfmt.blocks import WrapBlock as WB
from .utils import with_commas
from .query import SQLQuery
from .query_impl import SQLOrderLimitOffset
from .expr import SQLExpr
from .const import SQLConstant
from .const import SQLNumber
from .ident import SQLIdentifierPath
from .node import SQLNode
from .node import SQLNodeList
from .expr_funcs import SQLFuncExpr, SQLCustomFuncs
@dataclass(frozen=True)
class SQLArrayLiteral(SQLExpr):
args: SQLNodeList
def sqlf(self, compact):
compact_sql = LB([
TB('['),
LB(with_commas(True, self.args)),
TB(']')
])
if compact:
return compact_sql
return CB([
compact_sql,
LB([
TB('['),
WB(with_commas(compact, self.args, tail=']'))
]),
])
@staticmethod
def consume(lex) -> 'Optional[SQLArrayLiteral]':
if not lex.consume('['):
return None
exprs: List[SQLExpr] = []
while True:
exprs.append(SQLExpr.parse(lex))
if not lex.consume(','):
break
lex.expect(']')
return SQLArrayLiteral(SQLNodeList(exprs))
@dataclass(frozen=True)
class SQLArraySelect(SQLExpr):
query: SQLQuery
def sqlf(self, compact):
compact_sql = LB([
TB('ARRAY('), self.query.sqlf(True), TB(')')
])
if compact:
return compact_sql
return CB([
compact_sql,
SB([
TB('ARRAY('),
IB(self.query.sqlf(compact)),
TB(')')
])
])
@staticmethod
def consume(lex) -> 'Optional[SQLArraySelect]':
if not lex.consume('ARRAY'):
return None
lex.expect('(')
query = SQLQuery.parse(lex)
lex.expect(')')
return SQLArraySelect(query)
@dataclass(frozen=True)
class SQLArrayAgg(SQLExpr):
is_distinct: bool
expr: SQLNode
nulls: Optional[str]
order_limit_offset: Optional[SQLOrderLimitOffset]
analytic: Optional[SQLNode]
def sqlf(self, compact):
lines = [TB('ARRAY_AGG(')]
if self.is_distinct:
lines.append(TB('DISTINCT '))
lines.append(self.expr.sqlf(True))
if self.nulls:
lines.append(TB(self.nulls) + ' NULLS')
if self.order_limit_offset:
lines.append(self.order_limit_offset.sqlf(True))
if self.analytic:
lines.append(self.analytic.sqlf(True))
lines.append(TB(')'))
compact_sql = LB(lines)
if compact:
return compact_sql
stack = [TB('ARRAY_AGG(')]
indent = []
if self.is_distinct:
indent.append(
LB([TB('DISTINCT '), self.expr.sqlf(compact)]))
else:
indent.append(self.expr.sqlf(compact))
if self.nulls:
indent.append(TB(self.nulls) + ' NULLS')
if self.order_limit_offset:
indent.append(self.order_limit_offset.sqlf(compact))
if self.analytic:
indent.append(self.analytic.sqlf(compact))
stack.append(IB(SB(indent)))
stack.append(TB(')'))
return CB([
compact_sql,
SB(stack)
])
@staticmethod
def consume(lex) -> 'Optional[SQLArrayAgg]':
if not lex.consume('ARRAY_AGG'):
return None
lex.expect('(')
is_distinct = bool(lex.consume('DISTINCT'))
expr = SQLExpr.parse(lex)
nulls = None
if lex.consume('IGNORE'):
nulls = 'IGNORE'
lex.expect('NULLS')
elif lex.consume('RESPECT'):
nulls = 'RESPECT'
lex.expect('NULLS')
order_limit_offset = SQLOrderLimitOffset.consume(lex)
analytic = SQLAnalytic.consume(lex)
lex.expect(')')
return SQLArrayAgg(is_distinct, expr, nulls,
order_limit_offset, analytic)
@dataclass(frozen=True)
class SQLExprWithAnalytic(SQLExpr):
function: SQLExpr
analytic: SQLNode
def sqlf(self, compact):
compact_sqlf = LB([self.function.sqlf(compact), TB(' '),
self.analytic.sqlf(compact)])
if compact:
return compact_sqlf
return CB([
compact_sqlf,
SB([self.function.sqlf(compact),
self.analytic.sqlf(compact)])
])
@staticmethod
def parse(lex) -> 'SQLExpr':
# Try alternatives first
expr: SQLExpr = (SQLConstant.consume(lex) or
SQLArrayLiteral.consume(lex) or
SQLArrayAgg.consume(lex) or
SQLArraySelect.consume(lex) or
SQLCustomFuncs.consume(lex) or
SQLIdentifierPath.parse(lex))
# If it is a SQLIdentifierPath, it may be
# a normal function call.
if isinstance(expr, SQLIdentifierPath) and lex.consume('('):
# Parse as a function
func_args: List[SQLExpr] = []
if not lex.consume(')'):
while True:
func_args.append(SQLExpr.parse(lex))
if not lex.consume(','):
break
lex.expect(')')
# Turn it into a function
expr = SQLFuncExpr(expr.names, SQLNodeList(func_args))
window = SQLAnalytic.consume(lex)
if window:
expr = SQLExprWithAnalytic(expr, window)
return expr
@dataclass(frozen=True)
class SQLAnalytic(SQLExpr):
partition_by: SQLNodeList
order_by: SQLNodeList
range_desc: str
def sqlf(self, compact):
lines = []
lines.append(TB('OVER ('))
if self.partition_by:
lines.append(TB('PARTITION BY '))
lines.extend(with_commas(True, self.partition_by, ' '))
if self.order_by:
lines.append(TB('ORDER BY '))
lines.extend(with_commas(True, self.order_by, ' '))
if self.range_desc:
lines.append(TB(self.range_desc))
lines.append(TB(')'))
if compact:
return LB(lines)
full_sql = [
TB('OVER ('),
]
if self.partition_by:
full_sql.append(
IB(
SB([
TB('PARTITION BY'), IB(
WB(with_commas(True, self.partition_by)))
])))
if self.order_by:
full_sql.append(
IB(
SB([
TB('ORDER BY'), IB(
WB(with_commas(True, self.order_by)))
])))
if self.range_desc:
full_sql.append(IB(TB(self.range_desc)))
full_sql.append(TB(')'))
r = CB([LB(lines), SB(full_sql)])
return r
@staticmethod
def consume(lex) -> 'Optional[SQLAnalytic]':
if not lex.consume('OVER'):
return None
lex.expect('(')
partition_by = []
if lex.consume('PARTITION'):
lex.expect('BY')
while True:
partition_by.append(SQLExpr.parse(lex))
if not lex.consume(','):
break
order_by = []
if lex.consume('ORDER'):
lex.expect('BY')
while True:
oby = SQLExpr.parse(lex)
order = None
if lex.consume('ASC'):
order = 'ASC'
elif lex.consume('DESC'):
order = 'DESC'
# TODO(scannell): Capture ASC/DESC
order_by.append(oby)
if not lex.consume(','):
break
win_spec = (lex.consume('ROWS') or lex.consume('RANGE'))
# If window specified, parse it out
if win_spec:
if lex.consume('BETWEEN'):
win_spec += (' BETWEEN ' +
SQLAnalytic._parse_frame_boundary(lex))
lex.expect('AND')
win_spec += ' AND ' + SQLAnalytic._parse_frame_boundary(lex)
else:
win_spec += ' ' + SQLAnalytic._parse_frame_boundary(lex)
lex.expect(')')
# Return analytics function
return SQLAnalytic(SQLNodeList(partition_by),
SQLNodeList(order_by),
win_spec)
@staticmethod
def _parse_frame_boundary(lex):
if lex.consume('UNBOUNDED'):
lex.expect('PRECEDING')
return 'UNBOUNDED PRECEDING'
if lex.consume('CURRENT'):
lex.expect('ROW')
return 'CURRENT ROW'
num = SQLNumber.consume(lex)
num_typ = (lex.consume('PRECEDING') or lex.consume('FOLLOWING') or
lex.error('Expected PRECEDING or FOLLOWING'))
return '{} {}'.format(num, num_typ)