-
Notifications
You must be signed in to change notification settings - Fork 243
/
bison_parser.y
executable file
·1266 lines (1089 loc) · 31.3 KB
/
bison_parser.y
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
%{
/**
* bison_parser.y
* defines bison_parser.h
* outputs bison_parser.c
*
* Grammar File Spec: http://dinosaur.compilertools.net/bison/bison_6.html
*
*/
/*********************************
** Section 1: C Declarations
*********************************/
#include "bison_parser.h"
#include "flex_lexer.h"
#include <stdio.h>
#include <string.h>
using namespace hsql;
int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const char *msg) {
result->setIsValid(false);
result->setErrorDetails(strdup(msg), llocp->first_line, llocp->first_column);
return 0;
}
%}
/*********************************
** Section 2: Bison Parser Declarations
*********************************/
// Specify code that is included in the generated .h and .c files
%code requires {
// %code requires block
#include "../sql/statements.h"
#include "../SQLParserResult.h"
#include "parser_typedef.h"
// Auto update column and line number
#define YY_USER_ACTION \
yylloc->first_line = yylloc->last_line; \
yylloc->first_column = yylloc->last_column; \
for(int i = 0; yytext[i] != '\0'; i++) { \
yylloc->total_column++; \
yylloc->string_length++; \
if(yytext[i] == '\n') { \
yylloc->last_line++; \
yylloc->last_column = 0; \
} \
else { \
yylloc->last_column++; \
} \
}
}
// Define the names of the created files (defined in Makefile)
// %output "bison_parser.cpp"
// %defines "bison_parser.h"
// Tell bison to create a reentrant parser
%define api.pure full
// Prefix the parser
%define api.prefix {hsql_}
%define api.token.prefix {SQL_}
%define parse.error verbose
%locations
%initial-action {
// Initialize
@$.first_column = 0;
@$.last_column = 0;
@$.first_line = 0;
@$.last_line = 0;
@$.total_column = 0;
@$.string_length = 0;
};
// Define additional parameters for yylex (http://www.gnu.org/software/bison/manual/html_node/Pure-Calling.html)
%lex-param { yyscan_t scanner }
// Define additional parameters for yyparse
%parse-param { hsql::SQLParserResult* result }
%parse-param { yyscan_t scanner }
/*********************************
** Define all data-types (http://www.gnu.org/software/bison/manual/html_node/Union-Decl.html)
*********************************/
%union {
double fval;
int64_t ival;
char* sval;
uintmax_t uval;
bool bval;
hsql::SQLStatement* statement;
hsql::SelectStatement* select_stmt;
hsql::ImportStatement* import_stmt;
hsql::ExportStatement* export_stmt;
hsql::CreateStatement* create_stmt;
hsql::InsertStatement* insert_stmt;
hsql::DeleteStatement* delete_stmt;
hsql::UpdateStatement* update_stmt;
hsql::DropStatement* drop_stmt;
hsql::PrepareStatement* prep_stmt;
hsql::ExecuteStatement* exec_stmt;
hsql::ShowStatement* show_stmt;
hsql::TransactionStatement* transaction_stmt;
hsql::TableName table_name;
hsql::TableRef* table;
hsql::Expr* expr;
hsql::OrderDescription* order;
hsql::OrderType order_type;
hsql::WithDescription* with_description_t;
hsql::DatetimeField datetime_field;
hsql::LimitDescription* limit;
hsql::ColumnDefinition* column_t;
hsql::ColumnType column_type_t;
hsql::ImportType import_type_t;
hsql::GroupByDescription* group_t;
hsql::UpdateClause* update_t;
hsql::Alias* alias_t;
hsql::SetOperation* set_operator_t;
std::vector<hsql::SQLStatement*>* stmt_vec;
std::vector<char*>* str_vec;
std::vector<hsql::TableRef*>* table_vec;
std::vector<hsql::ColumnDefinition*>* column_vec;
std::vector<hsql::UpdateClause*>* update_vec;
std::vector<hsql::Expr*>* expr_vec;
std::vector<hsql::OrderDescription*>* order_vec;
std::vector<hsql::WithDescription*>* with_description_vec;
}
/*********************************
** Destructor symbols
*********************************/
%destructor { } <fval> <ival> <uval> <bval> <order_type> <datetime_field> <column_type_t> <import_type_t>
%destructor { free( ($$.name) ); free( ($$.schema) ); } <table_name>
%destructor { free( ($$) ); } <sval>
%destructor {
if (($$) != nullptr) {
for (auto ptr : *($$)) {
delete ptr;
}
}
delete ($$);
} <str_vec> <table_vec> <column_vec> <update_vec> <expr_vec> <order_vec> <stmt_vec>
%destructor { delete ($$); } <*>
/*********************************
** Token Definition
*********************************/
%token <sval> IDENTIFIER STRING
%token <fval> FLOATVAL
%token <ival> INTVAL
/* SQL Keywords */
%token DEALLOCATE PARAMETERS INTERSECT TEMPORARY TIMESTAMP
%token DISTINCT NVARCHAR RESTRICT TRUNCATE ANALYZE BETWEEN
%token CASCADE COLUMNS CONTROL DEFAULT EXECUTE EXPLAIN
%token INTEGER NATURAL PREPARE PRIMARY SCHEMAS
%token SPATIAL VARCHAR VIRTUAL DESCRIBE BEFORE COLUMN CREATE DELETE DIRECT
%token DOUBLE ESCAPE EXCEPT EXISTS EXTRACT CAST FORMAT GLOBAL HAVING IMPORT
%token INSERT ISNULL OFFSET RENAME SCHEMA SELECT SORTED
%token TABLES UNIQUE UNLOAD UPDATE VALUES AFTER ALTER CROSS
%token DELTA FLOAT GROUP INDEX INNER LIMIT LOCAL MERGE MINUS ORDER
%token OUTER RIGHT TABLE UNION USING WHERE CALL CASE CHAR COPY DATE DATETIME
%token DESC DROP ELSE FILE FROM FULL HASH HINT INTO JOIN
%token LEFT LIKE LOAD LONG NULL PLAN SHOW TEXT THEN TIME
%token VIEW WHEN WITH ADD ALL AND ASC END FOR INT KEY
%token NOT OFF SET TOP AS BY IF IN IS OF ON OR TO
%token ARRAY CONCAT ILIKE SECOND MINUTE HOUR DAY MONTH YEAR
%token TRUE FALSE
%token TRANSACTION BEGIN COMMIT ROLLBACK
/*********************************
** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html)
*********************************/
%type <stmt_vec> statement_list
%type <statement> statement preparable_statement
%type <exec_stmt> execute_statement
%type <transaction_stmt> transaction_statement
%type <prep_stmt> prepare_statement
%type <select_stmt> select_statement select_with_paren select_no_paren select_clause select_within_set_operation select_within_set_operation_no_parentheses
%type <import_stmt> import_statement
%type <export_stmt> export_statement
%type <create_stmt> create_statement
%type <insert_stmt> insert_statement
%type <delete_stmt> delete_statement truncate_statement
%type <update_stmt> update_statement
%type <drop_stmt> drop_statement
%type <show_stmt> show_statement
%type <table_name> table_name
%type <sval> file_path prepare_target_query
%type <bval> opt_not_exists opt_exists opt_distinct opt_column_nullable opt_all
%type <uval> opt_join_type
%type <table> opt_from_clause from_clause table_ref table_ref_atomic table_ref_name nonjoin_table_ref_atomic
%type <table> join_clause table_ref_name_no_alias
%type <expr> expr operand scalar_expr unary_expr binary_expr logic_expr exists_expr extract_expr cast_expr
%type <expr> function_expr between_expr expr_alias param_expr
%type <expr> column_name literal int_literal num_literal string_literal bool_literal
%type <expr> comp_expr opt_where join_condition opt_having case_expr case_list in_expr hint
%type <expr> array_expr array_index null_literal
%type <limit> opt_limit opt_top
%type <order> order_desc
%type <order_type> opt_order_type
%type <datetime_field> datetime_field
%type <column_t> column_def
%type <column_type_t> column_type
%type <update_t> update_clause
%type <group_t> opt_group
%type <alias_t> opt_table_alias table_alias opt_alias alias
%type <with_description_t> with_description
%type <set_operator_t> set_operator set_type
// ImportType is used for compatibility reasons
%type <import_type_t> opt_file_type file_type
%type <str_vec> ident_commalist opt_column_list
%type <expr_vec> expr_list select_list opt_literal_list literal_list hint_list opt_hints
%type <table_vec> table_ref_commalist
%type <order_vec> opt_order order_list
%type <with_description_vec> opt_with_clause with_clause with_description_list
%type <update_vec> update_clause_commalist
%type <column_vec> column_def_commalist
/******************************
** Token Precedence and Associativity
** Precedence: lowest to highest
******************************/
%left OR
%left AND
%right NOT
%nonassoc '=' EQUALS NOTEQUALS LIKE ILIKE
%nonassoc '<' '>' LESS GREATER LESSEQ GREATEREQ
%nonassoc NOTNULL
%nonassoc ISNULL
%nonassoc IS /* sets precedence for IS NULL, etc */
%left '+' '-'
%left '*' '/' '%'
%left '^'
%left CONCAT
/* Unary Operators */
%right UMINUS
%left '[' ']'
%left '(' ')'
%left '.'
%left JOIN
%%
/*********************************
** Section 3: Grammar Definition
*********************************/
// Defines our general input.
input:
statement_list opt_semicolon {
for (SQLStatement* stmt : *$1) {
// Transfers ownership of the statement.
result->addStatement(stmt);
}
unsigned param_id = 0;
for (void* param : yyloc.param_list) {
if (param != nullptr) {
Expr* expr = (Expr*) param;
expr->ival = param_id;
result->addParameter(expr);
++param_id;
}
}
delete $1;
}
;
statement_list:
statement {
$1->stringLength = yylloc.string_length;
yylloc.string_length = 0;
$$ = new std::vector<SQLStatement*>();
$$->push_back($1);
}
| statement_list ';' statement {
$3->stringLength = yylloc.string_length;
yylloc.string_length = 0;
$1->push_back($3);
$$ = $1;
}
;
statement:
prepare_statement opt_hints {
$$ = $1;
$$->hints = $2;
}
| preparable_statement opt_hints {
$$ = $1;
$$->hints = $2;
}
| show_statement {
$$ = $1;
}
| import_statement {
$$ = $1;
}
| export_statement {
$$ = $1;
}
;
preparable_statement:
select_statement { $$ = $1; }
| create_statement { $$ = $1; }
| insert_statement { $$ = $1; }
| delete_statement { $$ = $1; }
| truncate_statement { $$ = $1; }
| update_statement { $$ = $1; }
| drop_statement { $$ = $1; }
| execute_statement { $$ = $1; }
| transaction_statement { $$ = $1; }
;
/******************************
* Hints
******************************/
opt_hints:
WITH HINT '(' hint_list ')' { $$ = $4; }
| /* empty */ { $$ = nullptr; }
;
hint_list:
hint { $$ = new std::vector<Expr*>(); $$->push_back($1); }
| hint_list ',' hint { $1->push_back($3); $$ = $1; }
;
hint:
IDENTIFIER {
$$ = Expr::make(kExprHint);
$$->name = $1;
}
| IDENTIFIER '(' literal_list ')' {
$$ = Expr::make(kExprHint);
$$->name = $1;
$$->exprList = $3;
}
;
/******************************
* Transaction Statement
******************************/
transaction_statement:
BEGIN opt_transaction_keyword {
$$ = new TransactionStatement(kBeginTransaction);
}
| ROLLBACK opt_transaction_keyword {
$$ = new TransactionStatement(kRollbackTransaction);
}
| COMMIT opt_transaction_keyword {
$$ = new TransactionStatement(kCommitTransaction);
}
;
opt_transaction_keyword:
TRANSACTION
| /* empty */
;
/******************************
* Prepared Statement
******************************/
prepare_statement:
PREPARE IDENTIFIER FROM prepare_target_query {
$$ = new PrepareStatement();
$$->name = $2;
$$->query = $4;
}
;
prepare_target_query: STRING
execute_statement:
EXECUTE IDENTIFIER {
$$ = new ExecuteStatement();
$$->name = $2;
}
| EXECUTE IDENTIFIER '(' opt_literal_list ')' {
$$ = new ExecuteStatement();
$$->name = $2;
$$->parameters = $4;
}
;
/******************************
* Import Statement
* IMPORT FROM TBL FILE 'test/students.tbl' INTO students
* COPY students FROM 'test/students.tbl' [WITH FORMAT TBL]
******************************/
import_statement:
IMPORT FROM file_type FILE file_path INTO table_name {
$$ = new ImportStatement($3);
$$->filePath = $5;
$$->schema = $7.schema;
$$->tableName = $7.name;
}
| COPY table_name FROM file_path opt_file_type {
$$ = new ImportStatement($5);
$$->filePath = $4;
$$->schema = $2.schema;
$$->tableName = $2.name;
}
;
file_type:
IDENTIFIER {
if (strcasecmp($1, "csv") == 0) {
$$ = kImportCSV;
} else if (strcasecmp($1, "tbl") == 0) {
$$ = kImportTbl;
} else if (strcasecmp($1, "binary") == 0 || strcasecmp($1, "bin") == 0) {
$$ = kImportBinary;
} else {
free($1);
yyerror(&yyloc, result, scanner, "File type is unknown.");
YYERROR;
}
free($1);
}
;
file_path:
string_literal { $$ = strdup($1->name); delete $1; }
;
opt_file_type:
WITH FORMAT file_type {
$$ = $3;
}
| /* empty */ { $$ = kImportAuto; }
;
/******************************
* Export Statement
* COPY students TO 'test/students.tbl' (WITH FORMAT TBL)
******************************/
export_statement:
COPY table_name TO file_path opt_file_type {
$$ = new ExportStatement($5);
$$->filePath = $4;
$$->schema = $2.schema;
$$->tableName = $2.name;
}
;
/******************************
* Show Statement
* SHOW TABLES;
******************************/
show_statement:
SHOW TABLES {
$$ = new ShowStatement(kShowTables);
}
| SHOW COLUMNS table_name {
$$ = new ShowStatement(kShowColumns);
$$->schema = $3.schema;
$$->name = $3.name;
}
| DESCRIBE table_name {
$$ = new ShowStatement(kShowColumns);
$$->schema = $2.schema;
$$->name = $2.name;
}
;
/******************************
* Create Statement
* CREATE TABLE students (name TEXT, student_number INTEGER, city TEXT, grade DOUBLE)
* CREATE TABLE students FROM TBL FILE 'test/students.tbl'
******************************/
create_statement:
CREATE TABLE opt_not_exists table_name FROM IDENTIFIER FILE file_path {
$$ = new CreateStatement(kCreateTableFromTbl);
$$->ifNotExists = $3;
$$->schema = $4.schema;
$$->tableName = $4.name;
if (strcasecmp($6, "tbl") != 0) {
free($6);
yyerror(&yyloc, result, scanner, "File type is unknown.");
YYERROR;
}
free($6);
$$->filePath = $8;
}
| CREATE TABLE opt_not_exists table_name '(' column_def_commalist ')' {
$$ = new CreateStatement(kCreateTable);
$$->ifNotExists = $3;
$$->schema = $4.schema;
$$->tableName = $4.name;
$$->columns = $6;
}
| CREATE TABLE opt_not_exists table_name AS select_statement {
$$ = new CreateStatement(kCreateTable);
$$->ifNotExists = $3;
$$->schema = $4.schema;
$$->tableName = $4.name;
$$->select = $6;
}
| CREATE VIEW opt_not_exists table_name opt_column_list AS select_statement {
$$ = new CreateStatement(kCreateView);
$$->ifNotExists = $3;
$$->schema = $4.schema;
$$->tableName = $4.name;
$$->viewColumns = $5;
$$->select = $7;
}
;
opt_not_exists:
IF NOT EXISTS { $$ = true; }
| /* empty */ { $$ = false; }
;
column_def_commalist:
column_def { $$ = new std::vector<ColumnDefinition*>(); $$->push_back($1); }
| column_def_commalist ',' column_def { $1->push_back($3); $$ = $1; }
;
column_def:
IDENTIFIER column_type opt_column_nullable {
$$ = new ColumnDefinition($1, $2, $3);
}
;
column_type:
INT { $$ = ColumnType{DataType::INT}; }
| INTEGER { $$ = ColumnType{DataType::INT}; }
| LONG { $$ = ColumnType{DataType::LONG}; }
| FLOAT { $$ = ColumnType{DataType::FLOAT}; }
| DOUBLE { $$ = ColumnType{DataType::DOUBLE}; }
| VARCHAR '(' INTVAL ')' { $$ = ColumnType{DataType::VARCHAR, $3}; }
| CHAR '(' INTVAL ')' { $$ = ColumnType{DataType::CHAR, $3}; }
| TEXT { $$ = ColumnType{DataType::TEXT}; }
| DATETIME { $$ = ColumnType{DataType::DATETIME}; }
| DATE { $$ = ColumnType{DataType::DATE}; }
;
opt_column_nullable:
NULL { $$ = true; }
| NOT NULL { $$ = false; }
| /* empty */ { $$ = false; }
;
/******************************
* Drop Statement
* DROP TABLE students;
* DEALLOCATE PREPARE stmt;
******************************/
drop_statement:
DROP TABLE opt_exists table_name {
$$ = new DropStatement(kDropTable);
$$->ifExists = $3;
$$->schema = $4.schema;
$$->name = $4.name;
}
| DROP VIEW opt_exists table_name {
$$ = new DropStatement(kDropView);
$$->ifExists = $3;
$$->schema = $4.schema;
$$->name = $4.name;
}
| DEALLOCATE PREPARE IDENTIFIER {
$$ = new DropStatement(kDropPreparedStatement);
$$->ifExists = false;
$$->name = $3;
}
;
opt_exists:
IF EXISTS { $$ = true; }
| /* empty */ { $$ = false; }
;
/******************************
* Delete Statement / Truncate statement
* DELETE FROM students WHERE grade > 3.0
* DELETE FROM students <=> TRUNCATE students
******************************/
delete_statement:
DELETE FROM table_name opt_where {
$$ = new DeleteStatement();
$$->schema = $3.schema;
$$->tableName = $3.name;
$$->expr = $4;
}
;
truncate_statement:
TRUNCATE table_name {
$$ = new DeleteStatement();
$$->schema = $2.schema;
$$->tableName = $2.name;
}
;
/******************************
* Insert Statement
* INSERT INTO students VALUES ('Max', 1112233, 'Musterhausen', 2.3)
* INSERT INTO employees SELECT * FROM stundents
******************************/
insert_statement:
INSERT INTO table_name opt_column_list VALUES '(' literal_list ')' {
$$ = new InsertStatement(kInsertValues);
$$->schema = $3.schema;
$$->tableName = $3.name;
$$->columns = $4;
$$->values = $7;
}
| INSERT INTO table_name opt_column_list select_no_paren {
$$ = new InsertStatement(kInsertSelect);
$$->schema = $3.schema;
$$->tableName = $3.name;
$$->columns = $4;
$$->select = $5;
}
;
opt_column_list:
'(' ident_commalist ')' { $$ = $2; }
| /* empty */ { $$ = nullptr; }
;
/******************************
* Update Statement
* UPDATE students SET grade = 1.3, name='Felix Fürstenberg' WHERE name = 'Max Mustermann';
******************************/
update_statement:
UPDATE table_ref_name_no_alias SET update_clause_commalist opt_where {
$$ = new UpdateStatement();
$$->table = $2;
$$->updates = $4;
$$->where = $5;
}
;
update_clause_commalist:
update_clause { $$ = new std::vector<UpdateClause*>(); $$->push_back($1); }
| update_clause_commalist ',' update_clause { $1->push_back($3); $$ = $1; }
;
update_clause:
IDENTIFIER '=' expr {
$$ = new UpdateClause();
$$->column = $1;
$$->value = $3;
}
;
/******************************
* Select Statement
******************************/
select_statement:
opt_with_clause select_with_paren {
$$ = $2;
$$->withDescriptions = $1;
}
| opt_with_clause select_no_paren {
$$ = $2;
$$->withDescriptions = $1;
}
| opt_with_clause select_with_paren set_operator select_within_set_operation opt_order opt_limit {
$$ = $2;
if ($$->setOperations == nullptr) {
$$->setOperations = new std::vector<SetOperation*>();
}
$$->setOperations->push_back($3);
$$->setOperations->back()->nestedSelectStatement = $4;
$$->setOperations->back()->resultOrder = $5;
$$->setOperations->back()->resultLimit = $6;
$$->withDescriptions = $1;
}
;
select_within_set_operation:
select_with_paren
| select_within_set_operation_no_parentheses;
select_within_set_operation_no_parentheses:
select_clause { $$ = $1; }
| select_clause set_operator select_within_set_operation {
$$ = $1;
if ($$->setOperations == nullptr) {
$$->setOperations = new std::vector<SetOperation*>();
}
$$->setOperations->push_back($2);
$$->setOperations->back()->nestedSelectStatement = $3;
}
;
select_with_paren:
'(' select_no_paren ')' { $$ = $2; }
| '(' select_with_paren ')' { $$ = $2; }
;
select_no_paren:
select_clause opt_order opt_limit {
$$ = $1;
$$->order = $2;
// Limit could have been set by TOP.
if ($3 != nullptr) {
delete $$->limit;
$$->limit = $3;
}
}
| select_clause set_operator select_within_set_operation opt_order opt_limit {
$$ = $1;
if ($$->setOperations == nullptr) {
$$->setOperations = new std::vector<SetOperation*>();
}
$$->setOperations->push_back($2);
$$->setOperations->back()->nestedSelectStatement = $3;
$$->setOperations->back()->resultOrder = $4;
$$->setOperations->back()->resultLimit = $5;
}
;
set_operator:
set_type opt_all {
$$ = $1;
$$->isAll = $2;
}
;
set_type:
UNION {
$$ = new SetOperation();
$$->setType = SetType::kSetUnion;
}
| INTERSECT {
$$ = new SetOperation();
$$->setType = SetType::kSetIntersect;
}
| EXCEPT {
$$ = new SetOperation();
$$->setType = SetType::kSetExcept;
}
;
opt_all:
ALL {
$$ = true;
}
| /* empty */ {
$$ = false;
}
;
select_clause:
SELECT opt_top opt_distinct select_list opt_from_clause opt_where opt_group {
$$ = new SelectStatement();
$$->limit = $2;
$$->selectDistinct = $3;
$$->selectList = $4;
$$->fromTable = $5;
$$->whereClause = $6;
$$->groupBy = $7;
}
;
opt_distinct:
DISTINCT { $$ = true; }
| /* empty */ { $$ = false; }
;
select_list:
expr_list
;
opt_from_clause:
from_clause { $$ = $1; }
| /* empty */ { $$ = nullptr; }
;
from_clause:
FROM table_ref { $$ = $2; }
;
opt_where:
WHERE expr { $$ = $2; }
| /* empty */ { $$ = nullptr; }
;
opt_group:
GROUP BY expr_list opt_having {
$$ = new GroupByDescription();
$$->columns = $3;
$$->having = $4;
}
| /* empty */ { $$ = nullptr; }
;
opt_having:
HAVING expr { $$ = $2; }
| /* empty */ { $$ = nullptr; }
;
opt_order:
ORDER BY order_list { $$ = $3; }
| /* empty */ { $$ = nullptr; }
;
order_list:
order_desc { $$ = new std::vector<OrderDescription*>(); $$->push_back($1); }
| order_list ',' order_desc { $1->push_back($3); $$ = $1; }
;
order_desc:
expr opt_order_type { $$ = new OrderDescription($2, $1); }
;
opt_order_type:
ASC { $$ = kOrderAsc; }
| DESC { $$ = kOrderDesc; }
| /* empty */ { $$ = kOrderAsc; }
;
// TODO: TOP and LIMIT can take more than just int literals.
opt_top:
TOP int_literal { $$ = new LimitDescription($2, nullptr); }
| /* empty */ { $$ = nullptr; }
;
opt_limit:
LIMIT expr { $$ = new LimitDescription($2, nullptr); }
| OFFSET expr { $$ = new LimitDescription(nullptr, $2); }
| LIMIT expr OFFSET expr { $$ = new LimitDescription($2, $4); }
| LIMIT ALL { $$ = new LimitDescription(nullptr, nullptr); }
| LIMIT ALL OFFSET expr { $$ = new LimitDescription(nullptr, $4); }
| /* empty */ { $$ = nullptr; }
;
/******************************
* Expressions
******************************/
expr_list:
expr_alias { $$ = new std::vector<Expr*>(); $$->push_back($1); }
| expr_list ',' expr_alias { $1->push_back($3); $$ = $1; }
;
opt_literal_list:
literal_list { $$ = $1; }
| /* empty */ { $$ = nullptr; }
;
literal_list:
literal { $$ = new std::vector<Expr*>(); $$->push_back($1); }
| literal_list ',' literal { $1->push_back($3); $$ = $1; }
;
expr_alias:
expr opt_alias {
$$ = $1;
if ($2) {
$$->alias = strdup($2->name);
delete $2;
}
}
;
expr:
operand
| between_expr
| logic_expr
| exists_expr
| in_expr
;
operand:
'(' expr ')' { $$ = $2; }
| array_index
| scalar_expr
| unary_expr
| binary_expr
| case_expr
| function_expr
| extract_expr
| cast_expr
| array_expr
| '(' select_no_paren ')' { $$ = Expr::makeSelect($2); }
;
scalar_expr:
column_name
| literal
;
unary_expr:
'-' operand { $$ = Expr::makeOpUnary(kOpUnaryMinus, $2); }
| NOT operand { $$ = Expr::makeOpUnary(kOpNot, $2); }
| operand ISNULL { $$ = Expr::makeOpUnary(kOpIsNull, $1); }
| operand IS NULL { $$ = Expr::makeOpUnary(kOpIsNull, $1); }
| operand IS NOT NULL { $$ = Expr::makeOpUnary(kOpNot, Expr::makeOpUnary(kOpIsNull, $1)); }
;
binary_expr:
comp_expr
| operand '-' operand { $$ = Expr::makeOpBinary($1, kOpMinus, $3); }
| operand '+' operand { $$ = Expr::makeOpBinary($1, kOpPlus, $3); }
| operand '/' operand { $$ = Expr::makeOpBinary($1, kOpSlash, $3); }
| operand '*' operand { $$ = Expr::makeOpBinary($1, kOpAsterisk, $3); }
| operand '%' operand { $$ = Expr::makeOpBinary($1, kOpPercentage, $3); }
| operand '^' operand { $$ = Expr::makeOpBinary($1, kOpCaret, $3); }
| operand LIKE operand { $$ = Expr::makeOpBinary($1, kOpLike, $3); }
| operand NOT LIKE operand { $$ = Expr::makeOpBinary($1, kOpNotLike, $4); }
| operand ILIKE operand { $$ = Expr::makeOpBinary($1, kOpILike, $3); }
| operand CONCAT operand { $$ = Expr::makeOpBinary($1, kOpConcat, $3); }
;
logic_expr:
expr AND expr { $$ = Expr::makeOpBinary($1, kOpAnd, $3); }
| expr OR expr { $$ = Expr::makeOpBinary($1, kOpOr, $3); }
;
in_expr:
operand IN '(' expr_list ')' { $$ = Expr::makeInOperator($1, $4); }
| operand NOT IN '(' expr_list ')' { $$ = Expr::makeOpUnary(kOpNot, Expr::makeInOperator($1, $5)); }
| operand IN '(' select_no_paren ')' { $$ = Expr::makeInOperator($1, $4); }
| operand NOT IN '(' select_no_paren ')' { $$ = Expr::makeOpUnary(kOpNot, Expr::makeInOperator($1, $5)); }
;
// CASE grammar based on: flex & bison by John Levine
// https://www.safaribooksonline.com/library/view/flex-bison/9780596805418/ch04.html#id352665
case_expr:
CASE expr case_list END { $$ = Expr::makeCase($2, $3, nullptr); }
| CASE expr case_list ELSE expr END { $$ = Expr::makeCase($2, $3, $5); }
| CASE case_list END { $$ = Expr::makeCase(nullptr, $2, nullptr); }
| CASE case_list ELSE expr END { $$ = Expr::makeCase(nullptr, $2, $4); }
;
case_list:
WHEN expr THEN expr { $$ = Expr::makeCaseList(Expr::makeCaseListElement($2, $4)); }
| case_list WHEN expr THEN expr { $$ = Expr::caseListAppend($1, Expr::makeCaseListElement($3, $5)); }
;
exists_expr:
EXISTS '(' select_no_paren ')' { $$ = Expr::makeExists($3); }
| NOT EXISTS '(' select_no_paren ')' { $$ = Expr::makeOpUnary(kOpNot, Expr::makeExists($4)); }
;
comp_expr:
operand '=' operand { $$ = Expr::makeOpBinary($1, kOpEquals, $3); }
| operand EQUALS operand { $$ = Expr::makeOpBinary($1, kOpEquals, $3); }
| operand NOTEQUALS operand { $$ = Expr::makeOpBinary($1, kOpNotEquals, $3); }
| operand '<' operand { $$ = Expr::makeOpBinary($1, kOpLess, $3); }
| operand '>' operand { $$ = Expr::makeOpBinary($1, kOpGreater, $3); }
| operand LESSEQ operand { $$ = Expr::makeOpBinary($1, kOpLessEq, $3); }
| operand GREATEREQ operand { $$ = Expr::makeOpBinary($1, kOpGreaterEq, $3); }
;
function_expr:
IDENTIFIER '(' ')' { $$ = Expr::makeFunctionRef($1, new std::vector<Expr*>(), false); }
| IDENTIFIER '(' opt_distinct expr_list ')' { $$ = Expr::makeFunctionRef($1, $4, $3); }
;
extract_expr:
EXTRACT '(' datetime_field FROM expr ')' { $$ = Expr::makeExtract($3, $5); }
;
cast_expr:
CAST '(' expr AS column_type ')' { $$ = Expr::makeCast($3, $5); }
;