-
Notifications
You must be signed in to change notification settings - Fork 20
/
parser.py
1058 lines (896 loc) · 27.8 KB
/
parser.py
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from mindsdb_sql.parser.logger import ParserLogger
from mindsdb_sql.parser.parser import SQLParser
from mindsdb_sql.parser.ast import *
from mindsdb_sql.parser.dialects.mysql.lexer import MySQLLexer
from mindsdb_sql.exceptions import ParsingException
from mindsdb_sql.parser.utils import ensure_select_keyword_order, JoinType
"""
Unfortunately the rules are not iherited from base SQLParser, because it just doesn't work with Sly due to metaclass magic.
"""
class MySQLParser(SQLParser):
log = ParserLogger()
tokens = MySQLLexer.tokens
precedence = (
('left', OR),
('left', AND),
('right', UNOT),
('left', EQUALS, NEQUALS),
('left', PLUS, MINUS),
('left', STAR, DIVIDE),
('right', UMINUS), # Unary minus operator, unary not
('nonassoc', LESS, LEQ, GREATER, GEQ, IN, BETWEEN, IS, IS_NOT, LIKE),
)
# Top-level statements
@_('show',
'start_transaction',
'commit_transaction',
'rollback_transaction',
'alter_table',
'explain',
'set',
'use',
'describe',
'union',
'select',
'insert',
'update',
'delete',
'drop_database',
'drop_view',
'drop_table',
'create_table',
)
def query(self, p):
return p[0]
# Explain
@_('EXPLAIN identifier')
def explain(self, p):
return Explain(target=p.identifier)
# Alter table
@_('ALTER TABLE identifier id id')
def alter_table(self, p):
return AlterTable(target=p.identifier,
arg=' '.join([p.id0, p.id1]))
# DROP VEW
@_('DROP VIEW identifier')
@_('DROP VIEW IF_EXISTS identifier')
def drop_view(self, p):
if_exists = hasattr(p, 'IF_EXISTS')
return DropView([p.identifier], if_exists=if_exists)
@_('DROP VIEW enumeration')
@_('DROP VIEW IF_EXISTS enumeration')
def drop_view(self, p):
if_exists = hasattr(p, 'IF_EXISTS')
return DropView(p.enumeration, if_exists=if_exists)
# DROP DATABASE
@_('DROP DATABASE identifier')
@_('DROP DATABASE IF_EXISTS identifier')
@_('DROP SCHEMA identifier')
@_('DROP SCHEMA IF_EXISTS identifier')
def drop_database(self, p):
if_exists = hasattr(p, 'IF_EXISTS')
return DropDatabase(name=p.identifier, if_exists=if_exists)
# DROP TABLE
@_('DROP TABLE IF_EXISTS identifier')
@_('DROP TABLE identifier')
def drop_table(self, p):
if_exists = hasattr(p, 'IF_EXISTS')
return DropTables(tables=[p.identifier], if_exists=if_exists)
# Transactions
@_('START TRANSACTION',
'BEGIN')
def start_transaction(self, p):
# https://dev.mysql.com/doc/refman/8.0/en/commit.html
return StartTransaction()
@_('COMMIT')
def commit_transaction(self, p):
return CommitTransaction()
@_('ROLLBACK')
def rollback_transaction(self, p):
return RollbackTransaction()
# --- Set ---
@_('SET set_item_list')
def set(self, p):
set_list = p[1]
if len(set_list) == 1:
return set_list[0]
return Set(set_list=set_list)
@_('set_item',
'set_item_list COMMA set_item')
def set_item_list(self, p):
arr = getattr(p, 'set_item_list', [])
arr.append(p.set_item)
return arr
# set names
@_('id id',
'id constant',
'id id COLLATE constant',
'id id COLLATE id',
'id constant COLLATE constant',
'id constant COLLATE id')
def set_item(self, p):
category = p[0]
if category.lower() != 'names':
raise ParsingException(f'Expected "SET names", got "SET {category}"')
if isinstance(p[1], Constant):
value = p[1]
else:
# is id
value = Constant(p[1], with_quotes=False)
params = {}
if hasattr(p, 'COLLATE'):
if isinstance(p[3], Constant):
val = p[3]
else:
val = Constant(p[3], with_quotes=False)
params['COLLATE'] = val
return Set(category=category, value=value, params=params)
# set charset
@_('charset constant',
'charset id')
def set_item(self, p):
if hasattr(p, 'id'):
arg = Constant(p.id, with_quotes=False)
else:
arg = p.constant
return Set(category='CHARSET', value=arg)
@_('CHARACTER SET',
'CHARSET',
)
def charset(self, p):
return p[0]
# set transaction
@_('set_scope TRANSACTION transact_property_list',
'TRANSACTION transact_property_list')
def set_item(self, p):
isolation_level = None
access_mode = None
transact_scope = getattr(p, 'set_scope', None)
for prop in p.transact_property_list:
if prop['type'] == 'iso_level':
isolation_level = prop['value']
else:
access_mode = prop['value']
params = {}
if isolation_level is not None:
params['isolation level'] = isolation_level
if access_mode is not None:
params['access_mode'] = access_mode
return Set(
category='TRANSACTION',
scope=transact_scope,
params=params
)
@_('transact_property_list COMMA transact_property')
def transact_property_list(self, p):
return p.transact_property_list + [p.transact_property]
@_('transact_property')
def transact_property_list(self, p):
return [p[0]]
@_('ISOLATION LEVEL transact_level',
'transact_access_mode')
def transact_property(self, p):
if hasattr(p, 'transact_level'):
return {'type':'iso_level', 'value':p.transact_level}
else:
return {'type':'access_mode', 'value':p.transact_access_mode}
@_('REPEATABLE READ',
'READ COMMITTED',
'READ UNCOMMITTED',
'SERIALIZABLE')
def transact_level(self, p):
return ' '.join([x for x in p])
@_('READ WRITE',
'READ ONLY')
def transact_access_mode(self, p):
return ' '.join([x for x in p])
@_('identifier EQUALS expr',
'set_scope identifier EQUALS expr',
'variable EQUALS expr',
'set_scope variable EQUALS expr')
def set_item(self, p):
scope = None
name = p[0]
if hasattr(p, 'set_scope'):
scope = p.set_scope
name=p[1]
return Set(name=name, value=p.expr, scope=scope)
@_('GLOBAL',
'PERSIST',
'PERSIST_ONLY',
'SESSION',
)
def set_scope(self, p):
return p[0]
# --- Show ---
@_('show WHERE expr')
def show(self, p):
command = p.show
command.where = p.expr
return command
@_('show LIKE string')
def show(self, p):
command = p.show
command.like = p.string
return command
@_('show FROM identifier')
def show(self, p):
command = p.show
value0 = command.from_table
value1 = p.identifier
if value0 is not None:
value1.parts = value1.parts + value0.parts
command.from_table = value1
return command
@_('show IN identifier')
def show(self, p):
command = p.show
value0 = command.in_table
value1 = p.identifier
if value0 is not None:
value1.parts = value1.parts + value0.parts
command.in_table = value1
return command
@_('SHOW show_category',
'SHOW show_modifier_list show_category')
def show(self, p):
modes = getattr(p, 'show_modifier_list', None)
return Show(
category=p.show_category,
modes=modes
)
@_('SCHEMAS',
'DATABASES',
'TABLES',
'OPEN TABLES',
'TRIGGERS',
'COLUMNS',
'FIELDS',
'PLUGINS',
'VARIABLES',
'INDEXES',
'KEYS',
'SESSION VARIABLES',
'GLOBAL VARIABLES',
'GLOBAL STATUS',
'SESSION STATUS',
'PROCEDURE STATUS',
'FUNCTION STATUS',
'TABLE STATUS',
'MASTER STATUS',
'STATUS',
'STORAGE ENGINES',
'PROCESSLIST',
'INDEX',
'CREATE TABLE',
'WARNINGS',
'ENGINES',
'CHARSET',
'CHARACTER SET',
'COLLATION',
'BINARY LOGS',
'MASTER LOGS',
'PRIVILEGES',
'PROFILES',
'REPLICAS',
'SLAVE HOSTS',
)
def show_category(self, p):
return ' '.join([x for x in p])
# custom show commands
@_('SHOW ENGINE identifier STATUS',
'SHOW ENGINE identifier MUTEX')
def show(self, p):
return Show(
category=p[1],
name=p.identifier.to_string(),
modes=[p[3]]
)
@_('SHOW FUNCTION CODE identifier',
'SHOW PROCEDURE CODE identifier')
def show(self, p):
category = p[1] + ' ' + p[2]
return Show(
category=category,
name=p.identifier.to_string()
)
@_('SHOW REPLICA STATUS FOR CHANNEL id',
'SHOW SLAVE STATUS FOR CHANNEL id',
'SHOW REPLICA STATUS',
'SHOW SLAVE STATUS',)
def show(self, p):
name = getattr(p, 'id', None)
return Show(
category='REPLICA STATUS', # slave = replica
name=name
)
@_('show_modifier',
'show_modifier_list show_modifier')
def show_modifier_list(self, p):
if hasattr(p, 'empty'):
return None
params = getattr(p, 'show_modifier_list', [])
params.append(p.show_modifier)
return params
@_('EXTENDED',
'FULL')
def show_modifier(self, p):
return p[0]
# DELETE
@_('DELETE FROM from_table WHERE expr')
@_('DELETE FROM from_table')
def delete(self, p):
where = getattr(p, 'expr', None)
if where is not None and not isinstance(where, Operation):
raise ParsingException(
f"WHERE must contain an operation that evaluates to a boolean, got: {str(where)}")
return Delete(table=p.from_table, where=where)
# UPDATE
@_('UPDATE identifier SET update_parameter_list')
@_('UPDATE identifier SET update_parameter_list WHERE expr')
def update(self, p):
where = getattr(p, 'expr', None)
return Update(table=p.identifier,
update_columns=p.update_parameter_list,
where=where)
# INSERT
@_('INSERT INTO from_table LPAREN result_columns RPAREN select')
@_('INSERT INTO from_table select')
def insert(self, p):
columns = getattr(p, 'result_columns', None)
return Insert(table=p.from_table, columns=columns, from_select=p.select)
@_('INSERT INTO from_table LPAREN result_columns RPAREN VALUES expr_list_set')
@_('INSERT INTO from_table VALUES expr_list_set')
def insert(self, p):
columns = getattr(p, 'result_columns', None)
return Insert(table=p.from_table, columns=columns, values=p.expr_list_set)
@_('expr_list_set COMMA expr_list_set')
def expr_list_set(self, p):
return p.expr_list_set0 + p.expr_list_set1
@_('LPAREN expr_list RPAREN')
def expr_list_set(self, p):
return [p.expr_list]
# DESCRIBE
@_('DESCRIBE identifier')
def describe(self, p):
return Describe(value=p.identifier)
# USE
@_('USE identifier')
def use(self, p):
return Use(value=p.identifier)
# UNION / UNION ALL
@_('select UNION select')
def union(self, p):
return Union(left=p.select0, right=p.select1, unique=True)
@_('select UNION ALL select')
def union(self, p):
return Union(left=p.select0, right=p.select1, unique=False)
# tableau
@_('LPAREN select RPAREN')
def select(self, p):
return p.select
# create table
@_('CREATE TABLE identifier select')
@_('CREATE TABLE identifier LPAREN select RPAREN')
@_('CREATE OR REPLACE TABLE identifier select')
@_('CREATE OR REPLACE TABLE identifier LPAREN select RPAREN')
def create_table(self, p):
# TODO create table with columns
is_replace = False
if hasattr(p, 'REPLACE'):
is_replace = True
return CreateTable(
name=p.identifier,
is_replace=is_replace,
from_select=p.select
)
# WITH
@_('ctes select')
def select(self, p):
select = p.select
select.cte = p.ctes
return select
@_('ctes COMMA identifier cte_columns_or_nothing AS LPAREN select RPAREN')
def ctes(self, p):
ctes = p.ctes
ctes = ctes + [
CommonTableExpression(
name=p.identifier,
columns=p.cte_columns_or_nothing,
query=p.select)
]
return ctes
@_('WITH identifier cte_columns_or_nothing AS LPAREN select RPAREN')
def ctes(self, p):
return [
CommonTableExpression(
name=p.identifier,
columns=p.cte_columns_or_nothing,
query=p.select)
]
@_('empty')
def cte_columns_or_nothing(self, p):
pass
@_('LPAREN enumeration RPAREN')
def cte_columns_or_nothing(self, p):
return p.enumeration
# SELECT
@_('select FOR UPDATE')
def select(self, p):
select = p.select
ensure_select_keyword_order(select, 'MODE')
select.mode = 'FOR UPDATE'
return select
@_('select OFFSET constant')
def select(self, p):
select = p.select
if select.offset is not None:
raise ParsingException(f'OFFSET already specified for this query')
ensure_select_keyword_order(select, 'OFFSET')
if not isinstance(p.constant.value, int):
raise ParsingException(f'OFFSET must be an integer value, got: {p.constant.value}')
select.offset = p.constant
return select
@_('select LIMIT constant COMMA constant')
def select(self, p):
select = p.select
ensure_select_keyword_order(select, 'LIMIT')
if not isinstance(p.constant0.value, int) or not isinstance(p.constant1.value, int):
raise ParsingException(f'LIMIT must have integer arguments, got: {p.constant0.value}, {p.constant1.value}')
select.offset = p.constant0
select.limit = p.constant1
return select
@_('select LIMIT constant')
def select(self, p):
select = p.select
ensure_select_keyword_order(select, 'LIMIT')
if not isinstance(p.constant.value, int):
raise ParsingException(f'LIMIT must be an integer value, got: {p.constant.value}')
select.limit = p.constant
return select
@_('select ORDER_BY ordering_terms')
def select(self, p):
select = p.select
ensure_select_keyword_order(select, 'ORDER BY')
select.order_by = p.ordering_terms
return select
@_('ordering_terms COMMA ordering_term')
def ordering_terms(self, p):
terms = p.ordering_terms
terms.append(p.ordering_term)
return terms
@_('ordering_term')
def ordering_terms(self, p):
return [p.ordering_term]
@_('ordering_term NULLS_FIRST')
def ordering_term(self, p):
p.ordering_term.nulls = p.NULLS_FIRST
return p.ordering_term
@_('ordering_term NULLS_LAST')
def ordering_term(self, p):
p.ordering_term.nulls = p.NULLS_LAST
return p.ordering_term
@_('identifier DESC')
def ordering_term(self, p):
return OrderBy(field=p.identifier, direction='DESC')
@_('identifier ASC')
def ordering_term(self, p):
return OrderBy(field=p.identifier, direction='ASC')
@_('identifier')
def ordering_term(self, p):
return OrderBy(field=p.identifier, direction='default')
@_('select HAVING expr')
def select(self, p):
select = p.select
ensure_select_keyword_order(select, 'HAVING')
having = p.expr
if not isinstance(having, Operation):
raise ParsingException(
f"HAVING must contain an operation that evaluates to a boolean, got: {str(having)}")
select.having = having
return select
@_('select GROUP_BY expr_list')
def select(self, p):
select = p.select
ensure_select_keyword_order(select, 'GROUP BY')
group_by = p.expr_list
if not isinstance(group_by, list):
group_by = [group_by]
select.group_by = group_by
return select
@_('select WHERE expr')
def select(self, p):
select = p.select
ensure_select_keyword_order(select, 'WHERE')
where_expr = p.expr
if not isinstance(where_expr, Operation):
raise ParsingException(
f"WHERE must contain an operation that evaluates to a boolean, got: {str(where_expr)}")
select.where = where_expr
return select
@_('select FROM from_table_aliased',
'select FROM join_tables_implicit',
'select FROM join_tables')
def select(self, p):
select = p.select
ensure_select_keyword_order(select, 'FROM')
select.from_table = p[2]
return select
# --- join ---
@_('from_table_aliased join_clause from_table_aliased',
'join_tables join_clause from_table_aliased')
def join_tables(self, p):
return Join(left=p[0],
right=p[2],
join_type=p.join_clause)
@_('from_table_aliased join_clause from_table_aliased ON expr',
'join_tables join_clause from_table_aliased ON expr')
def join_tables(self, p):
return Join(left=p[0],
right=p[2],
join_type=p.join_clause,
condition=p.expr)
@_('from_table_aliased COMMA from_table_aliased',
'join_tables_implicit COMMA from_table_aliased')
def join_tables_implicit(self, p):
return Join(left=p[0],
right=p[2],
join_type=JoinType.INNER_JOIN,
implicit=True)
@_('from_table AS identifier',
'from_table identifier',
'from_table AS dquote_string',
'from_table dquote_string',
'from_table')
def from_table_aliased(self, p):
entity = p.from_table
if hasattr(p, 'identifier'):
entity.alias = p.identifier
if hasattr(p, 'dquote_string'):
entity.alias = Identifier(p.dquote_string)
return entity
@_('LPAREN query RPAREN')
def from_table(self, p):
query = p.query
query.parentheses = True
return query
# keywords for table
@_('PLUGINS')
@_('ENGINES')
def from_table(self, p):
return Identifier.from_path_str(p[0])
@_('identifier')
def from_table(self, p):
return p.identifier
@_('parameter')
def from_table(self, p):
return p.parameter
@_('JOIN',
'LEFT JOIN',
'RIGHT JOIN',
'INNER JOIN',
'FULL JOIN',
'CROSS JOIN',
'OUTER JOIN',
'LEFT OUTER JOIN',
'FULL OUTER JOIN',
)
def join_clause(self, p):
return ' '.join([x for x in p])
@_('SELECT DISTINCT result_columns')
def select(self, p):
targets = p.result_columns
return Select(targets=targets, distinct=True)
@_('SELECT result_columns')
def select(self, p):
targets = p.result_columns
return Select(targets=targets)
@_('result_columns COMMA result_column')
def result_columns(self, p):
p.result_columns.append(p.result_column)
return p.result_columns
@_('result_column')
def result_columns(self, p):
return [p.result_column]
@_('result_column AS identifier',
'result_column identifier',
'result_column AS dquote_string',
'result_column dquote_string')
def result_column(self, p):
col = p.result_column
if col.alias:
raise ParsingException(f'Attempt to provide two aliases for {str(col)}')
if hasattr(p, 'dquote_string'):
alias = Identifier(p.dquote_string)
else:
alias = p.identifier
col.alias = alias
return col
@_('LPAREN select RPAREN')
def result_column(self, p):
select = p.select
select.parentheses = True
return select
@_('star')
def result_column(self, p):
return p.star
@_('expr',
'function',
'window_function',
'case')
def result_column(self, p):
return p[0]
# case
@_('CASE case_conditions ELSE expr END')
def case(self, p):
return Case(rules=p.case_conditions, default=p.expr)
@_('case_condition',
'case_conditions case_condition')
def case_conditions(self, p):
arr = getattr(p, 'case_conditions', [])
arr.append(p.case_condition)
return arr
@_('WHEN expr THEN expr')
def case_condition(self, p):
return [p.expr0, p.expr1]
# Window function
@_('function OVER LPAREN window RPAREN')
def window_function(self, p):
return WindowFunction(
function=p.function,
order_by=p.window.get('order_by'),
partition=p.window.get('partition'),
)
@_('window PARTITION_BY expr_list')
def window(self, p):
window = p.window
part_by = p.expr_list
if not isinstance(part_by, list):
part_by = [part_by]
window['partition'] = part_by
return window
@_('window ORDER_BY ordering_terms')
def window(self, p):
window = p.window
window['order_by'] = p.ordering_terms
return window
@_('empty')
def window(self, p):
return {}
# OPERATIONS
@_('LPAREN select RPAREN')
def expr(self, p):
select = p.select
select.parentheses = True
return select
@_('LPAREN expr RPAREN')
def expr(self, p):
if isinstance(p.expr, ASTNode):
p.expr.parentheses = True
return p.expr
@_('id LPAREN expr FROM expr RPAREN')
def function(self, p):
return Function(op=p.id, args=[p.expr0], from_arg=p.expr1)
@_('DATABASE LPAREN RPAREN')
def function(self, p):
return Function(op=p.DATABASE, args=[])
@_('id LPAREN DISTINCT expr_list RPAREN')
def function(self, p):
return Function(op=p.id, distinct=True, args=p.expr_list)
@_('id LPAREN expr_list_or_nothing RPAREN')
@_('id LPAREN star RPAREN')
def function(self, p):
if hasattr(p, 'star'):
args = [p.star]
else:
args = p.expr_list_or_nothing
if not args:
args = []
return Function(op=p.id, args=args)
# arguments are optional in functions, so that things like `select database()` are possible
@_('expr BETWEEN expr AND expr')
def expr(self, p):
return BetweenOperation(args=(p.expr0, p.expr1, p.expr2))
@_('expr_list')
def expr_list_or_nothing(self, p):
return p.expr_list
@_('empty')
def expr_list_or_nothing(self, p):
pass
@_('CAST LPAREN expr AS id LPAREN integer RPAREN RPAREN')
def expr(self, p):
return TypeCast(arg=p.expr, type_name=str(p.id), length=p.integer)
@_('CAST LPAREN expr AS id RPAREN')
def expr(self, p):
return TypeCast(arg=p.expr, type_name=str(p.id))
@_('CONVERT LPAREN expr COMMA id RPAREN')
@_('CONVERT LPAREN expr USING id RPAREN')
def expr(self, p):
return TypeCast(arg=p.expr, type_name=str(p.id))
@_('enumeration')
def expr_list(self, p):
return p.enumeration
@_('expr')
def expr_list(self, p):
return [p.expr]
@_('LPAREN enumeration RPAREN')
def expr(self, p):
tup = Tuple(items=p.enumeration)
return tup
@_('STAR')
def star(self, p):
return Star()
@_('expr NOT IN expr')
def expr(self, p):
op = p[1] + ' ' + p[2]
return BinaryOperation(op=op, args=(p.expr0, p.expr1))
@_('expr PLUS expr',
'expr MINUS expr',
'expr STAR expr',
'expr DIVIDE expr',
'expr MODULO expr',
'expr EQUALS expr',
'expr NEQUALS expr',
'expr GEQ expr',
'expr GREATER expr',
'expr LEQ expr',
'expr LESS expr',
'expr AND expr',
'expr OR expr',
'expr IS_NOT expr',
'expr NOT expr',
'expr IS expr',
'expr LIKE expr',
'expr CONCAT expr',
'expr IN expr')
def expr(self, p):
return BinaryOperation(op=p[1], args=(p.expr0, p.expr1))
@_('MINUS expr %prec UMINUS',
'NOT expr %prec UNOT', )
def expr(self, p):
return UnaryOperation(op=p[0], args=(p.expr,))
# update fields list
@_('update_parameter',
'update_parameter_list COMMA update_parameter')
def update_parameter_list(self, p):
params = getattr(p, 'update_parameter_list', {})
params.update(p.update_parameter)
return params
@_('id EQUALS expr')
def update_parameter(self, p):
return {p.id: p.expr}
# EXPRESSIONS
@_('enumeration COMMA expr')
def enumeration(self, p):
return p.enumeration + [p.expr]
@_('expr COMMA expr')
def enumeration(self, p):
return [p.expr0, p.expr1]
@_('identifier')
@_('parameter')
@_('constant')
@_('function')
def expr(self, p):
return p[0]
@_('NULL')
def constant(self, p):
return NullConstant()
@_('TRUE')
def constant(self, p):
return Constant(value=True)
@_('FALSE')
def constant(self, p):
return Constant(value=False)
@_('integer')
def constant(self, p):
return Constant(value=int(p.integer))
@_('float')
def constant(self, p):
return Constant(value=float(p.float))
@_('string')
def constant(self, p):
return Constant(value=str(p[0]))
@_('identifier DOT identifier')
@_('identifier DOT star')
def identifier(self, p):
node = p[0]
if isinstance(p[2], Star):
node.parts.append(p[2])
else:
node.parts += p[2].parts
return node
@_('id')
def identifier(self, p):
value = p[0]
return Identifier.from_path_str(value)
@_('quote_string',
'dquote_string')
def string(self, p):
return p[0]
@_('PARAMETER')
def parameter(self, p):
return Parameter(value=p.PARAMETER)
# convert to types
@_('ID',
'BEGIN',
'CAST',
'CHANNEL',
'CHARSET',
'CODE',
'COLLATION',
'COLUMNS',
'COMMIT',
'COMMITTED',
'DATABASES',
'DATABASE',
'ENGINE',
'ENGINES',
'EXTENDED',
'FIELDS',
# 'FULL', # fixme: is parsed as alias
'GLOBAL',
'HOSTS',
'INDEXES',
'ISOLATION',
'KEYS',
'LEVEL',
'LOGS',
'MASTER',
'MUTEX',
'OFFSET',
'ONLY',
'OPEN',
'PERSIST',
'PLUGINS',
'PRIVILEGES',
'PROCESSLIST',
'PROFILES',
'REPEATABLE',
'REPLICA',