From: drh Date: Fri, 8 Feb 2013 18:48:23 +0000 (+0000) Subject: Loop through the elements on the RHS of an IN operator in reverse order when X-Git-Tag: version-3.7.16~47^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2d96b934c8da4141ef53e5a380467903e41c96e5;p=thirdparty%2Fsqlite.git Loop through the elements on the RHS of an IN operator in reverse order when the ORDER BY clauses specifies DESC. FossilOrigin-Name: f78395c8896666bb1359b83fbcd58d5e3dbc39d3 --- diff --git a/manifest b/manifest index fd6cb8ae6b..3637f1e541 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Allow\sthe\s"a=?1\sOR\sa=?2"\sto\s"a\sIN\s(?1,?2)"\stransformation\sto\swork\son\svirtual\ntables\sagain.\s\sThis\swas\sformerly\srestricted\sbecause\svirtual\stables\scould\snot\noptimize\sIN\sterms.\s\s(See\scheck-in\s[fad88e71cf195e].)\s\sBut\sIN\sterms\sare\snow\nused\sby\svirtual\stables\s(as\sof\scheck-in\s[3d65c70343])\sso\sthe\srestriction\scan\nnow\sbe\sremoved. -D 2013-02-08T16:04:19.844 +C Loop\sthrough\sthe\selements\son\sthe\sRHS\sof\san\sIN\soperator\sin\sreverse\sorder\swhen\nthe\sORDER\sBY\sclauses\sspecifies\sDESC. +D 2013-02-08T18:48:23.501 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in a48faa9e7dd7d556d84f5456eabe5825dd8a6282 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -179,7 +179,7 @@ F src/shell.c 266791241d7add796ccce2317977ae6c3c67d77f F src/sqlite.h.in 39cc33bb08897c748fe3383c29ccf56585704177 F src/sqlite3.rc fea433eb0a59f4c9393c8e6d76a6e2596b1fe0c0 F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477 -F src/sqliteInt.h 6267485592261a1420ae9195e388242d9e451bdb +F src/sqliteInt.h c1e7fe135ec1957d4305f0f8016b5a12d339cd53 F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d F src/status.c bedc37ec1a6bb9399944024d63f4c769971955a9 F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e @@ -252,7 +252,7 @@ F src/vtab.c b05e5f1f4902461ba9f5fc49bb7eb7c3a0741a83 F src/wal.c f5c7b5027d0ed0e9bc9afeb4a3a8dfea762ec7d2 F src/wal.h 29c197540b19044e6cd73487017e5e47a1d3dac6 F src/walker.c 3d75ba73de15e0f8cd0737643badbeb0e002f07b -F src/where.c e09d21a4506a19c0a8b17e9614d362043c9ffeb1 +F src/where.c 36370580f2e3f805df161db44288bfb7d9ac007a F test/8_3_names.test 631ea964a3edb091cf73c3b540f6bcfdb36ce823 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 F test/aggnested.test 45c0201e28045ad38a530b5a144b73cd4aa2cfd6 @@ -970,8 +970,8 @@ F test/walro.test a31deb621033442a76c3a61e44929250d06f81b1 F test/walshared.test 6dda2293880c300baf5d791c307f653094585761 F test/walslow.test e7be6d9888f83aa5d3d3c7c08aa9b5c28b93609a F test/walthread.test de8dbaf6d9e41481c460ba31ca61e163d7348f8e -F test/where.test 55f6780f986b38b870e0fc50c3d842c13e7946a0 -F test/where2.test 61acb16d8e67e0d8717b2ad1a132f3b1ad81fd62 +F test/where.test 15ac8611c9439a2c5f4a6ac10cfe4c1ec9854c24 +F test/where2.test 399b3178289925a0aa976b3d60ef139740540ecd F test/where3.test 667e75642102c97a00bf9b23d3cb267db321d006 F test/where4.test e9b9e2f2f98f00379e6031db6a6fca29bae782a2 F test/where5.test fdf66f96d29a064b63eb543e28da4dfdccd81ad2 @@ -1034,7 +1034,7 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381 F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac -P 98bf668ab1a8683b46ee8c94cb60f8215aabc517 -R 77749b931237e0163d95186ceeef39fd +P a917c1f09254b54e03e31b119cc49c551269d82e +R f8ff7e2997f1e9085eb15a7df82507fc U drh -Z afe286dca485d6e9ab8ba5a0ac8b0dbf +Z d65a74f3ef1f6a5ee91ecd28380ca50d diff --git a/manifest.uuid b/manifest.uuid index 71e8e48900..8b749ce569 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -a917c1f09254b54e03e31b119cc49c551269d82e \ No newline at end of file +f78395c8896666bb1359b83fbcd58d5e3dbc39d3 \ No newline at end of file diff --git a/src/sqliteInt.h b/src/sqliteInt.h index f036970931..c58b0afb6e 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -1965,6 +1965,7 @@ struct WhereLevel { struct InLoop { int iCur; /* The VDBE cursor used by this IN operator */ int addrInTop; /* Top of the IN loop */ + u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ } *aInLoop; /* Information about each nested IN operator */ } in; /* Used when plan.wsFlags&WHERE_IN_ABLE */ Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ diff --git a/src/where.c b/src/where.c index 14727270de..a3c5e24d39 100644 --- a/src/where.c +++ b/src/where.c @@ -3008,15 +3008,7 @@ static int isSortingIndex( if( pConstraint==0 ){ isEq = 0; }else if( (pConstraint->eOperator & WO_IN)!=0 ){ -#if 0 - /* Constraints of the form: "X IN ..." cannot be used with an ORDER BY - ** because we do not know in what order the values on the RHS of the IN - ** operator will occur. */ - break; -#else - if( termSortOrder ) break; isEq = 0; -#endif }else if( (pConstraint->eOperator & WO_ISNULL)!=0 ){ uniqueNotNull = 0; isEq = 1; /* "X IS NULL" means X has only a single value */ @@ -3786,12 +3778,13 @@ static int codeEqualityTerm( int eType; int iTab; struct InLoop *pIn; + u8 bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0; assert( pX->op==TK_IN ); iReg = iTarget; eType = sqlite3FindInIndex(pParse, pX, 0); iTab = pX->iTable; - sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); + sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); assert( pLevel->plan.wsFlags & WHERE_IN_ABLE ); if( pLevel->u.in.nIn==0 ){ pLevel->addrNxt = sqlite3VdbeMakeLabel(v); @@ -3809,6 +3802,7 @@ static int codeEqualityTerm( }else{ pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); } + pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next; sqlite3VdbeAddOp1(v, OP_IsNull, iReg); }else{ pLevel->u.in.nIn = 0; @@ -5550,7 +5544,7 @@ void sqlite3WhereEnd(WhereInfo *pWInfo){ sqlite3VdbeResolveLabel(v, pLevel->addrNxt); for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ sqlite3VdbeJumpHere(v, pIn->addrInTop+1); - sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->addrInTop); + sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); sqlite3VdbeJumpHere(v, pIn->addrInTop-1); } sqlite3DbFree(db, pLevel->u.in.aInLoop); diff --git a/test/where.test b/test/where.test index 4daa7fe34b..2dbc283417 100644 --- a/test/where.test +++ b/test/where.test @@ -398,7 +398,7 @@ ifcapable subquery { count { SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1 DESC; } - } {3 1 16 2 1 9 1 0 4 14} + } {3 1 16 2 1 9 1 0 4 12} do_test where-5.4 { count { SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1; @@ -467,6 +467,30 @@ ifcapable subquery { SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,16) ORDER BY 1; } } {2 1 9 3 1 16 11} + do_test where-5.100 { + db eval { + SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969) + ORDER BY x, y + } + } {2 1 9 54 5 3025 62 5 3969} + do_test where-5.101 { + db eval { + SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969) + ORDER BY x DESC, y DESC + } + } {62 5 3969 54 5 3025 2 1 9} + do_test where-5.102 { + db eval { + SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969) + ORDER BY x DESC, y + } + } {54 5 3025 62 5 3969 2 1 9} + do_test where-5.103 { + db eval { + SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969) + ORDER BY x, y DESC + } + } {2 1 9 62 5 3969 54 5 3025} } # This procedure executes the SQL. Then it checks to see if the OP_Sort @@ -535,7 +559,7 @@ ifcapable subquery { cksort { SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a DESC LIMIT 3 } - } {9 92 100 7 94 64 5 96 36 sort} + } {9 92 100 7 94 64 5 96 36 nosort} } do_test where-6.9.1 { cksort { diff --git a/test/where2.test b/test/where2.test index 7d71ffe254..e8c2f3613f 100644 --- a/test/where2.test +++ b/test/where2.test @@ -182,7 +182,7 @@ ifcapable subquery { AND y IN (10000,10001,10002,10003,10004,10005) ORDER BY x DESC } - } {99 6 10000 10006 sort t1 i1xy} + } {99 6 10000 10006 nosort t1 i1xy} do_test where2-4.6c { queryplan { SELECT * FROM t1 @@ -246,7 +246,7 @@ ifcapable subquery { queryplan { SELECT * FROM t1 WHERE w IN (99) ORDER BY w DESC } - } {99 6 10000 10006 sort t1 i1w} + } {99 6 10000 10006 nosort t1 i1w} } # Verify that OR clauses get translated into IN operators.