mirror of https://github.com/sqlite/sqlite
Get the min/max optimization working with descending indices. Ticket #2514. (CVS 4161)
FossilOrigin-Name: a80a3c9d0a5e0a8a3d67bd841e2076893fd5e9aa
This commit is contained in:
parent
e927818455
commit
309be02483
13
manifest
13
manifest
|
@ -1,5 +1,5 @@
|
|||
C Additional\sdiagnostics\sadded\sto\sthe\s"out"\sfile\sgenerated\sby\slemon.\s(CVS\s4160)
|
||||
D 2007-07-18T18:16:30
|
||||
C Get\sthe\smin/max\soptimization\sworking\swith\sdescending\sindices.\s\sTicket\s#2514.\s(CVS\s4161)
|
||||
D 2007-07-18T18:17:12
|
||||
F Makefile.in 0c0e53720f658c7a551046442dd7afba0b72bfbe
|
||||
F Makefile.linux-gcc 65241babba6faf1152bf86574477baab19190499
|
||||
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
|
||||
|
@ -104,7 +104,7 @@ F src/pragma.c 7914a6b9ea05f158800116dfcae11e52ab8e39c4
|
|||
F src/prepare.c 609bb27860ce98ab39889fecc0998dfd8220891b
|
||||
F src/printf.c 9b3048d270e8bb2f8b910b491ac3aadece6cfab2
|
||||
F src/random.c 6119474a6f6917f708c1dee25b9a8e519a620e88
|
||||
F src/select.c e363327d0eba8d758ab00055de962a3bb0bc213e
|
||||
F src/select.c 3b167744fc375bddfddcef87feb18f5171737677
|
||||
F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96
|
||||
F src/shell.c e7534cce78398bc1cac4a643e931fc6221c2897e
|
||||
F src/sqlite.h.in bbcc5481af9f40ce5762c323cf555581a025f3de
|
||||
|
@ -315,6 +315,7 @@ F test/manydb.test 8de36b8d33aab5ef295b11d9e95310aeded31af8
|
|||
F test/memdb.test a67bda4ff90a38f2b19f6c7f95aa7289e051d893
|
||||
F test/memleak.test d2d2a1ff7105d32dc3fdf691458cf6cba58c7217
|
||||
F test/minmax.test 66434d8ee04869fe4c220b665b73748accbb9163
|
||||
F test/minmax2.test 8294b6728819608861ba0e06ac1d9a87c4d815b5
|
||||
F test/misc1.test 27a6ad11ba6e4b73aeee650ab68053ad7dfd0433
|
||||
F test/misc2.test d35faf7df27750d25ab656ca92dceebbd89f7368
|
||||
F test/misc3.test 7bd937e2c62bcc6be71939faf068d506467b1e03
|
||||
|
@ -517,7 +518,7 @@ F www/tclsqlite.tcl 8be95ee6dba05eabcd27a9d91331c803f2ce2130
|
|||
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
|
||||
F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
|
||||
F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5
|
||||
P 4daadf659afe6b2524e1b37347247f233a211950
|
||||
R 5d58fd77215d6d5adff31f6d536d8ece
|
||||
P 7ef2aaf72a8a953df7a763dd94657bb4ff05294f
|
||||
R ff608d553175a9b0892b61edd3a60768
|
||||
U drh
|
||||
Z cf03852661dc72cf4141c4def40720dd
|
||||
Z 2ad3f38523c32921466d9249c05ca115
|
||||
|
|
|
@ -1 +1 @@
|
|||
7ef2aaf72a8a953df7a763dd94657bb4ff05294f
|
||||
a80a3c9d0a5e0a8a3d67bd841e2076893fd5e9aa
|
12
src/select.c
12
src/select.c
|
@ -12,7 +12,7 @@
|
|||
** This file contains C code routines that are called by the parser
|
||||
** to handle SELECT statements in SQLite.
|
||||
**
|
||||
** $Id: select.c,v 1.353 2007/06/26 10:38:55 danielk1977 Exp $
|
||||
** $Id: select.c,v 1.354 2007/07/18 18:17:12 drh Exp $
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
|
||||
|
@ -2514,6 +2514,16 @@ static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
|
|||
sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0);
|
||||
seekOp = OP_MoveGt;
|
||||
}
|
||||
if( pIdx->aSortOrder[0]==SQLITE_SO_DESC ){
|
||||
/* Ticket #2514: invert the seek operator if we are using
|
||||
** a descending index. */
|
||||
if( seekOp==OP_Last ){
|
||||
seekOp = OP_Rewind;
|
||||
}else{
|
||||
assert( seekOp==OP_MoveGt );
|
||||
seekOp = OP_MoveLt;
|
||||
}
|
||||
}
|
||||
sqlite3VdbeAddOp(v, seekOp, iIdx, 0);
|
||||
sqlite3VdbeAddOp(v, OP_IdxRowid, iIdx, 0);
|
||||
sqlite3VdbeAddOp(v, OP_Close, iIdx, 0);
|
||||
|
|
|
@ -0,0 +1,387 @@
|
|||
# 2007 July 17
|
||||
#
|
||||
# The author disclaims copyright to this source code. In place of
|
||||
# a legal notice, here is a blessing:
|
||||
#
|
||||
# May you do good and not evil.
|
||||
# May you find forgiveness for yourself and forgive others.
|
||||
# May you share freely, never taking more than you give.
|
||||
#
|
||||
#***********************************************************************
|
||||
# This file implements regression tests for SQLite library. The
|
||||
# focus of this file is testing SELECT statements that contain
|
||||
# aggregate min() and max() functions and which are handled as
|
||||
# as a special case. This file makes sure that the min/max
|
||||
# optimization works right in the presence of descending
|
||||
# indices. Ticket #2514.
|
||||
#
|
||||
# $Id: minmax2.test,v 1.1 2007/07/18 18:17:12 drh Exp $
|
||||
|
||||
set testdir [file dirname $argv0]
|
||||
source $testdir/tester.tcl
|
||||
|
||||
do_test minmax2-1.0 {
|
||||
execsql {
|
||||
PRAGMA legacy_file_format=0;
|
||||
BEGIN;
|
||||
CREATE TABLE t1(x, y);
|
||||
INSERT INTO t1 VALUES(1,1);
|
||||
INSERT INTO t1 VALUES(2,2);
|
||||
INSERT INTO t1 VALUES(3,2);
|
||||
INSERT INTO t1 VALUES(4,3);
|
||||
INSERT INTO t1 VALUES(5,3);
|
||||
INSERT INTO t1 VALUES(6,3);
|
||||
INSERT INTO t1 VALUES(7,3);
|
||||
INSERT INTO t1 VALUES(8,4);
|
||||
INSERT INTO t1 VALUES(9,4);
|
||||
INSERT INTO t1 VALUES(10,4);
|
||||
INSERT INTO t1 VALUES(11,4);
|
||||
INSERT INTO t1 VALUES(12,4);
|
||||
INSERT INTO t1 VALUES(13,4);
|
||||
INSERT INTO t1 VALUES(14,4);
|
||||
INSERT INTO t1 VALUES(15,4);
|
||||
INSERT INTO t1 VALUES(16,5);
|
||||
INSERT INTO t1 VALUES(17,5);
|
||||
INSERT INTO t1 VALUES(18,5);
|
||||
INSERT INTO t1 VALUES(19,5);
|
||||
INSERT INTO t1 VALUES(20,5);
|
||||
COMMIT;
|
||||
SELECT DISTINCT y FROM t1 ORDER BY y;
|
||||
}
|
||||
} {1 2 3 4 5}
|
||||
|
||||
do_test minmax2-1.1 {
|
||||
set sqlite_search_count 0
|
||||
execsql {SELECT min(x) FROM t1}
|
||||
} {1}
|
||||
do_test minmax2-1.2 {
|
||||
set sqlite_search_count
|
||||
} {19}
|
||||
do_test minmax2-1.3 {
|
||||
set sqlite_search_count 0
|
||||
execsql {SELECT max(x) FROM t1}
|
||||
} {20}
|
||||
do_test minmax2-1.4 {
|
||||
set sqlite_search_count
|
||||
} {19}
|
||||
do_test minmax2-1.5 {
|
||||
execsql {CREATE INDEX t1i1 ON t1(x DESC)}
|
||||
set sqlite_search_count 0
|
||||
execsql {SELECT min(x) FROM t1}
|
||||
} {1}
|
||||
do_test minmax2-1.6 {
|
||||
set sqlite_search_count
|
||||
} {2}
|
||||
do_test minmax2-1.7 {
|
||||
set sqlite_search_count 0
|
||||
execsql {SELECT max(x) FROM t1}
|
||||
} {20}
|
||||
do_test minmax2-1.8 {
|
||||
set sqlite_search_count
|
||||
} {1}
|
||||
do_test minmax2-1.9 {
|
||||
set sqlite_search_count 0
|
||||
execsql {SELECT max(y) FROM t1}
|
||||
} {5}
|
||||
do_test minmax2-1.10 {
|
||||
set sqlite_search_count
|
||||
} {19}
|
||||
|
||||
do_test minmax2-2.0 {
|
||||
execsql {
|
||||
CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
|
||||
INSERT INTO t2 SELECT * FROM t1;
|
||||
}
|
||||
set sqlite_search_count 0
|
||||
execsql {SELECT min(a) FROM t2}
|
||||
} {1}
|
||||
do_test minmax2-2.1 {
|
||||
set sqlite_search_count
|
||||
} {0}
|
||||
do_test minmax2-2.2 {
|
||||
set sqlite_search_count 0
|
||||
execsql {SELECT max(a) FROM t2}
|
||||
} {20}
|
||||
do_test minmax2-2.3 {
|
||||
set sqlite_search_count
|
||||
} {0}
|
||||
|
||||
do_test minmax2-3.0 {
|
||||
ifcapable subquery {
|
||||
execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
|
||||
} else {
|
||||
db function max_a_t2 {execsql {SELECT max(a) FROM t2}}
|
||||
execsql {INSERT INTO t2 VALUES(max_a_t2()+1,999)}
|
||||
}
|
||||
set sqlite_search_count 0
|
||||
execsql {SELECT max(a) FROM t2}
|
||||
} {21}
|
||||
do_test minmax2-3.1 {
|
||||
set sqlite_search_count
|
||||
} {0}
|
||||
do_test minmax2-3.2 {
|
||||
ifcapable subquery {
|
||||
execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
|
||||
} else {
|
||||
db function max_a_t2 {execsql {SELECT max(a) FROM t2}}
|
||||
execsql {INSERT INTO t2 VALUES(max_a_t2()+1,999)}
|
||||
}
|
||||
set sqlite_search_count 0
|
||||
ifcapable subquery {
|
||||
execsql { SELECT b FROM t2 WHERE a=(SELECT max(a) FROM t2) }
|
||||
} else {
|
||||
execsql { SELECT b FROM t2 WHERE a=max_a_t2() }
|
||||
}
|
||||
} {999}
|
||||
do_test minmax2-3.3 {
|
||||
set sqlite_search_count
|
||||
} {0}
|
||||
|
||||
ifcapable {compound && subquery} {
|
||||
do_test minmax2-4.1 {
|
||||
execsql {
|
||||
SELECT coalesce(min(x+0),-1), coalesce(max(x+0),-1) FROM
|
||||
(SELECT * FROM t1 UNION SELECT NULL as 'x', NULL as 'y')
|
||||
}
|
||||
} {1 20}
|
||||
do_test minmax2-4.2 {
|
||||
execsql {
|
||||
SELECT y, coalesce(sum(x),0) FROM
|
||||
(SELECT null AS x, y+1 AS y FROM t1 UNION SELECT * FROM t1)
|
||||
GROUP BY y ORDER BY y;
|
||||
}
|
||||
} {1 1 2 5 3 22 4 92 5 90 6 0}
|
||||
do_test minmax2-4.3 {
|
||||
execsql {
|
||||
SELECT y, count(x), count(*) FROM
|
||||
(SELECT null AS x, y+1 AS y FROM t1 UNION SELECT * FROM t1)
|
||||
GROUP BY y ORDER BY y;
|
||||
}
|
||||
} {1 1 1 2 2 3 3 4 5 4 8 9 5 5 6 6 0 1}
|
||||
} ;# ifcapable compound
|
||||
|
||||
# Make sure the min(x) and max(x) optimizations work on empty tables
|
||||
# including empty tables with indices. Ticket #296.
|
||||
#
|
||||
do_test minmax2-5.1 {
|
||||
execsql {
|
||||
CREATE TABLE t3(x INTEGER UNIQUE NOT NULL);
|
||||
SELECT coalesce(min(x),999) FROM t3;
|
||||
}
|
||||
} {999}
|
||||
do_test minmax2-5.2 {
|
||||
execsql {
|
||||
SELECT coalesce(min(rowid),999) FROM t3;
|
||||
}
|
||||
} {999}
|
||||
do_test minmax2-5.3 {
|
||||
execsql {
|
||||
SELECT coalesce(max(x),999) FROM t3;
|
||||
}
|
||||
} {999}
|
||||
do_test minmax2-5.4 {
|
||||
execsql {
|
||||
SELECT coalesce(max(rowid),999) FROM t3;
|
||||
}
|
||||
} {999}
|
||||
do_test minmax2-5.5 {
|
||||
execsql {
|
||||
SELECT coalesce(max(rowid),999) FROM t3 WHERE rowid<25;
|
||||
}
|
||||
} {999}
|
||||
|
||||
# Make sure the min(x) and max(x) optimizations work when there
|
||||
# is a LIMIT clause. Ticket #396.
|
||||
#
|
||||
do_test minmax2-6.1 {
|
||||
execsql {
|
||||
SELECT min(a) FROM t2 LIMIT 1
|
||||
}
|
||||
} {1}
|
||||
do_test minmax2-6.2 {
|
||||
execsql {
|
||||
SELECT max(a) FROM t2 LIMIT 3
|
||||
}
|
||||
} {22}
|
||||
do_test minmax2-6.3 {
|
||||
execsql {
|
||||
SELECT min(a) FROM t2 LIMIT 0,100
|
||||
}
|
||||
} {1}
|
||||
do_test minmax2-6.4 {
|
||||
execsql {
|
||||
SELECT max(a) FROM t2 LIMIT 1,100
|
||||
}
|
||||
} {}
|
||||
do_test minmax2-6.5 {
|
||||
execsql {
|
||||
SELECT min(x) FROM t3 LIMIT 1
|
||||
}
|
||||
} {{}}
|
||||
do_test minmax2-6.6 {
|
||||
execsql {
|
||||
SELECT max(x) FROM t3 LIMIT 0
|
||||
}
|
||||
} {}
|
||||
do_test minmax2-6.7 {
|
||||
execsql {
|
||||
SELECT max(a) FROM t2 LIMIT 0
|
||||
}
|
||||
} {}
|
||||
|
||||
# Make sure the max(x) and min(x) optimizations work for nested
|
||||
# queries. Ticket #587.
|
||||
#
|
||||
do_test minmax2-7.1 {
|
||||
execsql {
|
||||
SELECT max(x) FROM t1;
|
||||
}
|
||||
} 20
|
||||
ifcapable subquery {
|
||||
do_test minmax2-7.2 {
|
||||
execsql {
|
||||
SELECT * FROM (SELECT max(x) FROM t1);
|
||||
}
|
||||
} 20
|
||||
}
|
||||
do_test minmax2-7.3 {
|
||||
execsql {
|
||||
SELECT min(x) FROM t1;
|
||||
}
|
||||
} 1
|
||||
ifcapable subquery {
|
||||
do_test minmax2-7.4 {
|
||||
execsql {
|
||||
SELECT * FROM (SELECT min(x) FROM t1);
|
||||
}
|
||||
} 1
|
||||
}
|
||||
|
||||
# Make sure min(x) and max(x) work correctly when the datatype is
|
||||
# TEXT instead of NUMERIC. Ticket #623.
|
||||
#
|
||||
do_test minmax2-8.1 {
|
||||
execsql {
|
||||
CREATE TABLE t4(a TEXT);
|
||||
INSERT INTO t4 VALUES('1234');
|
||||
INSERT INTO t4 VALUES('234');
|
||||
INSERT INTO t4 VALUES('34');
|
||||
SELECT min(a), max(a) FROM t4;
|
||||
}
|
||||
} {1234 34}
|
||||
do_test minmax2-8.2 {
|
||||
execsql {
|
||||
CREATE TABLE t5(a INTEGER);
|
||||
INSERT INTO t5 VALUES('1234');
|
||||
INSERT INTO t5 VALUES('234');
|
||||
INSERT INTO t5 VALUES('34');
|
||||
SELECT min(a), max(a) FROM t5;
|
||||
}
|
||||
} {34 1234}
|
||||
|
||||
# Ticket #658: Test the min()/max() optimization when the FROM clause
|
||||
# is a subquery.
|
||||
#
|
||||
ifcapable {compound && subquery} {
|
||||
do_test minmax2-9.1 {
|
||||
execsql {
|
||||
SELECT max(rowid) FROM (
|
||||
SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5
|
||||
)
|
||||
}
|
||||
} {1}
|
||||
do_test minmax2-9.2 {
|
||||
execsql {
|
||||
SELECT max(rowid) FROM (
|
||||
SELECT max(rowid) FROM t4 EXCEPT SELECT max(rowid) FROM t5
|
||||
)
|
||||
}
|
||||
} {{}}
|
||||
} ;# ifcapable compound&&subquery
|
||||
|
||||
# If there is a NULL in an aggregate max() or min(), ignore it. An
|
||||
# aggregate min() or max() will only return NULL if all values are NULL.
|
||||
#
|
||||
do_test minmax2-10.1 {
|
||||
execsql {
|
||||
CREATE TABLE t6(x);
|
||||
INSERT INTO t6 VALUES(1);
|
||||
INSERT INTO t6 VALUES(2);
|
||||
INSERT INTO t6 VALUES(NULL);
|
||||
SELECT coalesce(min(x),-1) FROM t6;
|
||||
}
|
||||
} {1}
|
||||
do_test minmax2-10.2 {
|
||||
execsql {
|
||||
SELECT max(x) FROM t6;
|
||||
}
|
||||
} {2}
|
||||
do_test minmax2-10.3 {
|
||||
execsql {
|
||||
CREATE INDEX i6 ON t6(x DESC);
|
||||
SELECT coalesce(min(x),-1) FROM t6;
|
||||
}
|
||||
} {1}
|
||||
do_test minmax2-10.4 {
|
||||
execsql {
|
||||
SELECT max(x) FROM t6;
|
||||
}
|
||||
} {2}
|
||||
do_test minmax2-10.5 {
|
||||
execsql {
|
||||
DELETE FROM t6 WHERE x NOT NULL;
|
||||
SELECT count(*) FROM t6;
|
||||
}
|
||||
} 1
|
||||
do_test minmax2-10.6 {
|
||||
execsql {
|
||||
SELECT count(x) FROM t6;
|
||||
}
|
||||
} 0
|
||||
ifcapable subquery {
|
||||
do_test minmax2-10.7 {
|
||||
execsql {
|
||||
SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
|
||||
}
|
||||
} {{} {}}
|
||||
}
|
||||
do_test minmax2-10.8 {
|
||||
execsql {
|
||||
SELECT min(x), max(x) FROM t6;
|
||||
}
|
||||
} {{} {}}
|
||||
do_test minmax2-10.9 {
|
||||
execsql {
|
||||
INSERT INTO t6 SELECT * FROM t6;
|
||||
INSERT INTO t6 SELECT * FROM t6;
|
||||
INSERT INTO t6 SELECT * FROM t6;
|
||||
INSERT INTO t6 SELECT * FROM t6;
|
||||
INSERT INTO t6 SELECT * FROM t6;
|
||||
INSERT INTO t6 SELECT * FROM t6;
|
||||
INSERT INTO t6 SELECT * FROM t6;
|
||||
INSERT INTO t6 SELECT * FROM t6;
|
||||
INSERT INTO t6 SELECT * FROM t6;
|
||||
INSERT INTO t6 SELECT * FROM t6;
|
||||
SELECT count(*) FROM t6;
|
||||
}
|
||||
} 1024
|
||||
do_test minmax2-10.10 {
|
||||
execsql {
|
||||
SELECT count(x) FROM t6;
|
||||
}
|
||||
} 0
|
||||
ifcapable subquery {
|
||||
do_test minmax2-10.11 {
|
||||
execsql {
|
||||
SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
|
||||
}
|
||||
} {{} {}}
|
||||
}
|
||||
do_test minmax2-10.12 {
|
||||
execsql {
|
||||
SELECT min(x), max(x) FROM t6;
|
||||
}
|
||||
} {{} {}}
|
||||
|
||||
|
||||
finish_test
|
Loading…
Reference in New Issue