AryaWu/sqlite
0
1# 2024-08-032#3# The author disclaims copyright to this source code. In place of4# 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 14set testdir [file dirname $argv0]15source $testdir/tester.tcl16set testprefix bestindexD17 18ifcapable !vtab {19 finish_test20 return21}22 23register_tcl_module db24 25proc 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 100000036 set used ""37 38 set cons 039 foreach c [$hdl constraints] {40 set cost [expr $cost/10]41 append used " use $cons"42 incr cons43 }44 45 return "cost $cost rows $cost $used"46 }47 }48 49 return {}50}51 52do_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 the60# xBestIndex method matches the actual columns used, which is ascertained 61# by searching the compiled VM code for VColumn instructions.62#63proc do_colsused_test {tn sql} {64 set ::colUsed ""65 execsql $sql66 set got $::colUsed67 68 set expect 069 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 78do_colsused_test 1.1 { SELECT a FROM x1 } 79do_colsused_test 1.2 { SELECT a,c FROM x1 } 80do_colsused_test 1.3 { SELECT b FROM x1 } 81do_colsused_test 1.4 { SELECT b FROM x1 WHERE c=? } 82 83do_colsused_test 1.5 {84 select 1 from t2 full join x1;85}86 87do_colsused_test 1.6 {88 select 1 from x1 WHERE (b=? AND c=?) OR (b=? AND c=?)89}90 91finish_test92 93 94 