Index: src/where.c ================================================================== --- src/where.c +++ src/where.c @@ -2189,10 +2189,14 @@ ** of iUpper are requested of whereKeyStats() and the smaller used. */ tRowcnt iLower; tRowcnt iUpper; + if( pRec ){ + testcase( pRec->nField!=pBuilder->nRecValid ); + pRec->nField = pBuilder->nRecValid; + } if( nEq==p->nKeyCol ){ aff = SQLITE_AFF_INTEGER; }else{ aff = p->pTable->aCol[p->aiColumn[nEq]].affinity; } Index: test/whereJ.test ================================================================== --- test/whereJ.test +++ test/whereJ.test @@ -368,8 +368,55 @@ AND t2b.id BETWEEN t1b.minChild AND t1b.maxChild AND t3b.id BETWEEN t2b.minChild AND t2b.maxChild AND t4.id BETWEEN t3a.minChild AND t3b.maxChild ORDER BY t4.x; } {~/SCAN/} + +############################################################################ + +ifcapable stat4 { + # Create and populate table. + do_execsql_test 3.1 { CREATE TABLE t1(a, b, c) } + for {set i 0} {$i < 32} {incr i 2} { + for {set x 0} {$x < 100} {incr x} { + execsql { INSERT INTO t1 VALUES($i, $x, $c) } + incr c + } + execsql { INSERT INTO t1 VALUES($i+1, 5, $c) } + incr c + } + + do_execsql_test 3.2 { + SELECT a, count(*) FROM t1 GROUP BY a HAVING a < 8; + } { + 0 100 1 1 2 100 3 1 4 100 5 1 6 100 7 1 + } + + do_execsql_test 3.3 { + CREATE INDEX idx_ab ON t1(a, b); + CREATE INDEX idx_c ON t1(c); + ANALYZE; + } {} + + # This one should use index "idx_c". + do_eqp_test 3.4 { + SELECT * FROM t1 WHERE + a = 4 AND b BETWEEN 20 AND 80 -- Matches 80 rows + AND + c BETWEEN 150 AND 160 -- Matches 10 rows + } { + 0 0 0 {SEARCH TABLE t1 USING INDEX idx_c (c>? AND c? AND b