Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/expert/expert1.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / expert / expert1.test` 730 lines ยท 18047 bytes Raw Blame History 1 # 2009 Nov 11 2 # 3 # The author disclaims copyright to this source code. In place of 4 # a legal notice, here is a blessing: 5 # 6 # May you do good and not evil. 7 # May you find forgiveness for yourself and forgive others. 8 # May you share freely, never taking more than you give. 9 # 10 #*********************************************************************** 11 # TESTRUNNER: shell 12 # 13 # The focus of this file is testing the CLI shell tool. Specifically, 14 # the ".recommend" command. 15 # 16 # 17 18 # Test plan: 19 # 20 # 21 if {![info exists testdir]} { 22 set testdir [file join [file dirname [info script]] .. .. test] 23 } 24 source $testdir/tester.tcl 25 set testprefix expert1 26 27 if {[info commands sqlite3_expert_new]==""} { 28 finish_test 29 return 30 } 31 32 33 set CLI [test_binary_name sqlite3] 34 set CMD [test_binary_name sqlite3_expert] 35 36 proc squish {txt} { 37 regsub -all {[[:space:]]+} $txt { } 38 } 39 40 proc do_setup_rec_test {tn setup sql res} { 41 reset_db 42 if {[info exists ::set_main_db_name]} { 43 dbconfig_maindbname_icecube db 44 } 45 db eval $setup 46 uplevel [list do_rec_test $tn $sql $res] 47 } 48 49 foreach {tn setup} { 50 1 { 51 if {![file executable $CMD]} { continue } 52 53 proc do_rec_test {tn sql res} { 54 set res [squish [string trim $res]] 55 set tst [subst -nocommands { 56 squish [string trim [exec $::CMD -verbose 0 -sql {$sql;} test.db]] 57 }] 58 uplevel [list do_test $tn $tst $res] 59 } 60 } 61 2 { 62 if {[info commands sqlite3_expert_new]==""} { continue } 63 64 proc do_rec_test {tn sql res} { 65 set expert [sqlite3_expert_new db] 66 $expert sql $sql 67 $expert analyze 68 69 set result [list] 70 for {set i 0} {$i < [$expert count]} {incr i} { 71 set idx [string trim [$expert report $i indexes]] 72 if {$idx==""} {set idx "(no new indexes)"} 73 lappend result $idx 74 lappend result [string trim [$expert report $i plan]] 75 } 76 77 $expert destroy 78 79 set tst [subst -nocommands {set {} [squish [join {$result}]]}] 80 uplevel [list do_test $tn $tst [string trim [squish $res]]] 81 } 82 } 83 3 { 84 if {[info commands sqlite3_expert_new]==""} { continue } 85 set ::set_main_db_name 1 86 } 87 4 { 88 if {![file executable $CLI]} { continue } 89 90 proc do_rec_test {tn sql res} { 91 set res [squish [string trim $res]] 92 set tst [subst -nocommands { 93 squish [string trim [exec $::CLI -noinit test.db ".expert" {$sql;}]] 94 }] 95 uplevel [list do_test $tn $tst $res] 96 } 97 } 98 } { 99 100 eval $setup 101 102 103 do_setup_rec_test $tn.1 { CREATE TABLE t1(a, b, c) } { 104 SELECT * FROM t1 105 } { 106 (no new indexes) 107 SCAN t1 108 } 109 110 do_setup_rec_test $tn.2 { 111 CREATE TABLE t1(a, b, c); 112 } { 113 SELECT * FROM t1 WHERE b>?; 114 } { 115 CREATE INDEX t1_idx_`00000062` ON t1(b); 116 SEARCH t1 USING INDEX t1_idx_`00000062` (b>?) 117 } 118 119 do_setup_rec_test $tn.3 { 120 CREATE TABLE t1(a, b, c); 121 } { 122 SELECT * FROM t1 WHERE b COLLATE nocase BETWEEN ? AND ? 123 } { 124 CREATE INDEX t1_idx_`3e094c27` ON t1(b COLLATE NOCASE); 125 SEARCH t1 USING INDEX t1_idx_`3e094c27` (b>? AND b? AND b? 359 } { 360 (no new indexes) 361 SEARCH example USING INDEX sqlite_autoindex_example_1 (A=? AND B>?) 362 } 363 do_setup_rec_test $tn.17.5 { 364 CREATE TABLE example (A INTEGER, B INTEGER, C INTEGER, PRIMARY KEY (A,B)); 365 } { 366 SELECT * FROM example WHERE a>? AND b=? 367 } { 368 CREATE INDEX example_idx_`0000cb3f` ON example(B, A); 369 SEARCH example USING INDEX example_idx_`0000cb3f` (B=? AND A>?) 370 } 371 372 do_setup_rec_test $tn.18.0 { 373 CREATE TABLE SomeObject ( 374 a INTEGER PRIMARY KEY, 375 x TEXT GENERATED ALWAYS AS(HEX(a)) VIRTUAL 376 ); 377 } { 378 SELECT x FROM SomeObject; 379 } { 380 (no new indexes) 381 SCAN SomeObject 382 } 383 do_setup_rec_test $tn.18.1 { 384 CREATE TABLE SomeObject ( 385 a INTEGER PRIMARY KEY, 386 x TEXT GENERATED ALWAYS AS(HEX(a)) VIRTUAL 387 ); 388 } { 389 SELECT * FROM SomeObject WHERE x=?; 390 } { 391 CREATE INDEX SomeObject_idx_`00000078` ON SomeObject(x); 392 SEARCH SomeObject USING COVERING INDEX SomeObject_idx_`00000078` (x=?) 393 } 394 395 396 do_setup_rec_test $tn.19.0 { 397 CREATE TABLE t1("index"); 398 } { 399 SELECT * FROM t1 ORDER BY "index"; 400 } { 401 CREATE INDEX t1_idx_`01a7214e` ON t1('index'); 402 SCAN t1 USING COVERING INDEX t1_idx_`01a7214e` 403 } 404 405 ifcapable fts5 { 406 do_setup_rec_test $tn.20.0 { 407 CREATE VIRTUAL TABLE ft USING fts5(a); 408 CREATE TABLE t1(x, y); 409 } { 410 SELECT * FROM ft, t1 WHERE a=x 411 } { 412 CREATE INDEX t1_idx_`00000078` ON t1(x); 413 SCAN ft VIRTUAL TABLE INDEX 0: 414 SEARCH t1 USING INDEX t1_idx_`00000078` (x=?) 415 } 416 } 417 418 # Bug 2026-10-28T05:10:06Z: Identifiers that begin with a digit must 419 # be quoted in candidate CREATE INDEX statements. 420 # 421 do_setup_rec_test $tn.21.1 { 422 CREATE TABLE t("2023" INT, "1abc" INT, x); 423 } { 424 SELECT * FROM t WHERE "2023" = 5; 425 } { 426 CREATE INDEX t_idx_`0028bfa5` ON t('2023'); 427 SEARCH t USING INDEX t_idx_`0028bfa5` (2023=?) 428 } 429 do_setup_rec_test $tn.21.2 { 430 CREATE TABLE t("2023" INT, "1abc" INT, x); 431 } { 432 SELECT * FROM t WHERE "1abc" = 5; 433 } { 434 CREATE INDEX t_idx_`0029426d` ON t('1abc'); 435 SEARCH t USING INDEX t_idx_`0029426d` (1abc=?) 436 } 437 do_setup_rec_test $tn.21.3 { 438 CREATE TABLE "9t"(a, b); 439 } { 440 SELECT * FROM "9t" WHERE a=5; 441 } { 442 CREATE INDEX "9t_idx_`00000061`" ON "9t"(a); 443 SEARCH 9t USING INDEX 9t_idx_`00000061` (a=?) 444 } 445 446 } 447 448 proc do_candidates_test {tn sql res} { 449 set res [squish [string trim $res]] 450 451 set expert [sqlite3_expert_new db] 452 $expert sql $sql 453 $expert analyze 454 455 set candidates [squish [string trim [$expert report 0 candidates]]] 456 $expert destroy 457 458 uplevel [list do_test $tn [list set {} $candidates] $res] 459 } 460 461 462 reset_db 463 do_execsql_test 5.0 { 464 CREATE TABLE t1(a, b); 465 CREATE TABLE t2(c, d); 466 467 WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<100) 468 INSERT INTO t1 SELECT (i-1)/50, (i-1)/20 FROM s; 469 470 WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<100) 471 INSERT INTO t2 SELECT (i-1)/20, (i-1)/5 FROM s; 472 473 CREATE INDEX i1 ON t1( lower(a) ); 474 } 475 do_candidates_test 5.1 { 476 SELECT * FROM t1,t2 WHERE (b=? OR a=?) AND (c=? OR d=?) 477 } { 478 CREATE INDEX t1_idx_`00000062` ON t1(b); -- stat1: 100 20 479 CREATE INDEX t1_idx_`00000061` ON t1(a); -- stat1: 100 50 480 CREATE INDEX t2_idx_`00000063` ON t2(c); -- stat1: 100 20 481 CREATE INDEX t2_idx_`00000064` ON t2(d); -- stat1: 100 5 482 } 483 484 do_candidates_test 5.2 { 485 SELECT * FROM t1,t2 WHERE a=? AND b=? AND c=? AND d=? 486 } { 487 CREATE INDEX t1_idx_`000123a7` ON t1(a, b); -- stat1: 100 50 17 488 CREATE INDEX t2_idx_`0001295b` ON t2(c, d); -- stat1: 100 20 5 489 } 490 491 do_execsql_test 5.3 { 492 CREATE INDEX t1_idx_`00000061` ON t1(a); -- stat1: 100 50 493 CREATE INDEX t1_idx_`00000062` ON t1(b); -- stat1: 100 20 494 CREATE INDEX t1_idx_`000123a7` ON t1(a, b); -- stat1: 100 50 16 495 496 CREATE INDEX t2_idx_`00000063` ON t2(c); -- stat1: 100 20 497 CREATE INDEX t2_idx_`00000064` ON t2(d); -- stat1: 100 5 498 CREATE INDEX t2_idx_`0001295b` ON t2(c, d); -- stat1: 100 20 5 499 500 ANALYZE; 501 SELECT * FROM sqlite_stat1 ORDER BY 1, 2; 502 } { 503 t1 i1 {100 50} 504 t1 t1_idx_`00000061` {100 50} 505 t1 t1_idx_`00000062` {100 20} 506 t1 t1_idx_`000123a7` {100 50 17} 507 t2 t2_idx_`00000063` {100 20} 508 t2 t2_idx_`00000064` {100 5} 509 t2 t2_idx_`0001295b` {100 20 5} 510 } 511 512 do_catchsql_test 5.4 { 513 SELECT sqlite_expert_rem(123, 123); 514 } {1 {no such function: sqlite_expert_rem}} 515 do_catchsql_test 5.5 { 516 SELECT sqlite_expert_sample(); 517 } {1 {no such function: sqlite_expert_sample}} 518 519 if 0 { 520 do_test expert1-6.0 { 521 catchcmd :memory: { 522 .expert 523 select base64(''); 524 .expert 525 select name from pragma_collation_list order by name collate uint; 526 } 527 } {0 {(no new indexes) 528 529 SCAN CONSTANT ROW 530 531 (no new indexes) 532 533 SCAN pragma_collation_list VIRTUAL TABLE INDEX 0: 534 USE TEMP B-TREE FOR ORDER BY 535 }} 536 } 537 538 do_execsql_test 6.0 { 539 CREATE TABLE x1(a, b, c, d); 540 CREATE INDEX x1ab ON x1(a, lower(b)); 541 CREATE INDEX x1dcba ON x1(d, b+c, a); 542 } 543 544 do_candidates_test 6.1 { 545 SELECT * FROM x1 WHERE b=? ORDER BY a; 546 } { 547 CREATE INDEX x1_idx_`0001267f` ON x1(b, a); 548 CREATE INDEX x1_idx_`00000062` ON x1(b); 549 } 550 551 #------------------------------------------------------------------------- 552 ifcapable fts5 { 553 reset_db 554 do_execsql_test 7.0 { 555 CREATE VIRTUAL TABLE ft USING fts5(a); 556 CREATE TABLE t1(x, y); 557 } 558 559 do_candidates_test 7.1 { 560 SELECT * FROM ft, t1 WHERE a=x 561 } { 562 CREATE INDEX t1_idx_`00000078` ON t1(x); 563 } 564 565 register_tcl_module db 566 proc vtab_command {method args} { 567 global G 568 569 switch -- $method { 570 xConnect { 571 return "CREATE TABLE t1(a, b, c);" 572 } 573 574 xBestIndex { 575 return [list] 576 } 577 578 xFilter { 579 return [list sql "SELECT rowid, * FROM t0"] 580 } 581 } 582 583 return {} 584 } 585 586 do_execsql_test 7.2 { 587 CREATE TABLE t0(a, b, c); 588 INSERT INTO t0 VALUES(1, 2, 3), (11, 22, 33); 589 CREATE VIRTUAL TABLE t2 USING tcl(vtab_command); 590 } 591 592 do_execsql_test 7.3 { 593 SELECT * FROM t2 594 } { 595 1 2 3 596 11 22 33 597 } 598 599 do_candidates_test 7.4 { 600 SELECT * FROM ft, t1 WHERE a=x 601 } { 602 CREATE INDEX t1_idx_`00000078` ON t1(x); 603 } 604 605 do_test 7.5 { 606 set expert [sqlite3_expert_new db] 607 list [catch { $expert sql "SELECT * FROM ft, t2 WHERE b=1" } msg] $msg 608 } {1 {no such table: t2}} 609 $expert destroy 610 611 reset_db 612 do_execsql_test 7.6 { 613 BEGIN TRANSACTION; 614 CREATE TABLE IF NOT EXISTS 'bfts_idx_data'(id INTEGER PRIMARY KEY, block BLOB); 615 CREATE TABLE IF NOT EXISTS 'fts_idx_data'(id INTEGER PRIMARY KEY, block BLOB); 616 INSERT INTO fts_idx_data VALUES(1,X''); 617 INSERT INTO fts_idx_data VALUES(10,X'`00000000ff000001000000`'); 618 CREATE TABLE IF NOT EXISTS 'fts_idx_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID; 619 CREATE TABLE IF NOT EXISTS 'fts_idx_docsize'(id INTEGER PRIMARY KEY, sz BLOB, origin INTEGER); 620 CREATE TABLE IF NOT EXISTS 'fts_idx_config'(k PRIMARY KEY, v) WITHOUT ROWID; 621 INSERT INTO fts_idx_config VALUES('version',4); 622 PRAGMA writable_schema=ON; 623 INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)VALUES('table','fts_idx','fts_idx',0,'CREATE VIRTUAL TABLE fts_idx USING fts5(Title, Description, Channel, Tags, content='''', contentless_delete=1)'); 624 625 CREATE TABLE f(x BLOB, y); 626 COMMIT; 627 PRAGMA writable_schema = RESET; 628 } 629 630 do_candidates_test 7.4 { 631 SELECT * FROM fts_idx, f WHERE x = fts_idx.Channel 632 } { 633 CREATE INDEX f_idx_`00000078` ON f(x); 634 } 635 } 636 637 #------------------------------------------------------------------------- 638 reset_db 639 640 set ci {CREATE INDEX i1 ON t1(a COLLATE "binary,sqlite_expert_rem(999,0)");} 641 642 do_execsql_test 8.0 { 643 BEGIN TRANSACTION; 644 CREATE TABLE t1(a TEXT, b TEXT); 645 INSERT INTO t1 VALUES('v0','d0'); 646 INSERT INTO t1 VALUES('v1','d1'); 647 INSERT INTO t1 VALUES('v2','d2'); 648 INSERT INTO t1 VALUES('v3','d3'); 649 INSERT INTO t1 VALUES('v4','d4'); 650 INSERT INTO t1 VALUES('v5','d5'); 651 INSERT INTO t1 VALUES('v6','d6'); 652 INSERT INTO t1 VALUES('v7','d7'); 653 INSERT INTO t1 VALUES('v8','d8'); 654 INSERT INTO t1 VALUES('v9','d9'); 655 CREATE INDEX i1 ON t1(a); 656 COMMIT; 657 PRAGMA writable_schema = ON; 658 UPDATE sqlite_schema SET sql = $ci WHERE name = 'i1'; 659 } 660 661 db close 662 sqlite3 db test.db 663 664 do_test 8.1 { 665 set expert [sqlite3_expert_new db] 666 $expert sql { SELECT 1234 } 667 list [catch { $expert analyze } msg] $msg 668 } {1 {no such collation sequence: binary,sqlite_expert_rem(999,0)}} 669 670 $expert destroy 671 672 #------------------------------------------------------------------------- 673 reset_db 674 675 proc evil {x} { return $x } 676 db func evil -deterministic -innocuous evil 677 678 do_execsql_test 9.0 { 679 CREATE TABLE t1(a, g AS (evil(a)) VIRTUAL); 680 INSERT INTO t1(a) VALUES('one'),('two'),('three'); 681 CREATE INDEX i1 ON t1(g); 682 } 683 684 proc evil {x} { 685 db eval { SELECT sqlite_expert_rem(`10000000`, 0) } 686 return $x 687 } 688 689 do_test 9.1 { 690 set expert [sqlite3_expert_new db] 691 $expert sql { SELECT 1234 } 692 list [catch { $expert analyze } msg] $msg 693 } {1 SQLITE_ERROR} 694 695 $expert destroy 696 697 #------------------------------------------------------------------------- 698 # Collation sequences with names of 0 or 1 characters. 699 # 700 foreach {tn coll} {1 x 2 {}} { 701 reset_db 702 db collate $coll {string compare} 703 do_execsql_test 10.$tn.0 "CREATE TABLE t1(a COLLATE \"$coll\", b)" 704 do_test 10.$tn.1 { 705 set expert [sqlite3_expert_new db] 706 $expert sql { SELECT * FROM t1 WHERE a=? } 707 $expert analyze 708 set res [string trim [$expert report 0 indexes]] 709 $expert destroy 710 set res 711 } {CREATE INDEX t1_idx_`00000061` ON t1(a);} 712 } 713 714 # A collation sequence whose name begins with a digit. 715 # 716 reset_db 717 db collate 1x {string compare} 718 do_execsql_test 11.0 { CREATE TABLE t1(a, b) } 719 do_test 11.1 { 720 set expert [sqlite3_expert_new db] 721 $expert sql { SELECT * FROM t1 WHERE a=? COLLATE "1x" } 722 $expert analyze 723 set res [string trim [$expert report 0 indexes]] 724 $expert destroy 725 set res 726 } {CREATE INDEX t1_idx_`960cf414` ON t1(a COLLATE '1x');} 727 728 729 730 finish_test