2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#***********************************************************************
2N/A# This file implements regression tests for SQLite library. The
2N/A# focus of this file is testing the use of indices in WHERE clases.
2N/Aset testdir [file dirname $argv0]
2N/A# Build some test data
2N/A CREATE TABLE t1(w int, x int, y int);
2N/A CREATE TABLE t2(p int, q int, r int, s int);
2N/A for {set i 1} {$i<=100} {incr i} {
2N/A set x [expr {int(log($i)/log(2))}]
2N/A set y [expr {$i*$i + 2*$i + 1}]
2N/A execsql "INSERT INTO t1 VALUES($w,$x,$y)"
2N/A INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1;
2N/A CREATE INDEX i1w ON t1(w);
2N/A CREATE INDEX i1xy ON t1(x,y);
2N/A CREATE INDEX i2p ON t2(p);
2N/A CREATE INDEX i2r ON t2(r);
2N/A CREATE INDEX i2qs ON t2(q, s);
2N/A# Do an SQL statement. Append the search count to the end of the result.
2N/A set ::sqlite_search_count 0
2N/A return [concat [execsql $sql] $::sqlite_search_count]
2N/A# Verify that queries use an index. We are using the special variable
2N/A# "sqlite_search_count" which tallys the number of executions of MoveTo
2N/A# and Next operators in the VDBE. By verifing that the search count is
2N/A# small we can be assured that indices are being used properly.
2N/A count {SELECT x, y FROM t1 WHERE w=10}
2N/A count {SELECT x, y FROM t1 WHERE w=11}
2N/A count {SELECT x, y FROM t1 WHERE 11=w}
2N/A count {SELECT x, y FROM t1 WHERE 11=w AND x>2}
2N/A count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
2N/A count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
2N/A count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
2N/A count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
2N/A count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
2N/A count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
2N/A count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
2N/A# New for SQLite version 2.1: Verify that that inequality constraints
2N/A# are used correctly.
2N/A count {SELECT w FROM t1 WHERE x=3 AND y<100}
2N/A count {SELECT w FROM t1 WHERE x=3 AND 100>y}
2N/A count {SELECT w FROM t1 WHERE 3=x AND y<100}
2N/A count {SELECT w FROM t1 WHERE 3=x AND 100>y}
2N/A count {SELECT w FROM t1 WHERE x=3 AND y<=100}
2N/A count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
2N/A count {SELECT w FROM t1 WHERE x=3 AND y>225}
2N/A count {SELECT w FROM t1 WHERE x=3 AND 225<y}
2N/A count {SELECT w FROM t1 WHERE x=3 AND y>=225}
2N/A count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
2N/A count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
2N/A count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
2N/A count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
2N/A count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
2N/A# Need to work on optimizing the BETWEEN operator.
2N/A# do_test where-1.26 {
2N/A# count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
2N/A count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
2N/A count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
2N/A count {SELECT w FROM t1 WHERE y==121}
2N/A count {SELECT w FROM t1 WHERE w>97}
2N/A count {SELECT w FROM t1 WHERE w>=97}
2N/A count {SELECT w FROM t1 WHERE w==97}
2N/A count {SELECT w FROM t1 WHERE w+1==98}
2N/A count {SELECT w FROM t1 WHERE w<3}
2N/A count {SELECT w FROM t1 WHERE w<=3}
2N/A count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
2N/A count {SELECT (w) FROM t1 WHERE (w)>(97)}
2N/A count {SELECT (w) FROM t1 WHERE (w)>=(97)}
2N/A count {SELECT (w) FROM t1 WHERE (w)==(97)}
2N/A count {SELECT (w) FROM t1 WHERE ((w)+(1))==(98)}
2N/A# Do the same kind of thing except use a join as the data source.
2N/A SELECT w, p FROM t2, t1
2N/A WHERE x=q AND y=s AND r=8977
2N/A SELECT w, p FROM t2, t1
2N/A WHERE x=q AND s=y AND r=8977
2N/A SELECT w, p FROM t2, t1
2N/A WHERE x=q AND s=y AND r=8977 AND w>10
2N/A SELECT w, p FROM t2, t1
2N/A WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
2N/A SELECT w, p FROM t2, t1
2N/A WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
2N/A SELECT w, p FROM t2, t1
2N/A WHERE x=q AND p=77 AND s=y AND w>5
2N/A SELECT w, p FROM t1, t2
2N/A WHERE x=q AND p>77 AND s=y AND w=5
2N/A# Lets do a 3-way join.
2N/A# Test to see that the special case of a constant WHERE clause is
2N/A SELECT * FROM t1 WHERE 0
2N/A SELECT * FROM t1 WHERE 1 LIMIT 1
2N/A# Verify that IN operators in a WHERE clause are handled correctly.
2N/A SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
2N/A} {1 0 4 2 1 9 3 1 16 0}
2N/A SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
2N/A} {1 0 4 2 1 9 3 1 16 199}
2N/A SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
2N/A} {1 0 4 2 1 9 3 1 16 10}
2N/A SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
2N/A} {1 0 4 2 1 9 3 1 16 199}
2N/A SELECT * FROM t1 WHERE rowid IN
2N/A (select rowid from t1 where rowid IN (-1,2,4))
2N/A SELECT * FROM t1 WHERE rowid+0 IN
2N/A (select rowid from t1 where rowid IN (-1,2,4))
2N/A SELECT * FROM t1 WHERE w IN
2N/A (select rowid from t1 where rowid IN (-1,2,4))
2N/A SELECT * FROM t1 WHERE w+0 IN
2N/A (select rowid from t1 where rowid IN (-1,2,4))
2N/A SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
2N/A SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
2N/A SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
2N/A} {79 6 6400 89 6 8100 199}
2N/A SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
2N/A} {79 6 6400 89 6 8100 74}
2N/A SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
2N/A SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
2N/A# This procedure executes the SQL. Then it checks the generated program
2N/A# for the SQL and appends a "nosort" to the result if the program contains the
2N/A# SortCallback opcode. If the program does not contain the SortCallback
2N/A# opcode it appends "sort"
2N/A set data [execsql $sql]
2N/A set prog [execsql "EXPLAIN $sql"]
2N/A if {[regexp SortCallback $prog]} {set x sort} {set x nosort}
2N/A# Check out the logic that attempts to implement the ORDER BY clause
2N/A# using an index rather than by sorting.
2N/A CREATE TABLE t3(a,b,c);
2N/A CREATE INDEX t3a ON t3(a);
2N/A CREATE INDEX t3bc ON t3(b,c);
2N/A CREATE INDEX t3acb ON t3(a,c,b);
2N/A INSERT INTO t3 SELECT w, 101-w, y FROM t1;
2N/A SELECT count(*), sum(a), sum(b), sum(c) FROM t3;
2N/A} {100 5050 5050 348550}
2N/A SELECT * FROM t3 ORDER BY a LIMIT 3
2N/A} {1 100 4 2 99 9 3 98 16 nosort}
2N/A SELECT * FROM t3 ORDER BY a+1 LIMIT 3
2N/A} {1 100 4 2 99 9 3 98 16 sort}
2N/A SELECT * FROM t3 WHERE a<10 ORDER BY a LIMIT 3
2N/A} {1 100 4 2 99 9 3 98 16 nosort}
2N/A SELECT * FROM t3 WHERE a>0 AND a<10 ORDER BY a LIMIT 3
2N/A} {1 100 4 2 99 9 3 98 16 nosort}
2N/A SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3
2N/A} {1 100 4 2 99 9 3 98 16 nosort}
2N/A SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 3
2N/A} {1 100 4 2 99 9 3 98 16 sort}
2N/A SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
2N/A} {1 100 4 2 99 9 3 98 16 sort}
2N/Ado_test where-6.9.1 {
2N/A SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
2N/Ado_test where-6.9.2 {
2N/A SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
2N/Ado_test where-6.9.3 {
2N/A SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c LIMIT 3
2N/Ado_test where-6.9.4 {
2N/A SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC LIMIT 3
2N/Ado_test where-6.9.5 {
2N/A SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c DESC LIMIT 3
2N/Ado_test where-6.9.6 {
2N/A SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c DESC LIMIT 3
2N/Ado_test where-6.9.7 {
2N/A SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c,a LIMIT 3
2N/Ado_test where-6.9.8 {
2N/A SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c ASC LIMIT 3
2N/Ado_test where-6.9.9 {
2N/A SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a ASC, c DESC LIMIT 3
2N/A SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
2N/A SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
2N/A SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c,b LIMIT 3
2N/A SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
2N/A} {100 1 10201 99 2 10000 98 3 9801 nosort}
2N/Ado_test where-6.13.1 {
2N/A SELECT * FROM t3 WHERE a>0 ORDER BY -a LIMIT 3
2N/A} {100 1 10201 99 2 10000 98 3 9801 sort}
2N/A SELECT * FROM t3 ORDER BY b LIMIT 3
2N/A} {100 1 10201 99 2 10000 98 3 9801 nosort}
2N/A} {1 0 2 1 3 1 nosort}
2N/A SELECT y FROM t1 ORDER BY w COLLATE text LIMIT 3;
2N/A SELECT y FROM t1 ORDER BY w COLLATE numeric LIMIT 3;
2N/A SELECT y FROM t1 ORDER BY w LIMIT 3;
2N/A# Tests for reverse-order sorting.
2N/A SELECT w FROM t1 WHERE x=3 ORDER BY y;
2N/A} {8 9 10 11 12 13 14 15 nosort}
2N/A SELECT w FROM t1 WHERE x=3 ORDER BY y DESC;
2N/A} {15 14 13 12 11 10 9 8 nosort}
2N/A SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y LIMIT 3;
2N/A SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y DESC LIMIT 3;
2N/A SELECT w FROM t1 WHERE x=3 AND y>121 ORDER BY y DESC;
2N/A} {15 14 13 12 11 nosort}
2N/A SELECT w FROM t1 WHERE x=3 AND y>=121 ORDER BY y DESC;
2N/A} {15 14 13 12 11 10 nosort}
2N/A SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y DESC;
2N/A SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y DESC;
2N/A} {13 12 11 10 nosort}
2N/A SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y DESC;
2N/A SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y DESC;
2N/A SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y;
2N/A SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y;
2N/A} {10 11 12 13 nosort}
2N/A SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y;
2N/A SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y;
2N/A SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y;
2N/A SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y;
2N/A SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y;
2N/A SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y;
2N/A SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y DESC;
2N/A SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y DESC;
2N/A SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y DESC;
2N/A SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y DESC;
2N/A SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y;
2N/A SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y;
2N/A SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y;
2N/A SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y;
2N/A SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y DESC;
2N/A SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y DESC;
2N/A SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y DESC;
2N/A SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y DESC;
2N/A CREATE TABLE t4 AS SELECT * FROM t1;
2N/A CREATE INDEX i4xy ON t4(x,y);
2N/A SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
2N/A SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
2N/A# Make sure searches with an index work with an empty table.
2N/A CREATE TABLE t5(x PRIMARY KEY);
2N/A SELECT * FROM t5 WHERE x<10;
2N/A SELECT * FROM t5 WHERE x<10 ORDER BY x DESC;
2N/A SELECT * FROM t5 WHERE x=10;
2N/A SELECT 1 WHERE abs(random())<0
2N/A proc tclvar_func {vname} {return [set ::$vname]}
2N/A db function tclvar tclvar_func
2N/A SELECT count(*) FROM t1 WHERE tclvar('v1');
2N/A SELECT count(*) FROM t1 WHERE tclvar('v1');
2N/A proc tclvar_func {vname} {
2N/A SELECT count(*) FROM t1 WHERE tclvar('v1');
2N/Aintegrity_check {where-99.0}