From: dan Date: Wed, 19 May 2021 14:49:51 +0000 (+0000) Subject: Allow aggregate sub-selects within ORDER BY and PARTITION BY clauses of window frame... X-Git-Tag: version-3.36.0~86 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3d691fd9ff63368d78125e0cee60601c90a2853c;p=thirdparty%2Fsqlite.git Allow aggregate sub-selects within ORDER BY and PARTITION BY clauses of window frame definitions. FossilOrigin-Name: 3daab94977af5e8a95690acc555540311403d890a3261a9757c633fcaaf428a9 --- diff --git a/manifest b/manifest index 3b0f14fef6..0aa7c7bbf4 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Improved\scolumn\sname\sand\scolumn\stype\sdetermination\sfor\sthe\sRETURNING\sclause. -D 2021-05-19T12:17:03.024 +C Allow\saggregate\ssub-selects\swithin\sORDER\sBY\sand\sPARTITION\sBY\sclauses\sof\swindow\sframe\sdefinitions. +D 2021-05-19T14:49:51.085 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -634,7 +634,7 @@ F src/where.c 32f41c3c93c6785e0077e3a2cdc669c3ccfe70173787847be77f294c18fc7dc3 F src/whereInt.h 9248161dd004f625ce5d3841ca9b99fed3fc8d61522cf76340fc5217dbe1375b F src/wherecode.c 110ed13049e0f1dc27e9dd942eb870417b36480cb7819302f5804cbcf9330b0e F src/whereexpr.c 5a9c9f5d2dac4bcdcaae3035034b4667523f731df228e0bb1d4efc669efa9da5 -F src/window.c 0c910a222f357e3e175a998874abd12f3e2f312e10950d304f3d28b0fb6bc509 +F src/window.c ce5e73ab88a8527d268673906bf89cbe58c61bca8d54d38ed8c33c3220a276ee F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2 F test/affinity2.test ce1aafc86e110685b324e9a763eab4f2a73f737842ec3b687bd965867de90627 F test/affinity3.test eecb0dabee4b7765a8465439d5e99429279ffba23ca74a7eae270a452799f9e7 @@ -1770,7 +1770,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 138f328242eca1523154eca116b23033a688fcf3e6c01c572373df31c1e90029 +F test/window1.test 7cb66f49d30d89ca9d28dc20f7f1d85229365651e8e6211f7da1448740866511 F test/window2.tcl 492c125fa550cda1dd3555768a2303b3effbeceee215293adf8871efc25f1476 F test/window2.test e466a88bd626d66edc3d352d7d7e1d5531e0079b549ba44efb029d1fbff9fd3c F test/window3.tcl acea6e86a4324a210fd608d06741010ca83ded9fde438341cb978c49928faf03 @@ -1781,8 +1781,8 @@ F test/window5.test d328dd18221217c49c144181975eea17339eaeaf0e9aa558cee3afb84652 F test/window6.test f8d674254b23289cc17c84d79dec7eda7caa1dfb7836c43122cfdf3640d1df32 F test/window7.tcl 6a1210f05d40ec89c22960213a22cd3f98d4e2f2eb20646c83c8c30d4d76108f F test/window7.test 1d31276961ae7801edc72173edaf7593e3cbc79c06d1f1f09e20d8418af403cd -F test/window8.tcl 3d3c206107b92e173504daa20617ff85d8d3a7beb33e4d209356a7c47cfcb592 -F test/window8.test 2cdfaa1fd9ea591aea1a917ff361ff1a1aabe270f566244286e9d1d9c8c951a0 +F test/window8.tcl 5e02e41d9d9a80f597063aed1a381eb19d1d0ef677a4f0df352c5365cf23f79c +F test/window8.test 4ab16817414af0c904abe2ebdf88eb6c2b00058b84f9748c6174ff11fc45f1ed F test/window9.test 349c71eab4288a1ffc19e2f65872ec2c37e6cf8a1dda2ad300364b7450ae4836 F test/windowA.test 6d63dc1260daa17141a55007600581778523a8b420629f1282d2acfc36af23be F test/windowB.test 6e601f8178ba8ba28b2f19e74fe613815084bb4a8d2ad942defc7d42e191e521 @@ -1913,7 +1913,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 1155696c700862de1a8b1318bc41cd5cd01dec1af2c7720d8ef1e5c3321c425d -R 7a418ba4927ef1a44dce5080a3f72ff2 -U drh -Z b65c23573c14276e5eb048c11a67185b +P 699c33990a9438f28673ecf34f1e521d1af0b01c6ee30a608c0c91d2d593590e +R 0588b9cafca27fd37e2a788a361f330d +U dan +Z 63f7d84596bbfc34c19eb092e10320fc diff --git a/manifest.uuid b/manifest.uuid index 49f6d0d86f..cead1d48d8 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -699c33990a9438f28673ecf34f1e521d1af0b01c6ee30a608c0c91d2d593590e \ No newline at end of file +3daab94977af5e8a95690acc555540311403d890a3261a9757c633fcaaf428a9 \ No newline at end of file diff --git a/src/window.c b/src/window.c index 48a54dcea7..a997b9c6c9 100644 --- a/src/window.c +++ b/src/window.c @@ -984,6 +984,7 @@ int sqlite3WindowRewrite(Parse *pParse, Select *p){ sqlite3WalkSelect(&w, p); if( (p->selFlags & SF_Aggregate)==0 ){ w.xExprCallback = disallowAggregatesInOrderByCb; + w.xSelectCallback = 0; sqlite3WalkExprList(&w, p->pOrderBy); } diff --git a/test/window1.test b/test/window1.test index a75489a38c..f4b573d298 100644 --- a/test/window1.test +++ b/test/window1.test @@ -1919,7 +1919,55 @@ 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); +} {0 {{} {} {}}} + +foreach tn {1 2} { + if {$tn==2} { optimization_control db query-flattener 0 } + do_catchsql_test 61.2.$tn { + SELECT + (SELECT max(x)OVER(ORDER BY x) / min(x) OVER() ) + FROM ( + SELECT (SELECT sum(a) FROM t1 ) AS x FROM t1 + ) + + } {0 {1.0 1.0 1.0}} +} + +reset_db +optimization_control db all 0 +do_execsql_test 61.3.0 { + CREATE TABLE t1(a); + CREATE TABLE t2(y); +} + +do_execsql_test 61.3.1 { + SELECT ( + SELECT count(a) OVER ( ORDER BY (SELECT sum(y) FROM t2) ) + + total(a) OVER() + ) + FROM t1 +} {} +do_execsql_test 61.4.2 { + SELECT ( + SELECT count(a) OVER ( ORDER BY sum(a) ) + + total(a) OVER() + ) + FROM t1 +} {0.0} + +do_catchsql_test 61.4.3 { + SELECT + sum(a) OVER ( ORDER BY a ) + FROM t1 + ORDER BY (SELECT sum(a) FROM t2) } {1 {misuse of aggregate: sum()}} +do_execsql_test 61.4.4 { + SELECT + sum(a) OVER ( ORDER BY a ) + FROM t1 + ORDER BY (SELECT sum(y) FROM t2) +} + #------------------------------------------------------------------------- reset_db diff --git a/test/window8.tcl b/test/window8.tcl index d07777d74f..69ad0ad263 100644 --- a/test/window8.tcl +++ b/test/window8.tcl @@ -472,6 +472,22 @@ execsql_test 8.4 { ); } +========== + +execsql_test 9.1 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a INTEGER); + CREATE TABLE t2(y INTEGER); +} + +execsql_test 9.2 { + SELECT ( + SELECT max(a) OVER ( ORDER BY (SELECT sum(a) FROM t1) ) + + min(a) OVER() + ) + FROM t1 +} finish_test diff --git a/test/window8.test b/test/window8.test index 167cba8e65..0c5d39badb 100644 --- a/test/window8.test +++ b/test/window8.test @@ -6511,7 +6511,6 @@ do_execsql_test 8.3 { ) FROM tx; } {2 6 12 1 4 9} -breakpoint do_execsql_test 8.4 { WITH map2 AS ( SELECT * FROM map @@ -6524,4 +6523,21 @@ do_execsql_test 8.4 { ); } {2 6 12 1 4 9} +#========================================================================== + +do_execsql_test 9.1 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a INTEGER); + CREATE TABLE t2(y INTEGER); +} {} + +do_execsql_test 9.2 { + SELECT ( + SELECT max(a) OVER ( ORDER BY (SELECT sum(a) FROM t1) ) + + min(a) OVER() + ) + FROM t1 +} {} + finish_test