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 18set testdir [file dirname $argv0] 19source $testdir/tester.tcl 20 21do_execsql_test limit2-100 { 22 CREATE TABLE t1(a,b); 23 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) 24 INSERT INTO t1(a,b) SELECT 1, (x*17)%1000 + 1000 FROM c; 25 INSERT INTO t1(a,b) VALUES(2,2),(3,1006),(4,4),(5,9999); 26 CREATE INDEX t1ab ON t1(a,b); 27} 28set sqlite_search_count 0 29do_execsql_test limit2-100.1 { 30 SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY b LIMIT 5; 31} {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |} 32set fast_count $sqlite_search_count 33set sqlite_search_count 0 34do_execsql_test limit2-100.2 { 35 SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY +b LIMIT 5; 36} {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |} 37do_test limit2-100.3 { 38 set slow_count $sqlite_search_count 39 expr {$fast_count < 0.02*$slow_count} 40} {1} 41 42do_execsql_test limit2-110 { 43 CREATE TABLE t2(x,y); 44 INSERT INTO t2(x,y) VALUES('a',1),('a',2),('a',3),('a',4); 45 INSERT INTO t2(x,y) VALUES('b',1),('c',2),('d',3),('e',4); 46 CREATE INDEX t2xy ON t2(x,y); 47} 48set sqlite_search_count 0 49do_execsql_test limit2-110.1 { 50 SELECT a, b, '|' FROM t2, t1 WHERE t2.x='a' AND t1.a=t2.y ORDER BY t1.b LIMIT 5; 51} {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |} 52set fast_count $sqlite_search_count 53set sqlite_search_count 0 54do_execsql_test limit2-110.2 { 55 SELECT a, b, '|' FROM t2, t1 WHERE t2.x='a' AND t1.a=t2.y ORDER BY +t1.b LIMIT 5; 56} {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |} 57set slow_count $sqlite_search_count 58do_test limit2-110.3 { 59 expr {$fast_count < 0.02*$slow_count} 60} {1} 61 62do_execsql_test limit2-120 { 63 DROP INDEX t1ab; 64 CREATE INDEX t1ab ON t1(a,b DESC); 65} 66set sqlite_search_count 0 67do_execsql_test limit2-120.1 { 68 SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY b DESC LIMIT 5; 69} {5 9999 | 1 1999 | 1 1998 | 1 1997 | 1 1996 |} 70set fast_count $sqlite_search_count 71set sqlite_search_count 0 72do_execsql_test limit2-120.2 { 73 SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY +b DESC LIMIT 5; 74} {5 9999 | 1 1999 | 1 1998 | 1 1997 | 1 1996 |} 75do_test limit2-120.3 { 76 set slow_count $sqlite_search_count 77 expr {$fast_count < 0.02*$slow_count} 78} {1} 79 80# Bug report against the new ORDER BY LIMIT optimization just prior to 81# release. (Unreleased so there is no ticket). 82# 83# Make sure the optimization is not applied if the inner loop can only 84# provide a single row of output. 85# 86do_execsql_test limit2-200 { 87 CREATE TABLE t200(a, b); 88 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) 89 INSERT INTO t200(a,b) SELECT x, x FROM c; 90 CREATE TABLE t201(x INTEGER PRIMARY KEY, y); 91 INSERT INTO t201(x,y) VALUES(2,12345); 92 93 SELECT *, '|' FROM t200, t201 WHERE x=b ORDER BY y LIMIT 3; 94} {2 2 2 12345 |} 95do_execsql_test limit2-210 { 96 SELECT *, '|' FROM t200 LEFT JOIN t201 ON x=b ORDER BY y LIMIT 3; 97} {1 1 {} {} | 3 3 {} {} | 4 4 {} {} |} 98 99# Bug in the ORDER BY LIMIT optimization reported on 2016-09-06. 100# Ticket https://www.sqlite.org/src/info/559733b09e96 101# 102do_execsql_test limit2-300 { 103 CREATE TABLE t300(a,b,c); 104 CREATE INDEX t300x ON t300(a,b,c); 105 INSERT INTO t300 VALUES(0,1,99),(0,1,0),(0,0,0); 106 SELECT *,'.' FROM t300 WHERE a=0 AND (c=0 OR c=99) ORDER BY c DESC; 107} {0 1 99 . 0 0 0 . 0 1 0 .} 108do_execsql_test limit2-310 { 109 SELECT *,'.' FROM t300 WHERE a=0 AND (c=0 OR c=99) ORDER BY c DESC LIMIT 1; 110} {0 1 99 .} 111 112# Make sure the SELECT loop is ordered correctly for the direction of 113# the ORDER BY 114# 115do_execsql_test limit2-400 { 116 CREATE TABLE t400(a,b); 117 CREATE INDEX t400_ab ON t400(a,b); 118 INSERT INTO t400(a,b) VALUES(1,90),(1,40),(2,80),(2,30),(3,70),(3,20); 119 SELECT *,'x' FROM t400 WHERE a IN (1,2,3) ORDER BY b DESC LIMIT 3; 120 SELECT *,'y' FROM t400 WHERE a IN (1,2,3) ORDER BY +b DESC LIMIT 3; 121} {1 90 x 2 80 x 3 70 x 1 90 y 2 80 y 3 70 y} 122 123finish_test 124