| 1 |
# 2024-08-03 |
| 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 |
|
| 14 |
set testdir [file dirname $argv0] |
| 15 |
source $testdir/tester.tcl |
| 16 |
set testprefix bestindexD |
| 17 |
|
| 18 |
ifcapable !vtab { |
| 19 |
finish_test |
| 20 |
return |
| 21 |
} |
| 22 |
|
| 23 |
register_tcl_module db |
| 24 |
|
| 25 |
proc vtab_command {method args} { |
| 26 |
switch -- $method { |
| 27 |
xConnect { |
| 28 |
return "CREATE TABLE t1(a PRIMARY KEY, b, c) WITHOUT ROWID" |
| 29 |
} |
| 30 |
|
| 31 |
xBestIndex { |
| 32 |
set hdl [lindex $args 0] |
| 33 |
set ::colUsed [$hdl mask] |
| 34 |
|
| 35 |
set cost 1000000 |
| 36 |
set used "" |
| 37 |
|
| 38 |
set cons 0 |
| 39 |
foreach c [$hdl constraints] { |
| 40 |
set cost [expr $cost/10] |
| 41 |
append used " use $cons" |
| 42 |
incr cons |
| 43 |
} |
| 44 |
|
| 45 |
return "cost $cost rows $cost $used" |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return {} |
| 50 |
} |
| 51 |
|
| 52 |
do_execsql_test 1.0 { |
| 53 |
CREATE VIRTUAL TABLE x1 USING tcl(vtab_command); |
| 54 |
|
| 55 |
CREATE TABLE t2(a, b); |
| 56 |
} {} |
| 57 |
|
| 58 |
# This proc assumes that there is only one use of a virtual table - x1 - |
| 59 |
# in SQL statement $sql. It tests that the colUsed value passed to the |
| 60 |
# xBestIndex method matches the actual columns used, which is ascertained |
| 61 |
# by searching the compiled VM code for VColumn instructions. |
| 62 |
# |
| 63 |
proc do_colsused_test {tn sql} { |
| 64 |
set ::colUsed "" |
| 65 |
execsql $sql |
| 66 |
set got $::colUsed |
| 67 |
|
| 68 |
set expect 0 |
| 69 |
db eval "EXPLAIN $sql" x { |
| 70 |
if {$x(opcode)=="VColumn"} { |
| 71 |
set expect [expr $expect | (1<<$x(p2))] |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
uplevel [list do_test $tn.($expect/$got) [list expr ($expect & $got)] $expect] |
| 76 |
} |
| 77 |
|
| 78 |
do_colsused_test 1.1 { SELECT a FROM x1 } |
| 79 |
do_colsused_test 1.2 { SELECT a,c FROM x1 } |
| 80 |
do_colsused_test 1.3 { SELECT b FROM x1 } |
| 81 |
do_colsused_test 1.4 { SELECT b FROM x1 WHERE c=? } |
| 82 |
|
| 83 |
do_colsused_test 1.5 { |
| 84 |
select 1 from t2 full join x1; |
| 85 |
} |
| 86 |
|
| 87 |
do_colsused_test 1.6 { |
| 88 |
select 1 from x1 WHERE (b=? AND c=?) OR (b=? AND c=?) |
| 89 |
} |
| 90 |
|
| 91 |
finish_test |
| 92 |
|
| 93 |
|