1
# Copyright (C) 2008-2009 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
24
require GenTest::Simplifier::SQL;
27
# This script demonstrates the simplification of queries that fail sporadically. In order to account for the sporadicity,
28
# we define an oracle() that runs every query 5000 times, and reports that the problem is gone only if all 5000 instances
29
# returned the same number of rows. Otherwise, it reports that the problem remains, and the Simplifier will use this information
30
# to further quide the simplification process. More information is available at:
32
# http://forge.mysql.com/wiki/RandomQueryGeneratorSimplification
35
my $query = " SELECT table3 .`date_key` field1 FROM B table1 LEFT JOIN B JOIN ( B table3 JOIN ( AA table4 JOIN ( C table6 JOIN A table7 ON table6 .`varchar_nokey` ) ON table6 .`int_key` ) ON table6 .`int_nokey` ) ON table6 .`varchar_nokey` ON table6 .`date_key` WHERE NOT ( ( NOT ( NOT ( NOT ( NOT table7 .`int_key` >= table1 .`varchar_key` OR table3 .`time_nokey` <> table1 .`pk` ) AND table4 .`date_key` >= table1 .`date_key` ) OR table3 .`time_key` > '2005-07-24 11:06:03' ) AND table1 .`datetime_key` <= 8 ) AND table7 .`pk` < 6 ) GROUP BY field1 ORDER BY field1 , field1 , field1 , field1 LIMIT 7 ";
38
my $dsn = 'dbi:mysql:host=127.0.0.1:port=19306:user=root:database=test';
39
my $dbh = DBI->connect($dsn);
41
my $simplifier = GenTest::Simplifier::SQL->new(
44
print "testing $query\n";
46
foreach my $trial (1..$trials) {
47
my $sth = $dbh->prepare($query);
49
return 0 if $sth->err() > 0;
50
$outcomes{$sth->rows()}++;
52
return 1 if scalar(keys %outcomes) > 1;
57
my $simplified = $simplifier->simplify($query);
59
print "Simplified query:\n$simplified;\n";