~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

« back to all changes in this revision

Viewing changes to tests/randgen/lib/GenTest/Transform/DisableChosenPlan.pm

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
4
#
 
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.
 
9
#
 
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
 
13
# USA
 
14
 
 
15
package GenTest::Transform::DisableChosenPlan;
 
16
 
 
17
require Exporter;
 
18
@ISA = qw(GenTest GenTest::Transform);
 
19
 
 
20
use strict;
 
21
use lib 'lib';
 
22
use GenTest;
 
23
use GenTest::Transform;
 
24
use GenTest::Constants;
 
25
use Data::Dumper;
 
26
 
 
27
#
 
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.
 
31
#
 
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.
 
37
#
 
38
 
 
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'" ]
 
55
);
 
56
 
 
57
my $available_switches;
 
58
 
 
59
sub transform {
 
60
        my ($class, $original_query, $executor) = @_;
 
61
 
 
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'"}++;
 
68
                }
 
69
 
 
70
                if ($executor->dbh()->selectrow_array('SELECT @@optimizer_use_mrr')) {
 
71
                        $available_switches->{"optimizer_use_mrr='disable'"}++;
 
72
                }
 
73
 
 
74
                if ($executor->dbh()->selectrow_array('SELECT @@join_cache_level')) {
 
75
                        $available_switches->{"join_cache_level=0"}++;
 
76
                }
 
77
 
 
78
                if ($executor->dbh()->selectrow_array('SELECT @@optimizer_join_cache_level')) {
 
79
                        $available_switches->{"optimizer_join_cache_level=0"}++;
 
80
                }
 
81
        }
 
82
 
 
83
        return STATUS_WONT_HANDLE if $original_query !~ m{^\s*SELECT}sio;
 
84
 
 
85
        my $original_explain = $executor->execute("EXPLAIN EXTENDED $original_query");
 
86
 
 
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;
 
91
        }
 
92
 
 
93
        my $original_explain_string = Dumper($original_explain->data())."\n".Dumper($original_explain->warnings());
 
94
 
 
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'
 
106
                        );
 
107
                }
 
108
        }
 
109
 
 
110
        if ($#transformed_queries > -1) {
 
111
                return \@transformed_queries;
 
112
        } else {
 
113
                return STATUS_WONT_HANDLE;
 
114
        }
 
115
}
 
116
 
 
117
1;