Index: src/select.c ================================================================== --- src/select.c +++ src/select.c @@ -4565,10 +4565,14 @@ iNewParent = pSubSrc->a[i].iCursor; memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); } pSrc->a[iFrom].fg.jointype &= JT_LTORJ; pSrc->a[iFrom].fg.jointype |= jointype | ltorj; + + if( isAgg && (jointype & JT_LEFT) ){ + pSrc->a[iFrom].fg.noIndexOnly = 1; + } /* Now begin substituting subquery result set expressions for ** references to the iParent in the outer query. ** ** Example: Index: src/sqliteInt.h ================================================================== --- src/sqliteInt.h +++ src/sqliteInt.h @@ -3102,10 +3102,11 @@ unsigned notCte :1; /* This item may not match a CTE */ unsigned isUsing :1; /* u3.pUsing is valid */ unsigned isOn :1; /* u3.pOn was once valid and non-NULL */ unsigned isSynthUsing :1; /* u3.pUsing is synthensized from NATURAL */ unsigned isNestedFrom :1; /* pSelect is a SF_NestedFrom subquery */ + unsigned noIndexOnly :1; /* Do not use an index-only scan for this loop */ } fg; int iCursor; /* The VDBE cursor number used to access this table */ union { Expr *pOn; /* fg.isUsing==0 => The ON clause of a join */ IdList *pUsing; /* fg.isUsing==1 => The USING clause of a join */ Index: src/where.c ================================================================== --- src/where.c +++ src/where.c @@ -3466,11 +3466,13 @@ rc = whereLoopInsert(pBuilder, pNew); pNew->nOut = rSize; if( rc ) break; }else{ Bitmask m; - if( pProbe->isCovering ){ + if( pSrc->fg.noIndexOnly ){ + pNew->wsFlags = WHERE_INDEXED; + }else if( pProbe->isCovering ){ pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; m = 0; }else{ m = pSrc->colUsed & pProbe->colNotIdxed; pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; Index: test/select3.test ================================================================== --- test/select3.test +++ test/select3.test @@ -327,7 +327,42 @@ CREATE TABLE t2(c, d); SELECT max(t1.a), (SELECT 'xyz' FROM (SELECT * FROM t2 WHERE 0) WHERE t1.b=1) FROM t1; } {{} {}} + +#------------------------------------------------------------------------- +# dbsqlfuzz crash-8e17857db2c5a9294c975123ac807156a6559f13.txt +# +reset_db +do_execsql_test select3-10.1 { + CREATE TABLE t1(a); + CREATE TABLE t2(x); + CREATE INDEX t2x ON t2(x); + INSERT INTO t1 VALUES('abc'); +} +do_execsql_test select3.10.2 { + SELECT max(a), val FROM t1 LEFT JOIN ( + SELECT 'constant' AS val FROM t2 WHERE x=1234 + ) +} {abc {}} +do_execsql_test select3.10.3 { + INSERT INTO t2 VALUES(123); + SELECT max(a), val FROM t1 LEFT JOIN ( + SELECT 'constant' AS val FROM t2 WHERE x=1234 + ) +} {abc {}} +do_execsql_test select3.10.4 { + INSERT INTO t2 VALUES(1234); + SELECT max(a), val FROM t1 LEFT JOIN ( + SELECT 'constant' AS val FROM t2 WHERE x=1234 + ) +} {abc constant} +do_execsql_test select3.10.5 { + DELETE FROM t2; + INSERT INTO t2 VALUES(1234); + SELECT max(a), val FROM t1 LEFT JOIN ( + SELECT 'constant' AS val FROM t2 WHERE x=1234 + ) +} {abc constant} finish_test