diff --git a/bin/pt-table-checksum b/bin/pt-table-checksum index 48390f7b..03d086a3 100755 --- a/bin/pt-table-checksum +++ b/bin/pt-table-checksum @@ -3514,7 +3514,7 @@ sub new { . ($args{select} ? $args{select} : join(', ', map { $q->quote($_) } @cols)) . " FROM " . $q->quote(@{$tbl}{qw(db tbl)}) - . ($where ? " AND ($where)" : '') + . ($where ? " WHERE $where" : '') . " /*checksum table*/"; MKDEBUG && _d('One nibble statement:', $nibble_sql); @@ -3523,7 +3523,7 @@ sub new { . ($args{select} ? $args{select} : join(', ', map { $q->quote($_) } @cols)) . " FROM " . $q->quote(@{$tbl}{qw(db tbl)}) - . ($where ? " AND ($where)" : '') + . ($where ? " WHERE $where" : '') . " /*explain checksum table*/"; MKDEBUG && _d('Explain one nibble statement:', $explain_nibble_sql); diff --git a/lib/NibbleIterator.pm b/lib/NibbleIterator.pm index 88d4d6a2..91ab19a6 100644 --- a/lib/NibbleIterator.pm +++ b/lib/NibbleIterator.pm @@ -95,7 +95,7 @@ sub new { . ($args{select} ? $args{select} : join(', ', map { $q->quote($_) } @cols)) . " FROM " . $q->quote(@{$tbl}{qw(db tbl)}) - . ($where ? " AND ($where)" : '') + . ($where ? " WHERE $where" : '') . " /*checksum table*/"; MKDEBUG && _d('One nibble statement:', $nibble_sql); @@ -104,7 +104,7 @@ sub new { . ($args{select} ? $args{select} : join(', ', map { $q->quote($_) } @cols)) . " FROM " . $q->quote(@{$tbl}{qw(db tbl)}) - . ($where ? " AND ($where)" : '') + . ($where ? " WHERE $where" : '') . " /*explain checksum table*/"; MKDEBUG && _d('Explain one nibble statement:', $explain_nibble_sql); diff --git a/t/lib/NibbleIterator.t b/t/lib/NibbleIterator.t index 93b61102..5ab973c6 100644 --- a/t/lib/NibbleIterator.t +++ b/t/lib/NibbleIterator.t @@ -39,7 +39,7 @@ if ( !$dbh ) { plan skip_all => 'Cannot connect to sandbox master'; } else { - plan tests => 45; + plan tests => 46; } my $q = new Quoter(); @@ -711,6 +711,21 @@ is_deeply( "Nibbles only values in --where clause range" ); +$ni = make_nibble_iter( + sql_file => "a-z.sql", + db => 'test', + tbl => 't', + one_nibble => 1, + argv => [qw(--databases test --where c>'m')], +); +1 while $ni->next(); +my $sql = $ni->statements()->{nibble}->{Statement}; +is( + $sql, + "SELECT `c` FROM `test`.`t` WHERE c>'m' /*checksum table*/", + "One nibble SQL with where" +); + # The real number of rows is 13, but MySQL may estimate a little. cmp_ok( $ni->row_estimate(), @@ -719,7 +734,6 @@ cmp_ok( "row_estimate()" ); - # ############################################################################ # Empty table. # ############################################################################ diff --git a/t/pt-table-checksum/chunk_index.t b/t/pt-table-checksum/chunk_index.t index 1d908775..34ff46f4 100644 --- a/t/pt-table-checksum/chunk_index.t +++ b/t/pt-table-checksum/chunk_index.t @@ -32,7 +32,7 @@ else { # so we need to specify --lock-wait-timeout=3 else the tool will die. # And --max-load "" prevents waiting for status variables. my $master_dsn = 'h=127.1,P=12345,u=msandbox,p=msandbox'; -my @args = ($master_dsn, qw(--lock-wait-timeout 3 -d issue_519 --explain --explain --chunk-size 3), '--max-load', ''); +my @args = ($master_dsn, qw(--lock-wait-timeout 3 --explain --chunk-size 3), '--max-load', ''); my $output; my $out = "t/pt-table-checksum/samples/"; @@ -40,7 +40,7 @@ $sb->load_file('master', "t/pt-table-checksum/samples/issue_519.sql"); ok( no_diff( - sub { pt_table_checksum::main(@args) }, + sub { pt_table_checksum::main(@args, qw(-t issue_519.t --explain)) }, "$out/chunkidx001.txt", ), "Chooses chunk index by default" @@ -48,7 +48,8 @@ ok( ok( no_diff( - sub { pt_table_checksum::main(@args, qw(--chunk-index dog)) }, + sub { pt_table_checksum::main(@args, qw(--chunk-index dog), + qw(-t issue_519.t --explain)) }, "$out/chunkidx001.txt", ), "Chooses chunk index if --chunk-index doesn't exist" @@ -56,7 +57,8 @@ ok( ok( no_diff( - sub { pt_table_checksum::main(@args, qw(--chunk-index myidx)) }, + sub { pt_table_checksum::main(@args, qw(--chunk-index myidx), + qw(-t issue_519.t --explain)) }, "$out/chunkidx002.txt", ), "Use --chunk-index" @@ -66,7 +68,8 @@ ok( # is a single column index, so there's really not "left-most". ok( no_diff( - sub { pt_table_checksum::main(@args, qw(--chunk-index y)) }, + sub { pt_table_checksum::main(@args, qw(--chunk-index y), + qw(-t issue_519.t --explain)) }, "$out/chunkidx003.txt", ), "Chunks on left-most --chunk-index column" @@ -79,22 +82,23 @@ ok( # This issue affect --chunk-index. Tool should auto-choose chunk-index # when --where is given but no explicit --chunk-index|column is given. -# Given the --where clause, MySQL will prefer the y index. +# Given the --where clause, MySQL will prefer the idx_fk_country_id index. ok( no_diff( - sub { pt_table_checksum::main(@args, "--where", "y > 2009") }, + sub { pt_table_checksum::main(@args, "--where", "country_id > 100", + qw(-t sakila.city)) }, "$out/chunkidx004.txt", ), "Auto-chosen --chunk-index for --where (issue 378)" ); # If user specifies --chunk-index, then ignore the index MySQL wants to -# use (y in this case) and use the user's index. +# use (idx_fk_country_id in this case) and use the user's index. ok( no_diff( sub { pt_table_checksum::main(@args, qw(--chunk-index PRIMARY), - "--where", "y > 2009") }, + "--where", "country_id > 100", qw(-t sakila.city)) }, "$out/chunkidx005.txt", ), "Explicit --chunk-index overrides MySQL's index for --where" diff --git a/t/pt-table-checksum/samples/chunkidx001.txt b/t/pt-table-checksum/samples/chunkidx001.txt index dcc96f95..62ca8d11 100644 --- a/t/pt-table-checksum/samples/chunkidx001.txt +++ b/t/pt-table-checksum/samples/chunkidx001.txt @@ -2,7 +2,7 @@ -- issue_519.t -- -REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*) AS cnt, COALESCE(LOWER(CONV(BIT_XOR(CAST(CRC32(CONCAT_WS('#', `i`, `y`, `t`, CONCAT(ISNULL(`t`)))) AS UNSIGNED)), 10, 16)), 0) AS crc FROM `issue_519`.`t` FORCE INDEX(`PRIMARY`) WHERE ((`i` >= ?)) AND ((`i` <= ?)) ORDER BY `i` /*checksum chunk*/ +REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*) AS cnt, COALESCE(LOWER(CONV(BIT_XOR(CAST(CRC32(CONCAT_WS('#', `i`, `y`, `t`, CONCAT(ISNULL(`t`)))) AS UNSIGNED)), 10, 16)), 0) AS crc FROM `issue_519`.`t` FORCE INDEX(`PRIMARY`) WHERE ((`i` >= ?)) AND ((`i` <= ?)) /*checksum chunk*/ REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*), '0' FROM `issue_519`.`t` FORCE INDEX(`PRIMARY`) WHERE ((`i` < ?)) ORDER BY `i` /*past lower chunk*/ diff --git a/t/pt-table-checksum/samples/chunkidx002.txt b/t/pt-table-checksum/samples/chunkidx002.txt index 5adba728..5b532454 100644 --- a/t/pt-table-checksum/samples/chunkidx002.txt +++ b/t/pt-table-checksum/samples/chunkidx002.txt @@ -2,7 +2,7 @@ -- issue_519.t -- -REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*) AS cnt, COALESCE(LOWER(CONV(BIT_XOR(CAST(CRC32(CONCAT_WS('#', `i`, `y`, `t`, CONCAT(ISNULL(`t`)))) AS UNSIGNED)), 10, 16)), 0) AS crc FROM `issue_519`.`t` FORCE INDEX(`myidx`) WHERE ((`i` > ?) OR (`i` = ? AND `y` >= ?)) AND ((`i` < ?) OR (`i` = ? AND `y` <= ?)) ORDER BY `i`, `y` /*checksum chunk*/ +REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*) AS cnt, COALESCE(LOWER(CONV(BIT_XOR(CAST(CRC32(CONCAT_WS('#', `i`, `y`, `t`, CONCAT(ISNULL(`t`)))) AS UNSIGNED)), 10, 16)), 0) AS crc FROM `issue_519`.`t` FORCE INDEX(`myidx`) WHERE ((`i` > ?) OR (`i` = ? AND `y` >= ?)) AND ((`i` < ?) OR (`i` = ? AND `y` <= ?)) /*checksum chunk*/ REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*), '0' FROM `issue_519`.`t` FORCE INDEX(`myidx`) WHERE ((`i` < ?) OR (`i` = ? AND `y` < ?)) ORDER BY `i`, `y` /*past lower chunk*/ diff --git a/t/pt-table-checksum/samples/chunkidx003.txt b/t/pt-table-checksum/samples/chunkidx003.txt index dbdb9090..ba0c7647 100644 --- a/t/pt-table-checksum/samples/chunkidx003.txt +++ b/t/pt-table-checksum/samples/chunkidx003.txt @@ -2,7 +2,7 @@ -- issue_519.t -- -REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*) AS cnt, COALESCE(LOWER(CONV(BIT_XOR(CAST(CRC32(CONCAT_WS('#', `i`, `y`, `t`, CONCAT(ISNULL(`t`)))) AS UNSIGNED)), 10, 16)), 0) AS crc FROM `issue_519`.`t` FORCE INDEX(`y`) WHERE ((`y` >= ?)) AND ((`y` <= ?)) ORDER BY `y` /*checksum chunk*/ +REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*) AS cnt, COALESCE(LOWER(CONV(BIT_XOR(CAST(CRC32(CONCAT_WS('#', `i`, `y`, `t`, CONCAT(ISNULL(`t`)))) AS UNSIGNED)), 10, 16)), 0) AS crc FROM `issue_519`.`t` FORCE INDEX(`y`) WHERE ((`y` >= ?)) AND ((`y` <= ?)) /*checksum chunk*/ REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*), '0' FROM `issue_519`.`t` FORCE INDEX(`y`) WHERE ((`y` < ?)) ORDER BY `y` /*past lower chunk*/ diff --git a/t/pt-table-checksum/samples/chunkidx004.txt b/t/pt-table-checksum/samples/chunkidx004.txt index 0a00b1eb..b3ceb684 100644 --- a/t/pt-table-checksum/samples/chunkidx004.txt +++ b/t/pt-table-checksum/samples/chunkidx004.txt @@ -1,16 +1,12 @@ -- --- issue_519.t +-- sakila.city -- -REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*) AS cnt, COALESCE(LOWER(CONV(BIT_XOR(CAST(CRC32(CONCAT_WS('#', `i`, `y`, `t`, CONCAT(ISNULL(`t`)))) AS UNSIGNED)), 10, 16)), 0) AS crc FROM `issue_519`.`t` FORCE INDEX(`y`) WHERE ((`y` >= ?)) AND ((`y` <= ?)) AND (y > 2009) ORDER BY `y` /*checksum chunk*/ +REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*) AS cnt, COALESCE(LOWER(CONV(BIT_XOR(CAST(CRC32(CONCAT_WS('#', `city_id`, `city`, `country_id`, `last_update` + 0)) AS UNSIGNED)), 10, 16)), 0) AS crc FROM `sakila`.`city` FORCE INDEX(`idx_fk_country_id`) WHERE ((`country_id` >= ?)) AND ((`country_id` <= ?)) AND (country_id > 100) /*checksum chunk*/ -REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*), '0' FROM `issue_519`.`t` FORCE INDEX(`y`) WHERE ((`y` < ?)) AND (y > 2009) ORDER BY `y` /*past lower chunk*/ +REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*), '0' FROM `sakila`.`city` FORCE INDEX(`idx_fk_country_id`) WHERE ((`country_id` < ?)) AND (country_id > 100) ORDER BY `country_id` /*past lower chunk*/ -REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*), '0' FROM `issue_519`.`t` FORCE INDEX(`y`) WHERE ((`y` > ?)) AND (y > 2009) ORDER BY `y` /*past upper chunk*/ +REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*), '0' FROM `sakila`.`city` FORCE INDEX(`idx_fk_country_id`) WHERE ((`country_id` > ?)) AND (country_id > 100) ORDER BY `country_id` /*past upper chunk*/ -SELECT /*!40001 SQL_NO_CACHE */ `y` FROM `issue_519`.`t` FORCE INDEX(`y`) WHERE ((`y` >= ?)) AND (y > 2009) ORDER BY `y` LIMIT ?, 2 /*next chunk boundary*/ - -1 2010 2010 -2 2010 -3 2010 +SELECT /*!40001 SQL_NO_CACHE */ `country_id` FROM `sakila`.`city` FORCE INDEX(`idx_fk_country_id`) WHERE ((`country_id` >= ?)) AND (country_id > 100) ORDER BY `country_id` LIMIT ?, 2 /*next chunk boundary*/ diff --git a/t/pt-table-checksum/samples/chunkidx005.txt b/t/pt-table-checksum/samples/chunkidx005.txt index b34f386c..ae55d193 100644 --- a/t/pt-table-checksum/samples/chunkidx005.txt +++ b/t/pt-table-checksum/samples/chunkidx005.txt @@ -1,16 +1,12 @@ -- --- issue_519.t +-- sakila.city -- -REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*) AS cnt, COALESCE(LOWER(CONV(BIT_XOR(CAST(CRC32(CONCAT_WS('#', `i`, `y`, `t`, CONCAT(ISNULL(`t`)))) AS UNSIGNED)), 10, 16)), 0) AS crc FROM `issue_519`.`t` FORCE INDEX(`PRIMARY`) WHERE ((`i` >= ?)) AND ((`i` <= ?)) AND (y > 2009) ORDER BY `i` /*checksum chunk*/ +REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*) AS cnt, COALESCE(LOWER(CONV(BIT_XOR(CAST(CRC32(CONCAT_WS('#', `city_id`, `city`, `country_id`, `last_update` + 0)) AS UNSIGNED)), 10, 16)), 0) AS crc FROM `sakila`.`city` FORCE INDEX(`PRIMARY`) WHERE ((`city_id` >= ?)) AND ((`city_id` <= ?)) AND (country_id > 100) /*checksum chunk*/ -REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*), '0' FROM `issue_519`.`t` FORCE INDEX(`PRIMARY`) WHERE ((`i` < ?)) AND (y > 2009) ORDER BY `i` /*past lower chunk*/ +REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*), '0' FROM `sakila`.`city` FORCE INDEX(`PRIMARY`) WHERE ((`city_id` < ?)) AND (country_id > 100) ORDER BY `city_id` /*past lower chunk*/ -REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*), '0' FROM `issue_519`.`t` FORCE INDEX(`PRIMARY`) WHERE ((`i` > ?)) AND (y > 2009) ORDER BY `i` /*past upper chunk*/ +REPLACE INTO `percona`.`checksums` (db, tbl, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) SELECT ?, ?, ?, ?, ?, ?, COUNT(*), '0' FROM `sakila`.`city` FORCE INDEX(`PRIMARY`) WHERE ((`city_id` > ?)) AND (country_id > 100) ORDER BY `city_id` /*past upper chunk*/ -SELECT /*!40001 SQL_NO_CACHE */ `i` FROM `issue_519`.`t` FORCE INDEX(`PRIMARY`) WHERE ((`i` >= ?)) AND (y > 2009) ORDER BY `i` LIMIT ?, 2 /*next chunk boundary*/ - -1 11 11 -2 11 -3 11 +SELECT /*!40001 SQL_NO_CACHE */ `city_id` FROM `sakila`.`city` FORCE INDEX(`PRIMARY`) WHERE ((`city_id` >= ?)) AND (country_id > 100) ORDER BY `city_id` LIMIT ?, 2 /*next chunk boundary*/