diff --git a/manifest b/manifest index 9ff8efa3d0..b4f7b35efb 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\san\sassert()\sin\sfts3\sthat\scould\sfail\swhen\saccessing\sa\scorrupt\sdatabase. -D 2019-09-19T13:51:52.264 +C Fix\sa\sproblem\swith\squeries\scontaining\sa\smin()\sor\smax()\sfunction\sfor\swhich\sthe\sFILTER\sclause\sexcludes\sall\srows. +D 2019-09-20T20:52:16.031 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -525,7 +525,7 @@ F src/printf.c 9be6945837c839ba57837b4bc3af349eba630920fa5532aa518816defe42a7d4 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384 F src/resolve.c 9891cf5fd155bb199f8b1ff5d1429b9f70484487f4c455bba94348d4cb6f829f F src/rowset.c d977b011993aaea002cab3e0bb2ce50cf346000dff94e944d547b989f4b1fe93 -F src/select.c e2c870548541d33d090a066e89ab2e7943299006e2827c1abfae39561041fc0b +F src/select.c fd3ef107dfa6cdad83b76abc04c48411ba6103624f19a867954232ec7fb5edc7 F src/shell.c.in 68698630c21c5489fb3dc961a3ab3840e726c3c01e475dab96055788a7b6e5e6 F src/sqlite.h.in 5725a6b20190a1e8d662077a1c1c8ea889ad7be90dd803f914c2de226f5fe6ab F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 @@ -859,7 +859,7 @@ F test/extraquick.test cb254400bd42bfb777ff675356aabf3287978f79 F test/fallocate.test 37a62e396a68eeede8f8d2ecf23573a80faceb630788d314d0a073d862616717 F test/filectrl.test 6e871c2d35dead1d9a88e176e8d2ca094fec6bb3 F test/filefmt.test f393e80c4b8d493b7a7f8f3809a8425bbf4292af1f5140f01cb1427798a2bbd4 -F test/filter1.test 94a6d26588db0e4e7305e1f0985a99405e98dd034dc393dd6264603eed6645d4 +F test/filter1.test f0ee87b2adf6a90aa81456f20a0b097bb75ed3f6bdcf641fe103cc797dd109b9 F test/filter2.tcl 44e525497ce07382915f01bd29ffd0fa49dab3adb87253b5e5103ba8f93393e8 F test/filter2.test 485cf95d1f6d6ceee5632201ca52a71868599836f430cdee42e5f7f14666e30a F test/filterfault.test c08fb491d698e8df6c122c98f7db1c65ffcfcad2c1ab0e07fa8a5be1b34eaa8b @@ -1844,7 +1844,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P b6f2a7f9cdb547c925f08306df82519e41a7a0dd2cabd9861a48917bb6eef845 -R 689779cd455e90c69e11e47694961c4a +P 601ce9532d34f97a5faa170b6d8a272e5c54f2eabff84660201a1840e0fd9929 +R 3f32110f8bafe29b4265ff0c8cd184cc U dan -Z 6843ff5a51f220102304ec1c80cb5d67 +Z 38f3480169c6f6b8dfb01762d9e1b1b0 diff --git a/manifest.uuid b/manifest.uuid index 8141fe0c9b..d6c287a7f3 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -601ce9532d34f97a5faa170b6d8a272e5c54f2eabff84660201a1840e0fd9929 \ No newline at end of file +b1d7e104e034655fe41bc55c562d91e8074a2973d538b8b29301458db45afc57 \ No newline at end of file diff --git a/src/select.c b/src/select.c index 811671dd69..c23c258fb3 100644 --- a/src/select.c +++ b/src/select.c @@ -5343,6 +5343,7 @@ static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){ int i; int regHit = 0; int addrHitTest = 0; + int bFilterMinMax = 0; struct AggInfo_func *pF; struct AggInfo_col *pC; @@ -5356,6 +5357,17 @@ static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){ assert( !IsWindowFunc(pF->pExpr) ); if( ExprHasProperty(pF->pExpr, EP_WinFunc) ){ Expr *pFilter = pF->pExpr->y.pWin->pFilter; + if( pAggInfo->nAccumulator + && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL) + ){ + bFilterMinMax = 1; + if( regHit==0 ) regHit = ++pParse->nMem; + /* If this is the first row of the group (regAcc==0), clear the + ** "magnet" register regHit so that the accumulator registers + ** are populated even if the FILTER clause causes control to + ** skip over the invocation of min() or max() altogether */ + sqlite3VdbeAddOp2(v, OP_Copy, regAcc, regHit); + } addrNext = sqlite3VdbeMakeLabel(pParse); sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL); } @@ -5406,6 +5418,13 @@ static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){ for(i=0, pC=pAggInfo->aCol; inAccumulator; i++, pC++){ sqlite3ExprCode(pParse, pC->pExpr, pC->iMem); } + if( bFilterMinMax ){ + /* If there is a min() or max() with a FILTER clause, then ensure that + ** the "magnet" register is set to indicate "do not attract" after + ** loading column values into the accumulator registers */ + sqlite3VdbeAddOp2(v, OP_Integer, 1, regHit); + } + pAggInfo->directMode = 0; if( addrHitTest ){ sqlite3VdbeJumpHere(v, addrHitTest); @@ -6581,13 +6600,18 @@ int sqlite3Select( { int regAcc = 0; /* "populate accumulators" flag */ - /* If there are accumulator registers but no min() or max() functions, - ** allocate register regAcc. Register regAcc will contain 0 the first - ** time the inner loop runs, and 1 thereafter. The code generated - ** by updateAccumulator() only updates the accumulator registers if - ** regAcc contains 0. */ + /* If there are accumulator registers but no min() or max() functions + ** without FILTER clauses, allocate register regAcc. Register regAcc + ** will contain 0 the first time the inner loop runs, and 1 thereafter. + ** The code generated by updateAccumulator() uses this to ensure + ** that the accumulator registers are (a) updated only once if + ** there are no min() or max functions or (b) always updated for the + ** first row visited by the aggregate, so that they are updated at + ** least once even if the FILTER clause means the min() or max() + ** function visits zero rows. */ if( sAggInfo.nAccumulator ){ for(i=0; ifuncFlags&SQLITE_FUNC_NEEDCOLL ) break; } if( i==sAggInfo.nFunc ){ diff --git a/test/filter1.test b/test/filter1.test index e15022fd6f..be51b6f31f 100644 --- a/test/filter1.test +++ b/test/filter1.test @@ -101,4 +101,37 @@ do_catchsql_test 2.3 { SELECT sum(a) FILTER (WHERE 1 - count(a)) FROM t1 } {1 {misuse of aggregate function count()}} +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1, 1); +} +do_execsql_test 3.1 { + SELECT b, max(a) FILTER (WHERE b='x') FROM t1; +} {1 {}} + +do_execsql_test 3.2 { + CREATE TABLE t2(a, b, c); + INSERT INTO t2 VALUES(1, 2, 3); + INSERT INTO t2 VALUES(1, 3, 4); + INSERT INTO t2 VALUES(2, 5, 6); + INSERT INTO t2 VALUES(2, 7, 8); +} +do_execsql_test 3.3 { + SELECT a, c, max(b) FILTER (WHERE c='x') FROM t2 GROUP BY a; +} {1 3 {} 2 6 {}} + +do_execsql_test 3.4 { + DELETE FROM t2; + INSERT INTO t2 VALUES(1, 5, 'x'); + INSERT INTO t2 VALUES(1, 2, 3); + INSERT INTO t2 VALUES(1, 4, 'x'); + INSERT INTO t2 VALUES(2, 5, 6); + INSERT INTO t2 VALUES(2, 7, 8); +} +do_execsql_test 3.5 { + SELECT a, c, max(b) FILTER (WHERE c='x') FROM t2 GROUP BY a; +} {1 x 5 2 6 {}} + finish_test