From 7a95789c0c21a39e8bd9972b92262c63a2ae820a Mon Sep 17 00:00:00 2001 From: drh Date: Thu, 2 Feb 2012 17:35:43 +0000 Subject: [PATCH 1/2] For queries of the form "SELECT p, max(q) FROM t1", the value of column p returned is the one on the same row that holds the maximum value of q. FossilOrigin-Name: adb29232b659c5332b6841776372407fcc350b08 --- manifest | 23 +++++++++++++---------- manifest.uuid | 2 +- src/func.c | 10 ++++++++++ src/select.c | 11 ++++++++++- src/vdbe.c | 15 ++++++++++++++- src/vdbeInt.h | 1 + test/e_select.test | 2 +- 7 files changed, 50 insertions(+), 14 deletions(-) diff --git a/manifest b/manifest index a02f0e5e2c..c28861e94c 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Data\sstructure\scleanup.\s\sRemove\sunused\sfields.\s\sRearrange\sother\sfiles\sfor\ntighter\spacking\sand\sreduced\smemory\susage. -D 2012-02-02T15:50:17.008 +C For\squeries\sof\sthe\sform\s"SELECT\sp,\smax(q)\sFROM\st1",\sthe\svalue\sof\scolumn\sp\nreturned\sis\sthe\sone\son\sthe\ssame\srow\sthat\sholds\sthe\smaximum\svalue\sof\sq. +D 2012-02-02T17:35:43.157 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 3f79a373e57c3b92dabf76f40b065e719d31ac34 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -137,7 +137,7 @@ F src/delete.c 51d32f0a9c880663e54ce309f52e40c325d5e112 F src/expr.c 00675123e0beec98f999aa4594d2cbe1fec33c1b F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb F src/fkey.c 657212460bf5cfd3ae607d12ea62092844c227b5 -F src/func.c 6261ce00aad9c63cd5b4219249b05683979060e9 +F src/func.c f66c87956c9afc098e1b74612d3c2b38760a41dc F src/global.c 4cfdca5cb0edd33c4d021baec4ede958cb2c793b F src/hash.c 458488dcc159c301b8e7686280ab209f1fb915af F src/hash.h 2894c932d84d9f892d4b4023a75e501f83050970 @@ -180,7 +180,7 @@ F src/printf.c 7ffb4ebb8b341f67e049695ba031da717b3d2699 F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50 F src/resolve.c 3d3e80a98f203ac6b9329e9621e29eda85ddfd40 F src/rowset.c 69afa95a97c524ba6faf3805e717b5b7ae85a697 -F src/select.c b6eec9d11ec5df0d1fd11e8a0eed0fc2d2d96b25 +F src/select.c 232283a2e60d91cbd9a5ddf2f6f7ecf53d590075 F src/shell.c 60d147c2411dd2d79a5151cfb9a068de87c7babe F src/sqlite.h.in 371c30e4be94b9b0ea6400ed66663fcf8e891eb4 F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477 @@ -240,9 +240,9 @@ F src/update.c d3076782c887c10e882996550345da9c4c9f9dea F src/utf.c 890c67dcfcc7a74623c95baac7535aadfe265e84 F src/util.c 9e07bd67dfafe9c75b1da78c87ba030cebbb5388 F src/vacuum.c 0c0ba2242355c6048d65e2b333abe0f7c06348fa -F src/vdbe.c 43ea77c83e7f35169328f13b20cc8140bd2cf473 +F src/vdbe.c 40b14dff04692b1ee421db40c67d4921ecf17a9d F src/vdbe.h 18f581cac1f4339ec3299f3e0cc6e11aec654cdb -F src/vdbeInt.h 2e79ce66ff45a01411151957bb2899af680f1560 +F src/vdbeInt.h 0f986e86a4a0afbf52d0402071708f487d1cfc15 F src/vdbeapi.c 3662b6a468a2a4605a15dfab313baa6dff81ad91 F src/vdbeaux.c 7683d772ad638faa4567142438c4594e47f173c4 F src/vdbeblob.c 32f2a4899d67f69634ea4dd93e3f651936d732cb @@ -386,7 +386,7 @@ F test/e_fts3.test 5c02288842e4f941896fd44afdef564dd5fc1459 F test/e_insert.test 234242b71855af8e8a9b1e141c3533f6d43d8645 F test/e_reindex.test dfedfc32c5a282b0596c6537cbcd4217fbb1a216 F test/e_resolve.test dcce9308fb13b934ce29591105d031d3e14fbba6 -F test/e_select.test 6f488c22851c1c280ed74e16a9a1430706a3a8cb +F test/e_select.test f0aa7a972a336e5e874700915b1f5abd7d63a7e2 F test/e_select2.test 5c3d3da19c7b3e90ae444579db2b70098599ab92 F test/e_update.test dba988a4d079156549a40854074ba4890b0a4577 F test/e_uri.test 6f35b491f80dac005c8144f38b2dfb4d96483596 @@ -988,7 +988,10 @@ F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/warnings-clang.sh 9f406d66e750e8ac031c63a9ef3248aaa347ef2a F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381 -P 25df2a7458d025bc00380b4a0893637639f9f0d4 -R d2ba4c243c1dae496337618e0657e24d +P 0bc594e861ae1142e8c6c83f39b095e1432856bf +R eff167ee91b10474f283fe35279e92e9 +T *branch * output-minmax-row +T *sym-output-minmax-row * +T -sym-trunk * U drh -Z ada6e2e4fcf7ab0bc8362d130037dce6 +Z cf65185cbe7093f978d4a185698ec6ea diff --git a/manifest.uuid b/manifest.uuid index 3536af2979..e33d5dd106 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -0bc594e861ae1142e8c6c83f39b095e1432856bf \ No newline at end of file +adb29232b659c5332b6841776372407fcc350b08 \ No newline at end of file diff --git a/src/func.c b/src/func.c index 3a1879ca69..deddd5375b 100644 --- a/src/func.c +++ b/src/func.c @@ -28,6 +28,14 @@ static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ return context->pColl; } +/* +** Indicate that the accumulator load should be skipped on this +** iteration of the aggregate loop. +*/ +static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){ + context->skipFlag = 1; +} + /* ** Implementation of the non-aggregate min() and max() functions */ @@ -1354,6 +1362,8 @@ static void minmaxStep( cmp = sqlite3MemCompare(pBest, pArg, pColl); if( (max && cmp<0) || (!max && cmp>0) ){ sqlite3VdbeMemCopy(pBest, pArg); + }else{ + sqlite3SkipAccumulatorLoad(context); } }else{ sqlite3VdbeMemCopy(pBest, pArg); diff --git a/src/select.c b/src/select.c index 6fcb37f70d..bbdc4e3c55 100644 --- a/src/select.c +++ b/src/select.c @@ -3596,6 +3596,8 @@ static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){ static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ Vdbe *v = pParse->pVdbe; int i; + int regHit = 0; + int addrHitTest = 0; struct AggInfo_func *pF; struct AggInfo_col *pC; @@ -3631,7 +3633,8 @@ static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ if( !pColl ){ pColl = pParse->db->pDfltColl; } - sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); + if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem; + sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ); } sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem, (void*)pF->pFunc, P4_FUNCDEF); @@ -3654,12 +3657,18 @@ static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ ** Another solution would be to change the OP_SCopy used to copy cached ** values to an OP_Copy. */ + if( regHit ){ + addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); + } sqlite3ExprCacheClear(pParse); for(i=0, pC=pAggInfo->aCol; inAccumulator; i++, pC++){ sqlite3ExprCode(pParse, pC->pExpr, pC->iMem); } pAggInfo->directMode = 0; sqlite3ExprCacheClear(pParse); + if( addrHitTest ){ + sqlite3VdbeJumpHere(v, addrHitTest); + } } /* diff --git a/src/vdbe.c b/src/vdbe.c index 111d5b52bb..9ce25942dd 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -1331,19 +1331,26 @@ arithmetic_result_is_null: break; } -/* Opcode: CollSeq * * P4 +/* Opcode: CollSeq P1 * * P4 ** ** P4 is a pointer to a CollSeq struct. If the next call to a user function ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will ** be returned. This is used by the built-in min(), max() and nullif() ** functions. ** +** If P1 is not zero, then it is a register that a subsequent min() or +** max() aggregate will set to 1 if the current row is not the minimum or +** maximum. The P1 register is initialized to 0 by this instruction. +** ** The interface used by the implementation of the aforementioned functions ** to retrieve the collation sequence set by this opcode is not available ** publicly, only to user functions defined in func.c. */ case OP_CollSeq: { assert( pOp->p4type==P4_COLLSEQ ); + if( pOp->p1 ){ + sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); + } break; } @@ -5356,6 +5363,7 @@ case OP_AggStep: { ctx.s.db = db; ctx.isError = 0; ctx.pColl = 0; + ctx.skipFlag = 0; if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){ assert( pOp>p->aOp ); assert( pOp[-1].p4type==P4_COLLSEQ ); @@ -5367,6 +5375,11 @@ case OP_AggStep: { sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); rc = ctx.isError; } + if( ctx.skipFlag ){ + assert( pOp[-1].opcode==OP_CollSeq ); + i = pOp[-1].p1; + if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); + } sqlite3VdbeMemRelease(&ctx.s); diff --git a/src/vdbeInt.h b/src/vdbeInt.h index e201bcde79..f3f9f73a35 100644 --- a/src/vdbeInt.h +++ b/src/vdbeInt.h @@ -258,6 +258,7 @@ struct sqlite3_context { Mem *pMem; /* Memory cell used to store aggregate context */ int isError; /* Error code returned by the function. */ CollSeq *pColl; /* Collating sequence */ + int skipFlag; /* Skip skip accumulator loading if true */ }; /* diff --git a/test/e_select.test b/test/e_select.test index 39a74cc8d7..656bbd1b56 100644 --- a/test/e_select.test +++ b/test/e_select.test @@ -800,7 +800,7 @@ do_select_tests e_select-4.1 { 6 "SELECT count(*), * FROM z1" {6 63 born -26} 7 "SELECT max(a), * FROM z1" {63 63 born -26} - 8 "SELECT *, min(a) FROM z1" {63 born -26 -5} + 8 "SELECT *, min(a) FROM z1" {{} 67 quartets -5} 9 "SELECT *,* FROM z1,z2 LIMIT 1" { 51.65 -59.58 belfries {} 21 51.65 -59.58 belfries {} 21 From 94a6d998f6ce481f7d0a2ef2e1619cdef5ebb10e Mon Sep 17 00:00:00 2001 From: drh Date: Thu, 2 Feb 2012 18:42:09 +0000 Subject: [PATCH 2/2] Fix a problem with NULL handling in aggregate min/max when returning values from the row containing the min or max. FossilOrigin-Name: f27c7b4fb193126548e6a620ac89664d1efa3856 --- manifest | 18 +++--- manifest.uuid | 2 +- src/func.c | 7 ++- test/e_select.test | 2 +- test/minmax4.test | 145 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 159 insertions(+), 15 deletions(-) create mode 100644 test/minmax4.test diff --git a/manifest b/manifest index c28861e94c..200cf03d50 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C For\squeries\sof\sthe\sform\s"SELECT\sp,\smax(q)\sFROM\st1",\sthe\svalue\sof\scolumn\sp\nreturned\sis\sthe\sone\son\sthe\ssame\srow\sthat\sholds\sthe\smaximum\svalue\sof\sq. -D 2012-02-02T17:35:43.157 +C Fix\sa\sproblem\swith\sNULL\shandling\sin\saggregate\smin/max\swhen\sreturning\nvalues\sfrom\sthe\srow\scontaining\sthe\smin\sor\smax. +D 2012-02-02T18:42:09.724 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 3f79a373e57c3b92dabf76f40b065e719d31ac34 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -137,7 +137,7 @@ F src/delete.c 51d32f0a9c880663e54ce309f52e40c325d5e112 F src/expr.c 00675123e0beec98f999aa4594d2cbe1fec33c1b F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb F src/fkey.c 657212460bf5cfd3ae607d12ea62092844c227b5 -F src/func.c f66c87956c9afc098e1b74612d3c2b38760a41dc +F src/func.c d7925f33a8ce2207f86dea5cfb1a4379413312fe F src/global.c 4cfdca5cb0edd33c4d021baec4ede958cb2c793b F src/hash.c 458488dcc159c301b8e7686280ab209f1fb915af F src/hash.h 2894c932d84d9f892d4b4023a75e501f83050970 @@ -386,7 +386,7 @@ F test/e_fts3.test 5c02288842e4f941896fd44afdef564dd5fc1459 F test/e_insert.test 234242b71855af8e8a9b1e141c3533f6d43d8645 F test/e_reindex.test dfedfc32c5a282b0596c6537cbcd4217fbb1a216 F test/e_resolve.test dcce9308fb13b934ce29591105d031d3e14fbba6 -F test/e_select.test f0aa7a972a336e5e874700915b1f5abd7d63a7e2 +F test/e_select.test 99202f99a9a3273c6fb0d2e7592b98faeb6c206e F test/e_select2.test 5c3d3da19c7b3e90ae444579db2b70098599ab92 F test/e_update.test dba988a4d079156549a40854074ba4890b0a4577 F test/e_uri.test 6f35b491f80dac005c8144f38b2dfb4d96483596 @@ -601,6 +601,7 @@ F test/memsubsys2.test 3a1c1a9de48e5726faa85108b02459fae8cb9ee9 F test/minmax.test 722d80816f7e096bf2c04f4111f1a6c1ba65453d F test/minmax2.test 33504c01a03bd99226144e4b03f7631a274d66e0 F test/minmax3.test cc1e8b010136db0d01a6f2a29ba5a9f321034354 +F test/minmax4.test c1fa9505fd135007fdb1fb699334fb3d4ea7952e F test/misc1.test 55cb2bfbf4a8cd61f4be1effc30426ad41696bff F test/misc2.test 00d7de54eda90e237fc9a38b9e5ccc769ebf6d4d F test/misc3.test fe55130a43e444ee75e2156ff75dc96e964b5738 @@ -988,10 +989,7 @@ F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/warnings-clang.sh 9f406d66e750e8ac031c63a9ef3248aaa347ef2a F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381 -P 0bc594e861ae1142e8c6c83f39b095e1432856bf -R eff167ee91b10474f283fe35279e92e9 -T *branch * output-minmax-row -T *sym-output-minmax-row * -T -sym-trunk * +P adb29232b659c5332b6841776372407fcc350b08 +R 087241beca277f1bd5ef577cbad5e6e6 U drh -Z cf65185cbe7093f978d4a185698ec6ea +Z d29e9619f6216c8dbc76413eb249e3f2 diff --git a/manifest.uuid b/manifest.uuid index e33d5dd106..1b02240b7b 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -adb29232b659c5332b6841776372407fcc350b08 \ No newline at end of file +f27c7b4fb193126548e6a620ac89664d1efa3856 \ No newline at end of file diff --git a/src/func.c b/src/func.c index deddd5375b..b7d0810686 100644 --- a/src/func.c +++ b/src/func.c @@ -1342,11 +1342,12 @@ static void minmaxStep( Mem *pBest; UNUSED_PARAMETER(NotUsed); - if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); if( !pBest ) return; - if( pBest->flags ){ + if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ + if( pBest->flags ) sqlite3SkipAccumulatorLoad(context); + }else if( pBest->flags ){ int max; int cmp; CollSeq *pColl = sqlite3GetFuncCollSeq(context); @@ -1373,7 +1374,7 @@ static void minMaxFinalize(sqlite3_context *context){ sqlite3_value *pRes; pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); if( pRes ){ - if( ALWAYS(pRes->flags) ){ + if( pRes->flags ){ sqlite3_result_value(context, pRes); } sqlite3VdbeMemRelease(pRes); diff --git a/test/e_select.test b/test/e_select.test index 656bbd1b56..103472cd92 100644 --- a/test/e_select.test +++ b/test/e_select.test @@ -800,7 +800,7 @@ do_select_tests e_select-4.1 { 6 "SELECT count(*), * FROM z1" {6 63 born -26} 7 "SELECT max(a), * FROM z1" {63 63 born -26} - 8 "SELECT *, min(a) FROM z1" {{} 67 quartets -5} + 8 "SELECT *, min(a) FROM z1" {-5 {} 75 -5} 9 "SELECT *,* FROM z1,z2 LIMIT 1" { 51.65 -59.58 belfries {} 21 51.65 -59.58 belfries {} 21 diff --git a/test/minmax4.test b/test/minmax4.test new file mode 100644 index 0000000000..12f56a86c3 --- /dev/null +++ b/test/minmax4.test @@ -0,0 +1,145 @@ +# 2012 February 02 +# +# 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. +# +#*********************************************************************** +# +# Test for queries of the form: +# +# SELECT p, max(q) FROM t1; +# +# Demonstration that the value returned for p is on the same row as +# the maximum q. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_test minmax4-1.1 { + db eval { + CREATE TABLE t1(p,q); + SELECT p, max(q) FROM t1; + } +} {{} {}} +do_test minmax4-1.2 { + db eval { + SELECT p, min(q) FROM t1; + } +} {{} {}} +do_test minmax4-1.3 { + db eval { + INSERT INTO t1 VALUES(1,2); + SELECT p, max(q) FROM t1; + } +} {1 2} +do_test minmax4-1.4 { + db eval { + SELECT p, min(q) FROM t1; + } +} {1 2} +do_test minmax4-1.5 { + db eval { + INSERT INTO t1 VALUES(3,4); + SELECT p, max(q) FROM t1; + } +} {3 4} +do_test minmax4-1.6 { + db eval { + SELECT p, min(q) FROM t1; + } +} {1 2} +do_test minmax4-1.7 { + db eval { + INSERT INTO t1 VALUES(5,0); + SELECT p, max(q) FROM t1; + } +} {3 4} +do_test minmax4-1.8 { + db eval { + SELECT p, min(q) FROM t1; + } +} {5 0} +do_test minmax4-1.9 { + db eval { + INSERT INTO t1 VALUES(6,1); + SELECT p, max(q) FROM t1; + } +} {3 4} +do_test minmax4-1.10 { + db eval { + SELECT p, min(q) FROM t1; + } +} {5 0} +do_test minmax4-1.11 { + db eval { + INSERT INTO t1 VALUES(7,NULL); + SELECT p, max(q) FROM t1; + } +} {3 4} +do_test minmax4-1.12 { + db eval { + SELECT p, min(q) FROM t1; + } +} {5 0} +do_test minmax4-1.13 { + db eval { + DELETE FROM t1 WHERE q IS NOT NULL; + SELECT p, max(q) FROM t1; + } +} {7 {}} +do_test minmax4-1.14 { + db eval { + SELECT p, min(q) FROM t1; + } +} {7 {}} + +do_test minmax4-2.1 { + db eval { + CREATE TABLE t2(a,b,c); + INSERT INTO t2 VALUES + (1,null,2), + (1,2,3), + (1,1,4), + (2,3,5); + SELECT a, max(b), c FROM t2 GROUP BY a ORDER BY a; + } +} {1 2 3 2 3 5} +do_test minmax4-2.2 { + db eval { + SELECT a, min(b), c FROM t2 GROUP BY a ORDER BY a; + } +} {1 1 4 2 3 5} +do_test minmax4-2.3 { + db eval { + SELECT a, min(b), avg(b), count(b), c FROM t2 GROUP BY a ORDER BY a DESC; + } +} {2 3 3.0 1 5 1 1 1.5 2 4} +do_test minmax4-2.4 { + db eval { + SELECT a, min(b), max(b), c FROM t2 GROUP BY a ORDER BY a; + } +} {1 1 2 3 2 3 3 5} +do_test minmax4-2.5 { + db eval { + SELECT a, max(b), min(b), c FROM t2 GROUP BY a ORDER BY a; + } +} {1 2 1 4 2 3 3 5} +do_test minmax4-2.6 { + db eval { + SELECT a, max(b), b, max(c), c FROM t2 GROUP BY a ORDER BY a; + } +} {1 2 1 4 4 2 3 3 5 5} +do_test minmax4-2.7 { + db eval { + SELECT a, min(b), b, min(c), c FROM t2 GROUP BY a ORDER BY a; + } +} {1 1 {} 2 2 2 3 3 5 5} + + + +finish_test