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
|
#!/usr/bin/env perl
BEGIN {
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 16;
use PerconaTest;
require "$trunk/bin/pt-table-usage";
my @args = qw();
my $in = "$trunk/t/pt-table-usage/samples/in";
my $out = "t/pt-table-usage/samples/out";
my $output = '';
# ############################################################################
# Basic queries that parse without problems.
# ############################################################################
ok(
no_diff(
sub { pt_table_usage::main(@args, "$in/slow001.txt") },
"$out/slow001.txt",
),
'Analysis for slow001.txt'
);
ok(
no_diff(
sub { pt_table_usage::main(@args, "$in/slow002.txt") },
"$out/slow002.txt",
),
'Analysis for slow002.txt (issue 1237)'
);
ok(
no_diff(
sub { pt_table_usage::main(@args, '--query',
'DROP TABLE IF EXISTS t') },
"$out/drop-table-if-exists.txt",
),
'DROP TABLE IF EXISTS'
);
ok(
no_diff(
sub { pt_table_usage::main(@args, '--query',
"create table temp.5 (
datetime DATETIME,
posted DATETIME,
PRIMARY KEY(datetime)
)
SELECT c FROM t WHERE id=1")
},
"$out/create001.txt",
),
'CREATE..SELECT'
);
ok(
no_diff(
sub { pt_table_usage::main(@args, '--query',
"select a.dt,a.hr,a.count
from temp.temp6 a left join n.type b using (dt,hr)
where b.type is null OR b.type=0")
},
"$out/query001.txt",
),
'Multi-column USING'
);
ok(
no_diff(
sub { pt_table_usage::main(@args, '--query',
"SELECT dt.datetime, MAX(re.pd) AS pd FROM d1.t1 t1a INNER JOIN d2.t2 t2a ON CONCAT(t1.a, ' ', t2.a) = t1.datetime INNER JOIN d3.t3 t3a ON t1a.c = t3a.c GROUP BY t1.datetime");
},
"$out/query002.txt",
),
'Function in JOIN clause'
);
# ############################################################################
# --id-attribute
# ############################################################################
ok(
no_diff(
sub { pt_table_usage::main(@args, "$in/slow003.txt",
qw(--id-attribute ts)) },
"$out/slow003-003.txt",
),
'Analysis for slow003.txt with --id-attribute'
);
# ############################################################################
# --constant-data-value
# ############################################################################
$output = output(
sub { pt_table_usage::main('--query', 'INSERT INTO t VALUES (42)',
qw(--constant-data-value <const>)) },
);
like(
$output,
qr/SELECT <const>/,
"--constant-data-value"
);
$output = output(
sub { pt_table_usage::main('--query', 'INSERT INTO t VALUES (42)',
qw(--constant-data-value), "") },
);
like(
$output,
qr/^SELECT\s+$/m,
'--constant-data-value ""'
);
# ############################################################################
# Queries with tables that can't be resolved.
# ############################################################################
# The tables in the WHERE can't be resolved so there's no WHERE access listed.
ok(
no_diff(
sub { pt_table_usage::main(@args, "$in/slow003.txt") },
"$out/slow003-001.txt",
),
'Analysis for slow003.txt'
);
# #############################################################################
# Process fingerprints.
# #############################################################################
my @queries = (
# [ original query, its fingerprint ],
[
"select * from t",
"select * from t",
],
[
"select * from t1, t2 as x, t3 y, z",
"select * from t1, t2 as x, t3 y, z",
],
[
"insert into t values (1, 2, 3)",
"insert into t values(?+)",
],
[
"delete from t where id < 1000",
"delete from t where id < ?",
],
[
"select * from a as t1, b as t2 where t1.id=t2.id",
"select * from a as t1, b as t2 where t1.id=t2.id",
],
[
"replace into t set foo='bar'",
"replace into t set foo=?",
],
);
foreach my $in ( @queries ) {
my $expected = output(
sub { pt_table_usage::main(qw(--query), $in->[0]) },
stderr => 1,
);
my $got = output(
sub { pt_table_usage::main(qw(--query), $in->[1]) },
stderr => 1,
);
is(
$got,
$expected,
"Fingerprint " . (length $in->[1] > 70 ? substr($in->[1], 0, 70)
: $in->[1])
);
}
# #############################################################################
# Done.
# #############################################################################
exit;
|