Skip to content

Commit

Permalink
Do not allow a term in the WHERE clause of the query to qualify a par…
Browse files Browse the repository at this point in the history
…tial

index on the right table of a LEFT JOIN.  Ticket [7f39060a24b47353]
  • Loading branch information
D. Richard Hipp committed Nov 30, 2019
1 parent b9fc654 commit a31cd0c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/where.c
Original file line number Diff line number Diff line change
Expand Up @@ -2790,19 +2790,25 @@ static int indexMightHelpWithOrderBy(
/* Check to see if a partial index with pPartIndexWhere can be used
** in the current query. Return true if it can be and false if not.
*/
static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
static int whereUsablePartialIndex(
int iTab, /* The table for which we want an index */
int isLeft, /* True if iTab is the right table of a LEFT JOIN */
WhereClause *pWC, /* The WHERE clause of the query */
Expr *pWhere /* The WHERE clause from the partial index */
){
int i;
WhereTerm *pTerm;
Parse *pParse = pWC->pWInfo->pParse;
while( pWhere->op==TK_AND ){
if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0;
if( !whereUsablePartialIndex(iTab,isLeft,pWC,pWhere->pLeft) ) return 0;
pWhere = pWhere->pRight;
}
if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
Expr *pExpr;
pExpr = pTerm->pExpr;
if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
&& (isLeft==0 || ExprHasProperty(pExpr, EP_FromJoin))
&& sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab)
){
return 1;
Expand Down Expand Up @@ -2965,8 +2971,11 @@ static int whereLoopAddBtree(
for(; rc==SQLITE_OK && pProbe;
pProbe=(pSrc->pIBIndex ? 0 : pProbe->pNext), iSortIdx++
){
int isLeft = (pSrc->fg.jointype & JT_OUTER)!=0;
if( pProbe->pPartIdxWhere!=0
&& !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
&& !whereUsablePartialIndex(pSrc->iCursor, isLeft, pWC,
pProbe->pPartIdxWhere)
){
testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
continue; /* Partial index inappropriate for this query */
}
Expand Down
22 changes: 22 additions & 0 deletions test/join.test
Original file line number Diff line number Diff line change
Expand Up @@ -953,4 +953,26 @@ do_execsql_test join-20.2 {
SELECT * FROM t0 LEFT JOIN t1 WHERE NULL IN (c1);
} {}

# 2019-11-30 ticket 7f39060a24b47353
# Do not allow a WHERE clause term to qualify a partial index on the
# right table of a LEFT JOIN.
#
do_execsql_test join-21.10 {
DROP TABLE t0;
DROP TABLE t1;
CREATE TABLE t0(aa);
CREATE TABLE t1(bb);
INSERT INTO t0(aa) VALUES (1);
INSERT INTO t1(bb) VALUES (1);
SELECT 11, * FROM t1 LEFT JOIN t0 WHERE aa ISNULL;
SELECT 12, * FROM t1 LEFT JOIN t0 WHERE +aa ISNULL;
SELECT 13, * FROM t1 LEFT JOIN t0 ON aa ISNULL;
SELECT 14, * FROM t1 LEFT JOIN t0 ON +aa ISNULL;
CREATE INDEX i0 ON t0(aa) WHERE aa ISNULL;
SELECT 21, * FROM t1 LEFT JOIN t0 WHERE aa ISNULL;
SELECT 22, * FROM t1 LEFT JOIN t0 WHERE +aa ISNULL;
SELECT 23, * FROM t1 LEFT JOIN t0 ON aa ISNULL;
SELECT 24, * FROM t1 LEFT JOIN t0 ON +aa ISNULL;
} {13 1 {} 14 1 {} 23 1 {} 24 1 {}}

finish_test

0 comments on commit a31cd0c

Please sign in to comment.