Better handle WHERE terms that are common to two or more OR branches when planning virtual table queries.
FossilOrigin-Name: 1976c3f7e1fe77cf3367710e8ada230a3672ed374e316425164e42b2622526c7
This commit is contained in:
@@ -210,4 +210,143 @@ do_catchsql_test 4.4 {
|
||||
CREATE VIRTUAL TABLE y1 USING tcl(vtab_command "CREATE TABLE x1(insert)");
|
||||
} {1 {declare_vtab: near "insert": syntax error}}
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
reset_db
|
||||
register_tcl_module db
|
||||
|
||||
proc quote {str} {
|
||||
return "'[string map {' ''} $str]'"
|
||||
}
|
||||
|
||||
proc vtab_command {lVal method args} {
|
||||
switch -- $method {
|
||||
xConnect {
|
||||
return "CREATE TABLE t1(a, b, c, d)"
|
||||
}
|
||||
|
||||
xBestIndex {
|
||||
set hdl [lindex $args 0]
|
||||
set clist [$hdl constraints]
|
||||
|
||||
set res [list]
|
||||
set idx 0
|
||||
set idxnum 0
|
||||
|
||||
set cols(0) a
|
||||
set cols(1) b
|
||||
set cols(2) c
|
||||
|
||||
set lCons [list]
|
||||
|
||||
foreach c $clist {
|
||||
array set a $c
|
||||
if {$a(usable)==0} continue
|
||||
|
||||
if {($a(op)=="eq" || $a(op)=="is") && [info exists cols($a(column))]} {
|
||||
lappend res omit $idx
|
||||
set coll [$hdl collation $idx]
|
||||
lappend lCons "$cols($a(column)) = %[llength $lCons]% COLLATE $coll"
|
||||
|
||||
set idxnum [expr {$idx + (1 << $a(column))}]
|
||||
catch { unset cols($a(column)) }
|
||||
}
|
||||
|
||||
incr idx
|
||||
}
|
||||
|
||||
if {[llength [array names cols]]>0} {
|
||||
set missing [list]
|
||||
for {set i 0} {$i < 3} {incr i} {
|
||||
catch { lappend missing $cols($i) }
|
||||
}
|
||||
set msg "missing required constraints: [join $missing ,]"
|
||||
return [list constraint $msg]
|
||||
}
|
||||
|
||||
set idxstr [join $lCons " AND "]
|
||||
return "cost 1000 rows 1000 idxnum $idxnum $res idxstr {$idxstr}"
|
||||
}
|
||||
|
||||
xFilter {
|
||||
foreach {idxnum idxstr lArg} $args {}
|
||||
set i 0
|
||||
set where $idxstr
|
||||
foreach a $lArg {
|
||||
set where [string map [list %$i% [quote $a]] $where]
|
||||
incr i
|
||||
}
|
||||
# puts $where
|
||||
return [list sql "SELECT rowid, * FROM $lVal WHERE $where"]
|
||||
}
|
||||
}
|
||||
|
||||
return {}
|
||||
}
|
||||
|
||||
do_execsql_test 5.1 {
|
||||
CREATE VIRTUAL TABLE x1 USING tcl(vtab_command t1);
|
||||
CREATE TABLE t1(a, b, c, d);
|
||||
}
|
||||
|
||||
foreach {tn where ok} {
|
||||
0 "WHERE a=? AND b=? AND c=? AND c=?" 1
|
||||
1 "WHERE a=? AND b=? AND c=?" 1
|
||||
2 "WHERE a=? AND b=? AND (c=? OR c=?)" 1
|
||||
3 "WHERE a=? AND b=? AND (c=? OR c=? OR c=?)" 1
|
||||
4 "WHERE a=? AND b=? AND (c IS ? OR c IS ?)" 1
|
||||
5 "WHERE a=? AND ((b=? AND c=?) OR (c=? AND b=?))" 1
|
||||
6 "WHERE a=? AND ((b=? AND c=?) OR (c=?))" 0
|
||||
} {
|
||||
do_test 5.2.$tn {
|
||||
catch { execsql "SELECT * FROM x1 $::where" } msg
|
||||
# if {$tn==0 || $tn==2 || $tn==3} { puts "MSG: $msg" }
|
||||
} [expr !$ok]
|
||||
}
|
||||
|
||||
do_execsql_test 5.3 {
|
||||
SELECT * FROM x1 WHERE (a, b, c) = (?, ?, ?);
|
||||
}
|
||||
|
||||
do_execsql_test 5.4 {
|
||||
INSERT INTO t1(rowid, a, b, c, d) VALUES(1, 'x', 'y', 'z', 'one');
|
||||
INSERT INTO t1(rowid, a, b, c, d) VALUES(2, 'X', 'Y', 'Z', 'two');
|
||||
SELECT * FROM x1 WHERE (a, b, c) = ('X', 'Y', 'Z');
|
||||
} {X Y Z two}
|
||||
do_execsql_test 5.5 {
|
||||
SELECT * FROM x1 WHERE a='x' AND b='y' AND c='z';
|
||||
} {x y z one}
|
||||
do_execsql_test 5.6 {
|
||||
SELECT * FROM x1
|
||||
WHERE a='x' COLLATE nocase AND b='y' COLLATE nocase AND c='z'COLLATE nocase;
|
||||
} {x y z one X Y Z two}
|
||||
|
||||
do_execsql_test 5.7 {
|
||||
DELETE FROM t1;
|
||||
|
||||
INSERT INTO t1(rowid, a, b, c, d) VALUES(0, 'x', 'y', 'z', 'zero');
|
||||
INSERT INTO t1(rowid, a, b, c, d) VALUES(1, 'x', 'y', 'Z', 'one');
|
||||
INSERT INTO t1(rowid, a, b, c, d) VALUES(2, 'x', 'Y', 'z', 'two');
|
||||
INSERT INTO t1(rowid, a, b, c, d) VALUES(3, 'x', 'Y', 'Z', 'three');
|
||||
INSERT INTO t1(rowid, a, b, c, d) VALUES(4, 'X', 'y', 'z', 'four');
|
||||
INSERT INTO t1(rowid, a, b, c, d) VALUES(5, 'X', 'y', 'Z', 'five');
|
||||
INSERT INTO t1(rowid, a, b, c, d) VALUES(6, 'X', 'Y', 'z', 'six');
|
||||
INSERT INTO t1(rowid, a, b, c, d) VALUES(7, 'X', 'Y', 'z', 'seven');
|
||||
}
|
||||
|
||||
do_execsql_test 5.8 {
|
||||
SELECT d FROM x1
|
||||
WHERE a='x' AND ((b='y' AND c='z') OR (b='Y' AND c='z' COLLATE nocase))
|
||||
} {
|
||||
zero two three
|
||||
}
|
||||
|
||||
do_execsql_test 5.9 {
|
||||
SELECT d FROM x1
|
||||
WHERE a='x' COLLATE nocase
|
||||
AND ((b='y' AND c='z') OR (b='Y' AND c='z' COLLATE nocase))
|
||||
} {
|
||||
zero four two
|
||||
three six seven
|
||||
}
|
||||
|
||||
finish_test
|
||||
|
||||
Reference in New Issue
Block a user