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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
|
#!/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 tests => 35;
use PerconaTest;
use QueryParser;
use SQLParser;
use TableUsage;
use Sandbox;
my $dp = new DSNParser(opts=>$dsn_opts);
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh = $sb->get_dbh_for('master');
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Quotekeys = 0;
my $qp = new QueryParser();
my $sp = new SQLParser();
my $ta = new TableUsage(QueryParser => $qp, SQLParser => $sp);
isa_ok($ta, 'TableUsage');
sub test_get_table_usage {
my ( $query, $cats, $desc ) = @_;
my $got = $ta->get_table_usage(query=>$query);
is_deeply(
$got,
$cats,
$desc,
) or print Dumper($got);
return;
}
# ############################################################################
# Queries parsable by SQLParser: SELECT, INSERT, UPDATE and DELETE
# ############################################################################
test_get_table_usage(
"SELECT * FROM d.t WHERE id>100",
[
[
{ context => 'SELECT',
table => 'd.t',
},
{ context => 'WHERE',
table => 'd.t',
},
],
],
"SELECT FROM one table"
);
test_get_table_usage(
"SELECT t1.* FROM d.t1 LEFT JOIN d.t2 USING (id) WHERE d.t2.foo IS NULL",
[
[
{ context => 'SELECT',
table => 'd.t1',
},
{ context => 'JOIN',
table => 'd.t1',
},
{ context => 'JOIN',
table => 'd.t2',
},
{ context => 'WHERE',
table => 'd.t2',
},
],
],
"SELECT JOIN two tables"
);
test_get_table_usage(
"DELETE FROM d.t WHERE type != 'D' OR type IS NULL",
[
[
{ context => 'DELETE',
table => 'd.t',
},
{ context => 'WHERE',
table => 'd.t',
},
],
],
"DELETE one table"
);
test_get_table_usage(
"INSERT INTO d.t (col1, col2) VALUES ('a', 'b')",
[
[
{ context => 'INSERT',
table => 'd.t',
},
{ context => 'SELECT',
table => 'DUAL',
},
],
],
"INSERT VALUES, no SELECT"
);
test_get_table_usage(
"INSERT INTO d.t SET col1='a', col2='b'",
[
[
{ context => 'INSERT',
table => 'd.t',
},
{ context => 'SELECT',
table => 'DUAL',
},
],
],
"INSERT SET, no SELECT"
);
test_get_table_usage(
"UPDATE d.t SET foo='bar' WHERE foo IS NULL",
[
[
{ context => 'UPDATE',
table => 'd.t',
},
{ context => 'SELECT',
table => 'DUAL',
},
{ context => 'WHERE',
table => 'd.t',
},
],
],
"UPDATE one table"
);
test_get_table_usage(
"SELECT * FROM zn.edp
INNER JOIN zn.edp_input_key edpik ON edp.id = edpik.id
INNER JOIN `zn`.`key` input_key ON edpik.input_key = input_key.id
WHERE edp.id = 296",
[
[
{ context => 'SELECT',
table => 'zn.edp',
},
{ context => 'SELECT',
table => 'zn.edp_input_key',
},
{ context => 'SELECT',
table => 'zn.key',
},
{ context => 'JOIN',
table => 'zn.edp',
},
{ context => 'JOIN',
table => 'zn.edp_input_key',
},
{ context => 'JOIN',
table => 'zn.key',
},
{ context => 'WHERE',
table => 'zn.edp',
},
],
],
"SELECT with 2 JOIN and WHERE"
);
test_get_table_usage(
"REPLACE INTO db.tblA (dt, ncpc)
SELECT dates.dt, scraped.total_r
FROM tblB AS dates
LEFT JOIN dbF.tblC AS scraped
ON dates.dt = scraped.dt AND dates.version = scraped.version",
[
[
{ context => 'REPLACE',
table => 'db.tblA',
},
{ context => 'SELECT',
table => 'tblB',
},
{ context => 'SELECT',
table => 'dbF.tblC',
},
{ context => 'JOIN',
table => 'tblB',
},
{ context => 'JOIN',
table => 'dbF.tblC',
},
],
],
"REPLACE SELECT JOIN"
);
test_get_table_usage(
'UPDATE t1 AS a JOIN t2 AS b USING (id) SET a.foo="bar" WHERE b.foo IS NOT NULL',
[
[
{ context => 'UPDATE',
table => 't1',
},
{ context => 'SELECT',
table => 'DUAL',
},
{ context => 'JOIN',
table => 't1',
},
{ context => 'JOIN',
table => 't2',
},
{ context => 'WHERE',
table => 't2',
},
],
],
"UPDATE joins 2 tables, writes to 1, filters by 1"
);
test_get_table_usage(
'UPDATE t1 INNER JOIN t2 USING (id) SET t1.foo="bar" WHERE t1.id>100 AND t2.id>200',
[
[
{ context => 'UPDATE',
table => 't1',
},
{ context => 'SELECT',
table => 'DUAL',
},
{ context => 'JOIN',
table => 't1',
},
{ context => 'JOIN',
table => 't2',
},
{ context => 'WHERE',
table => 't1',
},
{ context => 'WHERE',
table => 't2',
},
],
],
"UPDATE joins 2 tables, writes to 1, filters by 2"
);
test_get_table_usage(
'UPDATE t1 AS a JOIN t2 AS b USING (id) SET a.foo="bar", b.foo="bat" WHERE a.id=1',
[
[
{ context => 'UPDATE',
table => 't1',
},
{ context => 'SELECT',
table => 'DUAL',
},
{ context => 'JOIN',
table => 't1',
},
{ context => 'JOIN',
table => 't2',
},
{ context => 'WHERE',
table => 't1',
},
],
[
{ context => 'UPDATE',
table => 't2',
},
{ context => 'SELECT',
table => 'DUAL',
},
{ context => 'JOIN',
table => 't1',
},
{ context => 'JOIN',
table => 't2',
},
{ context => 'WHERE',
table => 't1',
},
],
],
"UPDATE joins 2 tables, writes to 2, filters by 1"
);
test_get_table_usage(
'insert into t1 (a, b, c) select x, y, z from t2 where x is not null',
[
[
{ context => 'INSERT',
table => 't1',
},
{ context => 'SELECT',
table => 't2',
},
{ context => 'WHERE',
table => 't2',
},
],
],
"INSERT INTO t1 SELECT FROM t2",
);
test_get_table_usage(
'insert into t (a, b, c) select a.x, a.y, b.z from a, b where a.id=b.id',
[
[
{ context => 'INSERT',
table => 't',
},
{ context => 'SELECT',
table => 'a',
},
{ context => 'SELECT',
table => 'b',
},
{ context => 'JOIN',
table => 'a',
},
{ context => 'JOIN',
table => 'b',
},
],
],
"INSERT INTO t SELECT FROM a, b"
);
test_get_table_usage(
'INSERT INTO bar
SELECT edpik.*
FROM zn.edp
INNER JOIN zn.edp_input_key AS edpik ON edpik.id = edp.id
INNER JOIN `zn`.`key` input_key
INNER JOIN foo
WHERE edp.id = 296
AND edpik.input_key = input_key.id',
[
[
{ context => 'INSERT',
table => 'bar',
},
{ context => 'SELECT',
table => 'zn.edp_input_key',
},
{ context => 'JOIN',
table => 'zn.edp',
},
{ context => 'JOIN',
table => 'zn.edp_input_key',
},
{ context => 'JOIN',
table => 'zn.key',
},
{ context => 'TLIST',
table => 'foo',
},
{ context => 'WHERE',
table => 'zn.edp',
},
],
],
"INSERT SELECT with TLIST table"
);
test_get_table_usage(
"select country.country, city.city from city join country using (country_id) where country = 'Brazil' and city like 'A%' limit 1",
[
[
{ context => 'SELECT',
table => 'country',
},
{ context => 'SELECT',
table => 'city',
},
{ context => 'JOIN',
table => 'city',
},
{ context => 'JOIN',
table => 'country',
},
],
],
"Unresolvable tables in WHERE"
);
test_get_table_usage(
"select c from t where 1",
[
[
{ context => 'SELECT',
table => 't',
},
{ context => 'WHERE',
table => 'DUAL',
},
],
],
"WHERE <constant>"
);
test_get_table_usage(
"select c from t where 1=1",
[
[
{ context => 'SELECT',
table => 't',
},
{ context => 'WHERE',
table => 'DUAL',
},
],
],
"WHERE <constant>=<constant>"
);
test_get_table_usage(
"select now()",
[
[
{ context => 'SELECT',
table => 'DUAL',
},
],
],
"SELECT NOW()"
);
#test_get_table_usage(
# "SELECT
# automated_process.id id,
# class,
# automated_process_instance.server,
# IF(start IS NULL, 0, 1),
# owner
# FROM
# zn.automated_process_instance
# INNER JOIN zn.automated_process ON automated_process=automated_process.id
# WHERE
# automated_process_instance.id = 5251414",
# [
# [
# { context => 'SELECT',
# table => 'zn.automated_process',
# },
# { context => 'SELECT',
# table => 'zn.automated_process_instance',
# },
# { context => 'JOIN',
# table => 'zn.automated_process_instance',
# },
# { context => 'JOIN',
# table => 'zn.automated_process',
# },
# { context => 'WHERE',
# table => 'zn.automated_process_instance',
# },
# ]
# ],
# "SELECT explicit INNER JOIN with condition"
#);
# ############################################################################
# Queries parsable by QueryParser
# ############################################################################
test_get_table_usage(
"ALTER TABLE tt.ks ADD PRIMARY KEY(`d`,`v`)",
[
[
{ context => 'ALTER',
table => 'tt.ks',
},
],
],
"ALTER TABLE"
);
test_get_table_usage(
"DROP TABLE foo",
[
[
{ context => 'DROP_TABLE',
table => 'foo',
},
],
],
"DROP TABLE"
);
test_get_table_usage(
"DROP TABLE IF EXISTS foo",
[
[
{ context => 'DROP_TABLE',
table => 'foo',
},
],
],
"DROP TABLE IF EXISTS"
);
# #############################################################################
# Change DUAL to something else.
# #############################################################################
$ta = new TableUsage(
QueryParser => $qp,
SQLParser => $sp,
constant_data_value => '<const>',
);
test_get_table_usage(
"INSERT INTO d.t (col1, col2) VALUES ('a', 'b')",
[
[
{ context => 'INSERT',
table => 'd.t',
},
{ context => 'SELECT',
table => '<const>',
},
],
],
"Change constant_data_value"
);
# Restore original TableUsage obj for other tests.
$ta = new TableUsage(
QueryParser => $qp,
SQLParser => $sp,
);
# ###########################################################################
# CREATE
# ###########################################################################
test_get_table_usage(
"CREATE TABLE db.tbl (id INT) ENGINE=InnoDB",
[
[
{ context => 'CREATE',
table => 'db.tbl',
},
],
],
"CREATE TABLE",
);
test_get_table_usage(
"CREATE TABLE db.tbl SELECT city_id FROM sakila.city WHERE city_id>100",
[
[
{ context => 'CREATE',
table => 'db.tbl',
},
{ context => 'SELECT',
table => 'sakila.city',
},
{ context => 'WHERE',
table => 'sakila.city',
},
],
],
"CREATE..SELECT"
);
# ############################################################################
# Use Schema instead of EXPLAIN EXTENDED.
# ############################################################################
use OptionParser;
use DSNParser;
use Quoter;
use TableParser;
use FileIterator;
use Schema;
use SchemaIterator;
my $o = new OptionParser(description => 'SchemaIterator');
$o->get_specs("$trunk/bin/pt-table-checksum");
my $q = new Quoter;
my $tp = new TableParser(Quoter => $q);
my $fi = new FileIterator();
my $file_itr = $fi->get_file_itr("$trunk/t/lib/samples/mysqldump-no-data/dump001.txt");
my $schema = new Schema();
my $schema_itr = new SchemaIterator(
file_itr => $file_itr,
OptionParser => $o,
Quoter => $q,
TableParser => $tp,
Schema => $schema,
);
# Init schema.
1 while ($schema_itr->next());
# Before, this is as correct as we can determine. The WHERE access is missing
# because c3 is not qualified and there's multiple tables, so the code can't
# figure out to which table it belongs.
test_get_table_usage(
"SELECT a.c1, c3 FROM a JOIN b ON a.c2=c3 WHERE NOW()<c3",
[
[
{ context => 'SELECT',
table => 'a',
},
{ context => 'JOIN',
table => 'a',
},
{ context => 'JOIN',
table => 'b',
},
],
],
"Tables without Schema"
);
# After, now we have a db for table b, but not for a because the schema
# we loaded has two table a (test.a and test2.a). The WHERE access is
# now present.
$sp->set_Schema($schema);
test_get_table_usage(
"SELECT a.c1, c3 FROM a JOIN b ON a.c2=c3 WHERE NOW()<c3",
[
[
{ context => 'SELECT',
table => 'a',
},
{ context => 'SELECT',
table => 'test.b',
},
{ context => 'JOIN',
table => 'a',
},
{ context => 'JOIN',
table => 'test.b',
},
{ context => 'WHERE',
table => 'test.b',
},
],
],
"Tables with Schema"
);
# Set it back for the next tests.
$sp->set_Schema(undef);
# #############################################################################
# Use a dbh for EXPLAIN EXTENDED.
# #############################################################################
SKIP: {
skip 'Cannot connect to sandbox master', 1 unless $dbh;
$ta = new TableUsage(
QueryParser => $qp,
SQLParser => $sp,
dbh => $dbh,
);
# Compare this with the same query/test after USE sakila.
test_get_table_usage(
"select city_id, country.country_id from city, country where city_id>100 or country='Brazil' limit 1",
[
[
{ context => 'SELECT',
table => 'country'
},
{ context => 'TLIST',
table => 'city'
},
{ context => 'TLIST',
table => 'country'
},
]
],
"Ambiguous tables"
);
is_deeply(
$ta->errors(),
[ 'NO_DB_SELECTED' ],
'NO_DB_SELECTED error'
);
$dbh->do('USE sakila');
test_get_table_usage(
"select city_id, country.country_id from city, country where city_id>100 or country='Brazil' limit 1",
[
[ { context => 'SELECT',
table => 'sakila.city'
},
{ context => 'SELECT',
table => 'sakila.country'
},
{ context => 'TLIST',
table => 'sakila.city'
},
{ context => 'TLIST',
table => 'sakila.country'
},
{ context => 'WHERE',
table => 'sakila.city'
},
{ context => 'WHERE',
table => 'sakila.country'
}
],
],
"Disambiguate WHERE columns"
);
test_get_table_usage(
"select city_id, country from city, country where city.city_id>100 or country.country='China' limit 1",
[
[ { context => 'SELECT',
table => 'sakila.city'
},
{ context => 'SELECT',
table => 'sakila.country'
},
{ context => 'TLIST',
table => 'sakila.city'
},
{ context => 'TLIST',
table => 'sakila.country'
},
{ context => 'WHERE',
table => 'sakila.city'
},
{ context => 'WHERE',
table => 'sakila.country'
}
],
],
"Disambiguate CLIST columns"
);
test_get_table_usage(
"select city.city, country.country from city join country on city=country where city.city_id>100 or country.country='China' limit 1",
[
[ { context => 'SELECT',
table => 'sakila.city'
},
{ context => 'SELECT',
table => 'sakila.country'
},
{ context => 'JOIN',
table => 'sakila.city'
},
{ context => 'JOIN',
table => 'sakila.country'
},
{ context => 'WHERE',
table => 'sakila.city'
},
{ context => 'WHERE',
table => 'sakila.country'
}
],
],
"Disambiguate JOIN columns"
);
test_get_table_usage(
"SELECT COUNT(*), MAX(country_id), MIN(country_id) FROM sakila.city A JOIN sakila.country B USING (country_id) WHERE B.country = 'Brazil'",
[
[
{ context => 'SELECT',
table => 'sakila.city',
},
{ context => 'JOIN',
table => 'sakila.city',
},
{ context => 'JOIN',
table => 'sakila.country',
},
{ context => 'WHERE',
table => 'sakila.country',
},
],
],
"SELECT with multiple CLIST functions"
);
}
# #############################################################################
# Done.
# #############################################################################
my $output = '';
{
local *STDERR;
open STDERR, '>', \$output;
$ta->_d('Complete test coverage');
}
like(
$output,
qr/Complete test coverage/,
'_d() works'
);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
exit;
|