From 6b10a6a7edaa09b0e9151fe5b0185d33dce0f06f Mon Sep 17 00:00:00 2001 From: drh Date: Thu, 27 Sep 2012 17:31:32 +0000 Subject: Test cases and bug fixes applied to the ORDER BY optimization for joins. Some test cases fail, but except for the new orderby1.test failures, all failures appear to be issues with the tests, not with the core code. FossilOrigin-Name: 75cda864ededb6dc0f84bd52ed3311753a58e351 --- src/where.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'src/where.c') diff --git a/src/where.c b/src/where.c index 039549657..01155c14d 100644 --- a/src/where.c +++ b/src/where.c @@ -2883,16 +2883,27 @@ static int isOrderedColumn(WhereBestIdx *p, int iTab, int iCol, int *pbRev){ if( pLevel->iTabCur!=iTab ) continue; if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ pIdx = pLevel->plan.u.pIdx; - for(j=0; jnColumn; j++){ - if( iCol==pIdx->aiColumn[j] ) break; + if( iCol<0 ){ + sortOrder = 0; + testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ); + }else{ + for(j=0; jnColumn; j++){ + if( iCol==pIdx->aiColumn[j] ) break; + } + if( j>=pIdx->nColumn ) return 0; + sortOrder = pIdx->aSortOrder[j]; + testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ); } - if( j>=pIdx->nColumn ) return 0; - sortOrder = pIdx->aSortOrder[j]; }else{ if( iCol!=(-1) ) return 0; sortOrder = 0; + testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ); + } + if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ){ + assert( sortOrder==0 || sortOrder==1 ); + testcase( sortOrder==1 ); + sortOrder = 1 - sortOrder; } - if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ) sortOrder = 1 - sortOrder; if( *pbRev==2 ){ *pbRev = sortOrder; return 1; @@ -3200,8 +3211,8 @@ static void bestBtreeIndex(WhereBestIdx *p){ if( nOrderBy==nOBSat ){ bSort = 0; wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY; - wsFlags |= (bRev==1 ? WHERE_REVERSE : 0); } + if( bRev ) wsFlags |= WHERE_REVERSE; } /* If there is a DISTINCT qualifier and this index will scan rows in -- cgit v1.2.3