drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 1 | # 2011 September 16 |
| 2 | # |
| 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
| 5 | # |
| 6 | # May you do good and not evil. |
| 7 | # May you find forgiveness for yourself and forgive others. |
| 8 | # May you share freely, never taking more than you give. |
| 9 | # |
| 10 | #************************************************************************* |
| 11 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this script is testing correlated subqueries |
| 13 | # |
| 14 | # |
| 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
dan | 4b2f358 | 2012-12-07 19:28:26 +0000 | [diff] [blame] | 18 | set ::testprefix subquery2 |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 19 | |
| 20 | ifcapable !subquery { |
| 21 | finish_test |
| 22 | return |
| 23 | } |
| 24 | |
| 25 | do_test subquery2-1.1 { |
| 26 | execsql { |
| 27 | BEGIN; |
| 28 | CREATE TABLE t1(a,b); |
| 29 | INSERT INTO t1 VALUES(1,2); |
| 30 | INSERT INTO t1 VALUES(3,4); |
| 31 | INSERT INTO t1 VALUES(5,6); |
| 32 | INSERT INTO t1 VALUES(7,8); |
| 33 | CREATE TABLE t2(c,d); |
| 34 | INSERT INTO t2 VALUES(1,1); |
| 35 | INSERT INTO t2 VALUES(3,9); |
| 36 | INSERT INTO t2 VALUES(5,25); |
| 37 | INSERT INTO t2 VALUES(7,49); |
| 38 | CREATE TABLE t3(e,f); |
| 39 | INSERT INTO t3 VALUES(1,1); |
| 40 | INSERT INTO t3 VALUES(3,27); |
| 41 | INSERT INTO t3 VALUES(5,125); |
| 42 | INSERT INTO t3 VALUES(7,343); |
| 43 | COMMIT; |
| 44 | } |
| 45 | execsql { |
| 46 | SELECT a FROM t1 |
| 47 | WHERE b IN (SELECT x+1 FROM (SELECT DISTINCT f/(a*a) AS x FROM t3)); |
| 48 | } |
| 49 | } {1 3 5 7} |
| 50 | do_test subquery2-1.2 { |
| 51 | execsql { |
| 52 | CREATE INDEX t1b ON t1(b); |
| 53 | SELECT a FROM t1 |
| 54 | WHERE b IN (SELECT x+1 FROM (SELECT DISTINCT f/(a*a) AS x FROM t3)); |
| 55 | } |
| 56 | } {1 3 5 7} |
| 57 | |
| 58 | do_test subquery2-1.11 { |
| 59 | execsql { |
| 60 | SELECT a FROM t1 |
| 61 | WHERE +b=(SELECT x+1 FROM (SELECT DISTINCT f/(a*a) AS x FROM t3)); |
| 62 | } |
| 63 | } {1} |
| 64 | do_test subquery2-1.12 { |
| 65 | execsql { |
| 66 | SELECT a FROM t1 |
| 67 | WHERE b=(SELECT x+1 FROM (SELECT DISTINCT f/(a*a) AS x FROM t3)); |
| 68 | } |
| 69 | } {1} |
| 70 | |
| 71 | do_test subquery2-1.21 { |
| 72 | execsql { |
| 73 | SELECT a FROM t1 |
| 74 | WHERE +b=(SELECT x+1 FROM |
| 75 | (SELECT DISTINCT f/d AS x FROM t2 JOIN t3 ON d*a=f)) |
| 76 | } |
| 77 | } {1 3 5 7} |
| 78 | do_test subquery2-1.22 { |
| 79 | execsql { |
| 80 | SELECT a FROM t1 |
| 81 | WHERE b=(SELECT x+1 FROM |
| 82 | (SELECT DISTINCT f/d AS x FROM t2 JOIN t3 ON d*a=f)) |
| 83 | } |
| 84 | } {1 3 5 7} |
| 85 | |
dan | 4b2f358 | 2012-12-07 19:28:26 +0000 | [diff] [blame] | 86 | #------------------------------------------------------------------------- |
| 87 | # Test that ticket d6b36be38a has been fixed. |
| 88 | do_execsql_test 2.1 { |
| 89 | CREATE TABLE t4(a, b); |
| 90 | CREATE TABLE t5(a, b); |
| 91 | INSERT INTO t5 VALUES(3, 5); |
| 92 | |
| 93 | INSERT INTO t4 VALUES(1, 1); |
| 94 | INSERT INTO t4 VALUES(2, 3); |
| 95 | INSERT INTO t4 VALUES(3, 6); |
| 96 | INSERT INTO t4 VALUES(4, 10); |
| 97 | INSERT INTO t4 VALUES(5, 15); |
| 98 | } |
| 99 | |
| 100 | do_execsql_test 2.2 { |
| 101 | SELECT * |
| 102 | FROM (SELECT * FROM t4 ORDER BY a LIMIT -1 OFFSET 1) |
| 103 | LIMIT (SELECT a FROM t5) |
| 104 | } {2 3 3 6 4 10} |
| 105 | |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 106 | |
| 107 | finish_test |