Index: src/where.c ================================================================== --- src/where.c +++ src/where.c @@ -1612,10 +1612,11 @@ pLoop = pLevel->pWLoop; idxCols = 0; for(pTerm=pWC->a; pTermprereq==0 && (pTerm->wtFlags & TERM_VIRTUAL)==0 + && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) && sqlite3ExprIsTableConstant(pTerm->pExpr, pSrc->iCursor) ){ pPartial = sqlite3ExprAnd(pParse->db, pPartial, sqlite3ExprDup(pParse->db, pTerm->pExpr, 0)); } if( termCanDriveIndex(pTerm, pSrc, notReady) ){ @@ -4692,11 +4693,15 @@ */ static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ int i; WhereTerm *pTerm; for(i=0, pTerm=pWC->a; inTerm; i++, pTerm++){ - if( sqlite3ExprImpliesExpr(pTerm->pExpr, pWhere, iTab) ) return 1; + if( sqlite3ExprImpliesExpr(pTerm->pExpr, pWhere, iTab) + && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) + ){ + return 1; + } } return 0; } /* Index: test/autoindex4.test ================================================================== --- test/autoindex4.test +++ test/autoindex4.test @@ -46,7 +46,38 @@ SELECT (SELECT count(*) FROM t1, t2 WHERE a=e AND x=f), e, f, '|' FROM t3 ORDER BY rowid; } {1 123 654 | 0 555 444 | 4 234 987 |} + +# Ticket [2326c258d02ead33d] +# Two joins, one with and the other without an ORDER BY clause. +# The one without ORDER BY correctly returns two rows of result. +# The one with ORDER BY returns no rows. +# +do_execsql_test autoindex4-3.0 { + CREATE TABLE A(Name text); + CREATE TABLE Items(ItemName text , Name text); + INSERT INTO Items VALUES('Item1','Parent'); + INSERT INTO Items VALUES('Item2','Parent'); + CREATE TABLE B(Name text); + + SELECT Items.ItemName + FROM Items + LEFT JOIN A ON (A.Name = Items.ItemName and Items.ItemName = 'dummy') + LEFT JOIN B ON (B.Name = Items.ItemName) + WHERE Items.Name = 'Parent' + ORDER BY Items.ItemName; +} {Item1 Item2} +do_execsql_test autoindex4-3.1 { + CREATE INDEX Items_x1 ON Items(ItemName,Name) WHERE ItemName = 'dummy'; + + SELECT Items.ItemName + FROM Items + LEFT JOIN A ON (A.Name = Items.ItemName and Items.ItemName = 'dummy') + LEFT JOIN B ON (B.Name = Items.ItemName) + WHERE Items.Name = 'Parent' + ORDER BY Items.ItemName; +} {Item1 Item2} + finish_test Index: test/index6.test ================================================================== --- test/index6.test +++ test/index6.test @@ -265,7 +265,35 @@ } {123 789} do_execsql_test index6-6.2 { PRAGMA integrity_check; } {ok} +# Test case for ticket [2326c258d02ead33d69faa63de8f4686b9b1b9d9] on +# 2015-02-24. Any use of a partial index qualifying constraint inside +# the ON clause of a LEFT JOIN was causing incorrect results for all +# versions of SQLite 3.8.0 through 3.8.8. +# +do_execsql_test index6-7.0 { + CREATE TABLE t7a(x); + CREATE TABLE t7b(y); + INSERT INTO t7a(x) VALUES(1); + CREATE INDEX t7ax ON t7a(x) WHERE x=99; + PRAGMA automatic_index=OFF; + SELECT * FROM t7a LEFT JOIN t7b ON (x=99) ORDER BY x; +} {1 {}} +do_execsql_test index6-7.1 { + INSERT INTO t7b(y) VALUES(2); + SELECT * FROM t7a JOIN t7b ON (x=99) ORDER BY x; +} {} +do_execsql_test index6-7.2 { + INSERT INTO t7a(x) VALUES(99); + SELECT * FROM t7a LEFT JOIN t7b ON (x=99) ORDER BY x; +} {1 {} 99 2} +do_execsql_test index6-7.3 { + SELECT * FROM t7a JOIN t7b ON (x=99) ORDER BY x; +} {99 2} +do_execsql_test index6-7.4 { + EXPLAIN QUERY PLAN + SELECT * FROM t7a JOIN t7b ON (x=99) ORDER BY x; +} {/USING COVERING INDEX t7ax/} finish_test