-
Notifications
You must be signed in to change notification settings - Fork 14
/
sql.y
2961 lines (2705 loc) · 88.9 KB
/
sql.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
%{
#include <assert.h>
#include "adlist.h"
#include "sql.h"
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
Stmt *stmtArray[2000];
int indexArray = 0;
Stmt *curStmt = NULL;
void yyerror(char *s, ...);
void debug(char *s, ...);
/* for simple function */
#define NORMAL_FUNCTION(f) do {\
debug ("CALL %s", #f);\
Item *i = calloc(1, sizeof(*i));\
i->token1 = f;\
i->right = (yyvsp[(3) - (4)].item);\
(yyval.item) = i;}\
while(0)
#define NORMAL_FUNCTION_ONE_PARAM(f) do {\
debug ("CALL %s", #f);\
Item *i = calloc(1, sizeof(*i));\
i->token1 = f;\
i->token2 = UNIQUE;\
i->right = (yyvsp[(3) - (4)].item);\
(yyval.item) = i;}\
while(0)
#define NORMAL_FUNCTION_DISTINCT(f) do {\
debug ("CALL %s", #f);\
Item *i = calloc(1, sizeof(*i));\
i->token1 = f;\
i->token2 = DISTINCT;\
i->right = (yyvsp[(4) - (5)].item);\
(yyval.item) = i;}\
while(0)
#define NORMAL_FUNCTION_ANY_PARAM(f) do {\
debug ("CALL %s", #f);\
Item *i = calloc(1, sizeof(*i));\
i->token1 = f;\
i->right = (yyvsp[(3) - (4)].item);\
(yyval.item) = i;}\
while(0)
#define NORMAL_FUNCTION_WILD(f) do {\
debug ("CALL %s", #f);\
Item *i = calloc(1, sizeof(*i));\
i->token1 = f;\
i->name = strdup("*");\
(yyval.item) = i;}\
while(0)
#define NORMAL_FUNCTION_NO_PARAM(f) do {\
debug ("CALL %s", #f);\
Item *i = calloc(1, sizeof(*i));\
i->token1 = f;\
(yyval.item) = i;}\
while(0)
// extern int yydebug = 1;
%}
%union {
int intval;
double floatval;
char *strval;
int subtok;
list* listval;
Item* item;
Table* tableval;
Stmt *stmt;
}
/* names and literal values */
%token <listval> LIST
%token <strval> NAME
%token <strval> STRING
%token <intval> INTNUM
%token <intval> BOOL
%token <floatval> APPROXNUM
/* user @abc names */
%token <strval> USERVAR
/* operators and precedence levels */
%right ASSIGN
%left OR
%left XOR
%left ANDOP
%nonassoc IN IS LIKE REGEXP
%left NOT '!'
%left BETWEEN
%left <subtok> COMPARISON /* = <> < > <= >= <=> */
%left '|'
%left '&'
%left <subtok> SHIFT /* << >> */
%left '+' '-'
%left '*' '/' '%' MOD
%left '^'
%nonassoc UMINUS
/* TODO sql/lex.h */
%token ACCESSIBLE
%token ADD
%token ALL
%token ALTER
%token ANALYZE
%token AND
%token ANY
%token AS
%token ASENSITIVE
%token ASC
%token BEFORE
%token BETWEEN
%token BIGINT
%token BINARY
%token BIT
%token BLOB
%token BOTH
%token BY
%token CALL
%token CASCADE
%token CASE
%token CHANGE
%token CHAR
%token CHECK
%token COLLATE
%token COLUMN
%token CONDITION
%token CONSTRAINT
%token CONTINUE
%token CONVERT
%token CREATE
%token CROSS
%token CURRENT_DATE
%token CURRENT_TIME
%token CURRENT_TIMESTAMP
%token CURRENT_USER
%token CURSOR
%token DATABASE
%token DATABASES
%token DAY_HOUR
%token DAY_MICROSECOND
%token DAY_MINUTE
%token DAY_SECOND
%token DECIMAL
%token DECLARE
%token DEFAULT
%token DELAYED
%token DELETE
%token DESC
%token DETERMINISTIC
%token DISTINCT
%token DISTINCTROW
%token DIV
%token DOUBLE
%token DROP
%token DUAL
%token EACH
%token ELSE
%token ELSEIF
%token ENCLOSED
%token ENUM
%token ESCAPED
%token <subtok> EXISTS
%token EXIT
%token EXPLAIN
%token FALSE
%token FETCH
%token FLOAT
%token FOR
%token FORCE
%token FOREIGN
%token FROM
%token FULLTEXT
%token GRANT
%token GROUP
%token HAVING
%token HIGH_PRIORITY
%token HOUR_MICROSECOND
%token HOUR_MINUTE
%token HOUR_SECOND
%token IF
%token IGNORE
%token IN
%token INDEX
%token INFILE
%token INNER
%token INOUT
%token INSENSITIVE
%token INSERT
%token INT
%token INTEGER
%token INTERVAL
%token INTO
%token ITERATE
%token JOIN
%token KEY
%token KEYS
%token KILL
%token LEADING
%token LEAVE
%token LEFT
%token LIKE
%token LIMIT
%token LINES
%token LINER
%token LOAD
%token LOCALTIME
%token LOCALTIMESTAMP
%token LOCK
%token LONG
%token LONGBLOB
%token LONGTEXT
%token LOOP
%token LOW_PRIORITY
%token MATCH
%token MEDIUMBLOB
%token MEDIUMINT
%token MEDIUMTEXT
%token MINUTE_MICROSECOND
%token MINUTE_SECOND
%token MOD
%token MODIFIES
%token NATURAL
%token NOT
%token NO_WRITE_TO_BINLOG
%token NULLX
%token NUMERIC
%token ON
%token ONDUPLICATE
%token OPTIMIZE
%token OPTION
%token OPTIONALLY
%token OR
%token ORDER
%token OUT
%token OUTER
%token OUTFILE
%token PRECISION
%token PRIMARY
%token PROCEDURE
%token PURGE
%token READ
%token READS
%token REAL
%token REFERENCES
%token REGEXP
%token RELEASE
%token RENAME
%token REPEAT
%token REPLACE
%token REQUIRE
%token RESTRICT
%token RETURN
%token REVOKE
%token RIGHT
%token SCHEMA
%token SCHEMAS
%token SECOND_MICROSECOND
%token SELECT
%token SENSITIVE
%token SEPARATOR
%token SET
%token SETNAMES
%token SETCHARACTER
%token <strval> SETTRAN
%token SETPASSWORD
%token SETOPTION
%token SHOW
%token SHOWVARIABLES
%token SHOWCOLLATION
%token SMALLINT
%token SPATIAL
%token SPECIFIC
%token SQL
%token SQLEXCEPTION
%token SQLSTATE
%token SQLWARNING
%token SQL_BIG_RESULT
%token SQL_CALC_FOUND_ROWS
%token SQL_SMALL_RESULT
%token SSL
%token STARTING
%token STRAIGHT_JOIN
%token TABLE
%token TRUE
%token TERMINATED
%token THEN
%token TINYBLOB
%token TINYINT
%token TINYTEXT
%token TRUNCATE
%token TO
%token TRAILING
%token TRIGGER
%token UNDO
%token UNION
%token UNIQUE
%token UNLOCK
%token UNSIGNED
%token UPDATE
%token USAGE
%token USE
%token USING
%token UTC_DATE
%token UTC_TIME
%token UTC_TIMESTAMP
%token VALUES
%token VARBINARY
%token VARCHAR
%token VARYING
%token WHEN
%token WHERE
%token WHILE
%token WITH
%token WRITE
%token XOR
%token X509
%token YEAR_MONTH
%token ZEROFILL
/* functions with special syntax */
/* sql/item_create.cc func_array */
%token FSTART
%token FABS
%token FACOS
%token FADDTIME
%token FAES_DECRYPT
%token FAES_ENCRYPT
%token FAREA
%token FASBINARY
%token FASIN
%token FASTEXT
%token FASWKB
%token FASWKT
%token FATAN
%token FATAN2
%token FASCII
%token FAVG
%token FADDDATE
%token FBENCHMARK
%token FBIN
%token FBIT_AND
%token FBIT_OR
%token FBIT_XOR
%token FBIT_COUNT
%token FBIT_LENGTH
%token FCAST
%token FCEILING
%token FCENTROID
%token FCHAR
%token FCHARACTER_LENGTH
%token FCOERCIBILITY
%token FCOMPRESS
%token FCONCAT
%token FCONCAT_WS
%token FCONNECTION_ID
%token FCONV
%token FCONVERT_TZ
%token FCOS
%token FCOT
%token FCRC32
%token FCROSSESS
%token FCOUNT
%token FCHARSET
%token FCOLLATION
%token FCURRENT_USER
%token FCURDATE
%token FCURRENT_DATE
%token FCURTIME
%token FCURTIME_TIME
%token FCURRENT_TIMESTAMP
%token FDATE
%token FDATEDIFF
%token FDATENAME
%token FDATEOFMONTH
%token FDATEOFWEEK
%token FDATEOFYEAR
%token FDATE_ADD
%token FDATE_SUB
%token FDATE_FORMAT
%token FDATABASE
%token FDAY
%token FDAYNAME
%token FDAYOFMONTH
%token FDAYOFWEEK
%token FDAYOFYEAR
%token FDEFAULT
%token FDECODE
%token FDEGREES
%token FDES_DECRYPT
%token FDES_ENCRYPT
%token FDIMENSION
%token FISJOINT
%token FELT
%token FENCODE
%token FENCRYPT
%token FENDPOINT
%token FENVELOPE
%token FEQUALS
%token FEXP
%token FEXPORT_SET
%token FEXTRACT
%token FEXTERIORRING
%token FEXTRACTVALUE
%token FFIELD
%token FFIND_IN_SET
%token FFLOOR
%token FFORMAT
%token FFOUND_ROWS
%token FFROM_DAYS
%token FFROM_UNIXTIME
%token FGET_FORMAT
%token FGEOMCOLLFROMTEXT
%token FGEOMCOLLFROMWKB
%token FGEOMETRYCOLLECTIONFROMTEXT
%token FGEOMETRYCOLLECTIONFROMWKB
%token FGEOMETRYFROMTEXT
%token FGEOMETRYFROMWKB
%token FGEOMETRYN
%token FGEOMETRYTYPE
%token FGEOMFROMTEXT
%token FGEOMFROMWKB
%token FGET_LOCK
%token FGLENGTH
%token FGREATEST
%token FHEX
%token FHOUR
%token FINSERT
%token FINSTR
%token FIFNULL
%token FIF
%token FINET_ATON
%token FINET_NTOA
%token FINTERIORRINGN
%token FINTERSECTS
%token FISCLOSED
%token FISEMPTY
%token FISNULL
%token FISSIMPLE
%token FIS_FREE_LOCK
%token FIS_USED_LOCK
%token FLAST_DAY
%token FLAST_INSERT_ID
%token FLCASE
%token FLEFT
%token FLENGTH
%token FLINEFROMTEXT
%token FLINEFROMWKB
%token FLINESTRINGFROMTEXT
%token FLINESTRINGFROMWKB
%token FLN
%token FLOAD_FILE
%token FLOCATE
%token FLOG
%token FLOG10
%token FLOG2
%token FLOWER
%token FLPAD
%token FLTRIM
%token FLOCALTIME
%token FLOCALTIMESTAMP
%token FMAKEDATE
%token FMAKETIME
%token FMAKE_SET
%token FMASTER_POS_WAIT
%token FMBRCONTAINS
%token FMBRDISJOINT
%token FMBREQUAL
%token FMBRINTERSECTS
%token FMBROVERLAPS
%token FMBRTOUCHES
%token FMBRWITHIN
%token FMD5
%token FMLINEFROMTEXT
%token FMLINEFROMWKB
%token FMONTHNAME
%token FMPOINTFROMTEXT
%token FMPOINTFROMWKB
%token FMPOLYFROMTEXT
%token FMPOLYFROMWKB
%token FMULTILINESTRINGFROMTEXT
%token FMULTILINESTRINGFROMWKB
%token FMULTIPOINTFROMTEXT
%token FMULTIPOINTFROMWKB
%token FMULTIPOLYGONFROMTEXT
%token FMULTIPOLYGONFROMWKB
%token FMICROSECOND
%token FMINUTE
%token FMONTH
%token FGROUP_CONCAT
%token FMAX
%token FMID
%token FMIN
%token FMOD
%token FNOW
%token FNAME_CONST
%token FNULLIF
%token FNUMGEOMETRIES
%token FNUMINTERIORRINGS
%token FNUMPOINTS
%token FOCT
%token FOCTET_LENGTH
%token FORD
%token FOVERLAPS
%token FOLD_PASSWORD
%token FPERIOD_ADD
%token FPERIOD_DIFF
%token FPI
%token FPOSITION
%token FPASSWORD
%token FPOINTFROMTEXT
%token FPOINTFROMWKB
%token FPOINTN
%token FPOLYFROMTEXT
%token FPOLYFROMWKB
%token FPOLYGONFROMTEXT
%token FPOLYGONFROMWKB
%token FPOWER
%token FQUARTER
%token FQUOTE
%token FRADIANS
%token FRAND
%token FROUND
%token FROW_COUNT
%token FREPEAT
%token FREPLACE
%token FREVERSE
%token FRIGHT
%token FRPAD
%token FRTRIM
%token FRELEASE_LOCK
%token FSEC_TO_TIME
%token FSEC_TO_DATE
%token FSHA
%token FSHA1
%token FSLEEP
%token FSCHEMA
%token FSOUNDEX
%token FSPACE
%token FSQRT
%token FSRID
%token FSTARTPOINT
%token FSTRCMP
%token FSTR_TO_DATE
%token FSECOND
%token FSUBTIME
%token FSIGN
%token FSIN
%token FSUBSTRING
%token FSUBSTRING_INDEX
%token FSESSION_USER
%token FSTD
%token FSTDDEV
%token FSTDDEV_POP
%token FSTDDEV_SAMP
%token FSUBDATE
%token FSUM
%token FSYSDATE
%token FSYSTEM_USER
%token FTAN
%token FTIMEDIFF
%token FTIME_FORMAT
%token FTIME_TO_SEC
%token FTRUNCATE
%token FTOUCHES
%token FTO_DAYS
%token FTRIM
%token FTIME
%token FTIMESTAMP
%token FTIMESTAMPADD
%token FTIMESTAMPDIFF
%token FVARIANCE
%token FVAR_POP
%token FVAR_SAMP
%token FUCASE
%token FUNCOMPRESS
%token FUNCOMPRESSED_LENGTH
%token FUNHEX
%token FUSER
%token FUNIX_TIMESTAMP
%token FUPDATEXML
%token FUPPER
%token FUUID
%token FUUID_SHORT
%token FVERSION
%token FUTC_DATE
%token FUTC_TIME
%token FUTC_TIMESTAMP
%token FWEEK
%token FWEEKDAY
%token FWEEKOFYEAR
%token FYEAR
%token FYEARWEEK
%token FX
%token FY
%token FEND
%token SUB_OP ADD_OP MUL_OP DIV_OP
%token BITOR_OP BITXOR_OP BITAND_OP
%type <stmt> select_stmt
%type <intval> show_stmt set_stmt SHOWVARIABLES
%type <stmt> table_subquery
%type <listval> val_list opt_val_list case_list index_list
%type <item> expr select_expr set_expr set_password_opt set_opt set_opt_expr expr_or_null_default name_or_default interval_exp
%type <intval> select_opts select_expr_list set_list insert_asgn_list
%type <intval> groupby_list orderby_list opt_with_rollup opt_asc_desc
%type <intval> table_references opt_inner_cross opt_outer table_factor
%type <intval> left_or_right opt_left_or_right_outer column_list
%type <intval> opt_for_join
%type <strval> opt_as_alias
%type <intval> delete_opts delete_list
%type <intval> insert_opts insert_vals insert_vals_list
%type <intval> opt_if_not_exists update_opts update_asgn_list
%type <intval> opt_temporary opt_length opt_binary opt_uz enum_list
%type <intval> column_atts data_type opt_ignore_replace create_col_list
%start stmt_list
%%
/* must ; end */
stmt_list: error {
/* TODO free curStmt */
curStmt = NULL;
}
| stmt ';' {
stmtArray[indexArray] = curStmt;
indexArray++;
curStmt = NULL;
}
| stmt_list stmt ';' {
stmtArray[indexArray] = curStmt;
indexArray++;
curStmt = NULL;
}
;
/* SELECT */
stmt: select_stmt { debug("STMT"); }
;
select_reduce_stmt: SELECT {
Stmt *stmt = calloc(1, sizeof(*stmt));
stmtInit(stmt);
if (curStmt) {
stmt->father = curStmt;
}
debug("select From %p to child %p", curStmt, stmt);
curStmt = stmt;
};
select_stmt: select_reduce_stmt select_opts select_expr_list {
curStmt->sql_command = SQLCOM_SELECT;
// debug("SELECTNODATA %d %d", $2, $3);
$$ = curStmt;
}
| select_reduce_stmt select_opts select_expr_list
FROM table_references
opt_where opt_groupby opt_having opt_orderby opt_limit
opt_into_list {
curStmt->sql_command = SQLCOM_SELECT;
// debug("SELECT %d %d %d", $2, $3, $5);
$$ = curStmt;
}
;
opt_where: /* nil */
| WHERE expr {
debug("WHERE");
listAddNodeTail(curStmt->whereList, $2);
};
/* -GROUPBY */
opt_groupby: /* nil */
| GROUP BY groupby_list opt_with_rollup {
debug("GROUPBYLIST %d %d", $3, $4);
}
;
groupby_list: expr opt_asc_desc {
debug("GROUPBY1 %d", $2);
$$ = 1;
listAddNodeTail(curStmt->groupList, $1);
}
| groupby_list ',' expr opt_asc_desc {
debug("GROUPBY2 %d", $4);
$$ = $1 + 1;
listAddNodeTail(curStmt->groupList, $3);
}
;
opt_asc_desc: /* nil */ { $$ = 0; }
| ASC { $$ = 0; }
| DESC { $$ = 1; }
;
opt_with_rollup: /* nil */ { $$ = 0; }
| WITH NAME { $$ = 1; }
;
opt_having: /* nil */ | HAVING expr {
debug("HAVING");
listAddNodeTail(curStmt->havingList, $2);
};
/* -ORDER */
opt_orderby: /* nil */ | ORDER BY orderby_list {
debug("ORDERBYLIST %d", $3);
};
orderby_list: expr opt_asc_desc {
debug("ORDERBY 1 %d", $2);
$1->isDesc = $2;
listAddNodeTail(curStmt->orderList, $1);
$$ = 1;
}
| orderby_list ',' expr opt_asc_desc {
debug("ORDERBY 2 %d", $4);
$3->isDesc = $4;
listAddNodeTail(curStmt->orderList, $3);
$$ = $1 + 1;
}
;
opt_limit: /* nil */ | LIMIT expr {
debug("LIMIT 1");
listAddNodeTail(curStmt->limitList, $2);
}
| LIMIT expr ',' expr {
debug("LIMIT 2");
listAddNodeTail(curStmt->limitList, $2);
listAddNodeTail(curStmt->limitList, $4);
}
;
opt_into_list: /* nil */
| INTO column_list { debug("INTO %d", $2); }
;
/* using, insert column, create */
column_list: NAME {
debug("COLUMN %s", $1);
Item *i = calloc(1, sizeof(*i));
i->name = strdup($1);
i->token1 = NAME;
if (curStmt->step == InsertColumnStep) {
listAddNodeTail(curStmt->insertList, i);
} else if (curStmt->step == UsingStep) {
listAddNodeTail(curStmt->usingList, i);
}
free($1);
$$ = 1;
}
| column_list ',' NAME {
debug("COLUMN %s", $3);
Item *i = calloc(1, sizeof(*i));
i->name = strdup($3);
i->token1 = NAME;
if (curStmt->step == InsertColumnStep) {
listAddNodeTail(curStmt->insertList, i);
} else if (curStmt->step == UsingStep) {
listAddNodeTail(curStmt->usingList, i);
}
free($3);
$$ = $1 + 1;
}
;
select_opts: { $$ = 0; }
| select_opts ALL { if($$ & 01) yyerror("duplicate ALL option"); $$ = $1 | 01; }
| select_opts DISTINCT { if($$ & 02) yyerror("duplicate DISTINCT option"); $$ = $1 | 02; }
| select_opts DISTINCTROW { if($$ & 04) yyerror("duplicate DISTINCTROW option"); $$ = $1 | 04; }
| select_opts HIGH_PRIORITY { if($$ & 010) yyerror("duplicate HIGH_PRIORITY option"); $$ = $1 | 010; }
| select_opts STRAIGHT_JOIN { if($$ & 020) yyerror("duplicate STRAIGHT_JOIN option"); $$ = $1 | 020; }
| select_opts SQL_SMALL_RESULT { if($$ & 040) yyerror("duplicate SQL_SMALL_RESULT option"); $$ = $1 | 040; }
| select_opts SQL_BIG_RESULT { if($$ & 0100) yyerror("duplicate SQL_BIG_RESULT option"); $$ = $1 | 0100; }
| select_opts SQL_CALC_FOUND_ROWS { if($$ & 0200) yyerror("duplicate SQL_CALC_FOUND_ROWS option"); $$ = $1 | 0200; }
;
select_expr_list: select_expr {
debug("#####%p\n", $1);
listAddNodeTail(curStmt->select_expr_list, $1);
$$ = 1;
}
| select_expr_list ',' select_expr {
debug("#####%p\n", $3);
listAddNodeTail(curStmt->select_expr_list, $3);
$$ = 1;
}
| '*' { debug("SELECT *");
Item *i = calloc(1, sizeof(*i));
i->name = strdup("*");
i->token1 = NAME;
listAddNodeTail(curStmt->select_expr_list, i);
$$ = 1;
};
select_expr: expr_or_null_default opt_as_alias {
debug("SIMPLE SELECT");
if ($2) {
$1->alias = strdup($2);
free($2);
}
$$ = $1;
} | NAME '.' '*' { debug("SELECT %s.*", $1);
Item *i = calloc(1, sizeof(*i));
i->prefix = strdup($1);
i->name = strdup("*");
i->token1 = NAME;
$$ = i;
}
;
table_references: table_reference { $$ = 1; }
| table_references ',' table_reference { $$ = $1 + 1; }
;
table_reference: table_factor
| join_table
;
table_factor:
NAME opt_as_alias index_hint
{ debug("TABLE %s", $1);
Table *t = calloc(1, sizeof(*t));
t->name = strdup($1);
free($1);
if ($2) {
t->alias = strdup($2);
free($2);
}
listAddNodeTail(curStmt->joinList, t);
}
| NAME '.' NAME opt_as_alias index_hint {
debug("TABLE %s.%s", $1, $3);
Table *t = calloc(1, sizeof(*t));
t->db = strdup($1);
t->name = strdup($3);
free($1);
free($3);
if ($4) {
t->alias = strdup($4);
free($4);
}
listAddNodeTail(curStmt->joinList, t);
}
| table_subquery opt_as NAME {
debug("SUBQUERYAS %s", $3);
Table *t = calloc(1, sizeof(*t));
t->sub = $1;
if ($3) {
t->alias = strdup($3);
free($3);
}
listAddNodeTail(curStmt->joinList, t);
}
| '(' table_references ')' { debug("TABLEREFERENCES %d", $2); }
;
opt_as: AS
| /* nil */
;
/* alias possible keywords */
opt_as_alias: opt_as NAME { debug ("ALIAS %s", $2); $$=$2 }
| /* nil */ {$$ = NULL}
;
join_table:
table_reference opt_inner_cross JOIN table_factor opt_join_condition
{ debug("JOIN %d", 0100+$2); }
| table_reference STRAIGHT_JOIN table_factor
{ debug("JOIN %d", 0200); }
| table_reference STRAIGHT_JOIN table_factor ON expr
{ debug("JOIN %d", 0200); }
| table_reference left_or_right opt_outer JOIN table_factor join_condition
{ debug("JOIN %d", 0300+$2+$3); }
| table_reference NATURAL opt_left_or_right_outer JOIN table_factor
{ debug("JOIN %d", 0400+$3); }
;
opt_inner_cross: /* nil */ { $$ = 0; }
| INNER { $$ = 1; }
| CROSS { $$ = 2; }
;
opt_outer: /* nil */ { $$ = 0; }
| OUTER {$$ = 4; }
;
left_or_right: LEFT { $$ = 1; }
| RIGHT { $$ = 2; }
;
opt_left_or_right_outer: LEFT opt_outer { $$ = 1 + $2; }
| RIGHT opt_outer { $$ = 2 + $2; }
| /* nil */ { $$ = 0; }
;
opt_join_condition: join_condition | /* nil */ ;
reduce_using: USING {
curStmt->step = UsingStep;
}
join_condition:
ON expr {
debug("ONEXPR");
listAddNodeTail(curStmt->whereList, $2);
}
| reduce_using '(' column_list ')' { debug("USING %d", $3); }
;
index_hint:
USE KEY opt_for_join '(' index_list ')' {
debug("INDEXHINT %d %d", $5, 010+$3);
Item *i = calloc(1, sizeof(*i));
i->token1 = USE;
i->token2 = KEY;
i->right = $5;
} | IGNORE KEY opt_for_join '(' index_list ')' {
debug("INDEXHINT %d %d", $5, 020+$3);
Item *i = calloc(1, sizeof(*i));
i->token1 = IGNORE;
i->token2 = KEY;
i->right = $5;
} | FORCE KEY opt_for_join '(' index_list ')' {
debug("INDEXHINT %d %d", $5, 030+$3);
Item *i = calloc(1, sizeof(*i));
i->token1 = FORCE;
i->token2 = KEY;
i->right = $5;
} | USE INDEX opt_for_join '(' index_list ')' {
debug("INDEXHINT %d %d", $5, 010+$3);
Item *i = calloc(1, sizeof(*i));
i->token1 = USE;
i->token2 = INDEX;
i->right = $5;
} | IGNORE INDEX opt_for_join '(' index_list ')' {
debug("INDEXHINT %d %d", $5, 020+$3);
Item *i = calloc(1, sizeof(*i));
i->token1 = IGNORE;
i->token2 = INDEX;
i->right = $5;
} | FORCE INDEX opt_for_join '(' index_list ')' {
debug("INDEXHINT %d %d", $5, 030+$3);