mirror of
https://github.com/sqlite/sqlite.git
synced 2025-11-22 20:22:44 +03:00
Fix for 2a5629202f. When considering whether or not a UNIQUE index may be used to optimize an ORDER BY clause, do not assume that all index entries are distinct unless there is some reason to believe that the index contains no NULL values.
FossilOrigin-Name: 9870e4c4fef10112c987c40cb1b95255a7214202
This commit is contained in:
28
src/where.c
28
src/where.c
@@ -1718,14 +1718,26 @@ static int isSortingIndex(
|
||||
}
|
||||
if( pIdx->onError!=OE_None && i==pIdx->nColumn
|
||||
&& (wsFlags & WHERE_COLUMN_NULL)==0
|
||||
&& !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
|
||||
/* All terms of this index match some prefix of the ORDER BY clause
|
||||
** and the index is UNIQUE and no terms on the tail of the ORDER BY
|
||||
** clause reference other tables in a join. If this is all true then
|
||||
** the order by clause is superfluous. Not that if the matching
|
||||
** condition is IS NULL then the result is not necessarily unique
|
||||
** even on a UNIQUE index, so disallow those cases. */
|
||||
return 1;
|
||||
&& !referencesOtherTables(pOrderBy, pMaskSet, j, base)
|
||||
){
|
||||
Column *aCol = pIdx->pTable->aCol;
|
||||
int i;
|
||||
|
||||
/* All terms of this index match some prefix of the ORDER BY clause,
|
||||
** the index is UNIQUE, and no terms on the tail of the ORDER BY
|
||||
** refer to other tables in a join. So, assuming that the index entries
|
||||
** visited contain no NULL values, then this index delivers rows in
|
||||
** the required order.
|
||||
**
|
||||
** It is not possible for any of the first nEqCol index fields to be
|
||||
** NULL (since the corresponding "=" operator in the WHERE clause would
|
||||
** not be true). So if all remaining index columns have NOT NULL
|
||||
** constaints attached to them, we can be confident that the visited
|
||||
** index entries are free of NULLs. */
|
||||
for(i=nEqCol; i<pIdx->nColumn; i++){
|
||||
if( aCol[pIdx->aiColumn[i]].notNull==0 ) break;
|
||||
}
|
||||
return (i>=pIdx->nColumn);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user