File: KeySize.t

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (325 lines) | stat: -rw-r--r-- 8,012 bytes parent folder | download | duplicates (2)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/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;

use KeySize;
use TableParser;
use Quoter;
use DSNParser;
use Sandbox;
use PerconaTest;

my $dp  = new DSNParser(opts=>$dsn_opts);
my $sb  = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh = $sb->get_dbh_for('master');

if ( !$dbh ) {
   plan skip_all => "Cannot connect to sandbox master";
}

my $q  = new Quoter();
my $tp = new TableParser(Quoter => $q);
my $ks = new KeySize(Quoter=>$q);

my $tbl;
my $struct;
my %key;
my ($size, $chosen_key); 

sub key_info {
   my ( $file, $db, $tbl, $key, $cols ) = @_;
   $sb->load_file('master', $file, $db);
   my $tbl_name = $q->quote($db, $tbl);
   my $struct   = $tp->parse( load_file($file) );
   return (
      name       => $key,
      cols       => $cols || $struct->{keys}->{$key}->{cols},
      tbl_name   => $tbl_name,
      tbl_struct => $struct,
      dbh        => $dbh,
   );
}

$sb->create_dbs($dbh, ['test']);

isa_ok($ks, 'KeySize');

# With an empty table, the WHERE is impossible, so MySQL should optimize
# away the query, and key_len and rows will be NULL in EXPLAIN.
%key = key_info('t/lib/samples/dupe_key.sql', 'test', 'dupe_key', 'a');
is(
   $ks->get_key_size(%key),
   undef,
   'Empty table, impossible where'
);

# Populate the table to make the WHERE possible.
$dbh->do('INSERT INTO test.dupe_key VALUE (1,2,3),(4,5,6),(7,8,9),(0,0,0)');
is_deeply(
   [$ks->get_key_size(%key)],
   [20, 'a'],
   'Single column int key'
);

$key{name} = 'a_2';
is_deeply(
   [$ks->get_key_size(%key)],
   [40, 'a_2'],
   'Two column int key'
);

$sb->load_file('master', 't/lib/samples/issue_331-parent.sql', 'test');
%key = key_info('t/lib/samples/issue_331.sql', 'test', 'issue_331_t2', 'fk_1', ['id']);
($size, $chosen_key) = $ks->get_key_size(%key);
is(
   $size,
   8,
   'Foreign key size'
);
is(
   $chosen_key,
   'PRIMARY',
   'PRIMARY key chosen for foreign key'
);

# #############################################################################
# Issue 364: Argument "9,8" isn't numeric in multiplication (*) at
# mk-duplicate-key-checker line 1894
# #############################################################################
$dbh->do('USE test');
$dbh->do('DROP TABLE IF EXISTS test.issue_364');
%key = key_info(
   't/lib/samples/issue_364.sql',
   'test',
   'issue_364',
   'BASE_KID_ID',
   [qw(BASE_KID_ID ID)]
);
$sb->load_file('master', 't/lib/samples/issue_364-data.sql', 'test');

# This issue had another issue: the key is ALL CAPS, but TableParser
# lowercases all identifies, so KeySize said the key didn't exist.
# This was the root problem.  Once KeySize saw the key it added a
# FORCE INDEX and the index_merge went away.  Later, we'll drop the
# real key and add one back over the same columns so that KeySize
# won't see its key but one will exist with which to do merge_index.
ok(
   $ks->_key_exists(%key),
   'Key exists (issue 364)'
);

my $output = `/tmp/12345/use -D test -e 'EXPLAIN SELECT BASE_KID_ID, ID FROM test.issue_364 WHERE BASE_KID_ID=1 OR ID=1'`;
like(
   $output,
   qr/index_merge/,
   'Query uses index_merge (issue 364)'
);

# lets help get_key_size get an accurate figure by ANALYZING the table first
# otherwise we get spurious false positives that are off by just a bit
$dbh->do('ANALYZE TABLE test.issue_364');
($size, $chosen_key) = $ks->get_key_size(%key);
is(
   $size,
   17 * 176,
   'Key size (issue 364)'
);
is(
   $chosen_key,
   'BASE_KID_ID',
   'Chosen key (issue 364)'
);
is(
   $ks->error(),
   '',
   'No error (issue 364)'
);
my $want = $sandbox_version lt '5.7' ? 
   qr/^extra: Using where; Using index
