-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadql.peg
More file actions
604 lines (497 loc) · 19.2 KB
/
adql.peg
File metadata and controls
604 lines (497 loc) · 19.2 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/// A PEG grammar suitable for directly putting into PEGjs.
queries = (space* query space* ';')* space*
unqualified_schema_name = identifier
correlation_name = identifier
correlation_specification = ("AS"i space)? space* correlation_name
/// The spec says to have correlation_name as an alternate, but
/// table_name matches everything that correlation name matches,
/// so correlation_name will never match.
qualifier = table_name
/// I have to manually expand the possibilities in
/// column_reference and table name, because a catalog.schema can
/// match against schema.table or table.column, gobbling up the
/// table or column name and making the parse fail.
table_name =
(catalog_name '.' unqualified_schema_name '.' identifier)
/ (unqualified_schema_name '.' identifier)
/ identifier
column_reference =
(catalog_name '.' unqualified_schema_name
'.' identifier '.' identifier)
/ (unqualified_schema_name '.' identifier '.' identifier)
/ (identifier '.' identifier)
/ identifier
catalog_name = identifier
/// This puts term and numeric_value_expression on the left, not
/// right. Otherwise the rule greedily recurses on itself and
/// runs out of stack space. With that reordering, the first term
/// is always the same and the second part becomes optional.
term = factor (space* ('*' / '/') space* term)?
numeric_value_expression =
term (space* ('+' / '-') space* numeric_value_expression)?
string_value_function = string_geometry_function / user_defined_function
string_geometry_function = extract_coordsys
extract_coordsys = 'COORDSYS'i space* '(' space* geometry_value_expression space* ')'
/// Flipped the order here because a value_expression_primary can
/// match a function name that should be matched by
/// string_value_function
character_primary = string_value_function / value_expression_primary
character_factor = character_primary
concatenation_operator = '||'
/// Flip the order of character_factor and
/// character_value_expression to prevent recursion.
character_value_expression = character_factor
(space* concatenation_operator space* character_value_expression)?
string_value_expression = character_value_expression
concatenation_expression = character_factor space* concatenation_operator
space* character_value_expression
/// This expression first checks for a concatenation operator.
/// Otherwise a numeric_value_expression would match the first
/// half of a concatenation, meaning the concatenation operator
/// would cause the parse to fail. We can not put
/// string_value_expression first, because that would partially
/// match arithmetic. For 'a+b', it matches 'a' but not the '+'.
value_expression = concatenation_expression
/ numeric_value_expression / string_value_expression / geometry_value_expression
column_name = identifier
as = value_expression space* ('AS'i space+)? column_name
select_non_as_item = (qualifier '.*') / value_expression
select_item = as / select_non_as_item
select_list = select_item (space* ',' space* select_item)*
columns = '*' / select_list
set_function_type = 'AVG'i / 'MAX'i / 'MIN'i
/ 'SUM'i / 'COUNT'i
set_quantifier = ('DISTINCT'i / 'ALL'i)
&space
/// The '&nonidentifier_character' after set_function_type allows us
/// to fail immediately if we get a set_function that does not have a
/// parenthesis.
general_set_function =
set_function_type &nonidentifier_character
space* '('
(space* set_quantifier)?
space* value_expression space* ')'
set_function_specification
= ('COUNT'i space* '(' space* '*' space* ')')
/ general_set_function
when_operand = value_expression
result_expression = value_expression
result = result_expression / 'NULL'i
simple_when_clause = 'WHEN'i
space+ when_operand
space+ 'THEN'i
space+ result
else_clause = 'ELSE'i space+ result
value_expression_primary =
unsigned_value_specification
/ column_reference
/ set_function_specification
/ ('(' value_expression ')')
trig_function = (('ACOS'i
/ 'ASIN'i
/ 'ATAN'i
/ 'COS'i
/ 'COT'i
/ 'SIN'i
/ 'TAN'i)
&nonidentifier_character)
space* '('
space* numeric_value_expression
space* ')'
/ ('ATAN2'i &nonidentifier_character space* '('
space* numeric_value_expression space* ','
space* numeric_value_expression space* ')')
math_function = (('ABS'i
/ 'CEILING'i
/ 'DEGREES'i
/ 'EXP'i
/ 'FLOOR'i
/ 'LOG10'i
/ 'LOG'i
/ 'RADIANS'i
/ 'SQRT'i)
&nonidentifier_character
space* '('
space* numeric_value_expression
space* ')')
/ (('MOD'i / 'POWER'i)
&nonidentifier_character
space* '('
space* numeric_value_expression
space* ','
space* numeric_value_expression
space* ')')
/ ('PI'i &nonidentifier_character
space* '(' space* ')')
/ ('RAND'i &nonidentifier_character
space* '(' numeric_value_expression? ')')
/ (('ROUND'i / 'TRUNCATE'i) &nonidentifier_character
space* '('
space* numeric_value_expression
(space* ',' space* signed_integer)?
space* ')')
/// default_function_prefix is a bit useless since it is optional.
default_function_prefix = 'udf_'
user_defined_function_name = default_function_prefix?
regular_identifier
user_defined_function_param = value_expression
user_defined_function = user_defined_function_name
space* '('
(space* user_defined_function_param
(space* ',' space* user_defined_function_param)*)?
space* ')'
numeric_value_function = trig_function / math_function
/ user_defined_function / numeric_geometry_function
/// Flipped the order here, because a value_expression can match a
/// function name.
numeric_primary = numeric_value_function / value_expression_primary
factor = sign? numeric_primary
coord_sys = string_value_expression
column_or_number = column_reference / double
coordinates = numeric_value_expression space* ',' space* numeric_value_expression
point = 'POINT'i
space* '('
space* coord_sys
space* ','
space* coordinates
space* ')'
point_or_column = point / column_reference
circle = 'CIRCLE'i
space* '(' space*
coord_sys
space* ','
space* coordinates
space* ','
space* numeric_value_expression
space* ')'
box = 'BOX'i
space* '('
space* coord_sys
space* ','
space* coordinates
space* ','
space* numeric_value_expression
space* ','
space* numeric_value_expression
space* ')'
centroid = 'CENTROID'i space* '(' space* geometry_value_expression space* ')'
region = 'REGION'i space* '(' space* string_value_expression space* ')'
coord_list = coordinates (space* ',' space* coordinates)*
polygon = 'POLYGON'i space*
'(' space* coord_sys
space* ','
space* coord_list
space* ')'
geometry_value_expression = value_expression_primary / geometry_value_function
geometry_value_function = point / circle / box / polygon / centroid / region
area = 'AREA'i space* '(' space* geometry_value_expression space* ')'
coord1 = 'COORD1'i space* '(' space* coord_value space* ')'
coord2 = 'COORD2'i space* '(' space* coord_value space* ')'
distance = 'DISTANCE'i space* '(' space* coord_value space* ',' space* coord_value space* ')'
coord_value = point / column_reference
non_predicate_geometry_function = area / coord1 / coord2 / distance
contains = 'CONTAINS'i
space* '('
space* geometry_value_expression
space* ','
space* geometry_value_expression
space* ')'
intersects = 'INTERSECTS'i
space* '('
space* geometry_value_expression
space* ','
space* geometry_value_expression space* ')'
predicate_geometry_function = contains / intersects
numeric_geometry_function = predicate_geometry_function / non_predicate_geometry_function
keyword = (ADQL_reserved_word / SQL_reserved_word) &(!identifier_character)
simple_Latin_letter = [a-zA-Z]
identifier_character = digit / simple_Latin_letter / '_'
/// nonidentifier_character is to signal that, for example, in an
/// AND, clause, AND is followed by something that is not an
/// identifier (e.g. a space or parentheses).
nonidentifier_character = &(!identifier_character) char
all_identifiers = simple_Latin_letter identifier_character*
regular_identifier = &(!keyword) all_identifiers
nondoublequote_character = &(!'"') char
delimited_identifier_part = nondoublequote_character / '""'
delimited_identifier_body = delimited_identifier_part+
delimited_identifier = '"' delimited_identifier_body '"'
identifier = regular_identifier / delimited_identifier
join_specification = join_condition / named_columns_join
join_condition = 'ON'i space+ search_condition
named_columns_join = 'USING'i space+ '('
space* join_column_list space* ')'
join_column_list = column_name_list
column_name_list = column_name (space* ',' space* column_name)*
table_correlation = table_name (space* correlation_specification)?
/// Joins are a bit circuitous because of the possibility of
/// parentheses, but I think this is the same as the BNF
outer_join = ('LEFT'i / 'RIGHT'i / 'FULL'i) &space
(space* 'OUTER'i &space)?
join_type = 'INNER'i / outer_join
join_suffix =
('NATURAL'i space+)?
(join_type space+)?
'JOIN'i space+
table_reference (space* join_specification)?
qualified_join = (('(' space* joined_table space* ')')
/ table_correlation)
(space* join_suffix)+
joined_table = qualified_join / ('(' space* joined_table space* ')')
digit = [0-9]
unsigned_integer = digit+
exact_numeric_literal = (unsigned_integer ('.' unsigned_integer)?)
/ ('.' unsigned_integer)
sign = '+' / '-'
signed_integer = sign? unsigned_integer
mantissa = exact_numeric_literal
exponent = signed_integer
approximate_numeric_literal = mantissa ('E' / 'e') exponent
unsigned_numeric_literal = approximate_numeric_literal / exact_numeric_literal
double = sign? unsigned_numeric_literal
quote = '\''
literal_space = ' '
newline = '\n'
tab = '\t'
minus_sign = '-'
char = .
nonquote_character = &(!quote) char
character_representation = nonquote_character / '\'\''
comment_introducer = minus_sign minus_sign+
comment_character = &(!newline) char
comment = comment_introducer comment_character* newline
separator = comment / literal_space / newline
space = separator
/// String literals are implicitly concatenated when placed next to each other
character_string_literal= quote character_representation* quote
(separator+ quote character_representation* quote)*
general_literal = character_string_literal
unsigned_literal = unsigned_numeric_literal / general_literal
unsigned_value_specification = unsigned_literal
derived_correlation = subquery space* correlation_specification
table_reference = joined_table / table_correlation / derived_correlation
from_clause = 'FROM'i space+
table_reference (space* ',' space* table_reference)*
comparison_predicate = value_expression
space* ('=' / '!=' / '<>' / '<=' / '>=' / '<' / '>')
space* value_expression
between_predicate = value_expression &space
(space* 'NOT'i &space)?
space* 'BETWEEN'i &space
space* value_expression &space
space* 'AND'i &space
space* value_expression
in_predicate = value_expression &space
(space* 'NOT'i &space)?
space* 'IN'i &space
space* (subquery
/ ('(' space* (value_expression (space* ',' space* value_expression)*)
space* ')'))
null_predicate = value_expression
space* ('IS'i &space)
(space* 'NOT'i &space)?
space* 'NULL'i
match_value = character_value_expression
pattern = character_value_expression
like_predicate = match_value
(space* 'NOT'i &space)?
space* 'LIKE'i space+ pattern
exists_predicate= 'EXISTS'i space+ subquery
predicate = comparison_predicate / between_predicate / in_predicate
/ null_predicate / like_predicate / exists_predicate
where = 'WHERE'i space+ search_condition
grouping_column_reference = column_reference
grouping_column_reference_list = grouping_column_reference
(space* ',' space* column_reference)*
group_by_clause = 'GROUP'i &space
space* 'BY'i &space
space* grouping_column_reference_list
having_clause = 'HAVING'i &space
space* search_condition
sort_key = column_name / unsigned_integer
ordering_specification = 'ASC'i / 'DESC'i
/// I have the vague feeling that there are cases where there are
/// no spaces between the sort_key and ordering_specification, but
/// I can not think of any.
sort_specification = sort_key (space+ ordering_specification)?
sort_specification_list = sort_specification
(space* ',' space* sort_specification)*
order_by_clause = 'ORDER'i &space
space* 'BY'i &space
space* sort_specification_list
query = 'SELECT'i
(space+ set_quantifier)?
(space+ 'TOP'i space+ unsigned_integer)?
space+ columns
space+ from_clause
(space+ where)?
(space+ group_by_clause)?
(space+ having_clause)?
(space+ order_by_clause)?
subquery = '(' space* (query / joined_table) space* ')'
/// Reverse sort to avoid early matches.
ADQL_reserved_word = 'TRUNCATE'i / 'TOP'i
/ 'SQRT'i / 'SIN'i
/ 'ROUND'i / 'REGION'i
/ 'RAND'i / 'RADIANS'i
/ 'POWER'i / 'POLYGON'i
/ 'POINT'i / 'PI'i
/ 'MOD'i / 'LOG10'i
/ 'LOG'i / 'INTERSECTS'i
/ 'FLOOR'i / 'EXP'i
/ 'DISTANCE'i
/ 'DEGREES'i / 'COS'i
/ 'COORDSYS'i / 'COORD2'i
/ 'COORD1'i / 'CONTAINS'i
/ 'CIRCLE'i / 'CENTROID'i
/ 'CEILING'i / 'BOX'i
/ 'ATAN2'i / 'ATAN'i
/ 'ASIN'i / 'AREA'i
/ 'ACOS'i / 'ABS'i
/// Split up SQL_reserved_word to help memory usage and compile times.
SQL_reserved_word_00 = 'ZONE'i / 'YEAR'i
/ 'WRITE'i / 'WORK'i
/ 'WITH'i / 'WHERE'i
/ 'WHENEVER'i / 'WHEN'i
/ 'VIEW'i / 'VARYING'i
/ 'VARCHAR'i
/ 'VALUES'i / 'VALUE'i
/ 'USING'i / 'USER'i
/ 'USAGE'i
SQL_reserved_word_01
= 'UPPER'i / 'UPDATE'i
/ 'UNKNOWN'i / 'UNIQUE'i
/ 'UNION'i / 'TRUE'i
/ 'TRIM'i / 'TRANSLATION'i
/ 'TRANSLATE'i / 'TRANSACTION'i
/ 'TRAILING'i / 'TO'i
/ 'TIMEZONE_MINUTE'i / 'TIMEZONE_HOUR'i
/ 'TIMESTAMP'i
SQL_reserved_word_02
= 'TIME'i / 'THEN'i
/ 'TEMPORARY'i / 'TABLE'i
/ 'SYSTEM_USER'i / 'SUM'i
/ 'SUBSTRING'i / 'SQLSTATE'i
/ 'SQLERROR'i / 'SQLCODE'i
/ 'SQL'i / 'SPACE'i
/ 'SOME'i / 'SMALLINT'i
/ 'SIZE'i / 'SET'i
SQL_reserved_word_03
= 'SESSION_USER'i / 'SESSION'i
/ 'SELECT'i / 'SECTION'i
/ 'SECOND'i / 'SCROLL'i
/ 'SCHEMA'i / 'ROWS'i
/ 'ROLLBACK'i / 'RIGHT'i
/ 'REVOKE'i / 'RESTRICT'i
/ 'RELATIVE'i / 'REFERENCES'i
/ 'REAL'i / 'READ'i
SQL_reserved_word_10
= 'PUBLIC'i / 'PROCEDURE'i
/ 'PRIVILEGES'i / 'PRIOR'i
/ 'PRIMARY'i / 'PRESERVE'i
/ 'PREPARE'i / 'PRECISION'i
/ 'POSITION'i / 'PARTIAL'i
/ 'PAD'i / 'OVERLAPS'i
/ 'OUTPUT'i / 'OUTER'i
/ 'ORDER'i / 'OR'i
SQL_reserved_word_11
= 'OPTION'i / 'OPEN'i
/ 'ONLY'i / 'ON'i / 'OF'i
/ 'OCTET_LENGTH'i / 'NUMERIC'i
/ 'NULLIF'i / 'NULL'i
/ 'NOT'i / 'NO'i
/ 'NEXT'i / 'NCHAR'i
/ 'NATURAL'i / 'NATIONAL'i
SQL_reserved_word_12 = 'NAMES'i / 'MONTH'i
/ 'MODULE'i / 'MINUTE'i
/ 'MIN'i / 'MAX'i
/ 'MATCH'i / 'LOWER'i
/ 'LOCAL'i / 'LIKE'i
/ 'LEVEL'i / 'LEFT'i
/ 'LEADING'i / 'LAST'i
/ 'LANGUAGE'i / 'KEY'i
SQL_reserved_word_13
= 'JOIN'i / 'ISOLATION'i
/ 'IS'i / 'INTO'i
/ 'INTERVAL'i / 'INTERSECT'i
/ 'INTEGER'i / 'INT'i
/ 'INSERT'i / 'INSENSITIVE'i
/ 'INPUT'i / 'INNER'i
/ 'INITIALLY'i / 'INDICATOR'i
/ 'IN'i / 'IMMEDIATE'i
SQL_reserved_word_20 = 'IDENTITY'i / 'HOUR'i
/ 'HAVING'i / 'GROUP'i
/ 'GRANT'i / 'GOTO'i
/ 'GO'i / 'GLOBAL'i
/ 'GET'i / 'FULL'i
/ 'FROM'i / 'FOUND'i
/ 'FOREIGN'i / 'FOR'i
/ 'FLOAT'i / 'FIRST'i
SQL_reserved_word_21
= 'FETCH'i / 'FALSE'i
/ 'EXTRACT'i / 'EXTERNAL'i
/ 'EXISTS'i / 'EXECUTE'i
/ 'EXEC'i / 'EXCEPTION'i
/ 'EXCEPT'i / 'ESCAPE'i
/ 'END-EXEC'i / 'END'i
/ 'ELSE'i / 'DROP'i
/ 'DOUBLE'i / 'DOMAIN'i
SQL_reserved_word_22
= 'DISTINCT'i / 'DISCONNECT'i
/ 'DIAGNOSTICS'i / 'DESCRIPTOR'i
/ 'DESCRIBE'i / 'DESC'i
/ 'DELETE'i / 'DEFERRED'i
/ 'DEFERRABLE'i / 'DEFAULT'i
/ 'DECLARE'i / 'DECIMAL'i
/ 'DEALLOCATE'i / 'DAY'i
/ 'DATE'i / 'CURSOR'i
SQL_reserved_word_23
= 'CURRENT_USER'i / 'CURRENT_TIMESTAMP'i
/ 'CURRENT_TIME'i / 'CURRENT_DATE'i
/ 'CURRENT'i / 'CROSS'i
/ 'CREATE'i / 'COUNT'i
/ 'CORRESPONDING'i / 'CONVERT'i
/ 'CONTINUE'i / 'CONSTRAINTS'i
/ 'CONSTRAINT'i / 'CONNECTION'i
/ 'CONNECT'i / 'COMMIT'i
SQL_reserved_word_30
= 'COLUMN'i / 'COLLATION'i
/ 'COLLATE'i / 'COALESCE'i
/ 'CLOSE'i / 'CHECK'i
/ 'CHAR_LENGTH'i / 'CHARACTER_LENGTH'i
/ 'CHARACTER'i / 'CHAR'i
/ 'CATALOG'i / 'CAST'i
/ 'CASE'i / 'CASCADED'i
/ 'CASCADE'i / 'BY'i
SQL_reserved_word_31 = 'BOTH'i / 'BIT_LENGTH'i
/ 'BIT'i / 'BETWEEN'i
/ 'BEGIN'i / 'AVG'i
/ 'AUTHORIZATION'i
/ 'AT'i / 'ASSERTION'i
/ 'ASC'i / 'AS'i
/ 'ARE'i / 'ANY'i
/ 'AND'i / 'ALTER'i
/ 'ALLOCATE'i
SQL_reserved_word_32 = 'ALL'i / 'ADD'i
/ 'ACTION'i
/ 'ABSOLUTE'i
SQL_reserved_word_0 = SQL_reserved_word_00 / SQL_reserved_word_01
/ SQL_reserved_word_02 / SQL_reserved_word_03
SQL_reserved_word_1 = SQL_reserved_word_10 / SQL_reserved_word_11
/ SQL_reserved_word_12 / SQL_reserved_word_13
SQL_reserved_word_2 = SQL_reserved_word_20 / SQL_reserved_word_21
/ SQL_reserved_word_22 / SQL_reserved_word_23
SQL_reserved_word_3 = SQL_reserved_word_30 / SQL_reserved_word_31
/ SQL_reserved_word_32
SQL_reserved_word = SQL_reserved_word_0 / SQL_reserved_word_1
/ SQL_reserved_word_2 / SQL_reserved_word_3
boolean_literal = 'True'i / 'False'i
boolean_value_expression = boolean_literal / user_defined_function
boolean_primary = predicate
/ ('(' space* search_condition space* ')')
/ boolean_value_expression
boolean_factor = ('NOT'i &space)? space* boolean_primary
boolean_term = boolean_factor
space+ ('AND'i / 'OR'i)
space+ search_condition
search_condition = boolean_term / boolean_factor