Index: src/where.c ================================================================== --- src/where.c +++ src/where.c @@ -641,13 +641,12 @@ ** ** If there are multiple terms in the WHERE clause of the form "X " ** then try for the one with no dependencies on - in other words where ** is a constant expression of some kind. Only return entries of ** the form "X Y" where Y is a column in another table if no terms of -** the form "X " exist. Other than this priority, if there -** are two or more terms that match, then the choice of which term to return -** is arbitrary. +** the form "X " exist. If no terms with a constant RHS +** exist, try to return a term that does not use WO_EQUIV. */ static WhereTerm *findTerm( WhereClause *pWC, /* The WHERE clause to be searched */ int iCur, /* Cursor number of LHS */ int iColumn, /* Column number of LHS */ @@ -702,12 +701,16 @@ } if( sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ){ continue; } } - pResult = pTerm; - if( pTerm->prereqRight==0 ) goto findTerm_success; + if( pTerm->prereqRight==0 ){ + pResult = pTerm; + goto findTerm_success; + }else if( pResult==0 ){ + pResult = pTerm; + } } if( (pTerm->eOperator & WO_EQUIV)!=0 && nEquivpExpr->pRight); @@ -4229,10 +4232,11 @@ testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg); addrNxt = pLevel->addrNxt; sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); + sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); VdbeComment((v, "pk")); pLevel->op = OP_Noop; }else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){ /* Case 2: We have an inequality comparison against the ROWID field. ADDED test/tkt-fc7bd6358f.test Index: test/tkt-fc7bd6358f.test ================================================================== --- /dev/null +++ test/tkt-fc7bd6358f.test @@ -0,0 +1,78 @@ +# 2013 March 05 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. Specifically, +# it tests that ticket [fc7bd6358f]: +# +# The following SQL yields an incorrect result (zero rows) in all +# versions of SQLite between 3.6.14 and 3.7.15.2: +# +# CREATE TABLE t(textid TEXT); +# INSERT INTO t VALUES('12'); +# INSERT INTO t VALUES('34'); +# CREATE TABLE i(intid INTEGER PRIMARY KEY); +# INSERT INTO i VALUES(12); +# INSERT INTO i VALUES(34); +# +# SELECT t1.textid AS a, i.intid AS b, t2.textid AS c +# FROM t t1, i, t t2 +# WHERE t1.textid = i.intid +# AND t1.textid = t2.textid; +# +# The correct result should be two rows, one with 12|12|12 and the other +# with 34|34|34. With this bug, no rows are returned. Bisecting shows that +# this bug was introduced with check-in [dd4d67a67454] on 2009-04-23. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_test tkt-fc7bd6358f.100 { + db eval { + CREATE TABLE t(textid TEXT); + INSERT INTO t VALUES('12'); + INSERT INTO t VALUES('34'); + CREATE TABLE i(intid INTEGER PRIMARY KEY); + INSERT INTO i VALUES(12); + INSERT INTO i VALUES(34); + } +} {} +unset -nocomplain from +unset -nocomplain where +unset -nocomplain a +unset -nocomplain b +foreach {a from} { + 1 {FROM t t1, i, t t2} + 2 {FROM i, t t1, t t2} + 3 {FROM t t1, t t2, i} +} { + foreach {b where} { + 1 {WHERE t1.textid=i.intid AND t1.textid=t2.textid} + 2 {WHERE i.intid=t1.textid AND t1.textid=t2.textid} + 3 {WHERE t1.textid=i.intid AND i.intid=t2.textid} + 4 {WHERE t1.textid=i.intid AND t2.textid=i.intid} + 5 {WHERE i.intid=t1.textid AND i.intid=t2.textid} + 6 {WHERE i.intid=t1.textid AND t2.textid=i.intid} + 7 {WHERE t1.textid=t2.textid AND i.intid=t2.textid} + 8 {WHERE t1.textid=t2.textid AND t2.textid=i.intid} + } { + do_test tkt-fc7bd6358f.110.$a.$b.1 { + db eval {PRAGMA automatic_index=ON} + db eval "SELECT t1.textid, i.intid, t2.textid $from $where" + } {12 12 12 34 34 34} + do_test tkt-fc7bd6358f.110.$a.$b.2 { + db eval {PRAGMA automatic_index=OFF} + db eval "SELECT t1.textid, i.intid, t2.textid $from $where" + } {12 12 12 34 34 34} + } +} + + +finish_test