drh | 73fa473 | 2016-05-20 00:21:42 +0000 | [diff] [blame] | 1 | # 2016-05-20 |
| 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 file is testing the LIMIT in combination with ORDER BY |
| 13 | # and in particular, the optimizations in the inner loop that cause an |
| 14 | # early exit of the inner loop when the LIMIT is reached and the inner |
| 15 | # loop is emitting rows in ORDER BY order. |
| 16 | |
| 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
dan | 83465a6 | 2016-10-12 14:48:00 +0000 | [diff] [blame] | 20 | set testprefix limit2 |
drh | 73fa473 | 2016-05-20 00:21:42 +0000 | [diff] [blame] | 21 | |
| 22 | do_execsql_test limit2-100 { |
| 23 | CREATE TABLE t1(a,b); |
| 24 | WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) |
| 25 | INSERT INTO t1(a,b) SELECT 1, (x*17)%1000 + 1000 FROM c; |
| 26 | INSERT INTO t1(a,b) VALUES(2,2),(3,1006),(4,4),(5,9999); |
| 27 | CREATE INDEX t1ab ON t1(a,b); |
| 28 | } |
| 29 | set sqlite_search_count 0 |
| 30 | do_execsql_test limit2-100.1 { |
| 31 | SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY b LIMIT 5; |
| 32 | } {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |} |
| 33 | set fast_count $sqlite_search_count |
| 34 | set sqlite_search_count 0 |
| 35 | do_execsql_test limit2-100.2 { |
| 36 | SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY +b LIMIT 5; |
| 37 | } {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |} |
| 38 | do_test limit2-100.3 { |
| 39 | set slow_count $sqlite_search_count |
| 40 | expr {$fast_count < 0.02*$slow_count} |
| 41 | } {1} |
| 42 | |
| 43 | do_execsql_test limit2-110 { |
| 44 | CREATE TABLE t2(x,y); |
| 45 | INSERT INTO t2(x,y) VALUES('a',1),('a',2),('a',3),('a',4); |
| 46 | INSERT INTO t2(x,y) VALUES('b',1),('c',2),('d',3),('e',4); |
| 47 | CREATE INDEX t2xy ON t2(x,y); |
| 48 | } |
| 49 | set sqlite_search_count 0 |
| 50 | do_execsql_test limit2-110.1 { |
| 51 | SELECT a, b, '|' FROM t2, t1 WHERE t2.x='a' AND t1.a=t2.y ORDER BY t1.b LIMIT 5; |
| 52 | } {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |} |
| 53 | set fast_count $sqlite_search_count |
| 54 | set sqlite_search_count 0 |
| 55 | do_execsql_test limit2-110.2 { |
| 56 | SELECT a, b, '|' FROM t2, t1 WHERE t2.x='a' AND t1.a=t2.y ORDER BY +t1.b LIMIT 5; |
| 57 | } {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |} |
| 58 | set slow_count $sqlite_search_count |
| 59 | do_test limit2-110.3 { |
| 60 | expr {$fast_count < 0.02*$slow_count} |
| 61 | } {1} |
| 62 | |
| 63 | do_execsql_test limit2-120 { |
| 64 | DROP INDEX t1ab; |
| 65 | CREATE INDEX t1ab ON t1(a,b DESC); |
| 66 | } |
| 67 | set sqlite_search_count 0 |
| 68 | do_execsql_test limit2-120.1 { |
| 69 | SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY b DESC LIMIT 5; |
| 70 | } {5 9999 | 1 1999 | 1 1998 | 1 1997 | 1 1996 |} |
| 71 | set fast_count $sqlite_search_count |
| 72 | set sqlite_search_count 0 |
| 73 | do_execsql_test limit2-120.2 { |
| 74 | SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY +b DESC LIMIT 5; |
| 75 | } {5 9999 | 1 1999 | 1 1998 | 1 1997 | 1 1996 |} |
| 76 | do_test limit2-120.3 { |
| 77 | set slow_count $sqlite_search_count |
| 78 | expr {$fast_count < 0.02*$slow_count} |
| 79 | } {1} |
| 80 | |
drh | dd545d3 | 2016-08-04 09:09:44 +0000 | [diff] [blame] | 81 | # Bug report against the new ORDER BY LIMIT optimization just prior to |
| 82 | # release. (Unreleased so there is no ticket). |
| 83 | # |
| 84 | # Make sure the optimization is not applied if the inner loop can only |
| 85 | # provide a single row of output. |
| 86 | # |
| 87 | do_execsql_test limit2-200 { |
| 88 | CREATE TABLE t200(a, b); |
| 89 | WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) |
| 90 | INSERT INTO t200(a,b) SELECT x, x FROM c; |
| 91 | CREATE TABLE t201(x INTEGER PRIMARY KEY, y); |
| 92 | INSERT INTO t201(x,y) VALUES(2,12345); |
| 93 | |
| 94 | SELECT *, '|' FROM t200, t201 WHERE x=b ORDER BY y LIMIT 3; |
| 95 | } {2 2 2 12345 |} |
| 96 | do_execsql_test limit2-210 { |
| 97 | SELECT *, '|' FROM t200 LEFT JOIN t201 ON x=b ORDER BY y LIMIT 3; |
| 98 | } {1 1 {} {} | 3 3 {} {} | 4 4 {} {} |} |
| 99 | |
drh | 57a8c61 | 2016-09-07 01:51:46 +0000 | [diff] [blame] | 100 | # Bug in the ORDER BY LIMIT optimization reported on 2016-09-06. |
| 101 | # Ticket https://www.sqlite.org/src/info/559733b09e96 |
| 102 | # |
| 103 | do_execsql_test limit2-300 { |
| 104 | CREATE TABLE t300(a,b,c); |
| 105 | CREATE INDEX t300x ON t300(a,b,c); |
| 106 | INSERT INTO t300 VALUES(0,1,99),(0,1,0),(0,0,0); |
| 107 | SELECT *,'.' FROM t300 WHERE a=0 AND (c=0 OR c=99) ORDER BY c DESC; |
| 108 | } {0 1 99 . 0 0 0 . 0 1 0 .} |
| 109 | do_execsql_test limit2-310 { |
| 110 | SELECT *,'.' FROM t300 WHERE a=0 AND (c=0 OR c=99) ORDER BY c DESC LIMIT 1; |
| 111 | } {0 1 99 .} |
drh | dd545d3 | 2016-08-04 09:09:44 +0000 | [diff] [blame] | 112 | |
drh | 8dd099b | 2016-09-23 18:13:01 +0000 | [diff] [blame] | 113 | # Make sure the SELECT loop is ordered correctly for the direction of |
| 114 | # the ORDER BY |
| 115 | # |
| 116 | do_execsql_test limit2-400 { |
| 117 | CREATE TABLE t400(a,b); |
| 118 | CREATE INDEX t400_ab ON t400(a,b); |
| 119 | INSERT INTO t400(a,b) VALUES(1,90),(1,40),(2,80),(2,30),(3,70),(3,20); |
| 120 | SELECT *,'x' FROM t400 WHERE a IN (1,2,3) ORDER BY b DESC LIMIT 3; |
| 121 | SELECT *,'y' FROM t400 WHERE a IN (1,2,3) ORDER BY +b DESC LIMIT 3; |
| 122 | } {1 90 x 2 80 x 3 70 x 1 90 y 2 80 y 3 70 y} |
drh | 73fa473 | 2016-05-20 00:21:42 +0000 | [diff] [blame] | 123 | |
dan | 83465a6 | 2016-10-12 14:48:00 +0000 | [diff] [blame] | 124 | |
| 125 | do_execsql_test 500 { |
| 126 | CREATE TABLE t500(i INTEGER PRIMARY KEY, j); |
| 127 | INSERT INTO t500 VALUES(1, 1); |
| 128 | INSERT INTO t500 VALUES(2, 2); |
| 129 | INSERT INTO t500 VALUES(3, 3); |
| 130 | INSERT INTO t500 VALUES(4, 0); |
| 131 | INSERT INTO t500 VALUES(5, 5); |
| 132 | SELECT j FROM t500 WHERE i IN (1,2,3,4,5) ORDER BY j DESC LIMIT 3; |
| 133 | } {5 3 2} |
| 134 | do_execsql_test 501 { |
| 135 | CREATE TABLE t501(i INTEGER PRIMARY KEY, j); |
| 136 | INSERT INTO t501 VALUES(1, 5); |
| 137 | INSERT INTO t501 VALUES(2, 4); |
| 138 | INSERT INTO t501 VALUES(3, 3); |
| 139 | INSERT INTO t501 VALUES(4, 6); |
| 140 | INSERT INTO t501 VALUES(5, 1); |
| 141 | SELECT j FROM t501 WHERE i IN (1,2,3,4,5) ORDER BY j LIMIT 3; |
| 142 | } {1 3 4} |
| 143 | do_execsql_test 502 { |
| 144 | CREATE TABLE t502(i INT PRIMARY KEY, j); |
| 145 | INSERT INTO t502 VALUES(1, 5); |
| 146 | INSERT INTO t502 VALUES(2, 4); |
| 147 | INSERT INTO t502 VALUES(3, 3); |
| 148 | INSERT INTO t502 VALUES(4, 6); |
| 149 | INSERT INTO t502 VALUES(5, 1); |
| 150 | SELECT j FROM t502 WHERE i IN (1,2,3,4,5) ORDER BY j LIMIT 3; |
| 151 | } {1 3 4} |
| 152 | |
drh | 73fa473 | 2016-05-20 00:21:42 +0000 | [diff] [blame] | 153 | finish_test |