-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathexpr.ne
More file actions
356 lines (300 loc) · 11.7 KB
/
expr.ne
File metadata and controls
356 lines (300 loc) · 11.7 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
@lexer lexerAny
@include "base.ne"
@include "select.ne"
# https://www.postgresql.org/docs/13/sql-expressions.html
# === MACROS
array_of[EXP] -> $EXP (%comma $EXP {% last %}):* {% ([head, tail]) => {
return [head, ...(tail || [])];
} %}
array_of_many[EXP] -> $EXP (%comma $EXP {% last %}):+ {% ([head, tail]) => {
return [head, ...(tail || [])];
} %}
opt_paren[X]
-> lparen $X rparen {% x => x[1] %}
| $X {% ([x]) => x[0] %}
op_single[OP]
-> $OP {% x => track(x, {
op: (toStr(x, ' ') || '<error>').toUpperCase()
}) %}
op_scopable[OP]
-> op_single[$OP] {% unwrap %}
| kw_operator lparen ident dot $OP rparen {% x => track(x, {
op: (toStr(x[4], ' ') || '<error>').toUpperCase(),
opSchema: toStr(x[2]),
})%}
expr_binary[KW, This, Next]
-> ($This | expr_paren) $KW ($Next | expr_paren) {% x => track(x, {
type: 'binary',
left: unwrap(x[0]),
right: unwrap(x[2]),
...unwrap(x[1]),
}) %}
| $Next {% unwrap %}
expr_ternary[KW1, KW2, This, Next]
-> ($This | expr_paren) $KW1 ($This | expr_paren) $KW2 ($Next | expr_paren) {% x => track(x, {
type: 'ternary',
value: unwrap(x[0]),
lo: unwrap(x[2]),
hi: unwrap(x[4]),
op: (flattenStr(x[1]).join(' ') || '<error>').toUpperCase(),
}) %}
| $Next {% unwrap %}
expr_left_unary[KW, This, Next]
-> $KW ($This | expr_paren) {% x => track(x, {
type: 'unary',
...unwrap(x[0]),
operand: unwrap(x[1]),
}) %}
| $Next {% unwrap %}
# ======== Operator precedence
# -> https://www.postgresql.org/docs/12/sql-syntax-lexical.html#SQL-PRECEDENCE
expr -> expr_nostar {% unwrap %} | expr_star {% unwrap %}
expr_nostar -> expr_paren {% unwrap %} | expr_or {% unwrap %}
expr_paren -> lparen (expr_or_select | expr_list_many) rparen {% get(1) %}
expr_or -> expr_binary[op_single[%kw_or], expr_or, expr_and]
expr_and -> expr_binary[op_single[%kw_and], expr_and, expr_not]
expr_not -> expr_left_unary[op_single[%kw_not], expr_not, expr_eq]
expr_eq -> expr_binary[op_scopable[(%op_eq | %op_neq)], expr_eq, expr_is]
expr_star -> star {% x => track(x, { type: 'ref', name: '*' }) %}
expr_is
-> (expr_is | expr_paren) (%kw_isnull | %kw_is %kw_null) {% x => track(x, { type: 'unary', op: 'IS NULL', operand: unwrap(x[0]) }) %}
| (expr_is | expr_paren) (%kw_notnull | %kw_is kw_not_null) {% x => track(x, { type: 'unary', op: 'IS NOT NULL', operand: unwrap(x[0])}) %}
| (expr_is | expr_paren) %kw_is %kw_not:? (%kw_true | %kw_false) {% x => track(x, {
type: 'unary',
op: 'IS ' + flattenStr([x[2], x[3]])
.join(' ')
.toUpperCase(),
operand: unwrap(x[0]),
}) %}
| expr_compare {% unwrap %}
expr_compare -> expr_binary[op_scopable[%op_compare], expr_compare, expr_range]
expr_range -> expr_ternary[ops_between, %kw_and, expr_range, expr_others]
expr_others -> expr_binary[op_scopable[%ops_others], expr_others, expr_like]
expr_like -> expr_binary[op_single[ops_like], expr_like, expr_in]
expr_in -> expr_binary[op_single[ops_in], expr_in, expr_add]
expr_add -> expr_binary[op_scopable[(%op_plus | %op_minus | %op_additive)], expr_add, expr_mult]
expr_mult -> expr_binary[op_scopable[(%star | %op_div | %op_mod)], expr_mult, expr_exp]
expr_exp -> expr_binary[op_scopable[%op_exp], expr_exp, expr_unary_add]
expr_unary_add -> expr_left_unary[op_scopable[(%op_plus | %op_minus)], expr_unary_add, expr_various_constructs]
expr_various_constructs -> expr_binary[op_single[various_binaries], expr_various_constructs, expr_array_index]
expr_array_index
-> (expr_array_index | expr_paren) %lbracket expr_nostar %rbracket {% x => track(x, {
type: 'arrayIndex',
array: unwrap(x[0]),
index: unwrap(x[2]),
}) %}
| expr_member {% unwrap %}
expr_member
-> (expr_member | expr_paren) ops_member %op_minus int {% x => track(x, {
type: 'member',
operand: unwrap(x[0]),
op: x[1],
member: -unwrap(x[3])
}) %}
| (expr_member | expr_paren) ops_member (string | int) {% x => track(x, {
type: 'member',
operand: unwrap(x[0]),
op: x[1],
member: unwrap(x[2])
}) %}
| (expr_member | expr_paren) %op_cast data_type {% x => track(x, {
type: 'cast',
operand: unwrap(x[0]),
to: x[2],
}) %}
| %kw_cast lparen expr_nostar %kw_as data_type rparen {% x => track(x, {
type: 'cast',
operand: unwrap(x[2]),
to: x[4],
}) %}
| data_type string {% x => track(x, {
type: 'cast',
operand: track(x[1], {
type: 'string',
value: unbox(x[1]),
}),
to: unbox(x[0]),
}) %}
| expr_dot {% unwrap %}
expr_dot
-> qname %dot (word | star) {% x => track(x, {
type: 'ref',
table: unwrap(x[0]),
name: toStr(x[2])
}) %}
| expr_final {% unwrap %}
expr_final
-> expr_basic
| expr_primary
expr_basic
-> expr_special_calls
| expr_call
| expr_array
| expr_case
| expr_extract
| word {% x => track(x, {
type: 'ref',
name: unwrap(x[0]),
}) %}
expr_array -> %kw_array %lbracket expr_subarray_items:? %rbracket {% x => track(x, {
type: 'array',
expressions: x[2] || [],
}) %}
| %kw_array lparen selection rparen {% x => track(x, {
type: 'array select',
select: unwrap(x[2]),
}) %}
expr_subarray -> %lbracket expr_subarray_items:? %rbracket {% get(1) %}
expr_subarray_items
# Support ARRAY[expressions]
-> array_of[expr_list_item] {% x => x[0].map(unwrap) %}
# Support ARRAY[[expressions]]
| array_of[expr_subarray] {% (x: any) => {
return x[0].map((v: any[]) => {
return track(v, {
type: 'array',
expressions: v[0].map(unwrap),
})
})
} %}
# Cannot select from aggregate functions. Syntactically however, there is no way
# to determine that a function is an aggregate. At least we can rule out using
# DISTINCT, ALL, ORDER BY, and FILTER as part of the expression.
expr_function_call -> expr_fn_name
lparen
expr_list_raw:?
rparen
{% x => track(x, {
type: 'call',
function: unwrap(x[0]),
args: x[2] || [],
}) %}
expr_call -> expr_fn_name
lparen
(%kw_all | %kw_distinct):?
expr_list_raw:?
select_order_by:?
rparen
(kw_filter lparen %kw_where expr rparen {% get(3) %}):?
expr_call_within_group:?
expr_call_over:?
{% x => track(x, {
type: 'call',
function: unwrap(x[0]),
...x[2] && {distinct: toStr(x[2])},
args: x[3] || [],
...x[4] && {orderBy: x[4]},
...x[6] && {filter: unwrap(x[6])},
...x[7] && {withinGroup: x[7]},
...x[8] && {over: unwrap(x[8])},
}) %}
expr_call_over -> kw_over
lparen
(kw_partition kw_by expr_list_raw {% last %}):?
select_order_by:?
rparen {% x => track(x, {
...x[2] && { partitionBy: x[2] },
...x[3] && { orderBy: x[3] },
}) %}
expr_call_within_group -> (kw_within %kw_group)
lparen
(%kw_order kw_by)
select_order_by_expr
rparen
{% x => track(x, x[3]) %}
# https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
expr_extract -> (word {% kw('extract') %}) lparen word %kw_from expr rparen {% x => track(x, {
type: 'extract',
field: asName(x[2]),
from: x[4],
}) %}
expr_primary
-> float {% x => track(x, { type: 'numeric', value: unbox(x[0]) }) %}
| int {% x => track(x, { type: 'integer', value: unbox(x[0]) }) %}
| string {% x => track(x, { type: 'string', value: unbox(x[0]) }) %}
| %kw_true {% x => track(x, { type: 'boolean', value: true }) %}
| %kw_false {% x => track(x, { type: 'boolean', value: false }) %}
| %kw_null {% x => track(x, { type: 'null' }) %}
| value_keyword {% x => track(x, {type: 'keyword', keyword: toStr(x) }) %}
| %qparam {% x => track(x, { type: 'parameter', name: toStr(x[0]) }) %}
| %kw_default {% x => track(x, { type: 'default'}) %}
# LIKE-kind operators
ops_like -> ops_like_keywors | ops_like_operators
ops_like_keywors -> %kw_not:? (%kw_like | %kw_ilike)
ops_like_operators
-> (%op_like {% () => 'LIKE' %})
| (%op_ilike {% () => 'ILIKE' %})
| (%op_not_like {% () => 'NOT LIKE' %})
| (%op_not_ilike {% () => 'NOT ILIKE' %})
ops_in -> %kw_not:? %kw_in
ops_between -> %kw_not:? kw_between # {% x => x[0] ? `${x[0][0].value} ${x[1].value}`.toUpperCase() : x[1].value %}
ops_member -> (%op_member | %op_membertext) {% x => unwrap(x)?.value %}
# x,y,z
expr_list_item -> expr_or_select {% unwrap %} | expr_star {% unwrap %}
expr_list_raw -> array_of[expr_list_item] {% ([x]) => x.map(unwrap) %}
expr_list_raw_many -> array_of_many[expr_list_item] {% ([x]) => x.map(unwrap) %}
expr_or_select -> expr_nostar {% unwrap %}
| selection {%unwrap%}
expr_list_many -> expr_list_raw_many {% x => track(x, {
type: 'list',
expressions: x[0],
}) %}
expr_case -> %kw_case expr_nostar:? expr_case_whens:* expr_case_else:? %kw_end {% x => track(x, {
type: 'case',
value: x[1],
whens: x[2],
else: x[3],
}) %}
expr_case_whens -> %kw_when expr_nostar %kw_then expr_nostar {% x => track(x, {
when: x[1],
value: x[3],
}) %}
expr_case_else -> %kw_else expr_nostar {% last %}
expr_fn_name -> ((word %dot):? word_or_keyword {% x => track(x, {
name: unbox(unwrap(x[1])),
...x[0] && { schema: toStr(x[0][0]) },
}) %})
| ((%kw_any | %kw_some | %kw_all | %kw_left | %kw_right) {% x => track(x, {
name: toStr(unwrap(x)),
})%})
word_or_keyword
-> word
| value_keyword {% x => box(x, toStr(x)) %}
value_keyword
-> %kw_current_catalog
| %kw_current_date
| %kw_current_role
| %kw_current_schema
| %kw_current_timestamp
| %kw_current_time
| %kw_localtimestamp
| %kw_localtime
| %kw_session_user
| %kw_user
| %kw_current_user
expr_special_calls -> spe_overlay
| spe_substring
spe_overlay -> (word {% kw('overlay') %})
(%lparen expr_nostar)
(%kw_placing expr_nostar)
(%kw_from expr_nostar)
(%kw_for expr_nostar):?
%rparen {% x => track(x, {
type: 'overlay',
value: x[1][1],
placing: x[2][1],
from: x[3][1],
...x[4] && {for: x[4][1]},
}) %}
spe_substring -> (word {% kw('substring') %})
(%lparen expr_nostar)
(%kw_from expr_nostar):?
(%kw_for expr_nostar):?
%rparen {% x => track(x, {
type: 'substring',
value: x[1][1],
...x[2] && {from: x[2][1]},
...x[3] && {for: x[3][1]},
}) %}
various_binaries
-> kw_at kw_time kw_zone {% () => 'AT TIME ZONE' %}