Fixes to the logic that decides if the ORDER BY can be ignored due to the
use of an index. Tests updated. (CVS 796) FossilOrigin-Name: bfb9a2aa939ecffc5dc2c7c23bddd57d357bdf13
This commit is contained in:
parent
c045ec51be
commit
dd4852c3d1
16
manifest
16
manifest
@ -1,5 +1,5 @@
|
||||
C Scan\sthe\stable\sbackwards\sif\sthere\sis\san\sORDER\sBY\s...\sDESC\sclause\sthat\scan\nbe\ssatisfied\sby\san\sindex.\s(CVS\s795)
|
||||
D 2002-12-04T20:01:06
|
||||
C Fixes\sto\sthe\slogic\sthat\sdecides\sif\sthe\sORDER\sBY\scan\sbe\signored\sdue\sto\sthe\nuse\sof\san\sindex.\s\sTests\supdated.\s(CVS\s796)
|
||||
D 2002-12-04T21:50:16
|
||||
F Makefile.in 868c17a1ae1c07603d491274cc8f86c04acf2a1e
|
||||
F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906
|
||||
F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd
|
||||
@ -54,7 +54,7 @@ F src/update.c 881e4c8e7c786545da4fd2d95da19252b2e31137
|
||||
F src/util.c ca7650ef2cc2d50241e48029fca109a3016144ee
|
||||
F src/vdbe.c 84b224d0ecfb2555a976f074924dd3e6d3d91dfb
|
||||
F src/vdbe.h b7584044223104ba7896a7f87b66daebdd6022ba
|
||||
F src/where.c d48077fb86c1d06390f974d348ace081c824dd32
|
||||
F src/where.c af235636b7bc7f7f42ee1c7162d1958ad0102cab
|
||||
F test/all.test 873d30e25a41b3aa48fec5633a7ec1816e107029
|
||||
F test/bigfile.test 38d1071817caceb636c613e3546082b90e749a49
|
||||
F test/bigrow.test 8ab252dba108f12ad64e337b0f2ff31a807ac578
|
||||
@ -67,7 +67,7 @@ F test/copy.test 55d60a4d5ed342a0fa08b7cd07d46d43ea0d0d7f
|
||||
F test/delete.test 5821a95a66061ae09723a88938f23d10d8a881ad
|
||||
F test/expr.test dea1cd62684a8bf116426447c948f5e8fb2c84b6
|
||||
F test/fkey1.test d65c824459916249bee501532d6154ddab0b5db7
|
||||
F test/format3.test cbb168d446152fcf1dd85be299ad2d6cd507da4e
|
||||
F test/format3.test 64ab6c4db132b28a645996d413530f7b2a462cc2
|
||||
F test/func.test 000515779001ac6899eec4b54e65c6e2501279d4
|
||||
F test/in.test 15428c85d141edda7543bfc3f9a32ce65193717b
|
||||
F test/index.test 2a5a1b654f50ca0768fb10ae44b72e6a776b1f18
|
||||
@ -115,7 +115,7 @@ F test/update.test 7ffb062d580a972e7870d0f51d5af3ab9bfeae08
|
||||
F test/vacuum.test 059871b312eb910bbe49dafde1d01490cc2c6bbe
|
||||
F test/version.test 605fd0d7e7d571370c32b12dbf395b58953de246
|
||||
F test/view.test c64fa39ea57f3c2066c854290f032ad13b23b83d
|
||||
F test/where.test 9c1c2c13cfff419ea38451e78cf3d0d238fabc27
|
||||
F test/where.test 9e6e7e0a8bc6eff879bf12a497d6529df7fa9852
|
||||
F tool/diffdb.c 7524b1b5df217c20cd0431f6789851a4e0cb191b
|
||||
F tool/lemon.c 022adc2830c2705828f744d2c59798bd462eb465
|
||||
F tool/lempar.c 73a991cc3017fb34804250fa901488b5147b3717
|
||||
@ -152,7 +152,7 @@ F www/speed.tcl a20a792738475b68756ea7a19321600f23d1d803
|
||||
F www/sqlite.tcl ae3dcfb077e53833b59d4fcc94d8a12c50a44098
|
||||
F www/tclsqlite.tcl 1db15abeb446aad0caf0b95b8b9579720e4ea331
|
||||
F www/vdbe.tcl 2013852c27a02a091d39a766bc87cff329f21218
|
||||
P 0ad1d93879bee0d34b122591c025192a51b8490f
|
||||
R fb6076c646238a5812fd1b3f4dc5ed25
|
||||
P c7a3487981de0ed5b43ea3ff4d46ab4437068dca
|
||||
R ad809775145f9a28c47c1aa04b1e5814
|
||||
U drh
|
||||
Z 76431d0a75d4131ab7844f1098b48161
|
||||
Z 2d97197b4281b66cabb81cf1fc140ed8
|
||||
|
@ -1 +1 @@
|
||||
c7a3487981de0ed5b43ea3ff4d46ab4437068dca
|
||||
bfb9a2aa939ecffc5dc2c7c23bddd57d357bdf13
|
31
src/where.c
31
src/where.c
@ -13,7 +13,7 @@
|
||||
** the WHERE clause of SQL statements. Also found here are subroutines
|
||||
** to generate VDBE code to evaluate expressions.
|
||||
**
|
||||
** $Id: where.c,v 1.68 2002/12/04 20:01:06 drh Exp $
|
||||
** $Id: where.c,v 1.69 2002/12/04 21:50:16 drh Exp $
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
|
||||
@ -164,15 +164,25 @@ static void exprAnalyze(int base, ExprInfo *pInfo){
|
||||
**
|
||||
** If there are two or more indices that generate the correct sort order
|
||||
** and pPreferredIdx is one of those indices, then return pPreferredIdx.
|
||||
**
|
||||
** nEqCol is the number of columns of pPreferredIdx that are used as
|
||||
** equality constraints. Any index returned must have exactly this same
|
||||
** set of columns. The ORDER BY clause only matches index columns beyond the
|
||||
** the first nEqCol columns.
|
||||
**
|
||||
** All terms of the ORDER BY clause must be either ASC or DESC. The
|
||||
** *pbRev value is set to 1 if the ORDER BY clause is all DESC and it is
|
||||
** set to 0 if the ORDER BY clause is all ASC.
|
||||
*/
|
||||
static Index *findSortingIndex(
|
||||
Table *pTab, /* The table to be sorted */
|
||||
int base, /* Cursor number for pTab */
|
||||
ExprList *pOrderBy, /* The ORDER BY clause */
|
||||
Index *pPreferredIdx, /* Use this index, if possible and not NULL */
|
||||
int nEqCol, /* Number of index columns used with == constraints */
|
||||
int *pbRev /* Set to 1 if ORDER BY is DESC */
|
||||
){
|
||||
int i;
|
||||
int i, j;
|
||||
Index *pMatch;
|
||||
Index *pIdx;
|
||||
int sortOrder;
|
||||
@ -205,11 +215,17 @@ static Index *findSortingIndex(
|
||||
*/
|
||||
pMatch = 0;
|
||||
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
|
||||
if( pIdx->nColumn<pOrderBy->nExpr ) continue;
|
||||
for(i=0; i<pOrderBy->nExpr; i++){
|
||||
if( pOrderBy->a[i].pExpr->iColumn!=pIdx->aiColumn[i] ) break;
|
||||
int nExpr = pOrderBy->nExpr;
|
||||
if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue;
|
||||
for(i=j=0; i<nEqCol; i++){
|
||||
if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break;
|
||||
if( j<nExpr && pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] ){ j++; }
|
||||
}
|
||||
if( i>=pOrderBy->nExpr ){
|
||||
if( i<nEqCol ) continue;
|
||||
for(i=0; i+j<nExpr; i++){
|
||||
if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ) break;
|
||||
}
|
||||
if( i+j>=nExpr ){
|
||||
pMatch = pIdx;
|
||||
if( pIdx==pPreferredIdx ) break;
|
||||
}
|
||||
@ -621,7 +637,8 @@ WhereInfo *sqliteWhereBegin(
|
||||
*/
|
||||
pSortIdx = 0;
|
||||
}else{
|
||||
pSortIdx = findSortingIndex(pTab, base, *ppOrderBy, pIdx, &bRev);
|
||||
int nEqCol = (pWInfo->a[0].score+4)/8;
|
||||
pSortIdx = findSortingIndex(pTab, base, *ppOrderBy, pIdx, nEqCol, &bRev);
|
||||
}
|
||||
if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
|
||||
if( pIdx==0 ){
|
||||
|
@ -12,7 +12,7 @@
|
||||
# focus of this file is testing the the library is able to correctly
|
||||
# handle file-format 3 (version 2.6.x) databases.
|
||||
#
|
||||
# $Id: format3.test,v 1.1 2002/08/31 16:52:45 drh Exp $
|
||||
# $Id: format3.test,v 1.2 2002/12/04 21:50:16 drh Exp $
|
||||
|
||||
set testdir [file dirname $argv0]
|
||||
source $testdir/tester.tcl
|
||||
@ -638,6 +638,11 @@ do_test format3-10.13 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
|
||||
}
|
||||
} {100 1 10201 99 2 10000 98 3 9801 nosort}
|
||||
do_test format3-10.13.1 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a>0 ORDER BY a+1 DESC LIMIT 3
|
||||
}
|
||||
} {100 1 10201 99 2 10000 98 3 9801 sort}
|
||||
do_test format3-10.14 {
|
||||
cksort {
|
||||
|
122
test/where.test
122
test/where.test
@ -11,7 +11,7 @@
|
||||
# This file implements regression tests for SQLite library. The
|
||||
# focus of this file is testing the use of indices in WHERE clases.
|
||||
#
|
||||
# $Id: where.test,v 1.11 2002/10/22 23:38:04 drh Exp $
|
||||
# $Id: where.test,v 1.12 2002/12/04 21:50:16 drh Exp $
|
||||
|
||||
set testdir [file dirname $argv0]
|
||||
source $testdir/tester.tcl
|
||||
@ -415,11 +415,51 @@ do_test where-6.8 {
|
||||
SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
|
||||
}
|
||||
} {1 100 4 2 99 9 3 98 16 sort}
|
||||
do_test where-6.9 {
|
||||
do_test where-6.9.1 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
|
||||
}
|
||||
} {1 100 4 nosort}
|
||||
do_test where-6.9.2 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
|
||||
}
|
||||
} {1 100 4 nosort}
|
||||
do_test where-6.9.3 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c LIMIT 3
|
||||
}
|
||||
} {1 100 4 nosort}
|
||||
do_test where-6.9.4 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC LIMIT 3
|
||||
}
|
||||
} {1 100 4 nosort}
|
||||
do_test where-6.9.5 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c DESC LIMIT 3
|
||||
}
|
||||
} {1 100 4 nosort}
|
||||
do_test where-6.9.6 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c DESC LIMIT 3
|
||||
}
|
||||
} {1 100 4 nosort}
|
||||
do_test where-6.9.7 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c,a LIMIT 3
|
||||
}
|
||||
} {1 100 4 sort}
|
||||
do_test where-6.9.8 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c ASC LIMIT 3
|
||||
}
|
||||
} {1 100 4 sort}
|
||||
do_test where-6.9.9 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a ASC, c DESC LIMIT 3
|
||||
}
|
||||
} {1 100 4 sort}
|
||||
do_test where-6.10 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
|
||||
@ -439,6 +479,11 @@ do_test where-6.13 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
|
||||
}
|
||||
} {100 1 10201 99 2 10000 98 3 9801 nosort}
|
||||
do_test where-6.13.1 {
|
||||
cksort {
|
||||
SELECT * FROM t3 WHERE a>0 ORDER BY -a LIMIT 3
|
||||
}
|
||||
} {100 1 10201 99 2 10000 98 3 9801 sort}
|
||||
do_test where-6.14 {
|
||||
cksort {
|
||||
@ -471,6 +516,77 @@ do_test where-6.19 {
|
||||
}
|
||||
} {4 9 16 nosort}
|
||||
|
||||
|
||||
# Tests for reverse-order sorting.
|
||||
#
|
||||
do_test where-7.1 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 ORDER BY y;
|
||||
}
|
||||
} {8 9 10 11 12 13 14 15 nosort}
|
||||
do_test where-7.2 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 ORDER BY y DESC;
|
||||
}
|
||||
} {15 14 13 12 11 10 9 8 nosort}
|
||||
do_test where-7.3 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y LIMIT 3;
|
||||
}
|
||||
} {10 11 12 nosort}
|
||||
do_test where-7.4 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y DESC LIMIT 3;
|
||||
}
|
||||
} {15 14 13 nosort}
|
||||
do_test where-7.5 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 AND y>121 ORDER BY y DESC;
|
||||
}
|
||||
} {15 14 13 12 11 nosort}
|
||||
do_test where-7.6 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 AND y>=121 ORDER BY y DESC;
|
||||
}
|
||||
} {15 14 13 12 11 10 nosort}
|
||||
do_test where-7.7 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y DESC;
|
||||
}
|
||||
} {12 11 10 nosort}
|
||||
do_test where-7.8 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y DESC;
|
||||
}
|
||||
} {13 12 11 10 nosort}
|
||||
do_test where-7.9 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y DESC;
|
||||
}
|
||||
} {13 12 11 nosort}
|
||||
do_test where-7.10 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y DESC;
|
||||
}
|
||||
} {12 11 10 nosort}
|
||||
do_test where-7.11 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y;
|
||||
}
|
||||
} {10 11 12 nosort}
|
||||
do_test where-7.12 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y;
|
||||
}
|
||||
} {10 11 12 13 nosort}
|
||||
do_test where-7.13 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y;
|
||||
}
|
||||
} {11 12 13 nosort}
|
||||
do_test where-7.14 {
|
||||
cksort {
|
||||
SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y;
|
||||
}
|
||||
} {10 11 12 nosort}
|
||||
|
||||
finish_test
|
||||
|
Loading…
x
Reference in New Issue
Block a user