sl@0: # 2007 November 29 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file tests the optimisations made in November 2007 of expressions sl@0: # of the following form: sl@0: # sl@0: # IN (SELECT FROM ) sl@0: # sl@0: # $Id: in3.test,v 1.5 2008/08/04 03:51:24 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable !subquery { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # Return the number of OpenEphemeral instructions used in the sl@0: # implementation of the sql statement passed as a an argument. sl@0: # sl@0: proc nEphemeral {sql} { sl@0: set nEph 0 sl@0: foreach op [execsql "EXPLAIN $sql"] { sl@0: if {$op eq "OpenEphemeral"} {incr nEph} sl@0: } sl@0: set nEph sl@0: } sl@0: sl@0: # This proc works the same way as execsql, except that the number sl@0: # of OpenEphemeral instructions used in the implementation of the sl@0: # statement is inserted into the start of the returned list. sl@0: # sl@0: proc exec_neph {sql} { sl@0: return [concat [nEphemeral $sql] [execsql $sql]] sl@0: } sl@0: sl@0: do_test in3-1.1 { sl@0: execsql { sl@0: CREATE TABLE t1(a PRIMARY KEY, b); sl@0: INSERT INTO t1 VALUES(1, 2); sl@0: INSERT INTO t1 VALUES(3, 4); sl@0: INSERT INTO t1 VALUES(5, 6); sl@0: } sl@0: } {} sl@0: sl@0: # All of these queries should avoid using a temp-table: sl@0: # sl@0: do_test in3-1.2 { sl@0: exec_neph { SELECT rowid FROM t1 WHERE rowid IN (SELECT rowid FROM t1); } sl@0: } {0 1 2 3} sl@0: do_test in3-1.3 { sl@0: exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a FROM t1); } sl@0: } {0 1 3 5} sl@0: do_test in3-1.4 { sl@0: exec_neph { SELECT rowid FROM t1 WHERE rowid+0 IN (SELECT rowid FROM t1); } sl@0: } {0 1 2 3} sl@0: do_test in3-1.5 { sl@0: exec_neph { SELECT a FROM t1 WHERE a+0 IN (SELECT a FROM t1); } sl@0: } {0 1 3 5} sl@0: sl@0: # Because none of the sub-select queries in the following statements sl@0: # match the pattern ("SELECT FROM
"), the following do sl@0: # require a temp table. sl@0: # sl@0: do_test in3-1.6 { sl@0: exec_neph { SELECT rowid FROM t1 WHERE rowid IN (SELECT rowid+0 FROM t1); } sl@0: } {1 1 2 3} sl@0: do_test in3-1.7 { sl@0: exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a+0 FROM t1); } sl@0: } {1 1 3 5} sl@0: do_test in3-1.8 { sl@0: exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 WHERE 1); } sl@0: } {1 1 3 5} sl@0: do_test in3-1.9 { sl@0: exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 GROUP BY a); } sl@0: } {1 1 3 5} sl@0: sl@0: # This should not use a temp-table. Even though the sub-select does sl@0: # not exactly match the pattern "SELECT FROM
", in sl@0: # this case the ORDER BY is a no-op and can be ignored. sl@0: do_test in3-1.10 { sl@0: exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 ORDER BY a); } sl@0: } {0 1 3 5} sl@0: sl@0: # These do use the temp-table. Adding the LIMIT clause means the sl@0: # ORDER BY cannot be ignored. sl@0: do_test in3-1.11 { sl@0: exec_neph {SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 ORDER BY a LIMIT 1)} sl@0: } {1 1} sl@0: do_test in3-1.12 { sl@0: exec_neph { sl@0: SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 ORDER BY a LIMIT 1 OFFSET 1) sl@0: } sl@0: } {1 3} sl@0: sl@0: # Has to use a temp-table because of the compound sub-select. sl@0: # sl@0: ifcapable compound { sl@0: do_test in3-1.13 { sl@0: exec_neph { sl@0: SELECT a FROM t1 WHERE a IN ( sl@0: SELECT a FROM t1 UNION ALL SELECT a FROM t1 sl@0: ) sl@0: } sl@0: } {1 1 3 5} sl@0: } sl@0: sl@0: # The first of these queries has to use the temp-table, because the sl@0: # collation sequence used for the index on "t1.a" does not match the sl@0: # collation sequence used by the "IN" comparison. The second does not sl@0: # require a temp-table, because the collation sequences match. sl@0: # sl@0: do_test in3-1.14 { sl@0: exec_neph { SELECT a FROM t1 WHERE a COLLATE nocase IN (SELECT a FROM t1) } sl@0: } {1 1 3 5} sl@0: do_test in3-1.15 { sl@0: exec_neph { SELECT a FROM t1 WHERE a COLLATE binary IN (SELECT a FROM t1) } sl@0: } {0 1 3 5} sl@0: sl@0: # Neither of these queries require a temp-table. The collation sequence sl@0: # makes no difference when using a rowid. sl@0: # sl@0: do_test in3-1.16 { sl@0: exec_neph {SELECT a FROM t1 WHERE a COLLATE nocase IN (SELECT rowid FROM t1)} sl@0: } {0 1 3} sl@0: do_test in3-1.17 { sl@0: exec_neph {SELECT a FROM t1 WHERE a COLLATE binary IN (SELECT rowid FROM t1)} sl@0: } {0 1 3} sl@0: sl@0: # The following tests - in3.2.* - test a bug that was difficult to track sl@0: # down during development. They are not particularly well focused. sl@0: # sl@0: do_test in3-2.1 { sl@0: execsql { sl@0: DROP TABLE IF EXISTS t1; sl@0: CREATE TABLE t1(w int, x int, y int); sl@0: CREATE TABLE t2(p int, q int, r int, s int); sl@0: } sl@0: for {set i 1} {$i<=100} {incr i} { sl@0: set w $i sl@0: set x [expr {int(log($i)/log(2))}] sl@0: set y [expr {$i*$i + 2*$i + 1}] sl@0: execsql "INSERT INTO t1 VALUES($w,$x,$y)" sl@0: } sl@0: set maxy [execsql {select max(y) from t1}] sl@0: db eval { INSERT INTO t2 SELECT 101-w, x, $maxy+1-y, y FROM t1 } sl@0: } {} sl@0: do_test in3-2.2 { sl@0: execsql { sl@0: SELECT rowid sl@0: FROM t1 sl@0: WHERE rowid IN (SELECT rowid FROM t1 WHERE rowid IN (1, 2)); sl@0: } sl@0: } {1 2} sl@0: do_test in3-2.3 { sl@0: execsql { sl@0: select rowid from t1 where rowid IN (-1,2,4) sl@0: } sl@0: } {2 4} sl@0: do_test in3-2.4 { sl@0: execsql { sl@0: SELECT rowid FROM t1 WHERE rowid IN sl@0: (select rowid from t1 where rowid IN (-1,2,4)) sl@0: } sl@0: } {2 4} sl@0: sl@0: #------------------------------------------------------------------------- sl@0: # This next block of tests - in3-3.* - verify that column affinity is sl@0: # correctly handled in cases where an index might be used to optimise sl@0: # an IN (SELECT) expression. sl@0: # sl@0: do_test in3-3.1 { sl@0: catch {execsql { sl@0: DROP TABLE t1; sl@0: DROP TABLE t2; sl@0: }} sl@0: sl@0: execsql { sl@0: sl@0: CREATE TABLE t1(a BLOB, b NUMBER ,c TEXT); sl@0: CREATE UNIQUE INDEX t1_i1 ON t1(a); /* no affinity */ sl@0: CREATE UNIQUE INDEX t1_i2 ON t1(b); /* numeric affinity */ sl@0: CREATE UNIQUE INDEX t1_i3 ON t1(c); /* text affinity */ sl@0: sl@0: CREATE TABLE t2(x BLOB, y NUMBER, z TEXT); sl@0: CREATE UNIQUE INDEX t2_i1 ON t2(x); /* no affinity */ sl@0: CREATE UNIQUE INDEX t2_i2 ON t2(y); /* numeric affinity */ sl@0: CREATE UNIQUE INDEX t2_i3 ON t2(z); /* text affinity */ sl@0: sl@0: INSERT INTO t1 VALUES(1, 1, 1); sl@0: INSERT INTO t2 VALUES('1', '1', '1'); sl@0: } sl@0: } {} sl@0: sl@0: do_test in3-3.2 { sl@0: # No affinity is applied before comparing "x" and "a". Therefore sl@0: # the index can be used (the comparison is false, text!=number). sl@0: exec_neph { SELECT x IN (SELECT a FROM t1) FROM t2 } sl@0: } {0 0} sl@0: do_test in3-3.3 { sl@0: # Logically, numeric affinity is applied to both sides before sl@0: # the comparison. Therefore it is possible to use index t1_i2. sl@0: exec_neph { SELECT x IN (SELECT b FROM t1) FROM t2 } sl@0: } {0 1} sl@0: do_test in3-3.4 { sl@0: # No affinity is applied before the comparison takes place. Making sl@0: # it possible to use index t1_i3. sl@0: exec_neph { SELECT x IN (SELECT c FROM t1) FROM t2 } sl@0: } {0 1} sl@0: sl@0: do_test in3-3.5 { sl@0: # Numeric affinity should be applied to each side before the comparison sl@0: # takes place. Therefore we cannot use index t1_i1, which has no affinity. sl@0: exec_neph { SELECT y IN (SELECT a FROM t1) FROM t2 } sl@0: } {1 1} sl@0: do_test in3-3.6 { sl@0: # Numeric affinity is applied to both sides before sl@0: # the comparison. Therefore it is possible to use index t1_i2. sl@0: exec_neph { SELECT y IN (SELECT b FROM t1) FROM t2 } sl@0: } {0 1} sl@0: do_test in3-3.7 { sl@0: # Numeric affinity is applied before the comparison takes place. sl@0: # Making it impossible to use index t1_i3. sl@0: exec_neph { SELECT y IN (SELECT c FROM t1) FROM t2 } sl@0: } {1 1} sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # sl@0: # Test using a multi-column index. sl@0: # sl@0: do_test in3-4.1 { sl@0: execsql { sl@0: CREATE TABLE t3(a, b, c); sl@0: CREATE UNIQUE INDEX t3_i ON t3(b, a); sl@0: } sl@0: sl@0: execsql { sl@0: INSERT INTO t3 VALUES(1, 'numeric', 2); sl@0: INSERT INTO t3 VALUES(2, 'text', 2); sl@0: INSERT INTO t3 VALUES(3, 'real', 2); sl@0: INSERT INTO t3 VALUES(4, 'none', 2); sl@0: } sl@0: } {} sl@0: do_test in3-4.2 { sl@0: exec_neph { SELECT 'text' IN (SELECT b FROM t3) } sl@0: } {0 1} sl@0: do_test in3-4.3 { sl@0: exec_neph { SELECT 'TEXT' COLLATE nocase IN (SELECT b FROM t3) } sl@0: } {1 1} sl@0: do_test in3-4.4 { sl@0: # A temp table must be used because t3_i.b is not guaranteed to be unique. sl@0: exec_neph { SELECT b FROM t3 WHERE b IN (SELECT b FROM t3) } sl@0: } {1 none numeric real text} sl@0: do_test in3-4.5 { sl@0: execsql { CREATE UNIQUE INDEX t3_i2 ON t3(b) } sl@0: exec_neph { SELECT b FROM t3 WHERE b IN (SELECT b FROM t3) } sl@0: } {0 none numeric real text} sl@0: do_test in3-4.6 { sl@0: execsql { DROP INDEX t3_i2 } sl@0: } {} sl@0: sl@0: # The following two test cases verify that ticket #2991 has been fixed. sl@0: # sl@0: do_test in3-5.1 { sl@0: execsql { sl@0: CREATE TABLE Folders( sl@0: folderid INTEGER PRIMARY KEY, sl@0: parentid INTEGER, sl@0: rootid INTEGER, sl@0: path VARCHAR(255) sl@0: ); sl@0: } sl@0: } {} sl@0: do_test in3-5.2 { sl@0: catchsql { sl@0: DELETE FROM Folders WHERE folderid IN sl@0: (SELECT folderid FROM Folder WHERE path LIKE 'C:\MP3\Albums\' || '%'); sl@0: } sl@0: } {1 {no such table: Folder}} sl@0: sl@0: finish_test