From: drh <> Date: Fri, 7 May 2021 15:46:36 +0000 (+0000) Subject: Detect misuse of aggregate functions in the ORDER BY clause of a query X-Git-Tag: version-3.36.0~113 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4752bbd87214b4e3e570bd09f1544b34488d7671;p=thirdparty%2Fsqlite.git Detect misuse of aggregate functions in the ORDER BY clause of a query even if the query also contains window functions. FossilOrigin-Name: 0d11d777c8d368f0b6e9faf3afccf7da1b041d303a68782e43aebd713aaecf51 --- diff --git a/manifest b/manifest index e719220cd5..e9cb6a0d30 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fixes\sfor\scksmvfs\sand\srbu\sso\sthat\sthey\swork\stogether. -D 2021-05-06T20:47:49.208 +C Detect\smisuse\sof\saggregate\sfunctions\sin\sthe\sORDER\sBY\sclause\sof\sa\squery\neven\sif\sthe\squery\salso\scontains\swindow\sfunctions. +D 2021-05-07T15:46:37.000 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -634,7 +634,7 @@ F src/where.c beb01392745d9badba63802c9cf18daa9aa2d99c34dd2d21d62bad217e9d70d4 F src/whereInt.h 9248161dd004f625ce5d3841ca9b99fed3fc8d61522cf76340fc5217dbe1375b F src/wherecode.c 4a14b647a68d1a4015139837a31ac756f1ae2f48b6c7ce3e4560c4b5f7ac1d1b F src/whereexpr.c 811f339ca85540157f3a400333ba90237ffbe7a2ba82dac63ce0677f4c4109d0 -F src/window.c 2e092a03ee2e7e6541dd44fa6cb4cd0abdd142fc9c9ed6bac2788daa53316e33 +F src/window.c 0c910a222f357e3e175a998874abd12f3e2f312e10950d304f3d28b0fb6bc509 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2 F test/affinity2.test ce1aafc86e110685b324e9a763eab4f2a73f737842ec3b687bd965867de90627 F test/affinity3.test eecb0dabee4b7765a8465439d5e99429279ffba23ca74a7eae270a452799f9e7 @@ -1769,7 +1769,7 @@ F test/win32heap.test 10fd891266bd00af68671e702317726375e5407561d859be1aa04696f2 F test/win32lock.test fbf107c91d8f5512be5a5b87c4c42ab9fdd54972 F test/win32longpath.test 4baffc3acb2e5188a5e3a895b2b543ed09e62f7c72d713c1feebf76222fe9976 F test/win32nolock.test ac4f08811a562e45a5755e661f45ca85892bdbbc -F test/window1.test 54e160bdeb8b06fb3b97993e0eec45ee4c3294f39ad63007c921c40961298815 +F test/window1.test 5950af3f6fe084b8cc889501b25721f62dcfc9ff548031f40c54b9617d88958b F test/window2.tcl 492c125fa550cda1dd3555768a2303b3effbeceee215293adf8871efc25f1476 F test/window2.test e466a88bd626d66edc3d352d7d7e1d5531e0079b549ba44efb029d1fbff9fd3c F test/window3.tcl acea6e86a4324a210fd608d06741010ca83ded9fde438341cb978c49928faf03 @@ -1912,7 +1912,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 5f12f89154843e83b1d69ea1652e881d583aa329df732af72dd59029cd7eb577 -R 86795aeae5d28acc6a0a1e14eea87e5a -U dan -Z f870463665fd7a903634b8e12e01817e +P f35f6972cdcf16915cc37908b65a3780742a1f75f65e974714b16e86f0c9e3f0 +R d24d9d7d19668c441be4636c06147330 +U drh +Z 057041a9e5f934338ae91fc2d8ade3cd diff --git a/manifest.uuid b/manifest.uuid index b473c0487a..19aa2cbf70 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f35f6972cdcf16915cc37908b65a3780742a1f75f65e974714b16e86f0c9e3f0 \ No newline at end of file +0d11d777c8d368f0b6e9faf3afccf7da1b041d303a68782e43aebd713aaecf51 \ No newline at end of file diff --git a/src/window.c b/src/window.c index f9e2396f76..48a54dcea7 100644 --- a/src/window.c +++ b/src/window.c @@ -941,6 +941,14 @@ static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){ return WRC_Continue; } +static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){ + sqlite3ErrorMsg(pWalker->pParse, + "misuse of aggregate: %s()", pExpr->u.zToken); + } + return WRC_Continue; +} + /* ** If the SELECT statement passed as the second argument does not invoke ** any SQL window functions, this function is a no-op. Otherwise, it @@ -974,6 +982,10 @@ int sqlite3WindowRewrite(Parse *pParse, Select *p){ } sqlite3AggInfoPersistWalkerInit(&w, pParse); sqlite3WalkSelect(&w, p); + if( (p->selFlags & SF_Aggregate)==0 ){ + w.xExprCallback = disallowAggregatesInOrderByCb; + sqlite3WalkExprList(&w, p->pOrderBy); + } p->pSrc = 0; p->pWhere = 0; diff --git a/test/window1.test b/test/window1.test index 604007e515..ccf798caa6 100644 --- a/test/window1.test +++ b/test/window1.test @@ -1915,11 +1915,11 @@ do_execsql_test 60.1 { # the Parse object is destroyed. # reset_db -do_execsql_test 61.1 { +do_catchsql_test 61.1 { CREATE TABLE t1(a); INSERT INTO t1 VALUES(5),(NULL),('seventeen'); SELECT (SELECT max(x)OVER(ORDER BY x) % min(x)OVER(ORDER BY CASE x WHEN 889 THEN x WHEN x THEN x END)) FROM (SELECT (SELECT sum(CAST(a IN(SELECT (SELECT max(x)OVER(ORDER BY CASE x WHEN 889 THEN 299 WHEN 863 THEN 863 END)) FROM (SELECT (SELECT sum(CAST((SELECT (SELECT max(x)OVER(ORDER BY x) / min(x)OVER(ORDER BY CASE x WHEN 889 THEN 299 WHEN -true THEN 863 END)) FROM (SELECT (SELECT sum(CAST(a IN(SELECT (SELECT max(x) & sum ( a )OVER(ORDER BY CASE x WHEN -8 THEN 299 WHEN 863 THEN 863 END)) FROM (SELECT (SELECT sum(CAST(a AS )) FROM t1) AS x FROM t1)) AS t1 )) FROM t1) AS x FROM t1)) AS x )) FROM t1) AS x FROM t1)) AS real)) FROM t1) AS x FROM t1); -} {{} {} {}} +} {1 {misuse of aggregate: sum()}} #------------------------------------------------------------------------- reset_db @@ -2092,4 +2092,16 @@ do_catchsql_test 67.1 { ) } {1 {1st ORDER BY term does not match any column in the result set}} +# 2021-05-07 +# Do not allow aggregate functions in the ORDER BY clause even if +# there are window functions in the result set. +# Forum: /forumpost/540fdfef77 +# +reset_db +do_catchsql_test 68.0 { + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES(0,0),(1,1),(2,4),(3,9),(4,99); + SELECT rowid, a, b, sum(a)OVER() FROM t1 ORDER BY count(b); +} {1 {misuse of aggregate: count()}} + finish_test