From 017bdf7838183f6978796c9ae852895ee26e59ff Mon Sep 17 00:00:00 2001 From: drh <> Date: Thu, 13 Apr 2023 14:50:50 +0000 Subject: [PATCH] Fix a code-generator issue associated with very unusual use of window functions. Both the expr.c or the window.c changes will each independently fix the problem. They are both included in this patch for defense in depth. [forum:/forumpost/0d48347967|Forum post 0d48347967]. FossilOrigin-Name: 1ba22631a7831e3562eda0eb6a5edf7f009c85c7ab4451d9eacd13ef0fb6036a --- manifest | 16 ++++++++-------- manifest.uuid | 2 +- src/expr.c | 2 +- src/window.c | 1 + test/window1.test | 17 +++++++++++++++++ 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/manifest b/manifest index 725c664488..a51af72cb2 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Unwrap\sthe\sloop\sin\sthe\sWAL\shash\sfunction. -D 2023-04-12T20:23:03.475 +C Fix\sa\scode-generator\sissue\sassociated\swith\svery\sunusual\suse\sof\swindow\nfunctions.\s\sBoth\sthe\sexpr.c\sor\sthe\swindow.c\schanges\swill\seach\nindependently\sfix\sthe\sproblem.\s\sThey\sare\sboth\sincluded\sin\sthis\spatch\sfor\ndefense\sin\sdepth.\s\s[forum:/forumpost/0d48347967|Forum\spost\s0d48347967]. +D 2023-04-13T14:50:50.896 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -575,7 +575,7 @@ F src/date.c f21815ca7172ce073db3163ac54c8d9f2841077165c1a6123b4d1c376a0c7ec7 F src/dbpage.c f3eea5f7ec47e09ee7da40f42b25092ecbe961fc59566b8e5f705f34335b2387 F src/dbstat.c ec92074baa61d883de58c945162d9e666c13cd7cf3a23bc38b4d1c4d0b2c2bef F src/delete.c a9c6d3f51c0a31e9b831e0a0580a98d702904b42d216fee530940e40dec34873 -F src/expr.c 86f843dba99f1d60836b0b9fa7f595ff2723d902416a4c7749217e950160f17c +F src/expr.c 6353f4d92d9f67ec3466d8e6978cd31a45e34cb755c4d11e689077f03f7c0a15 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007 F src/fkey.c 722f20779f5342a787922deded3628d8c74b5249cab04098cf17ee2f2aaff002 F src/func.c d187be57a886ddf4e6b7ef584a494361899be3df5eee6d4a747b68ff4aff4122 @@ -714,7 +714,7 @@ F src/where.c ef9e644d1d76e86f68c941eb05d30a98fc0811eeef2a110906d81fedde81b6bf F src/whereInt.h e25203e5bfee149f5f1225ae0166cfb4f1e65490c998a024249e98bb0647377c F src/wherecode.c 85790d7e5365ac41085713331ce52e4343586ad3d37d218ffe00572357baa62b F src/whereexpr.c 1dfda1695e4480c24248157df55bb4d66c732dc8d14ac16b4f076bb15de93d63 -F src/window.c 76a27cff9ea2ded0c2c3527187029259440fabcc4cc4c07b11d942c78494a614 +F src/window.c e075ea85bea322e30e361fa6e69eddba74f461e99e2a564dc09973f8a1fb27d9 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2 F test/affinity2.test ce1aafc86e110685b324e9a763eab4f2a73f737842ec3b687bd965867de90627 F test/affinity3.test f094773025eddf31135c7ad4cde722b7696f8eb07b97511f98585addf2a510a9 @@ -1902,7 +1902,7 @@ F test/win32heap.test 10fd891266bd00af68671e702317726375e5407561d859be1aa04696f2 F test/win32lock.test e0924eb8daac02bf80e9da88930747bd44dd9b230b7759fed927b1655b467c9c F test/win32longpath.test 4baffc3acb2e5188a5e3a895b2b543ed09e62f7c72d713c1feebf76222fe9976 F test/win32nolock.test ac4f08811a562e45a5755e661f45ca85892bdbbc -F test/window1.test c607e30741c034a1e88c104f97f107f07489f538a65c0f56ccce97e7a956065a +F test/window1.test 57a2107dd1a869d857be560927f9424486da98e849256700ff7e90adbe164f38 F test/window2.tcl 492c125fa550cda1dd3555768a2303b3effbeceee215293adf8871efc25f1476 F test/window2.test e466a88bd626d66edc3d352d7d7e1d5531e0079b549ba44efb029d1fbff9fd3c F test/window3.tcl acea6e86a4324a210fd608d06741010ca83ded9fde438341cb978c49928faf03 @@ -2052,8 +2052,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 2f7a36d2c374100019bd9d38aea51ea10cfcf9b1f0330ba084a75f0773df8ebf -R 0a0fe6e09d894bc0529190e100b44f7b +P eb94ae13206762a42ddad12e0d1461df83e9759e44a369f188cd3aad82f211c4 +R 790521fc0147ba246247d1806b2d1934 U drh -Z 3c22931f459153fffc356af79809978d +Z 4c16acea7a283d44dd652b716e84bf87 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 6df6e5eb70..22a4facc92 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -eb94ae13206762a42ddad12e0d1461df83e9759e44a369f188cd3aad82f211c4 \ No newline at end of file +1ba22631a7831e3562eda0eb6a5edf7f009c85c7ab4451d9eacd13ef0fb6036a \ No newline at end of file diff --git a/src/expr.c b/src/expr.c index 00c67a4e68..ebaf13af17 100644 --- a/src/expr.c +++ b/src/expr.c @@ -6493,7 +6493,7 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ } /* endif pExpr->iTable==pItem->iCursor */ } /* end loop over pSrcList */ } - return WRC_Prune; + return WRC_Continue; } case TK_AGG_FUNCTION: { if( (pNC->ncFlags & NC_InAggFunc)==0 diff --git a/src/window.c b/src/window.c index 56de38ba39..a8081aa244 100644 --- a/src/window.c +++ b/src/window.c @@ -785,6 +785,7 @@ static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){ } /* no break */ deliberate_fall_through + case TK_IF_NULL_ROW: case TK_AGG_FUNCTION: case TK_COLUMN: { int iCol = -1; diff --git a/test/window1.test b/test/window1.test index 5678dbb89b..05ceab073c 100644 --- a/test/window1.test +++ b/test/window1.test @@ -2330,4 +2330,21 @@ do_catchsql_test 75.1 { GROUP BY a1.a; } {1 {misuse of aggregate: count()}} +# 2023-04-13 https://sqlite.org/forum/forumpost/0d48347967 +reset_db +do_execsql_test 76.0 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1(a,b) VALUES (111,222),(111,223),(118,229); + CREATE INDEX t1a ON t1(a); + CREATE TABLE t2(x INT); + INSERT INTO t2 VALUES (333),(444),(555); +} +do_execsql_test 76.1 { + SELECT c, (SELECT c + sum(1) OVER ()) AS "res" + FROM t2 LEFT JOIN (SELECT +a AS c FROM t1) AS v1 ON true + GROUP BY c + ORDER by c; +} {111 112 118 119} +# ^^^^^^^^^^^^^^^^^-- results verified against PG 14.2 + finish_test