-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.yy
More file actions
451 lines (399 loc) · 13.6 KB
/
sql.yy
File metadata and controls
451 lines (399 loc) · 13.6 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
/*
sql yacc parser
*/
%{
#include <stdio.h>
#include <string>
#include <vector>
#include <string.h>
#include "gramparser.h"
#define YYLLOC_DEFAULT(Current, Rhs, N) \
do { \
if ((N) > 0) \
(Current) = (Rhs)[1]; \
else \
(Current) = (-1); \
} while (0)
static void base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner,
const char *msg);
static StatementBlock* make_stmt_block(SingleStatement* single_statement);
static void release_list_object(List* list);
static char* sql_strdup(const char* source);
inline List* list_make1(SQLNode* node) {
List* list = new List();
list->push_back(node);
return list;
}
inline List* lappend(List* list,SQLNode* node) {
list->push_back(node);
return list;
}
%}
%pure-parser
%expect 0
%name-prefix="base_yy"
%locations
%parse-param {core_yyscan_t yyscanner}
%lex-param {core_yyscan_t yyscanner}
%union {
core_YYSTYPE core_yystype;
/* these fields must match core_YYSTYPE: */
char* str;
const char* keyword;
int ival;
SQLNode* node;
List* list;
ResTarget* target;
StatementBlock* block;
SingleStatement* stmt;
}
%type <block> stmtblock
%type <stmt> single_statement SelectStmt simple_selectstmt select_with_parens
%type <node> a_expr columnref indirection_el qualified_name relation_expr table_ref
where_clause
%type <node> ColLabel ColId attr_name opt_alias_clause alias_clause opt_all_clause
%type <target> target_el
%type <list> opt_target_list target_list from_clause from_list indirection
%token <str> IDENT FCONST SCONST DOUBLE_SCONST BCONST XCONST Op
%token <ival> ICONST PARAM
%token DOT_DOT
%token LESS_EQUALS GREATER_EQUALS NOT_EQUALS
%type <keyword> unreserved_keyword type_func_name_keyword
%type <keyword> col_name_keyword reserved_keyword
%token <keyword> SELECT FROM WHERE AS SET INT LEFT LIKE RIGHT
OR AND ALL DISTINCT
%left OR
%left AND
%nonassoc '<' '>' '=' LESS_EQUALS GREATER_EQUALS NOT_EQUALS
%left '+' '-'
%left '*' '/' '%'
%left '.'
%left UMINUS
%%
stmtmuti: stmtblock
{
sp_yyget_extra(yyscanner)->block = $1;
}
;
stmtblock: stmtblock ';' single_statement
{
if ( $3 != NULL ) {
($1)->push($3);
}
$$ = $1;
}
| single_statement
{
if ( $1 != NULL ) {
$$ = make_stmt_block($1);
} else {
$$ = NULL;
}
}
;
single_statement:
SelectStmt { $$ = $1; }
| /*empty*/
{
$$ = NULL;
}
;
SelectStmt: simple_selectstmt { $$ = $1; } %prec UMINUS
| select_with_parens { $$ = $1; } %prec UMINUS
;
select_with_parens:
'(' simple_selectstmt ')' { $$ = $2; }
| '(' select_with_parens ')' { $$ = $2; }
;
simple_selectstmt:
SELECT opt_all_clause opt_target_list from_clause where_clause
{
SelectStatement* stmt = new SelectStatement();
stmt->opt_all_clause = (SQLBaseElem*)$2;
stmt->opt_target_list = $3;
stmt->from_list = $4;
stmt->where_clause = $5;
$$ = stmt;
}
;
opt_all_clause: ALL { $$ = new SQLBaseElem(sql_strdup("ALL")); }
| DISTINCT { $$ = new SQLBaseElem(sql_strdup("DISTINCT")); }
| /*empty*/
{
$$ = NULL
}
;
/*****************************************************************************
*
* target list for SELECT
*
*****************************************************************************/
opt_target_list: target_list { $$ = $1; }
| /* EMPTY */ { $$ = NULL; }
;
target_list:
target_el { $$ = list_make1($1); }
| target_list ',' target_el { $$ = lappend($1, $3); }
;
target_el:
a_expr AS ColLabel { $$ = new ResTarget($1,(SQLBaseElem*)$3); }
| a_expr { $$ = new ResTarget($1,NULL); }
| '*'
{
CloumnRef* ref = new CloumnRef();
char* buf = (char*)Allocator::malloc(8);
buf[0] = '*';
buf[1] = 0;
ref->fields.push_back(new SQLBaseElem(buf));
$$ = new ResTarget(ref,NULL);
}
;
a_expr: columnref { $$ = $1; }
| ICONST { $$ = new SQLBaseElem($1); }
| FCONST { $$ = new SQLBaseElem($1); }
| SCONST { $$ = new SQLBaseElem($1,SQLBaseElem::BASE_QUOTE_STRING); }
| BCONST { $$ = new SQLBaseElem($1); }
| XCONST { $$ = new SQLBaseElem($1); }
| a_expr '+' a_expr
{ $$ = new Expression(Expression::AEXPR_OP,"+",$1,$3); }
| a_expr '-' a_expr
{ $$ = new Expression(Expression::AEXPR_OP,"-",$1,$3); }
| a_expr '*' a_expr
{ $$ = new Expression(Expression::AEXPR_OP,"*",$1,$3); }
| a_expr '/' a_expr
{ $$ = new Expression(Expression::AEXPR_OP,"/",$1,$3); }
| a_expr '%' a_expr
{ $$ = new Expression(Expression::AEXPR_OP,"%",$1,$3); }
| a_expr '=' a_expr
{ $$ = new Expression(Expression::AEXPR_OP,"=",$1,$3); }
| a_expr '<' a_expr
{ $$ = new Expression(Expression::AEXPR_OP,"<",$1,$3); }
| a_expr '>' a_expr
{ $$ = new Expression(Expression::AEXPR_OP,">",$1,$3); }
| a_expr AND a_expr
{ $$ = new Expression(Expression::AND_EXPR,NULL,$1,$3); }
| a_expr OR a_expr
{ $$ = new Expression(Expression::OR_EXPR,NULL,$1,$3); }
;
columnref: ColId
{
CloumnRef* ref = new CloumnRef();
ref->fields.push_back((SQLBaseElem*)$1);
$$ = ref;
}
| ColId indirection
{
CloumnRef* ref = new CloumnRef();
ref->fields.push_back((SQLBaseElem*)$1);
for ( List::iterator itr = $2->begin() ; itr != $2->end() ; ++itr) {
ref->fields.push_back((SQLBaseElem*)*itr);
}
delete $2;
$$ = ref;
}
;
indirection_el:
'.' attr_name
{
$$ = $2;
}
;
indirection:
indirection_el { $$ = list_make1($1); }
| indirection indirection_el { $$ = lappend($1, $2); }
;
/*****************************************************************************
*
* clauses common to all Optimizable Stmts:
* from_clause - allow list of both JOIN expressions and table names
* where_clause - qualifications for joins or restrictions
*
*****************************************************************************/
from_clause:
FROM from_list { $$ = $2; }
| /*EMPTY*/ { $$ = NULL; }
;
from_list:
table_ref { $$ = list_make1($1); }
| from_list ',' table_ref { $$ = lappend($1, $3); }
;
where_clause:
WHERE a_expr { $$ = $2; }
| /*EMPTY*/ { $$ = NULL; }
;
/*
* table_ref is where an alias clause can be attached.
*/
table_ref: relation_expr opt_alias_clause
{
((SQLTable*)$1)->alias_ = (SQLBaseElem*)$2;
$$ = $1;
}
| select_with_parens opt_alias_clause
{
$$ = new SQLSubSelect((SelectStatement*)$1,(SQLBaseElem*)$2);
}
;
relation_expr:
qualified_name
{
/* default inheritance */
$$ = $1;
((SQLTable*)$$)->alias_ = NULL;
}
;
alias_clause:
ColId { $$ = $1; }
| AS ColId { $$ = $2; }
;
opt_alias_clause: alias_clause { $$ = $1; }
| /*EMPTY*/ { $$ = NULL; }
;
/*****************************************************************************
*
* Names and constants
*
*****************************************************************************/
/*
* The production for a qualified relation name has to exactly match the
* production for a qualified func_name, because in a FROM clause we cannot
* tell which we are parsing until we see what comes after it ('(' for a
* func_name, something else for a relation). Therefore we allow 'indirection'
* which may contain subscripts, and reject that case in the C code.
*/
qualified_name:
ColId
{
$$ = new SQLTable(NULL, (SQLBaseElem*)$1);
}
| ColId indirection
{
SQLTable* table = new SQLTable(NULL,NULL);
$$ = table;
if ( $2->size() > 0 ) {
table->schema_ = (SQLBaseElem*)$1;
table->table_ = (SQLBaseElem*)($2->back());
$2->pop_back();
release_list_object($2);
}
};
attr_name: ColLabel { $$ = $1; }
| '*'
{
char* buf = (char*)Allocator::malloc(8);
buf[0] = '*';
buf[1] = 0;
$$ = new SQLBaseElem(buf);
}
ColLabel:
IDENT { $$ = new SQLBaseElem($1); }
| DOUBLE_SCONST { $$ = new SQLBaseElem($1,SQLBaseElem::BASE_DQUOTE_STRING);}
| unreserved_keyword { $$ = new SQLBaseElem(sql_strdup($1)); }
| col_name_keyword { $$ = new SQLBaseElem(sql_strdup($1)); }
| type_func_name_keyword { $$ = new SQLBaseElem(sql_strdup($1)); }
| reserved_keyword { $$ = new SQLBaseElem(sql_strdup($1)); }
;
ColId:
IDENT { $$ = new SQLBaseElem($1); }
| DOUBLE_SCONST { $$ = new SQLBaseElem($1,SQLBaseElem::BASE_DQUOTE_STRING);}
| unreserved_keyword { $$ = new SQLBaseElem(sql_strdup($1)); }
| col_name_keyword { $$ = new SQLBaseElem(sql_strdup($1)); }
;
/*
* Keyword category lists. Generally, every keyword present in
* the Postgres grammar should appear in exactly one of these lists.
*
* Put a new keyword into the first list that it can go into without causing
* shift or reduce conflicts. The earlier lists define "less reserved"
* categories of keywords.
*
* Make sure that each keyword's category in kwlist.h matches where
* it is listed here. (Someday we may be able to generate these lists and
* kwlist.h's table from a common master list.)
*/
/* "Unreserved" keywords --- available for use as any kind of name.
*/
unreserved_keyword:
SET
;
/* Column identifier --- keywords that can be column, table, etc names.
*
* Many of these keywords will in fact be recognized as type or function
* names too; but they have special productions for the purpose, and so
* can't be treated as "generic" type or function names.
*
* The type names appearing here are not usable as function names
* because they can be followed by '(' in typename productions, which
* looks too much like a function call for an LR(1) parser.
*/
col_name_keyword:
INT
;
/* Type/function identifier --- keywords that can be type or function names.
*
* Most of these are keywords that are used as operators in expressions;
* in general such keywords can't be column names because they would be
* ambiguous with variables, but they are unambiguous as function identifiers.
*
* Do not include POSITION, SUBSTRING, etc here since they have explicit
* productions in a_expr to support the goofy SQL9x argument syntax.
* - thomas 2000-11-28
*/
type_func_name_keyword:
LEFT
| RIGHT
| LIKE
;
/* Reserved keyword --- these keywords are usable only as a ColLabel.
*
* Keywords appear here if they could not be distinguished from variable,
* type, or function names in some contexts. Don't put things here unless
* forced to.
*/
reserved_keyword:
SELECT
| AS
| WHERE
| FROM
| AND
| OR
| ALL
| DISTINCT
;
%%
static void
base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg)
{
printf("base_yyerror:%s pos:%d\n",msg,*yylloc);
}
StatementBlock* make_stmt_block(SingleStatement* single_statement) {
StatementBlock* block = new StatementBlock();
block->push(single_statement);
return block;
}
/* parser_init()
* Initialize to parse one query string
*/
void
parser_init(base_yy_extra_type *yyext)
{
yyext->block = NULL; /* in case grammar forgets to set it */
}
static void release_list_object(List* list) {
if ( list ) {
for ( List::iterator itr = list->begin(); itr != list->end() ; ++ itr) {
delete (*itr);
}
delete list;
}
}
char* sql_strdup(const char* source) {
if ( source ) {
int length = strlen(source);
char* dup = (char*)Allocator::malloc(length + 1);
strncpy(dup,source,length);
dup[length] = 0;
return dup;
}
return NULL;
}