~drizzle-trunk/drizzle/development

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
query:
  { @nonaggregates = () ; $tables = 0 ; $fields = 0 ; "" } query_type ;


query_type:
  select | select | select | select | select |
  select | select | select | select | select |
  select | select | select | select | select |
  update | update | update | delete | delete ;

distict:  DISTINCT | | | | | | | | | ;

select_option: SQL_SMALL_RESULT | | | | | | | | | | | | | | | | | | | | ;

explain_extended:
    | | | | | | | | | explain_extended2 ;

explain_extended2: | | | | EXPLAIN | EXPLAIN EXTENDED ;


select:
  simple_select | mixed_select ;

simple_select:
  explain_extended SELECT simple_select_list
  FROM join_list
  WHERE where_list 
  optional_group_by
  having_clause
  order_by_clause ;


mixed_select:
  explain_extended SELECT select_list
  FROM join_list
  WHERE where_list
  group_by_clause
  having_clause
  order_by_clause ;

update:
  int_update | char_update ;

int_update:
 int_update_query ; int_select_count ;

int_update_query:
  UPDATE _table[invariant] SET `int_signed` = _digit[invariant] WHERE special_where_list ; 

int_select_count:
  SELECT COUNT(*) FROM _table[invariant] WHERE `int_signed` = _digit[invariant];

char_update:
  char_update_query ; char_select_count ;

char_update_query:
  UPDATE _table[invariant] SET `varchar_5` = _char[invariant] WHERE special_where_list;

char_select_count:
  SELECT COUNT(*) FROM _table[invariant] WHERE `varchar_5` = _char[invariant];


delete:
  int_delete ;

int_delete:
  int_delete_query ; int_select_count ;

int_delete_query:
  DELETE FROM _table[invariant] WHERE `int_signed` = _digit[invariant] and_or special_where_list ;

special_where_list:
  ( special_where_item ) | ( special_where_item ) |
  ( special_where_list and_or special_where_item ) ;

special_where_item:
  _table[invariant] . partitioned_int_field arithmetic_operator _digit |
  _table[invariant] . partitioned_int_field not BETWEEN _digit[invariant] AND ( _digit[invariant] + _digit ) |
  _table[invariant] . partitioned_int_field not IN ( number_list ) |
  _table[invariant] . partitioned_char_field arithmetic_operator _char |
  _table[invariant] . partitioned_char_field not IN (char_list ) ;

#######################################################
# query clause rules
#######################################################

select_list:
   new_select_item | new_select_item | new_select_item |
   new_select_item, select_list | new_select_item, select_list ;

simple_select_list:
   nonaggregate_select_item | nonaggregate_select_item | nonaggregate_select_item |
   nonaggregate_select_item | nonaggregate_select_item, simple_select_list | nonaggregate_select_item, simple_select_list ;

join_list:
   new_table_item join_type new_table_item ON ( join_condition ) |
   new_table_item | new_table_item | new_table_item | new_table_item ;

join_type:
   INNER JOIN | left_right outer JOIN | STRAIGHT_JOIN ;

join_condition:
   current_table_item . int_indexed = previous_table_item . int_indexed |
   current_table_item . char_indexed = previous_table_item . char_indexed ; 


#########################################################
# We use partition pruning friendly clauses here
#########################################################

where_list:
  ( where_item ) | ( where_item ) |
  ( where_list and_or where_item ) ;

where_item:
  table1 . partitioned_int_field arithmetic_operator existing_table_item . int_field |
  table1 . partitioned_int_field arithmetic_operator _digit |
  table1 . partitioned_int_field not BETWEEN _digit[invariant] AND ( _digit[invariant] + _digit ) |
  table1 . partitioned_int_field not IN ( number_list ) |
  table1 . partitioned_char_field arithmetic_operator existing_table_item . char_field |
  table1 . partitioned_char_field arithmetic_operator _char |
  table1 . partitioned_char_field not IN (char_list ) ;

partitioned_int_field:
    `int_signed` ;

partitioned_char_field:
    `varchar_5` ;

int_field:
    `int_signed` | `int_signed_key` ;

char_field:
    `varchar_5` | `varchar_5_key` | `varchar_10` | `varchar_10_key` ;

date_field:
  `datetime` | `date_key` | `datetime_key` | `date` ; 

non_int_field:
  char_field | date_field ;

number_list:
        _digit | number_list, _digit ;

char_list: 
        _char | char_list, _char ;

#########################################################
# GROUP BY / HAVING / ORDER BY rules
#########################################################
group_by_clause:
        { scalar(@nonaggregates) > 0 ? " GROUP BY ".join (', ' , @nonaggregates ) : "" }  ;

optional_group_by:
        | | | | | | | | | group_by_clause ;

having_clause:
        | | | | | | | | | HAVING having_list;

having_list:
        having_item |
        having_item |
        (having_list and_or having_item)  ;

having_item:
        existing_select_item arithmetic_operator value |
        existing_select_item arithmetic_operator value |
        existing_select_item arithmetic_operator value |
        existing_select_item arithmetic_operator value |
        existing_select_item arithmetic_operator existing_select_item ;

order_by_clause:
 | | | | ORDER BY total_order_by desc limit ;

total_order_by:
        { join(', ', map { "field".$_ } (1..$fields) ) };

desc:
  ASC | | | | DESC ;

limit:
  | | | | | | | | | | LIMIT limit_size | LIMIT limit_size OFFSET _digit;

limit_size:
    1 | 2 | 10 | 100 | 1000;

#########################################################
# query component rules
#########################################################

new_select_item:
  nonaggregate_select_item  | aggregate_select_item ;

nonaggregate_select_item:
  table_one_two . _field AS { my $f = "field".++$fields ; push @nonaggregates , $f ; $f } ;

aggregate_select_item:
  aggregate table_one_two . non_int_field ) AS { "field".++$fields } |
  int_aggregate table_one_two . int_field ) AS { "field".++$fields } ;


new_table_item:
	_table AS { "table".++$tables };

table_one_two:
      table1 | table1 | table1 | table1 | table2 ;

current_table_item:
	{ "table".$tables };

previous_table_item:
	{ "table".($tables - 1) };

existing_table_item:
	{ "table".$prng->int(1,$tables) };

left_right:
        LEFT | RIGHT ;

outer: 
        | | | OUTER ;

existing_select_item:
	{ "field".$prng->int(1,$fields) };

int_indexed:
    `int_signed_key` ;

char_indexed:
    `varchar_5_key` | `varchar_10_key` ;

arithmetic_operator:
	= | > | < | != | <> | <= | >= ;

aggregate:
        COUNT( | MIN( | MAX( ;

int_aggregate:
        SUM( | aggregate ;

and_or:
        AND | AND | OR ;

not:
   | | | | NOT ;


value:
        _digit | _digit | _digit | _digit | _digit |
        _char | _char | _char | _char | _char ;