1059b2d50Sdrh# 2014-10-24 2059b2d50Sdrh# 3059b2d50Sdrh# The author disclaims copyright to this source code. In place of 4059b2d50Sdrh# a legal notice, here is a blessing: 5059b2d50Sdrh# 6059b2d50Sdrh# May you do good and not evil. 7059b2d50Sdrh# May you find forgiveness for yourself and forgive others. 8059b2d50Sdrh# May you share freely, never taking more than you give. 9059b2d50Sdrh# 10059b2d50Sdrh#************************************************************************* 11059b2d50Sdrh# 12059b2d50Sdrh# This file implements regression tests for SQLite library. The 13059b2d50Sdrh# focus of this script is testing automatic index creation logic, 14059b2d50Sdrh# and specifically creation of automatic partial indexes. 15059b2d50Sdrh# 16059b2d50Sdrh 17059b2d50Sdrhset testdir [file dirname $argv0] 18059b2d50Sdrhsource $testdir/tester.tcl 19059b2d50Sdrh 20059b2d50Sdrhdo_execsql_test autoindex4-1.0 { 21059b2d50Sdrh CREATE TABLE t1(a,b); 22059b2d50Sdrh INSERT INTO t1 VALUES(123,'abc'),(234,'def'),(234,'ghi'),(345,'jkl'); 23059b2d50Sdrh CREATE TABLE t2(x,y); 24059b2d50Sdrh INSERT INTO t2 VALUES(987,'zyx'),(654,'wvu'),(987,'rqp'); 25059b2d50Sdrh 26059b2d50Sdrh SELECT *, '|' FROM t1, t2 WHERE a=234 AND x=987 ORDER BY +b; 27059b2d50Sdrh} {234 def 987 rqp | 234 def 987 zyx | 234 ghi 987 rqp | 234 ghi 987 zyx |} 28059b2d50Sdrhdo_execsql_test autoindex4-1.1 { 29059b2d50Sdrh SELECT *, '|' FROM t1, t2 WHERE a=234 AND x=555; 30059b2d50Sdrh} {} 31059b2d50Sdrh 32059b2d50Sdrhdo_execsql_test autoindex4-1.2 { 33059b2d50Sdrh SELECT *, '|' FROM t1 LEFT JOIN t2 ON a=234 AND x=555; 34059b2d50Sdrh} {123 abc {} {} | 234 def {} {} | 234 ghi {} {} | 345 jkl {} {} |} 35*ec27077cSdrhdo_execsql_test autoindex4-1.2-rj { 36*ec27077cSdrh SELECT t1.*, t2.*, '|' FROM t2 RIGHT JOIN t1 ON a=234 AND x=555; 37*ec27077cSdrh} {123 abc {} {} | 234 def {} {} | 234 ghi {} {} | 345 jkl {} {} |} 38059b2d50Sdrhdo_execsql_test autoindex4-1.3 { 39059b2d50Sdrh SELECT *, '|' FROM t1 LEFT JOIN t2 ON x=555 WHERE a=234; 40059b2d50Sdrh} {234 def {} {} | 234 ghi {} {} |} 41*ec27077cSdrhdo_execsql_test autoindex4-1.3-rj { 42*ec27077cSdrh SELECT t1.*, t2.*, '|' FROM t2 RIGHT JOIN t1 ON x=555 WHERE a=234; 43*ec27077cSdrh} {234 def {} {} | 234 ghi {} {} |} 44059b2d50Sdrhdo_execsql_test autoindex4-1.4 { 45059b2d50Sdrh SELECT *, '|' FROM t1 LEFT JOIN t2 WHERE a=234 AND x=555; 46059b2d50Sdrh} {} 47*ec27077cSdrhdo_execsql_test autoindex4-1.4-rj { 48*ec27077cSdrh SELECT t1.*, t2.*, '|' FROM t2 RIGHT JOIN t1 WHERE a=234 AND x=555; 49*ec27077cSdrh} {} 50059b2d50Sdrh 51059b2d50Sdrh 52059b2d50Sdrhdo_execsql_test autoindex4-2.0 { 53059b2d50Sdrh CREATE TABLE t3(e,f); 54059b2d50Sdrh INSERT INTO t3 VALUES(123,654),(555,444),(234,987); 55059b2d50Sdrh 56059b2d50Sdrh SELECT (SELECT count(*) FROM t1, t2 WHERE a=e AND x=f), e, f, '|' 57059b2d50Sdrh FROM t3 58059b2d50Sdrh ORDER BY rowid; 59059b2d50Sdrh} {1 123 654 | 0 555 444 | 4 234 987 |} 60059b2d50Sdrh 6136f65bafSdrh# Ticket [2326c258d02ead33d] 6236f65bafSdrh# Two joins, one with and the other without an ORDER BY clause. 6336f65bafSdrh# The one without ORDER BY correctly returns two rows of result. 6436f65bafSdrh# The one with ORDER BY returns no rows. 6536f65bafSdrh# 6636f65bafSdrhdo_execsql_test autoindex4-3.0 { 6736f65bafSdrh CREATE TABLE A(Name text); 6836f65bafSdrh CREATE TABLE Items(ItemName text , Name text); 6936f65bafSdrh INSERT INTO Items VALUES('Item1','Parent'); 7036f65bafSdrh INSERT INTO Items VALUES('Item2','Parent'); 7136f65bafSdrh CREATE TABLE B(Name text); 7236f65bafSdrh 7336f65bafSdrh SELECT Items.ItemName 7436f65bafSdrh FROM Items 7536f65bafSdrh LEFT JOIN A ON (A.Name = Items.ItemName and Items.ItemName = 'dummy') 7636f65bafSdrh LEFT JOIN B ON (B.Name = Items.ItemName) 7736f65bafSdrh WHERE Items.Name = 'Parent' 7836f65bafSdrh ORDER BY Items.ItemName; 7936f65bafSdrh} {Item1 Item2} 80077f06edSdrhdo_execsql_test autoindex4-3.1 { 81*ec27077cSdrh SELECT Items.ItemName 82*ec27077cSdrh FROM A 83*ec27077cSdrh RIGHT JOIN Items ON (A.Name = Items.ItemName and Items.ItemName = 'dummy') 84*ec27077cSdrh LEFT JOIN B ON (B.Name = Items.ItemName) 85*ec27077cSdrh WHERE Items.Name = 'Parent' 86*ec27077cSdrh ORDER BY Items.ItemName; 87*ec27077cSdrh} {Item1 Item2} 88*ec27077cSdrhdo_execsql_test autoindex4-3.10 { 89077f06edSdrh CREATE INDEX Items_x1 ON Items(ItemName,Name) WHERE ItemName = 'dummy'; 90077f06edSdrh 91077f06edSdrh SELECT Items.ItemName 92077f06edSdrh FROM Items 93077f06edSdrh LEFT JOIN A ON (A.Name = Items.ItemName and Items.ItemName = 'dummy') 94077f06edSdrh LEFT JOIN B ON (B.Name = Items.ItemName) 95077f06edSdrh WHERE Items.Name = 'Parent' 96077f06edSdrh ORDER BY Items.ItemName; 97077f06edSdrh} {Item1 Item2} 98*ec27077cSdrhdo_execsql_test autoindex4-3.11 { 99*ec27077cSdrh SELECT Items.ItemName 100*ec27077cSdrh FROM A 101*ec27077cSdrh RIGHT JOIN Items ON (A.Name = Items.ItemName and Items.ItemName = 'dummy') 102*ec27077cSdrh LEFT JOIN B ON (B.Name = Items.ItemName) 103*ec27077cSdrh WHERE Items.Name = 'Parent' 104*ec27077cSdrh ORDER BY Items.ItemName; 105*ec27077cSdrh} {Item1 Item2} 10636f65bafSdrh 107c1085ea4Sdrh# 2021-11-30 - Enhancement to help the automatic index mechanism to 108c1085ea4Sdrh# create a partial index more often. 109c1085ea4Sdrh# 110c1085ea4Sdrhunset -nocomplain id data1 data2 jointype onclause whereclause answer 111c1085ea4Sdrhforeach {id data1 data2 jointype onclause whereclause answer} { 112c1085ea4Sdrh 1 113c1085ea4Sdrh VALUES(1,2),(3,4) 114c1085ea4Sdrh VALUES(1,2),(3,4) 115c1085ea4Sdrh {LEFT JOIN} 116c1085ea4Sdrh a=x 117c1085ea4Sdrh {y=4 OR y IS NULL} 118c1085ea4Sdrh {3 4 3 4} 119c1085ea4Sdrh 120c1085ea4Sdrh 2 121c1085ea4Sdrh VALUES(1,2),(3,4) 122c1085ea4Sdrh VALUES(1,2),(3,4) 123c1085ea4Sdrh {LEFT JOIN} 124c1085ea4Sdrh {a=x AND y=4} 125c1085ea4Sdrh {coalesce(y,4)==4} 126c1085ea4Sdrh {1 2 {} {} 3 4 3 4} 127c1085ea4Sdrh 128c1085ea4Sdrh 3 129c1085ea4Sdrh VALUES(1,2),(3,4) 130c1085ea4Sdrh VALUES(1,2),(3,4) 131c1085ea4Sdrh {JOIN} 132c1085ea4Sdrh {a=x} 133c1085ea4Sdrh {y=4 OR y IS NULL} 134c1085ea4Sdrh {3 4 3 4} 135c1085ea4Sdrh 136c1085ea4Sdrh 4 137c1085ea4Sdrh VALUES(1,2),(3,4) 138c1085ea4Sdrh VALUES(1,2),(3,4) 139c1085ea4Sdrh {JOIN} 140c1085ea4Sdrh {a=x AND y=4} 141c1085ea4Sdrh {coalesce(y,4)==4} 142c1085ea4Sdrh {3 4 3 4} 143c1085ea4Sdrh 144c1085ea4Sdrh 5 145c1085ea4Sdrh VALUES(1,2),(3,4),(NULL,4) 146c1085ea4Sdrh VALUES(1,2),(3,4) 147c1085ea4Sdrh {LEFT JOIN} 148c1085ea4Sdrh a=x 149c1085ea4Sdrh {y=4 OR y IS NULL} 150c1085ea4Sdrh {3 4 3 4 {} 4 {} {}} 151c1085ea4Sdrh 152c1085ea4Sdrh 6 153c1085ea4Sdrh VALUES(1,2),(3,4) 154c1085ea4Sdrh VALUES(1,2),(3,4),(NULL,4) 155c1085ea4Sdrh {LEFT JOIN} 156c1085ea4Sdrh {a=x AND y=4} 157c1085ea4Sdrh {coalesce(y,4)==4} 158c1085ea4Sdrh {1 2 {} {} 3 4 3 4} 159c1085ea4Sdrh 160c1085ea4Sdrh 7 161c1085ea4Sdrh VALUES(1,2),(3,4),(NULL,4) 162c1085ea4Sdrh VALUES(1,2),(3,4),(NULL,4) 163c1085ea4Sdrh {JOIN} 164c1085ea4Sdrh {a=x} 165c1085ea4Sdrh {y=4 OR y IS NULL} 166c1085ea4Sdrh {3 4 3 4} 167c1085ea4Sdrh 168c1085ea4Sdrh 8 169c1085ea4Sdrh VALUES(1,2),(3,4) 170c1085ea4Sdrh VALUES(1,2),(3,4) 171c1085ea4Sdrh {JOIN} 172c1085ea4Sdrh {a=x AND y=4} 173c1085ea4Sdrh {coalesce(y,4)==4} 174c1085ea4Sdrh {3 4 3 4} 175c1085ea4Sdrh} { 176c1085ea4Sdrh do_test autoindex4-4.$id.0 { 177c1085ea4Sdrh db eval { 178c1085ea4Sdrh DROP TABLE IF EXISTS t1; 179c1085ea4Sdrh CREATE TABLE t1(a INT, b INT); 180c1085ea4Sdrh DROP TABLE IF EXISTS t2; 181c1085ea4Sdrh CREATE TABLE t2(x INT, y INT); 182c1085ea4Sdrh } 183c1085ea4Sdrh db eval "INSERT INTO t1(a,b) $data1;" 184c1085ea4Sdrh db eval "INSERT INTO t2(x,y) $data2;" 185c1085ea4Sdrh } {} 186c1085ea4Sdrh set sql "SELECT * FROM t1 $jointype t2 ON $onclause WHERE $whereclause" 187c1085ea4Sdrh # puts "sql = $sql" 188c1085ea4Sdrh do_test autoindex4-4.$id.1 { 189c1085ea4Sdrh db eval {PRAGMA automatic_index=ON;} 190c1085ea4Sdrh db eval $sql 191c1085ea4Sdrh } $answer 192c1085ea4Sdrh do_test autoindex4-4.$id.2 { 193c1085ea4Sdrh db eval {PRAGMA automatic_index=OFF;} 194c1085ea4Sdrh db eval $sql 195c1085ea4Sdrh } $answer 196c1085ea4Sdrh} 197c1085ea4Sdrh 198c1085ea4Sdrh 199c1085ea4Sdrh 20036f65bafSdrh 201059b2d50Sdrhfinish_test 202