sl@0: # 2008 June 24 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 implements regression tests for SQLite library. sl@0: # sl@0: # $Id: select9.test,v 1.4 2008/07/01 14:39:35 danielk1977 Exp $ sl@0: sl@0: # The tests in this file are focused on test compound SELECT statements sl@0: # that have any or all of an ORDER BY, LIMIT or OFFSET clauses. As of sl@0: # version 3.6.0, SQLite contains code to use SQL indexes where possible sl@0: # to optimize such statements. sl@0: # sl@0: sl@0: # TODO Points: sl@0: # sl@0: # * Are there any "column affinity" issues to consider? sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: #set ISQUICK 1 sl@0: sl@0: #------------------------------------------------------------------------- sl@0: # test_compound_select TESTNAME SELECT RESULT sl@0: # sl@0: # This command is used to run multiple LIMIT/OFFSET test cases based on sl@0: # the single SELECT statement passed as the second argument. The SELECT sl@0: # statement may not contain a LIMIT or OFFSET clause. This proc tests sl@0: # many statements of the form: sl@0: # sl@0: # "$SELECT limit $X offset $Y" sl@0: # sl@0: # for various values of $X and $Y. sl@0: # sl@0: # The third argument, $RESULT, should contain the expected result of sl@0: # the command [execsql $SELECT]. sl@0: # sl@0: # The first argument, $TESTNAME, is used as the base test case name to sl@0: # pass to [do_test] for each individual LIMIT OFFSET test case. sl@0: # sl@0: proc test_compound_select {testname sql result} { sl@0: sl@0: set nCol 1 sl@0: db eval $sql A { sl@0: set nCol [llength $A(*)] sl@0: break sl@0: } sl@0: set nRow [expr {[llength $result] / $nCol}] sl@0: sl@0: set ::compound_sql $sql sl@0: do_test $testname { sl@0: execsql $::compound_sql sl@0: } $result sl@0: #return sl@0: sl@0: set iLimitIncr 1 sl@0: set iOffsetIncr 1 sl@0: if {[info exists ::ISQUICK] && $::ISQUICK && $nRow>=5} { sl@0: set iOffsetIncr [expr $nRow / 5] sl@0: set iLimitIncr [expr $nRow / 5] sl@0: } sl@0: sl@0: set iLimitEnd [expr $nRow+$iLimitIncr] sl@0: set iOffsetEnd [expr $nRow+$iOffsetIncr] sl@0: sl@0: for {set iOffset 0} {$iOffset < $iOffsetEnd} {incr iOffset $iOffsetIncr} { sl@0: for {set iLimit 0} {$iLimit < $iLimitEnd} {incr iLimit} { sl@0: sl@0: set ::compound_sql "$sql LIMIT $iLimit" sl@0: if {$iOffset != 0} { sl@0: append ::compound_sql " OFFSET $iOffset" sl@0: } sl@0: sl@0: set iStart [expr {$iOffset*$nCol}] sl@0: set iEnd [expr {($iOffset*$nCol) + ($iLimit*$nCol) -1}] sl@0: sl@0: do_test $testname.limit=$iLimit.offset=$iOffset { sl@0: execsql $::compound_sql sl@0: } [lrange $result $iStart $iEnd] sl@0: } sl@0: } sl@0: } sl@0: sl@0: #------------------------------------------------------------------------- sl@0: # test_compound_select_flippable TESTNAME SELECT RESULT sl@0: # sl@0: # This command is for testing statements of the form: sl@0: # sl@0: # ORDER BY sl@0: # sl@0: # where each is a simple (non-compound) select statement sl@0: # and is one of "INTERSECT", "UNION ALL" or "UNION". sl@0: # sl@0: # This proc calls [test_compound_select] twice, once with the select sl@0: # statement as it is passed to this command, and once with the positions sl@0: # of exchanged. sl@0: # sl@0: proc test_compound_select_flippable {testname sql result} { sl@0: test_compound_select $testname $sql $result sl@0: sl@0: set select [string trim $sql] sl@0: set RE {(.*)(UNION ALL|INTERSECT|UNION)(.*)(ORDER BY.*)} sl@0: set rc [regexp $RE $select -> s1 op s2 order_by] sl@0: if {!$rc} {error "Statement is unflippable: $select"} sl@0: sl@0: set flipsql "$s2 $op $s1 $order_by" sl@0: test_compound_select $testname.flipped $flipsql $result sl@0: } sl@0: sl@0: ############################################################################# sl@0: # Begin tests. sl@0: # sl@0: sl@0: # Create and populate a sample database. sl@0: # sl@0: do_test select9-1.0 { sl@0: execsql { sl@0: CREATE TABLE t1(a, b, c); sl@0: CREATE TABLE t2(d, e, f); sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES(1, 'one', 'I'); sl@0: INSERT INTO t1 VALUES(3, NULL, NULL); sl@0: INSERT INTO t1 VALUES(5, 'five', 'V'); sl@0: INSERT INTO t1 VALUES(7, 'seven', 'VII'); sl@0: INSERT INTO t1 VALUES(9, NULL, NULL); sl@0: INSERT INTO t1 VALUES(2, 'two', 'II'); sl@0: INSERT INTO t1 VALUES(4, 'four', 'IV'); sl@0: INSERT INTO t1 VALUES(6, NULL, NULL); sl@0: INSERT INTO t1 VALUES(8, 'eight', 'VIII'); sl@0: INSERT INTO t1 VALUES(10, 'ten', 'X'); sl@0: sl@0: INSERT INTO t2 VALUES(1, 'two', 'IV'); sl@0: INSERT INTO t2 VALUES(2, 'four', 'VIII'); sl@0: INSERT INTO t2 VALUES(3, NULL, NULL); sl@0: INSERT INTO t2 VALUES(4, 'eight', 'XVI'); sl@0: INSERT INTO t2 VALUES(5, 'ten', 'XX'); sl@0: INSERT INTO t2 VALUES(6, NULL, NULL); sl@0: INSERT INTO t2 VALUES(7, 'fourteen', 'XXVIII'); sl@0: INSERT INTO t2 VALUES(8, 'sixteen', 'XXXII'); sl@0: INSERT INTO t2 VALUES(9, NULL, NULL); sl@0: INSERT INTO t2 VALUES(10, 'twenty', 'XL'); sl@0: sl@0: COMMIT; sl@0: } sl@0: } {} sl@0: sl@0: # Each iteration of this loop runs the same tests with a different set sl@0: # of indexes present within the database schema. The data returned by sl@0: # the compound SELECT statements in the test cases should be the same sl@0: # in each case. sl@0: # sl@0: set iOuterLoop 1 sl@0: foreach indexes [list { sl@0: /* Do not create any indexes. */ sl@0: } { sl@0: CREATE INDEX i1 ON t1(a) sl@0: } { sl@0: CREATE INDEX i2 ON t1(b) sl@0: } { sl@0: CREATE INDEX i3 ON t2(d) sl@0: } { sl@0: CREATE INDEX i4 ON t2(e) sl@0: }] { sl@0: sl@0: do_test select9-1.$iOuterLoop.1 { sl@0: execsql $indexes sl@0: } {} sl@0: sl@0: # Test some 2-way UNION ALL queries. No WHERE clauses. sl@0: # sl@0: test_compound_select select9-1.$iOuterLoop.2 { sl@0: SELECT a, b FROM t1 UNION ALL SELECT d, e FROM t2 sl@0: } {1 one 3 {} 5 five 7 seven 9 {} 2 two 4 four 6 {} 8 eight 10 ten 1 two 2 four 3 {} 4 eight 5 ten 6 {} 7 fourteen 8 sixteen 9 {} 10 twenty} sl@0: test_compound_select select9-1.$iOuterLoop.3 { sl@0: SELECT a, b FROM t1 UNION ALL SELECT d, e FROM t2 ORDER BY 1 sl@0: } {1 one 1 two 2 two 2 four 3 {} 3 {} 4 four 4 eight 5 five 5 ten 6 {} 6 {} 7 seven 7 fourteen 8 eight 8 sixteen 9 {} 9 {} 10 ten 10 twenty} sl@0: test_compound_select select9-1.$iOuterLoop.4 { sl@0: SELECT a, b FROM t1 UNION ALL SELECT d, e FROM t2 ORDER BY 2 sl@0: } {3 {} 9 {} 6 {} 3 {} 6 {} 9 {} 8 eight 4 eight 5 five 4 four 2 four 7 fourteen 1 one 7 seven 8 sixteen 10 ten 5 ten 10 twenty 2 two 1 two} sl@0: test_compound_select_flippable select9-1.$iOuterLoop.5 { sl@0: SELECT a, b FROM t1 UNION ALL SELECT d, e FROM t2 ORDER BY 1, 2 sl@0: } {1 one 1 two 2 four 2 two 3 {} 3 {} 4 eight 4 four 5 five 5 ten 6 {} 6 {} 7 fourteen 7 seven 8 eight 8 sixteen 9 {} 9 {} 10 ten 10 twenty} sl@0: test_compound_select_flippable select9-1.$iOuterLoop.6 { sl@0: SELECT a, b FROM t1 UNION ALL SELECT d, e FROM t2 ORDER BY 2, 1 sl@0: } {3 {} 3 {} 6 {} 6 {} 9 {} 9 {} 4 eight 8 eight 5 five 2 four 4 four 7 fourteen 1 one 7 seven 8 sixteen 5 ten 10 ten 10 twenty 1 two 2 two} sl@0: sl@0: # Test some 2-way UNION queries. sl@0: # sl@0: test_compound_select select9-1.$iOuterLoop.7 { sl@0: SELECT a, b FROM t1 UNION SELECT d, e FROM t2 sl@0: } {1 one 1 two 2 four 2 two 3 {} 4 eight 4 four 5 five 5 ten 6 {} 7 fourteen 7 seven 8 eight 8 sixteen 9 {} 10 ten 10 twenty} sl@0: sl@0: test_compound_select select9-1.$iOuterLoop.8 { sl@0: SELECT a, b FROM t1 UNION SELECT d, e FROM t2 ORDER BY 1 sl@0: } {1 one 1 two 2 four 2 two 3 {} 4 eight 4 four 5 five 5 ten 6 {} 7 fourteen 7 seven 8 eight 8 sixteen 9 {} 10 ten 10 twenty} sl@0: sl@0: test_compound_select select9-1.$iOuterLoop.9 { sl@0: SELECT a, b FROM t1 UNION SELECT d, e FROM t2 ORDER BY 2 sl@0: } {3 {} 6 {} 9 {} 4 eight 8 eight 5 five 2 four 4 four 7 fourteen 1 one 7 seven 8 sixteen 5 ten 10 ten 10 twenty 1 two 2 two} sl@0: sl@0: test_compound_select_flippable select9-1.$iOuterLoop.10 { sl@0: SELECT a, b FROM t1 UNION SELECT d, e FROM t2 ORDER BY 1, 2 sl@0: } {1 one 1 two 2 four 2 two 3 {} 4 eight 4 four 5 five 5 ten 6 {} 7 fourteen 7 seven 8 eight 8 sixteen 9 {} 10 ten 10 twenty} sl@0: sl@0: test_compound_select_flippable select9-1.$iOuterLoop.11 { sl@0: SELECT a, b FROM t1 UNION SELECT d, e FROM t2 ORDER BY 2, 1 sl@0: } {3 {} 6 {} 9 {} 4 eight 8 eight 5 five 2 four 4 four 7 fourteen 1 one 7 seven 8 sixteen 5 ten 10 ten 10 twenty 1 two 2 two} sl@0: sl@0: # Test some 2-way INTERSECT queries. sl@0: # sl@0: test_compound_select select9-1.$iOuterLoop.11 { sl@0: SELECT a, b FROM t1 INTERSECT SELECT d, e FROM t2 sl@0: } {3 {} 6 {} 9 {}} sl@0: test_compound_select_flippable select9-1.$iOuterLoop.12 { sl@0: SELECT a, b FROM t1 INTERSECT SELECT d, e FROM t2 ORDER BY 1 sl@0: } {3 {} 6 {} 9 {}} sl@0: test_compound_select select9-1.$iOuterLoop.13 { sl@0: SELECT a, b FROM t1 INTERSECT SELECT d, e FROM t2 ORDER BY 2 sl@0: } {3 {} 6 {} 9 {}} sl@0: test_compound_select_flippable select9-1.$iOuterLoop.14 { sl@0: SELECT a, b FROM t1 INTERSECT SELECT d, e FROM t2 ORDER BY 2, 1 sl@0: } {3 {} 6 {} 9 {}} sl@0: test_compound_select_flippable select9-1.$iOuterLoop.15 { sl@0: SELECT a, b FROM t1 INTERSECT SELECT d, e FROM t2 ORDER BY 1, 2 sl@0: } {3 {} 6 {} 9 {}} sl@0: sl@0: # Test some 2-way EXCEPT queries. sl@0: # sl@0: test_compound_select select9-1.$iOuterLoop.16 { sl@0: SELECT a, b FROM t1 EXCEPT SELECT d, e FROM t2 sl@0: } {1 one 2 two 4 four 5 five 7 seven 8 eight 10 ten} sl@0: sl@0: test_compound_select select9-1.$iOuterLoop.17 { sl@0: SELECT a, b FROM t1 EXCEPT SELECT d, e FROM t2 ORDER BY 1 sl@0: } {1 one 2 two 4 four 5 five 7 seven 8 eight 10 ten} sl@0: sl@0: test_compound_select select9-1.$iOuterLoop.18 { sl@0: SELECT a, b FROM t1 EXCEPT SELECT d, e FROM t2 ORDER BY 2 sl@0: } {8 eight 5 five 4 four 1 one 7 seven 10 ten 2 two} sl@0: sl@0: test_compound_select select9-1.$iOuterLoop.19 { sl@0: SELECT a, b FROM t1 EXCEPT SELECT d, e FROM t2 ORDER BY 1, 2 sl@0: } {1 one 2 two 4 four 5 five 7 seven 8 eight 10 ten} sl@0: sl@0: test_compound_select select9-1.$iOuterLoop.20 { sl@0: SELECT a, b FROM t1 EXCEPT SELECT d, e FROM t2 ORDER BY 2, 1 sl@0: } {8 eight 5 five 4 four 1 one 7 seven 10 ten 2 two} sl@0: sl@0: incr iOuterLoop sl@0: } sl@0: sl@0: do_test select9-2.0 { sl@0: execsql { sl@0: DROP INDEX i1; sl@0: DROP INDEX i2; sl@0: DROP INDEX i3; sl@0: DROP INDEX i4; sl@0: } sl@0: } {} sl@0: sl@0: proc reverse {lhs rhs} { sl@0: return [string compare $rhs $lhs] sl@0: } sl@0: db collate reverse reverse sl@0: sl@0: # This loop is similar to the previous one (test cases select9-1.*) sl@0: # except that the simple select statements have WHERE clauses attached sl@0: # to them. Sometimes the WHERE clause may be satisfied using the same sl@0: # index used for ORDER BY, sometimes not. sl@0: # sl@0: set iOuterLoop 1 sl@0: foreach indexes [list { sl@0: /* Do not create any indexes. */ sl@0: } { sl@0: CREATE INDEX i1 ON t1(a) sl@0: } { sl@0: DROP INDEX i1; sl@0: CREATE INDEX i1 ON t1(b, a) sl@0: } { sl@0: CREATE INDEX i2 ON t2(d DESC, e COLLATE REVERSE ASC); sl@0: } { sl@0: CREATE INDEX i3 ON t1(a DESC); sl@0: }] { sl@0: do_test select9-2.$iOuterLoop.1 { sl@0: execsql $indexes sl@0: } {} sl@0: sl@0: test_compound_select_flippable select9-2.$iOuterLoop.2 { sl@0: SELECT * FROM t1 WHERE a<5 UNION SELECT * FROM t2 WHERE d>=5 ORDER BY 1 sl@0: } {1 one I 2 two II 3 {} {} 4 four IV 5 ten XX 6 {} {} 7 fourteen XXVIII 8 sixteen XXXII 9 {} {} 10 twenty XL} sl@0: sl@0: test_compound_select_flippable select9-2.$iOuterLoop.2 { sl@0: SELECT * FROM t1 WHERE a<5 UNION SELECT * FROM t2 WHERE d>=5 ORDER BY 2, 1 sl@0: } {3 {} {} 6 {} {} 9 {} {} 4 four IV 7 fourteen XXVIII 1 one I 8 sixteen XXXII 5 ten XX 10 twenty XL 2 two II} sl@0: sl@0: test_compound_select_flippable select9-2.$iOuterLoop.3 { sl@0: SELECT * FROM t1 WHERE a<5 UNION SELECT * FROM t2 WHERE d>=5 sl@0: ORDER BY 2 COLLATE reverse, 1 sl@0: } {3 {} {} 6 {} {} 9 {} {} 2 two II 10 twenty XL 5 ten XX 8 sixteen XXXII 1 one I 7 fourteen XXVIII 4 four IV} sl@0: sl@0: test_compound_select_flippable select9-2.$iOuterLoop.4 { sl@0: SELECT * FROM t1 WHERE a<5 UNION ALL SELECT * FROM t2 WHERE d>=5 ORDER BY 1 sl@0: } {1 one I 2 two II 3 {} {} 4 four IV 5 ten XX 6 {} {} 7 fourteen XXVIII 8 sixteen XXXII 9 {} {} 10 twenty XL} sl@0: sl@0: test_compound_select_flippable select9-2.$iOuterLoop.5 { sl@0: SELECT * FROM t1 WHERE a<5 UNION ALL SELECT * FROM t2 WHERE d>=5 ORDER BY 2, 1 sl@0: } {3 {} {} 6 {} {} 9 {} {} 4 four IV 7 fourteen XXVIII 1 one I 8 sixteen XXXII 5 ten XX 10 twenty XL 2 two II} sl@0: sl@0: test_compound_select_flippable select9-2.$iOuterLoop.6 { sl@0: SELECT * FROM t1 WHERE a<5 UNION ALL SELECT * FROM t2 WHERE d>=5 sl@0: ORDER BY 2 COLLATE reverse, 1 sl@0: } {3 {} {} 6 {} {} 9 {} {} 2 two II 10 twenty XL 5 ten XX 8 sixteen XXXII 1 one I 7 fourteen XXVIII 4 four IV} sl@0: sl@0: test_compound_select select9-2.$iOuterLoop.4 { sl@0: SELECT a FROM t1 WHERE a<8 EXCEPT SELECT d FROM t2 WHERE d<=3 ORDER BY 1 sl@0: } {4 5 6 7} sl@0: sl@0: test_compound_select select9-2.$iOuterLoop.4 { sl@0: SELECT a FROM t1 WHERE a<8 INTERSECT SELECT d FROM t2 WHERE d<=3 ORDER BY 1 sl@0: } {1 2 3} sl@0: sl@0: } sl@0: sl@0: do_test select9-2.X { sl@0: execsql { sl@0: DROP INDEX i1; sl@0: DROP INDEX i2; sl@0: DROP INDEX i3; sl@0: } sl@0: } {} sl@0: sl@0: # This procedure executes the SQL. Then it checks the generated program sl@0: # for the SQL and appends a "nosort" to the result if the program contains the sl@0: # SortCallback opcode. If the program does not contain the SortCallback sl@0: # opcode it appends "sort" sl@0: # sl@0: proc cksort {sql} { sl@0: set ::sqlite_sort_count 0 sl@0: set data [execsql $sql] sl@0: if {$::sqlite_sort_count} {set x sort} {set x nosort} sl@0: lappend data $x sl@0: return $data sl@0: } sl@0: sl@0: # If the right indexes exist, the following query: sl@0: # sl@0: # SELECT t1.a FROM t1 UNION ALL SELECT t2.d FROM t2 ORDER BY 1 sl@0: # sl@0: # can use indexes to run without doing a in-memory sort operation. sl@0: # This block of tests (select9-3.*) is used to check if the same sl@0: # is possible with: sl@0: # sl@0: # CREATE VIEW v1 AS SELECT a FROM t1 UNION ALL SELECT d FROM t2 sl@0: # SELECT a FROM v1 ORDER BY 1 sl@0: # sl@0: # It turns out that it is. sl@0: # sl@0: do_test select9-3.1 { sl@0: cksort { SELECT a FROM t1 ORDER BY 1 } sl@0: } {1 2 3 4 5 6 7 8 9 10 sort} sl@0: do_test select9-3.2 { sl@0: execsql { CREATE INDEX i1 ON t1(a) } sl@0: cksort { SELECT a FROM t1 ORDER BY 1 } sl@0: } {1 2 3 4 5 6 7 8 9 10 nosort} sl@0: do_test select9-3.3 { sl@0: cksort { SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 LIMIT 5 } sl@0: } {1 1 2 2 3 sort} sl@0: do_test select9-3.4 { sl@0: execsql { CREATE INDEX i2 ON t2(d) } sl@0: cksort { SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 LIMIT 5 } sl@0: } {1 1 2 2 3 nosort} sl@0: do_test select9-3.5 { sl@0: execsql { CREATE VIEW v1 AS SELECT a FROM t1 UNION ALL SELECT d FROM t2 } sl@0: cksort { SELECT a FROM v1 ORDER BY 1 LIMIT 5 } sl@0: } {1 1 2 2 3 nosort} sl@0: do_test select9-3.X { sl@0: execsql { sl@0: DROP INDEX i1; sl@0: DROP INDEX i2; sl@0: DROP VIEW v1; sl@0: } sl@0: } {} sl@0: sl@0: # This block of tests is the same as the preceding one, except that sl@0: # "UNION" is tested instead of "UNION ALL". sl@0: # sl@0: do_test select9-4.1 { sl@0: cksort { SELECT a FROM t1 ORDER BY 1 } sl@0: } {1 2 3 4 5 6 7 8 9 10 sort} sl@0: do_test select9-4.2 { sl@0: execsql { CREATE INDEX i1 ON t1(a) } sl@0: cksort { SELECT a FROM t1 ORDER BY 1 } sl@0: } {1 2 3 4 5 6 7 8 9 10 nosort} sl@0: do_test select9-4.3 { sl@0: cksort { SELECT a FROM t1 UNION SELECT d FROM t2 ORDER BY 1 LIMIT 5 } sl@0: } {1 2 3 4 5 sort} sl@0: do_test select9-4.4 { sl@0: execsql { CREATE INDEX i2 ON t2(d) } sl@0: cksort { SELECT a FROM t1 UNION SELECT d FROM t2 ORDER BY 1 LIMIT 5 } sl@0: } {1 2 3 4 5 nosort} sl@0: do_test select9-4.5 { sl@0: execsql { CREATE VIEW v1 AS SELECT a FROM t1 UNION SELECT d FROM t2 } sl@0: cksort { SELECT a FROM v1 ORDER BY 1 LIMIT 5 } sl@0: } {1 2 3 4 5 sort} sl@0: do_test select9-4.X { sl@0: execsql { sl@0: DROP INDEX i1; sl@0: DROP INDEX i2; sl@0: DROP VIEW v1; sl@0: } sl@0: } {} sl@0: sl@0: sl@0: finish_test