id: 1
key: BASE_KID_ID
key_len: 17
possible_keys: BASE_KID_ID
ref: NULL
rows: 17[1-9]
select_type: SIMPLE
table: issue_364
type: index\Z/ :
  qr/^extra: Using where; Using index
filtered: 1.13
id: 1
key: BASE_KID_ID
key_len: 17
partitions: NULL
possible_keys: BASE_KID_ID
ref: NULL
rows: 176
select_type: SIMPLE
table: issue_364
type: index/;

like(
   $ks->explain(),
   $want,
   'EXPLAIN plan (issue 364)'
);

is(
   $ks->query(),
   'EXPLAIN SELECT BASE_KID_ID, ID FROM `test`.`issue_364` FORCE INDEX (`BASE_KID_ID`) WHERE BASE_KID_ID=1 OR ID=1',
   'Query (issue 364)'
);

# KeySize doesn't actually check the table to see if the key exists.
# It trusts that tbl_struct->{keys} is accurate.  So if we delete the
# key here, we'll fool KeySize and simulate the original problem.
delete $key{tbl_struct}->{keys}->{'base_kid_id'};
($size, $chosen_key) = $ks->get_key_size(%key);
is(
   $size,
   undef,
   'Key size 0 (issue 364)'
);
is(
   $chosen_key,
   undef,
   'Chose multiple keys (issue 364)'
);
is(
   $ks->error(),
   'MySQL chose multiple keys: BASE_KID_ID,PRIMARY',
   'Error about multiple keys (issue 364)'
);
is(
   $ks->query(),
   'EXPLAIN SELECT BASE_KID_ID, ID FROM `test`.`issue_364` WHERE BASE_KID_ID=1 OR ID=1',
   'Query without FORCE INDEX (issue 364)'
);

# #############################################################################
# https://bugs.launchpad.net/percona-toolkit/+bug/1201443
# #############################################################################
$sb->load_file('master', "t/pt-duplicate-key-checker/samples/fk_chosen_index_bug_1201443.sql");

($size, $chosen_key) = $ks->get_key_size(
   name       => 'child_ibfk_2',
   cols       => [qw(parent_id)],
   tbl_name   => 'fk_chosen_index_bug_1201443.child',
   tbl_struct => {
        charset => 'latin1',
        clustered_key => undef,
        col_posn => {
          id => 0,
          parent_id => 1
        },
        cols => [
          'id',
          'parent_id'
        ],
        defs => {
          id => '  `id` int(11) NOT NULL AUTO_INCREMENT',
          parent_id => '  `parent_id` int(11) NOT NULL'
        },
        engine => 'InnoDB',
        is_autoinc => {
          id => 1,
          parent_id => 0
        },
        is_col => {
          id => 1,
          parent_id => 1
        },
        is_nullable => {},
        is_numeric => {
          id => 1,
          parent_id => 1
        },
        keys => {
          id => {
            col_prefixes => [
              undef
            ],
            colnames => '`id`',
            cols => [
              'id'
            ],
            ddl => 'KEY `id` (`id`),',
            is_col => {
              id => 1
            },
            is_nullable => 0,
            is_unique => 0,
            name => 'id',
            type => 'BTREE'
          },
          parent_id => {
            col_prefixes => [
              undef
            ],
            colnames => '`parent_id`',
            cols => [
              'parent_id'
            ],
            ddl => 'KEY `parent_id` (`parent_id`),',
            is_col => {
              parent_id => 1
            },
            is_nullable => 0,
            is_unique => 0,
            name => 'parent_id',
            type => 'BTREE'
          }
        },
        name => 'child',
        null_cols => [],
        numeric_cols => [
          'id',
          'parent_id'
        ],
        type_for => {
          id => 'int',
          parent_id => 'int'
        }
      },
   dbh => $dbh,
);

cmp_ok(
   $size,
   '>',
   15_000,  # estimages range from 15k to 30k
   "Bug 1201443: size"
);

is(
   $chosen_key,
   'parent_id',
   "Bug 1201443: chosen key"
);

# #############################################################################
# Done.
# #############################################################################
$output = '';
{
   local *STDERR;
   open STDERR, '>', \$output;
   $ks->_d('Complete test coverage');
}
like(
   $output,
   qr/Complete test coverage/,
   '_d() works'
);
$sb->wipe_clean($dbh);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
done_testing;