1
# This program is free software; you can redistribute it and/or modify
2
# it under the terms of the GNU General Public License as published by
3
# the Free Software Foundation; version 2 of the License.
5
# This program is distributed in the hope that it will be useful, but
6
# WITHOUT ANY WARRANTY; without even the implied warranty of
7
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8
# General Public License for more details.
10
# You should have received a copy of the GNU General Public License
11
# along with this program; if not, write to the Free Software
12
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
15
package GenTest::Transform::DisableChosenPlan;
18
@ISA = qw(GenTest GenTest::Transform);
23
use GenTest::Transform;
24
use GenTest::Constants;
28
# This Transform runs EXPLAIN on the query, determines which (subquery) optimizations were used
29
# and disables them so that the query can be rerun with a "second-best" plan. This way the best and
30
# the "second best" plans are checked against one another.
32
# This has the following benefits:
33
# 1. The query plan that is being validated is the one actually chosen by the optimizer, so that one can
34
# run a comprehensive subquery test without having to manually fiddle with @@optimizer_switch
35
# 2. The plan that is used for validation is hopefully also fast enough, as compared to using unindexed nested loop
36
# joins with re-execution of the enitre subquery for each loop.
39
my @explain2switch = (
40
[ 'sort_intersect' => "optimizer_switch='index_merge_sort_intersection=off'"],
41
[ 'intersect' => "optimizer_switch='index_merge_intersection=off'"],
42
[ 'firstmatch' => "optimizer_switch='firstmatch=off'" ],
43
[ '<expr_cache>' => "optimizer_switch='subquery_cache=off'" ],
44
[ 'materializ' => "optimizer_switch='materialization=off'" ], # hypothetical
45
[ 'semijoin' => "optimizer_switch='semijoin=off'" ],
46
[ 'loosescan' => "optimizer_switch='loosescan=off'" ],
47
[ '<exists>' => "optimizer_switch='in_to_exists=off'" ],
48
[ qr{BNLH|BKAH} => "optimizer_switch='join_cache_hashed=off'" ],
49
[ 'BKA' => "optimizer_switch='join_cache_bka=off'" ],
50
[ 'incremental' => "optimizer_switch='join_cache_incremental=off'" ],
51
[ 'join buffer' => "join_cache_level=0" ],
52
[ 'join buffer' => "optimizer_join_cache_level=0" ],
53
[ 'mrr' => "optimizer_use_mrr='disable'" ],
54
[ 'index condition' => "optimizer_switch='index_condition_pushdown=off'" ]
57
my $available_switches;
60
my ($class, $original_query, $executor) = @_;
62
if (not defined $available_switches) {
63
my $optimizer_switches = $executor->dbh()->selectrow_array('SELECT @@optimizer_switch');
64
my @optimizer_switches = split(',', $optimizer_switches);
65
foreach my $optimizer_switch (@optimizer_switches) {
66
my ($switch_name, $switch_value) = split('=', $optimizer_switch);
67
$available_switches->{"optimizer_switch='$switch_name=off'"}++;
70
if ($executor->dbh()->selectrow_array('SELECT @@optimizer_use_mrr')) {
71
$available_switches->{"optimizer_use_mrr='disable'"}++;
74
if ($executor->dbh()->selectrow_array('SELECT @@join_cache_level')) {
75
$available_switches->{"join_cache_level=0"}++;
78
if ($executor->dbh()->selectrow_array('SELECT @@optimizer_join_cache_level')) {
79
$available_switches->{"optimizer_join_cache_level=0"}++;
83
return STATUS_WONT_HANDLE if $original_query !~ m{^\s*SELECT}sio;
85
my $original_explain = $executor->execute("EXPLAIN EXTENDED $original_query");
87
if ($original_explain->status() == STATUS_SERVER_CRASHED) {
88
return STATUS_SERVER_CRASHED;
89
} elsif ($original_explain->status() ne STATUS_OK) {
90
return STATUS_ENVIRONMENT_FAILURE;
93
my $original_explain_string = Dumper($original_explain->data())."\n".Dumper($original_explain->warnings());
95
my @transformed_queries;
96
foreach my $explain2switch (@explain2switch) {
97
my ($explain_fragment, $optimizer_switch) = ($explain2switch->[0], $explain2switch->[1]);
98
next if not exists $available_switches->{$optimizer_switch};
99
if ($original_explain_string =~ m{$explain_fragment}si) {
100
my ($switch_name) = $optimizer_switch =~ m{^(.*?)=}sgio;
101
push @transformed_queries, (
102
'SET @switch_saved = @@'.$switch_name.';',
103
"SET SESSION $optimizer_switch;",
104
"$original_query /* TRANSFORM_OUTCOME_UNORDERED_MATCH */ ;",
105
'SET SESSION '.$switch_name.'=@switch_saved'
110
if ($#transformed_queries > -1) {
111
return \@transformed_queries;
113
return STATUS_WONT_HANDLE;