sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / test / bestindexG.test
| 1 | # 2025-07-14 |
| 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 | # |
| 12 | |
| 13 | set testdir [file dirname $argv0] |
| 14 | source $testdir/tester.tcl |
| 15 | set testprefix bestindexG |
| 16 | |
| 17 | ifcapable !vtab { |
| 18 | finish_test |
| 19 | return |
| 20 | } |
| 21 | |
| 22 | |
| 23 | proc vtab_command {method args} { |
| 24 | switch -- $method { |
| 25 | xConnect { |
| 26 | return "CREATE TABLE tt(id, k)" |
| 27 | } |
| 28 | |
| 29 | xBestIndex { |
| 30 | set hdl [lindex $args 0] |
| 31 | set conslist [$hdl constraints] |
| 32 | |
| 33 | set ret [list] |
| 34 | set idxnum 0 |
| 35 | set iCons 0 |
| 36 | foreach cons $conslist { |
| 37 | array set c $cons |
| 38 | if {$c(usable) && $c(op)=="eq" && $c(column)==0} { |
| 39 | lappend ret use $iCons |
| 40 | set idxnum 1 |
| 41 | break |
| 42 | } |
| 43 | incr iCons |
| 44 | } |
| 45 | |
| 46 | return [concat idxnum $idxnum $ret] |
| 47 | } |
| 48 | |
| 49 | xFilter { |
| 50 | set idxnum [lindex $args 0] |
| 51 | if {$idxnum==0} { |
| 52 | return [list sql "SELECT * FROM t1"] |
| 53 | } |
| 54 | |
| 55 | return [list sql "SELECT * FROM t1 WHERE id=[lindex $args 2 0]"] |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return {} |
| 60 | } |
| 61 | |
| 62 | register_tcl_module db |
| 63 | |
| 64 | do_execsql_test 1.0 { |
| 65 | CREATE TABLE t1(rowid INTEGER PRIMARY KEY, id, k); |
| 66 | INSERT INTO t1 VALUES(1, 1, 1); |
| 67 | INSERT INTO t1 VALUES(2, 2, 2); |
| 68 | INSERT INTO t1 VALUES(3, 3, 3); |
| 69 | INSERT INTO t1 VALUES(4, 4, 4); |
| 70 | |
| 71 | INSERT INTO t1 VALUES(5, 1, 11); |
| 72 | INSERT INTO t1 VALUES(6, 2, 22); |
| 73 | INSERT INTO t1 VALUES(7, 3, 33); |
| 74 | INSERT INTO t1 VALUES(8, 4, 44); |
| 75 | |
| 76 | CREATE VIRTUAL TABLE tt USING tcl(vtab_command) |
| 77 | } |
| 78 | |
| 79 | do_execsql_test 1.1 { |
| 80 | CREATE TABLE probe(id); |
| 81 | CREATE TABLE pr(id, k); |
| 82 | CREATE TABLE sl(id, dk); |
| 83 | |
| 84 | INSERT INTO probe VALUES(3), (44); |
| 85 | |
| 86 | INSERT INTO pr VALUES(3, 3); |
| 87 | INSERT INTO sl VALUES(3, 3); |
| 88 | } {} |
| 89 | |
| 90 | do_execsql_test 1.2 { |
| 91 | with v as ( |
| 92 | select sl.id as id |
| 93 | from tt |
| 94 | left join pr on pr.k = tt.k |
| 95 | join sl on sl.dk = tt.id |
| 96 | ) |
| 97 | select count(*) from probe join v on v.id = probe.id; |
| 98 | } {2} |
| 99 | |
| 100 | #------------------------------------------------------------------------- |
| 101 | # Report https://sqlite.org/bugs/forumpost/aec494fd56 |
| 102 | # |
| 103 | reset_db |
| 104 | ifcapable fts5 { |
| 105 | sqlite3 db "" |
| 106 | register_tcl_module db |
| 107 | |
| 108 | proc vtab_command {method args} { |
| 109 | switch -- $method { |
| 110 | xConnect { |
| 111 | return "CREATE TABLE x(a PRIMARY KEY, b) WITHOUT ROWID" |
| 112 | } |
| 113 | } |
| 114 | return {} |
| 115 | } |
| 116 | |
| 117 | do_execsql_test 2.0 { |
| 118 | CREATE VIRTUAL TABLE v40 USING fts5(c); |
| 119 | BEGIN EXCLUSIVE; |
| 120 | CREATE VIRTUAL TABLE v115 USING tcl(vtab_command); |
| 121 | } |
| 122 | |
| 123 | do_catchsql_test 2.1 { |
| 124 | CREATE VIRTUAL TABLE v123 USING fts5; |
| 125 | } {1 {vtable constructor failed: v123}} |
| 126 | |
| 127 | do_catchsql_test 2.2 { |
| 128 | CREATE TABLE v124 ( |
| 129 | a AS(a), |
| 130 | b INT |
| 131 | ); |
| 132 | } {1 {generated column loop on "a"}} |
| 133 | |
| 134 | do_execsql_test 2.3 { |
| 135 | PRAGMA quick_check; |
| 136 | } {ok} |
| 137 | |
| 138 | do_execsql_test 2.4 { |
| 139 | INSERT INTO v40 VALUES('one two three'); |
| 140 | SELECT rowid FROM v40('one'); |
| 141 | COMMIT; |
| 142 | SELECT rowid FROM v40('two'); |
| 143 | } {1 1} |
| 144 | } |
| 145 | |
| 146 | |
| 147 | finish_test |
| 148 |