sqlite/test/analyze3.test
dan 937d0dea4f Add the experimental sqlite3_reoptimize() API.
FossilOrigin-Name: 9bd6f3d8864d422fe42074688b191915b27ad8ea
2009-10-15 18:35:38 +00:00

600 lines
16 KiB
Plaintext

# 2009 August 06
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file implements regression tests for SQLite library. This file
# implements tests for the sqlite3_reoptimize() functionality.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
ifcapable !stat2 {
finish_test
return
}
#----------------------------------------------------------------------
# Test Organization:
#
# analyze3-1.*: Test that the values of bound parameters are considered
# in the same way as constants when planning queries that
# use range constraints.
#
# analyze3-2.*: Test that the values of bound parameters are considered
# in the same way as constants when planning queries that
# use LIKE expressions in the WHERE clause.
#
# analyze3-3.*: Test that sqlite3_reoptimize() is a no-op when there is
# no way for re-preparing the query to produce a superior
# query plan.
#
# analyze3-4.*: Test that SQL or authorization callback errors occuring
# within sqlite3_reoptimize() are handled correctly.
#
proc getvar {varname} { uplevel #0 set $varname }
db function var getvar
proc eqp {sql {db db}} {
uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
}
proc sf_execsql {sql {db db}} {
set ::sqlite_search_count 0
set r [uplevel [list execsql $sql $db]]
concat $::sqlite_search_count [$db status step] $r
}
#-------------------------------------------------------------------------
#
# analyze3-1.1.1:
# Create a table with two columns. Populate the first column (affinity
# INTEGER) with integer values from 100 to 1100. Create an index on this
# column. ANALYZE the table.
#
# analyze3-1.1.2 - 3.1.3
# Show that there are two possible plans for querying the table with
# a range constraint on the indexed column - "full table scan" or "use
# the index". When the range is specified using literal values, SQLite
# is able to pick the best plan based on the samples in sqlite_stat2.
#
# analyze3-1.1.4 - 3.1.9
# Show that using SQL variables produces the same results as using
# literal values to constrain the range scan. This works because the
# Tcl interface always calls [sqlite3_reoptimize] after binding values.
#
# These tests also check that the compiler code considers column
# affinities when estimating the number of rows scanned by the "use
# index strategy".
#
do_test analyze3-1.1.1 {
execsql {
BEGIN;
CREATE TABLE t1(x INTEGER, y);
CREATE INDEX i1 ON t1(x);
}
for {set i 0} {$i < 1000} {incr i} {
execsql { INSERT INTO t1 VALUES($i+100, $i) }
}
execsql {
COMMIT;
ANALYZE;
}
} {}
do_test analyze3-1.1.2 {
eqp { SELECT sum(y) FROM t1 WHERE x>200 AND x<300 }
} {0 0 {TABLE t1 WITH INDEX i1}}
do_test analyze3-1.1.3 {
eqp { SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 }
} {0 0 {TABLE t1}}
do_test analyze3-1.1.4 {
sf_execsql { SELECT sum(y) FROM t1 WHERE x>200 AND x<300 }
} {199 0 14850}
do_test analyze3-1.1.5 {
set l [string range "200" 0 end]
set u [string range "300" 0 end]
sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
} {199 0 14850}
do_test analyze3-1.1.6 {
set l [expr int(200)]
set u [expr int(300)]
sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
} {199 0 14850}
do_test analyze3-1.1.7 {
sf_execsql { SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 }
} {999 999 499500}
do_test analyze3-1.1.8 {
set l [string range "0" 0 end]
set u [string range "1100" 0 end]
sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
} {999 999 499500}
do_test analyze3-1.1.9 {
set l [expr int(0)]
set u [expr int(1100)]
sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
} {999 999 499500}
# The following tests are similar to the block above. The difference is
# that the indexed column has TEXT affinity in this case. In the tests
# above the affinity is INTEGER.
#
do_test analyze3-1.2.1 {
execsql {
BEGIN;
CREATE TABLE t2(x TEXT, y);
INSERT INTO t2 SELECT * FROM t1;
CREATE INDEX i2 ON t2(x);
COMMIT;
ANALYZE;
}
} {}
do_test analyze3-1.2.2 {
eqp { SELECT sum(y) FROM t2 WHERE x>1 AND x<2 }
} {0 0 {TABLE t2 WITH INDEX i2}}
do_test analyze3-1.2.3 {
eqp { SELECT sum(y) FROM t2 WHERE x>0 AND x<99 }
} {0 0 {TABLE t2}}
do_test analyze3-1.2.4 {
sf_execsql { SELECT sum(y) FROM t2 WHERE x>12 AND x<20 }
} {161 0 4760}
do_test analyze3-1.2.5 {
set l [string range "12" 0 end]
set u [string range "20" 0 end]
sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
} {161 0 text text 4760}
do_test analyze3-1.2.6 {
set l [expr int(12)]
set u [expr int(20)]
sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
} {161 0 integer integer 4760}
do_test analyze3-1.2.7 {
sf_execsql { SELECT sum(y) FROM t2 WHERE x>0 AND x<99 }
} {999 999 490555}
do_test analyze3-1.2.8 {
set l [string range "0" 0 end]
set u [string range "99" 0 end]
sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
} {999 999 text text 490555}
do_test analyze3-1.2.9 {
set l [expr int(0)]
set u [expr int(99)]
sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
} {999 999 integer integer 490555}
# Same tests a third time. This time, column x has INTEGER affinity and
# is not the leftmost column of the table. This triggered a bug causing
# SQLite to use sub-optimal query plans in 3.6.18 and earlier.
#
do_test analyze3-1.3.1 {
execsql {
BEGIN;
CREATE TABLE t3(y TEXT, x INTEGER);
INSERT INTO t3 SELECT y, x FROM t1;
CREATE INDEX i3 ON t3(x);
COMMIT;
ANALYZE;
}
} {}
do_test analyze3-1.3.2 {
eqp { SELECT sum(y) FROM t3 WHERE x>200 AND x<300 }
} {0 0 {TABLE t3 WITH INDEX i3}}
do_test analyze3-1.3.3 {
eqp { SELECT sum(y) FROM t3 WHERE x>0 AND x<1100 }
} {0 0 {TABLE t3}}
do_test analyze3-1.3.4 {
sf_execsql { SELECT sum(y) FROM t3 WHERE x>200 AND x<300 }
} {199 0 14850}
do_test analyze3-1.3.5 {
set l [string range "200" 0 end]
set u [string range "300" 0 end]
sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
} {199 0 14850}
do_test analyze3-1.3.6 {
set l [expr int(200)]
set u [expr int(300)]
sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
} {199 0 14850}
do_test analyze3-1.3.7 {
sf_execsql { SELECT sum(y) FROM t3 WHERE x>0 AND x<1100 }
} {999 999 499500}
do_test analyze3-1.3.8 {
set l [string range "0" 0 end]
set u [string range "1100" 0 end]
sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
} {999 999 499500}
do_test analyze3-1.3.9 {
set l [expr int(0)]
set u [expr int(1100)]
sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
} {999 999 499500}
#-------------------------------------------------------------------------
# Test that the values of bound SQL variables may be used for the LIKE
# optimization.
#
drop_all_tables
do_test analyze3-2.1 {
execsql {
PRAGMA case_sensitive_like=off;
BEGIN;
CREATE TABLE t1(a, b TEXT COLLATE nocase);
CREATE INDEX i1 ON t1(b);
}
for {set i 0} {$i < 1000} {incr i} {
set t ""
append t [lindex {a b c d e f g h i j} [expr $i/100]]
append t [lindex {a b c d e f g h i j} [expr ($i/10)%10]]
append t [lindex {a b c d e f g h i j} [expr ($i%10)]]
execsql { INSERT INTO t1 VALUES($i, $t) }
}
execsql COMMIT
} {}
do_test analyze3-2.2 {
eqp { SELECT count(a) FROM t1 WHERE b LIKE 'a%' }
} {0 0 {TABLE t1 WITH INDEX i1}}
do_test analyze3-2.3 {
eqp { SELECT count(a) FROM t1 WHERE b LIKE '%a' }
} {0 0 {TABLE t1}}
do_test analyze3-2.4 {
sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE 'a%' }
} {101 0 100}
do_test analyze3-2.5 {
sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE '%a' }
} {999 999 100}
do_test analyze3-2.4 {
set like "a%"
sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {101 0 100}
do_test analyze3-2.5 {
set like "%a"
sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {999 999 100}
#-------------------------------------------------------------------------
# This block of tests checks that sqlite3_reoptimize() is a no-op if
# the values bound to any parameters that may affect the query plan
# have not changed since the statement was last compiled.
#
# It is possible to tell if sqlite3_reoptimize() is a no-op by registering
# an authorization callback. If the auth callback is not invoked from
# within a give call to reoptimize(), then it must have been a no-op.
#
# Also test that:
#
# * sqlite3_reoptimize() returns SQLITE_MISUSE if called on a statement
# that was prepared using the legacy sqlite3_prepare() interface,
#
# * sqlite3_reoptimize() returns SQLITE_MISUSE if called on a statement
# that is not in the "reset" state.
#
drop_all_tables
db auth auth
proc auth {args} {
set ::auth 1
return SQLITE_OK
}
# Return true if calling reoptimize() on the statement handle passed
# as an argument causes the statement to be recompiled.
#
proc test_reoptimize {stmt} {
set ::auth 0
sqlite3_reoptimize $stmt
set ::auth
}
do_test analyze3-3.1 {
execsql {
BEGIN;
CREATE TABLE t1(a, b, c);
CREATE INDEX i1 ON t1(b);
}
for {set i 0} {$i < 100} {incr i} {
execsql { INSERT INTO t1 VALUES($i, $i, $i) }
}
execsql COMMIT
execsql ANALYZE
} {}
do_test analyze3-3.2.1 {
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE b>?" -1 dummy]
test_reoptimize $S
} {0}
do_test analyze3-3.2.2 {
sqlite3_bind_text $S 1 "abc" -1
test_reoptimize $S
} {1}
do_test analyze3-3.2.3 {
test_reoptimize $S
} {0}
do_test analyze3-3.2.4 {
sqlite3_finalize $S
} {SQLITE_OK}
do_test analyze3-3.2.1 {
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE b=?" -1 dummy]
test_reoptimize $S
} {0}
do_test analyze3-3.2.2 {
sqlite3_bind_text $S 1 "abc" -1
test_reoptimize $S
} {0}
do_test analyze3-3.2.3 {
sqlite3_finalize $S
} {SQLITE_OK}
do_test analyze3-3.3.1 {
set S [sqlite3_prepare db "SELECT * FROM t1 WHERE b=?" -1 dummy]
sqlite3_reoptimize $S
} {SQLITE_MISUSE}
do_test analyze3-3.3.2 {
sqlite3_finalize $S
} {SQLITE_OK}
do_test analyze3-3.3.1 {
set S [sqlite3_prepare_v2 db "SELECT * FROM t1" -1 dummy]
sqlite3_reoptimize $S
} {SQLITE_OK}
do_test analyze3-3.3.2 {
sqlite3_step $S
} {SQLITE_ROW}
do_test analyze3-3.3.3 {
sqlite3_reoptimize $S
} {SQLITE_MISUSE}
do_test analyze3-3.3.4 {
while {"SQLITE_ROW" == [sqlite3_step $S]} {}
sqlite3_reoptimize $S
} {SQLITE_MISUSE}
do_test analyze3-3.3.5 {
sqlite3_finalize $S
} {SQLITE_OK}
do_test analyze3-3.4.1 {
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
test_reoptimize $S
} {0}
do_test analyze3-3.4.2 {
sqlite3_bind_text $S 1 "abc" -1
test_reoptimize $S
} {0}
do_test analyze3-3.4.3 {
sqlite3_bind_text $S 2 "def" -1
test_reoptimize $S
} {1}
do_test analyze3-3.4.4 {
sqlite3_bind_text $S 2 "ghi" -1
test_reoptimize $S
} {1}
do_test analyze3-3.4.5 {
test_reoptimize $S
} {0}
do_test analyze3-3.4.6 {
sqlite3_finalize $S
} {SQLITE_OK}
do_test analyze3-3.5.1 {
set S [sqlite3_prepare_v2 db {
SELECT * FROM t1 WHERE a IN (
?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31
) AND b>?32;
} -1 dummy]
test_reoptimize $S
} {0}
do_test analyze3-3.5.2 {
sqlite3_bind_text $S 31 "abc" -1
test_reoptimize $S
} {0}
do_test analyze3-3.5.3 {
sqlite3_bind_text $S 32 "def" -1
test_reoptimize $S
} {1}
do_test analyze3-3.5.4 {
test_reoptimize $S
} {0}
do_test analyze3-3.5.5 {
sqlite3_finalize $S
} {SQLITE_OK}
do_test analyze3-3.6.1 {
set S [sqlite3_prepare_v2 db {
SELECT * FROM t1 WHERE a IN (
?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
) AND b>?33;
} -1 dummy]
test_reoptimize $S
} {0}
do_test analyze3-3.6.2 {
sqlite3_bind_text $S 32 "abc" -1
test_reoptimize $S
} {1}
do_test analyze3-3.6.3 {
sqlite3_bind_text $S 33 "def" -1
test_reoptimize $S
} {1}
do_test analyze3-3.6.4 {
test_reoptimize $S
} {0}
do_test analyze3-3.6.5 {
sqlite3_finalize $S
} {SQLITE_OK}
do_test analyze3-3.7.1 {
breakpoint
set S [sqlite3_prepare_v2 db {
SELECT * FROM t1 WHERE a IN (
?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?33,
?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
) AND b>?10;
} -1 dummy]
test_reoptimize $S
} {0}
do_test analyze3-3.7.2 {
sqlite3_bind_text $S 32 "abc" -1
test_reoptimize $S
} {0}
do_test analyze3-3.7.3 {
sqlite3_bind_text $S 33 "def" -1
test_reoptimize $S
} {0}
do_test analyze3-3.7.4 {
sqlite3_bind_text $S 10 "def" -1
test_reoptimize $S
} {1}
do_test analyze3-3.7.5 {
test_reoptimize $S
} {0}
do_test analyze3-3.7.6 {
sqlite3_finalize $S
} {SQLITE_OK}
do_test analyze3-3.8.1 {
execsql {
CREATE TABLE t4(x, y TEXT COLLATE NOCASE);
CREATE INDEX i4 ON t4(y);
}
} {}
do_test analyze3-3.8.2 {
set S [sqlite3_prepare_v2 db {
SELECT * FROM t4 WHERE x != ? AND y LIKE ?
} -1 dummy]
test_reoptimize $S
} {0}
do_test analyze3-3.8.3 {
sqlite3_bind_text $S 1 "abc" -1
test_reoptimize $S
} {0}
do_test analyze3-3.8.4 {
sqlite3_bind_text $S 2 "def" -1
test_reoptimize $S
} {1}
do_test analyze3-3.8.5 {
test_reoptimize $S
} {0}
do_test analyze3-3.8.6 {
sqlite3_expired $S
} {0}
do_test analyze3-3.8.7 {
sqlite3_bind_text $S 2 "ghi%" -1
sqlite3_expired $S
} {0}
do_test analyze3-3.8.8 {
test_reoptimize $S
} {1}
do_test analyze3-3.8.9 {
sqlite3_bind_text $S 2 "ghi%def" -1
sqlite3_expired $S
} {1}
do_test analyze3-3.8.10 {
test_reoptimize $S
} {1}
do_test analyze3-3.8.11 {
sqlite3_bind_text $S 2 "%ab" -1
sqlite3_expired $S
} {1}
do_test analyze3-3.8.12 {
test_reoptimize $S
} {1}
do_test analyze3-3.8.12 {
sqlite3_bind_text $S 2 "%de" -1
sqlite3_expired $S
} {0}
do_test analyze3-3.8.13 {
test_reoptimize $S
} {1}
do_test analyze3-3.8.14 {
sqlite3_finalize $S
} {SQLITE_OK}
#-------------------------------------------------------------------------
# These tests check that errors encountered while repreparing an SQL
# statement within sqlite3_reoptimize() are handled correctly.
#
# Check an schema error.
#
do_test analyze3-4.1.1 {
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
sqlite3_reoptimize $S
} {SQLITE_OK}
do_test analyze3-4.1.2 {
sqlite3_bind_text $S 2 "abc" -1
execsql { DROP TABLE t1 }
sqlite3_reoptimize $S
} {SQLITE_SCHEMA}
do_test analyze3-4.1.3 {
sqlite3_step $S
} {SQLITE_SCHEMA}
do_test analyze3-4.1.4 {
sqlite3_finalize $S
} {SQLITE_SCHEMA}
# Check an authorization error.
#
do_test analyze3-4.2.1 {
execsql {
BEGIN;
CREATE TABLE t1(a, b, c);
CREATE INDEX i1 ON t1(b);
}
for {set i 0} {$i < 100} {incr i} {
execsql { INSERT INTO t1 VALUES($i, $i, $i) }
}
execsql COMMIT
execsql ANALYZE
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
sqlite3_reoptimize $S
} {SQLITE_OK}
db auth auth
proc auth {args} {
if {[lindex $args 0] == "SQLITE_READ"} {return SQLITE_DENY}
return SQLITE_OK
}
do_test analyze3-4.2.2 {
sqlite3_bind_text $S 2 "abc" -1
sqlite3_reoptimize $S
} {SQLITE_SCHEMA}
do_test analyze3-4.2.3 {
sqlite3_step $S
} {SQLITE_SCHEMA}
do_test analyze3-4.2.4 {
sqlite3_finalize $S
} {SQLITE_SCHEMA}
# Check the effect of an authorization error that occurs in a re-prepare
# performed by sqlite3_step() is the same as one that occurs within
# sqlite3_reoptimize().
#
do_test analyze3-4.3.1 {
db auth {}
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
execsql { CREATE TABLE t2(d, e, f) }
db auth auth
sqlite3_step $S
} {SQLITE_SCHEMA}
do_test analyze3-4.3.2 {
sqlite3_finalize $S
} {SQLITE_SCHEMA}
finish_test