From: drh Date: Fri, 8 Feb 2013 20:39:02 +0000 (+0000) Subject: Make sure the virtual tables that take advantage of IN operators sort the X-Git-Tag: version-3.7.16~47^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3667033bfc236425644799261d461ddc3af909e3;p=thirdparty%2Fsqlite.git Make sure the virtual tables that take advantage of IN operators sort the RHS of the IN operator in the correct order according to the ORDER BY clause. FossilOrigin-Name: b016b7546d6fbfba06019398b9ac239b0cbe9086 --- diff --git a/manifest b/manifest index 3637f1e541..990aaddfef 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -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 +C Make\ssure\sthe\svirtual\stables\sthat\stake\sadvantage\sof\sIN\soperators\ssort\sthe\nRHS\sof\sthe\sIN\soperator\sin\sthe\scorrect\sorder\saccording\sto\sthe\sORDER\sBY\nclause. +D 2013-02-08T20:39:02.391 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in a48faa9e7dd7d556d84f5456eabe5825dd8a6282 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -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 36370580f2e3f805df161db44288bfb7d9ac007a +F src/where.c df3f8f610746165066bd029f24f35739b5d0a21f F test/8_3_names.test 631ea964a3edb091cf73c3b540f6bcfdb36ce823 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 F test/aggnested.test 45c0201e28045ad38a530b5a144b73cd4aa2cfd6 @@ -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 a917c1f09254b54e03e31b119cc49c551269d82e -R f8ff7e2997f1e9085eb15a7df82507fc +P f78395c8896666bb1359b83fbcd58d5e3dbc39d3 +R 080d4548a7ca8008fc94adcb8a7a49c2 U drh -Z d65a74f3ef1f6a5ee91ecd28380ca50d +Z 0d4d8d5d423c9c5e6216d31dab1445d7 diff --git a/manifest.uuid b/manifest.uuid index 8b749ce569..84f9305d63 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f78395c8896666bb1359b83fbcd58d5e3dbc39d3 \ No newline at end of file +b016b7546d6fbfba06019398b9ac239b0cbe9086 \ No newline at end of file diff --git a/src/where.c b/src/where.c index a3c5e24d39..c513c28b80 100644 --- a/src/where.c +++ b/src/where.c @@ -2270,8 +2270,9 @@ static void bestVirtualIndex(WhereBestIdx *p){ struct sqlite3_index_constraint *pIdxCons; struct sqlite3_index_constraint_usage *pUsage; WhereTerm *pTerm; - int i, j; + int i, j, k; int nOrderBy; + int sortOrder; /* Sort order for IN clauses */ int bAllowIN; /* Allow IN optimizations */ double rCost; @@ -2370,18 +2371,27 @@ static void bestVirtualIndex(WhereBestIdx *p){ return; } + sortOrder = SQLITE_SO_ASC; pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; for(i=0; inConstraint; i++, pIdxCons++){ if( pUsage[i].argvIndex>0 ){ j = pIdxCons->iTermOffset; pTerm = &pWC->a[j]; p->cost.used |= pTerm->prereqRight; - if( (pTerm->eOperator & WO_IN)!=0 && pUsage[i].omit==0 ){ - /* Do not attempt to use an IN constraint if the virtual table - ** says that the equivalent EQ constraint cannot be safely omitted. - ** If we do attempt to use such a constraint, some rows might be - ** repeated in the output. */ - break; + if( (pTerm->eOperator & WO_IN)!=0 ){ + if( pUsage[i].omit==0 ){ + /* Do not attempt to use an IN constraint if the virtual table + ** says that the equivalent EQ constraint cannot be safely omitted. + ** If we do attempt to use such a constraint, some rows might be + ** repeated in the output. */ + break; + } + for(k=0; knOrderBy; k++){ + if( pIdxInfo->aOrderBy[k].iColumn==pIdxCons->iColumn ){ + sortOrder = pIdxInfo->aOrderBy[k].desc; + break; + } + } } } } @@ -2411,7 +2421,8 @@ static void bestVirtualIndex(WhereBestIdx *p){ } p->cost.plan.u.pVtabIdx = pIdxInfo; if( pIdxInfo->orderByConsumed ){ - p->cost.plan.wsFlags |= WHERE_ORDERED; + assert( sortOrder==0 || sortOrder==1 ); + p->cost.plan.wsFlags |= WHERE_ORDERED + sortOrder*WHERE_REVERSE; p->cost.plan.nOBSat = nOrderBy; }else{ p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;