From 8f99cb9ad69c8d423c4760fe67b78a0c01a1cff6 Mon Sep 17 00:00:00 2001 From: drh <> Date: Wed, 25 Oct 2023 14:54:16 +0000 Subject: [PATCH] When doing a DISTINCT aggregate that contains an ORDER BY, only the arguments to the aggregate need to be distinct, not the ORDER BY terms. FossilOrigin-Name: d2dbbdf7194bab4e5e3b74d3dbffb012a335829824c775c72dd7347c013d2125 --- manifest | 16 ++++++++-------- manifest.uuid | 2 +- src/expr.c | 3 ++- src/select.c | 8 ++++++-- test/aggorderby.test | 17 +++++++++++++++++ 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/manifest b/manifest index 6d4c13160e..1fc4f1f48b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sthe\samalgamation\sgenerator\sso\sthat\swhen\sit\sis\sreporting\sthe\sFossil\nversion\snumber\sin\sthe\sheader\scomment\sof\sthe\samalgamation,\sit\sdoes\snot\nelide\sthe\sfirst\shex\sdigit\sof\sthe\sversion\shash. -D 2023-10-25T12:58:06.512 +C When\sdoing\sa\sDISTINCT\saggregate\sthat\scontains\san\sORDER\sBY,\sonly\sthe\narguments\sto\sthe\saggregate\sneed\sto\sbe\sdistinct,\snot\sthe\sORDER\sBY\sterms. +D 2023-10-25T14:54:16.468 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -671,7 +671,7 @@ F src/date.c eebc54a00e888d3c56147779e9f361b77d62fd69ff2008c5373946aa1ba1d574 F src/dbpage.c 80e46e1df623ec40486da7a5086cb723b0275a6e2a7b01d9f9b5da0f04ba2782 F src/dbstat.c 3b677254d512fcafd4d0b341bf267b38b235ccfddbef24f9154e19360fa22e43 F src/delete.c cb766727c78e715f9fb7ec8a7d03658ed2a3016343ca687acfcec9083cdca500 -F src/expr.c 072dc4029ccfb4be3d227b559d6d531c46c5fc95cd89f2db17a2e154a3144d71 +F src/expr.c 2ad50ae13cc6402973593eebd1f9af2c0915d5f7cc2c2f4f2f3472a9a7aeddce F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007 F src/fkey.c a47610f0a5c6cb0ad79f8fcef039c01833dec0c751bb695f28dc0ec6a4c3ba00 F src/func.c 472f6dcfa39cf54f89a6aec76c79c225fb880a6c14469c15d361331662b9bf43 @@ -721,7 +721,7 @@ F src/printf.c 9da63b9ae1c14789bcae12840f5d800fd9302500cd2d62733fac77f0041b4750 F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c F src/resolve.c 31229276a8eb5b5de1428cd2d80f6f1cf8ffc5248be25e47cf575df12f1b8f23 F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97 -F src/select.c f9d8ece7f0742d7b835efa9590ccda4eccee5b9def7581ec94f556e3c52efe51 +F src/select.c 845936a85dd39be43a7b76aa0a5c1e68105bf0b98e4986124f78052254e3fe5b F src/shell.c.in acc452c414fddd10289d165be3c89a7a2c36c919def04c93fb7dd11ac022e6ed F src/sqlite.h.in 81c70644aeef9c974f72c9cadeb505ebb9441d2f6db594c018604ae935a12e6e F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 @@ -817,7 +817,7 @@ F test/affinity3.test f094773025eddf31135c7ad4cde722b7696f8eb07b97511f98585addf2 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 F test/aggfault.test 777f269d0da5b0c2524c7ff6d99ae9a93db4f1b1839a914dd2a12e3035c29829 F test/aggnested.test 2e738bfe2980df301a782f6e7bbf9459266f64f7e72f58f3b5c843bf897c568c -F test/aggorderby.test 4a4b2d49532124fbb799a2c014e5134d0e6db2345b19286d027e227e0f383dfa +F test/aggorderby.test e6b98dbbf3ababa96892435d387de2dcf602ef02c2b848d2d817473066f154ba F test/alias.test 4529fbc152f190268a15f9384a5651bbbabc9d87 F test/all.test 2ecb8bbd52416642e41c9081182a8df05d42c75637afd4488aace78cc4b69e13 F test/alter.test 403a7f8842457044a994d0ffb42963d6e84fcfbf5e8f54556063b25d966cd454 @@ -2138,8 +2138,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P bc8afa3f15954bb35f65dbf940bf069de5e14d333036676c24430cf17b658d05 -R 6ec86e3a31586d43a486c38d42879355 +P cc66f526bd58c110cac027583f636099fa05b2b56d7ea9807b554cd950c1bf3c +R 2001118bd227fdbabf4cf18d4c63742c U drh -Z 0c4d685f28f353f462c813fc0dbcad7d +Z 9e38892552611c3668b16789a65f9f49 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 83cc6773bf..2a3eea65c3 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -cc66f526bd58c110cac027583f636099fa05b2b56d7ea9807b554cd950c1bf3c \ No newline at end of file +d2dbbdf7194bab4e5e3b74d3dbffb012a335829824c775c72dd7347c013d2125 \ No newline at end of file diff --git a/src/expr.c b/src/expr.c index 010c8ce6ab..e071c96908 100644 --- a/src/expr.c +++ b/src/expr.c @@ -6806,16 +6806,17 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ pItem->iOBTab = pParse->nTab++; pOBList = pExpr->pLeft->x.pList; assert( pOBList->nExpr>0 ); + assert( pItem->bOBUnique==0 ); if( pOBList->nExpr==1 && nArg==1 && sqlite3ExprCompare(0,pOBList->a[0].pExpr, pExpr->x.pList->a[0].pExpr,0)==0 ){ pItem->bOBPayload = 0; + pItem->bOBUnique = ExprHasProperty(pExpr, EP_Distinct); }else{ pItem->bOBPayload = 1; } - pItem->bOBUnique = ExprHasProperty(pExpr, EP_Distinct); }else{ pItem->iOBTab = -1; } diff --git a/src/select.c b/src/select.c index 29df3325f3..5530321744 100644 --- a/src/select.c +++ b/src/select.c @@ -6771,6 +6771,7 @@ static void updateAccumulator( int addrNext = 0; int regAgg; int regAggSz = 0; + int regDistinct = 0; ExprList *pList; assert( ExprUseXList(pF->pFExpr) ); assert( !IsWindowFunc(pF->pFExpr) ); @@ -6820,6 +6821,7 @@ static void updateAccumulator( } regAggSz++; /* One extra register to hold result of MakeRecord */ regAgg = sqlite3GetTempRange(pParse, regAggSz); + regDistinct = regAgg; sqlite3ExprCodeExprList(pParse, pOBList, regAgg, 0, SQLITE_ECEL_DUP); jj = pOBList->nExpr; if( !pF->bOBUnique ){ @@ -6827,11 +6829,13 @@ static void updateAccumulator( jj++; } if( pF->bOBPayload ){ - sqlite3ExprCodeExprList(pParse, pList, regAgg+jj, 0, SQLITE_ECEL_DUP); + regDistinct = regAgg+jj; + sqlite3ExprCodeExprList(pParse, pList, regDistinct, 0, SQLITE_ECEL_DUP); } }else if( pList ){ nArg = pList->nExpr; regAgg = sqlite3GetTempRange(pParse, nArg); + regDistinct = regAgg; sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP); }else{ nArg = 0; @@ -6842,7 +6846,7 @@ static void updateAccumulator( addrNext = sqlite3VdbeMakeLabel(pParse); } pF->iDistinct = codeDistinct(pParse, eDistinctType, - pF->iDistinct, addrNext, pList, regAgg); + pF->iDistinct, addrNext, pList, regDistinct); } if( pF->iOBTab>=0 ){ /* Insert a new record into the ORDER BY table */ diff --git a/test/aggorderby.test b/test/aggorderby.test index 61adb9ae14..f94c5ec898 100644 --- a/test/aggorderby.test +++ b/test/aggorderby.test @@ -102,4 +102,21 @@ do_execsql_test aggorderby-7.1 { json_group_array(x ORDER BY y NULLS LAST, x) FROM c; } {[2,4,5,3,1] [5,3,1,2,4]} +# The DISTINCT only applies to the function arguments, not to the +# ORDER BY arguments. +# +do_execsql_test aggorderby-8.0 { + WITH c(x,y,z) AS (VALUES('a',4,5),('b',3,6),('c',2,7),('c',1,8)) + SELECT group_concat(DISTINCT x ORDER BY y, z) FROM c; +} {c,b,a} +do_execsql_test aggorderby-8.1 { + WITH c(x,y,z) AS (VALUES('a',4,5),('b',3,6),('b',2,7),('c',1,8)) + SELECT group_concat(DISTINCT x ORDER BY y, z) FROM c; +} {c,b,a} +do_execsql_test aggorderby-8.2 { + WITH c(x,y) AS (VALUES(1,1),(2,2),(3,3),(3,4),(3,5),(3,6)) + SELECT sum(DISTINCT x ORDER BY y) FROM c; +} 6 + + finish_test