1
# Copyright (C) 2008-2010 Sun Microsystems, Inc. All rights reserved.
2
# Use is subject to license terms.
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; version 2 of the License.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
# General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18
# **NOTE** Joins for this grammar are currently not working as intended.
19
# For example, if we have tables 1, 2, and 3, we end up with ON conditions that
20
# only involve tables 2 and 3.
21
# This will be fixed, but initial attempts at altering this had a negative
22
# impact on the coverage the test was providing. To be fixed when scheduling
23
# permits. We are still seeing significant coverage with the grammar as-is.
25
################################################################################
26
# optimizer_no_subquery.yy: Random Query Generator grammar for testing #
27
# non-subquery optimizations. This grammar #
28
# *should* hit the optimizations listed here: #
29
# https://inside.mysql.com/wiki/Optimizer_grammar_worksheet #
30
# see: WL#5006 Random Query Generator testing of Azalea Optimizer- subqueries #
31
# https://intranet.mysql.com/worklog/QA-Sprint/?tid=5006 #
34
# queries: 10k+. We can see a lot with lower values, but over 10k is #
35
# best. The intersect optimization happens with low frequency #
36
# so larger values help us to hit it at least some of the time #
37
# engines: MyISAM *and* Innodb. Certain optimizations are only hit with #
38
# one engine or another and we should use both to ensure we #
39
# are getting maximum coverage #
40
# Validators: ResultsetComparatorSimplify #
41
# - used on server-server comparisons #
42
# Transformer - used on a single server #
43
# - creates equivalent versions of a single query #
44
# SelectStability - used on a single server #
45
# - ensures the same query produces stable result sets #
46
################################################################################
48
################################################################################
49
# The perl code in {} helps us with bookkeeping for writing more sensible #
50
# queries. We need to keep track of these items to ensure we get interesting #
51
# and stable queries that find bugs rather than wondering if our query is #
53
################################################################################
55
{ @nonaggregates = () ; $tables = 0 ; $fields = 0 ; "" } query_type ;
59
main_select | main_select | main_select | loose_scan ;
61
################################################################################
62
# The loose* rules listed below are to hit the 'Using index for group-by' #
63
# optimization. This optimization has some strict requirements, thus #
64
# we needed a separate query pattern to ensure we hit it. #
65
################################################################################
67
SELECT distinct loose_select_clause
69
WHERE generic_where_list;
74
# MIN( _field_indexed) AS { "field".++$fields } , loose_select_list |
75
# MAX( _field_indexed) AS { "field".++$fields } , loose_select_list |
76
# MIN( _field_indexed[invariant] ) AS { "field".++$fields }, MAX( _field_indexed[invariant] ) AS { "field".++$fields }, loose_select_list ;
80
loose_select_item , loose_select_list ;
83
_field AS { my $f = "field".++$fields ; push @nonaggregates , $f ; $f } ;
85
################################################################################
86
# The bulk of interesting things happen with this main rule #
87
################################################################################
89
SELECT distinct select_option select_list
99
select_option: | | | | | | | | | SQL_SMALL_RESULT ;
103
new_select_item , select_list |
104
new_select_item , select_list ;
107
################################################################################
108
# this limits us to 2 and 3 table joins / can use it if we hit #
109
# too many mega-join conditions which take too long to run #
110
################################################################################
111
( new_table_item join_type new_table_item ON (join_condition_list ) ) |
112
( new_table_item join_type ( ( new_table_item join_type new_table_item ON (join_condition_list ) ) ) ON (join_condition_list ) ) ;
115
################################################################################
116
# preventing deep join nesting for run time / table access methods are more #
117
# important here - join.yy can provide deeper join coverage #
118
# Enabling this / swapping out with join_list above can produce some #
119
# time-consuming queries. #
120
################################################################################
123
( new_table_item join_type join_list ON (join_condition_list ) ) ;
126
INNER JOIN | left_right outer JOIN | STRAIGHT_JOIN ;
129
join_condition_item |
130
( join_condition_item ) and_or ( join_condition_item ) |
131
( current_table_item .`pk` arithmetic_operator previous_table_item . int_field_name ) AND (current_table_item .`pk` arithmetic_operator previous_table_item . int_field_name ) ;
133
current_table_item . int_indexed = previous_table_item . int_field_name |
134
current_table_item . int_field_name = previous_table_item . int_indexed |
135
current_table_item . `col_varchar_key` = previous_table_item . char_field_name |
136
current_table_item . char_field_name = previous_table_item . `col_varchar_key` |
137
current_table_item . int_indexed arithmetic_operator previous_table_item . int_field_name |
138
current_table_item . int_field_name arithmetic_operator previous_table_item . int_indexed |
139
current_table_item . `col_varchar_key` arithmetic_operator previous_table_item . char_field_name |
140
current_table_item . char_field_name arithmetic_operator previous_table_item . `col_varchar_key`;
155
range_predicate1_list | range_predicate2_list |
156
range_predicate1_list and_or generic_where_list |
157
range_predicate2_list and_or generic_where_list ;
162
( where_list and_or where_item ) ;
167
################################################################################
168
# The IS not NULL values in where_item are to hit the ref_or_null and #
169
# the not_exists optimizations. The LIKE '%a%' rule is to try to hit the #
170
# rnd_pos optimization #
171
################################################################################
173
table1 .`pk` arithmetic_operator existing_table_item . int_field_name |
174
table1 .`pk` arithmetic_operator existing_table_item . int_field_name |
175
existing_table_item . int_field_name arithmetic_operator existing_table_item . int_field_name |
176
existing_table_item . char_field_name arithmetic_operator existing_table_item . char_field_name |
177
existing_table_item . int_field_name arithmetic_operator int_value |
178
existing_table_item . char_field_name arithmetic_operator char_value |
179
table1 .`pk` IS not NULL |
180
table1 . _field IS not NULL |
181
table1 . int_indexed arithmetic_operator int_value AND ( table1 . char_field_name LIKE '%a%' OR table1.char_field_name LIKE '%b%') ;
183
################################################################################
184
# The range_predicate_1* rules below are in place to ensure we hit the #
185
# index_merge/sort_union optimization. #
186
# NOTE: combinations of the predicate_1 and predicate_2 rules tend to hit the #
187
# index_merge/intersect optimization #
188
################################################################################
190
range_predicate1_list:
191
range_predicate1_item |
192
( range_predicate1_item OR range_predicate1_list ) ;
194
range_predicate1_item:
195
table1 . int_indexed not BETWEEN _tinyint_unsigned[invariant] AND ( _tinyint_unsigned[invariant] + _tinyint_unsigned ) |
196
table1 . `col_varchar_key` arithmetic_operator _char[invariant] |
197
table1 . int_indexed not IN (number_list) |
198
table1 . `col_varchar_key` not IN (char_list) |
199
table1 . `pk` > _tinyint_unsigned[invariant] AND table1 . `pk` < ( _tinyint_unsigned[invariant] + _tinyint_unsigned ) |
200
table1 . `col_int_key` > _tinyint_unsigned[invariant] AND table1 . `col_int_key` < ( _tinyint_unsigned[invariant] + _tinyint_unsigned ) ;
202
################################################################################
203
# The range_predicate_2* rules below are in place to ensure we hit the #
204
# index_merge/union optimization. #
205
# NOTE: combinations of the predicate_1 and predicate_2 rules tend to hit the #
206
# index_merge/intersect optimization #
207
################################################################################
209
range_predicate2_list:
210
range_predicate2_item |
211
( range_predicate2_item and_or range_predicate2_list ) ;
213
range_predicate2_item:
214
table1 . `pk` = _tinyint_unsigned |
215
table1 . `col_int_key` = _tinyint_unsigned |
216
table1 . `col_varchar_key` = _char |
217
table1 . int_indexed = _tinyint_unsigned |
218
table1 . `col_varchar_key` = _char |
219
table1 . int_indexed = existing_table_item . int_indexed |
220
table1 . `col_varchar_key` = existing_table_item . `col_varchar_key` ;
222
################################################################################
223
# The number and char_list rules are for creating WHERE conditions that test #
224
# 'field' IN (list_of_items) #
225
################################################################################
227
_tinyint_unsigned | number_list, _tinyint_unsigned ;
230
_char | char_list, _char ;
232
################################################################################
233
# We ensure that a GROUP BY statement includes all nonaggregates. #
234
# This helps to ensure the query is more useful in detecting real errors / #
235
# that the query doesn't lend itself to variable result sets #
236
################################################################################
239
{ scalar(@nonaggregates) > 0 ? " GROUP BY ".join (', ' , @nonaggregates ) : "" } ;
242
| HAVING having_list;
247
(having_list and_or having_item) ;
250
existing_select_item arithmetic_operator value ;
252
################################################################################
253
# We use the total_order_by rule when using the LIMIT operator to ensure that #
254
# we have a consistent result set - server1 and server2 should not differ #
255
################################################################################
259
ORDER BY table1 . _field_indexed desc , total_order_by limit |
260
ORDER BY order_by_list |
261
ORDER BY order_by_list, total_order_by limit ;
264
{ join(', ', map { "field".$_ } (1..$fields) ) };
268
order_by_item , order_by_list ;
271
table1 . _field_indexed , existing_table_item .`pk` desc |
272
table1 . _field_indexed desc |
273
existing_select_item desc |
274
CONCAT ( existing_table_item . char_field_name, existing_table_item . char_field_name );
278
################################################################################
279
# We mix digit and _digit here. We want to alter the possible values of LIMIT #
280
# To ensure we hit varying EXPLAIN plans, but the OFFSET can be smaller #
281
################################################################################
285
#| LIMIT limit_size | LIMIT limit_size OFFSET _digit;
288
nonaggregate_select_item |
289
nonaggregate_select_item |
290
# aggregate_select_item |
293
################################################################################
294
# We have the perl code here to help us write more sensible queries #
295
# It allows us to use field1...fieldn in the WHERE, ORDER BY, and GROUP BY #
296
# clauses so that the queries will produce more stable and interesting results #
297
################################################################################
299
nonaggregate_select_item:
300
table_one_two . _field_indexed AS { my $f = "field".++$fields ; push @nonaggregates , $f ; $f } |
301
table_one_two . _field_indexed AS { my $f = "field".++$fields ; push @nonaggregates , $f ; $f } |
302
table_one_two . _field AS { my $f = "field".++$fields ; push @nonaggregates , $f ; $f } ;
304
aggregate_select_item:
305
aggregate table_one_two . _field ) AS { "field".++$fields };
307
################################################################################
308
# The combo_select_items are for 'spice' - we actually found #
309
################################################################################
312
( ( table_one_two . int_field_name ) math_operator ( table_one_two . int_field_name ) ) AS { my $f = "field".++$fields ; push @nonaggregates , $f ; $f } |
313
CONCAT ( table_one_two . char_field_name , table_one_two . char_field_name ) AS { my $f = "field".++$fields ; push @nonaggregates , $f ; $f } ;
319
COUNT( distinct | SUM( distinct | MIN( distinct | MAX( distinct ;
321
################################################################################
322
# The following rules are for writing more sensible queries - that we don't #
323
# reference tables / fields that aren't present in the query and that we keep #
324
# track of what we have added. You shouldn't need to touch these ever #
325
################################################################################
327
_table AS { "table".++$tables };
333
{ "table".($tables - 1) };
336
{ "table".$prng->int(1,$tables) };
338
existing_select_item:
339
{ "field".$prng->int(1,$fields) };
340
################################################################################
341
# end of utility rules #
342
################################################################################
345
= | > | < | != | <> | <= | >= ;
347
################################################################################
348
# We are trying to skew the ON condition for JOINs to be largely based on #
349
# equalities, but to still allow other arithmetic operators #
350
################################################################################
351
join_condition_operator:
352
arithmetic_operator | = | = | = ;
354
################################################################################
355
# Used for creating combo_items - ie (field1 + field2) AS fieldX #
356
# We ignore division to prevent division by zero errors #
357
################################################################################
361
################################################################################
362
# We stack AND to provide more interesting options for the optimizer #
363
# Alter these percentages at your own risk / look for coverage regressions #
364
# with --debug if you play with these. Those optimizations that require an #
365
# OR-only list in the WHERE clause are specifically stacked in another rule #
366
################################################################################
372
_digit | _digit | _digit | _digit | _tinyint_unsigned|
373
_char(2) | _char(2) | _char(2) | _char(2) | _char(2) ;
376
_digit | _digit | _digit | _digit | _tinyint_unsigned ;
385
A | B | C | BB | CC | B | C | BB | CC |
386
C | C | C | C | C | C | C | C | C |
387
CC | CC | CC | CC | CC | CC | CC | CC |
390
################################################################################
391
# Add a possibility for 'view' to occur at the end of the previous '_table' rule
392
# to allow a chance to use views (when running the RQG with --views)
393
################################################################################
396
view_A | view_B | view_C | view_BB | view_CC ;
399
int_field_name | char_field_name ;
402
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | _tinyint_unsigned ;
406
`pk` | `col_int_key` | `col_int_nokey` ;
409
`pk` | `col_int_key` ;
413
`col_varchar_key` | `col_varchar_nokey` ;
415
################################################################################
416
# We define LIMIT_rows in this fashion as LIMIT values can differ depending on #
417
# how large the LIMIT is - LIMIT 2 = LIMIT 9 != LIMIT 19 #
418
################################################################################
421
1 | 2 | 10 | 100 | 1000;