From: dan Date: Wed, 7 Aug 2019 17:45:37 +0000 (+0000) Subject: Eliminate some more cases of redundant sorting in window-function queries. X-Git-Tag: version-3.30.0~160 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0e3c50c5dc156fd5f05d7284510c5905494f04db;p=thirdparty%2Fsqlite.git Eliminate some more cases of redundant sorting in window-function queries. FossilOrigin-Name: 8158d2aca68c5a253054376fdf1b8eaab2db874f4b93524742be7340e9c50dd5 --- diff --git a/manifest b/manifest index 72aeed451b..397d866523 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Do\snot\smake\sSQLITE_READ\sauthorizer\scalls\sfor\stables\swithout\snames,\sas\sall\nsuch\stables\swill\sbe\sinternal-use-only\stables\sfor\ssubqueries\sand\swhatnot. -D 2019-08-07T13:25:21.372 +C Eliminate\ssome\smore\scases\sof\sredundant\ssorting\sin\swindow-function\squeries. +D 2019-08-07T17:45:37.981 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -613,7 +613,7 @@ F src/where.c b6055f471431fdb8e2fe388789aaac239823f85c3bd394d53be0e0f727e4cd1c F src/whereInt.h 2bb9632f90a1a32952d43a52587209cdd536c3548715d7859bf0c1f9a65a2efd F src/wherecode.c cf885ea2d439af9827c5cbab7d4c12be5c079439b7bd12e97151ccfe088c13c0 F src/whereexpr.c 5e559bdd24b06e3bc2e68f258bf751302954dc1e432daf71fdd8098a71462326 -F src/window.c 89778546eccb05a81daf8e69957feaf8e22af6a3c63393bc3c34930fe07cde8b +F src/window.c 98b2571c66246bddadf42e76da45ed970fe7518a4c9c1ccc8cdace0711bfabba F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2 F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd F test/affinity3.test 6a101af2fc945ce2912f6fe54dd646018551710d @@ -1714,7 +1714,7 @@ F test/window7.tcl 6a1210f05d40ec89c22960213a22cd3f98d4e2f2eb20646c83c8c30d4d761 F test/window7.test ce7f865241fdd1c5c4db869cd7bb2986c3be836bc2e73649a6846dd920f63e0f F test/window8.tcl 9e9a82ae9eea90a4a83481d641a812b974980c38f9247f3b89a6e3c8bed45518 F test/window8.test df187dc19921f7be0ab709d531d681bd80ccaac96a913a89ecee8b272b91d43f -F test/window9.test 2bc9f311a97f1dd27de57636ebd29549fb6729c2be32f56063c0867634ed07aa +F test/window9.test 7ba6c69d3034b51db4ff680e552dbc01e945850a3e2ae74237581cc00f618978 F test/windowerr.tcl f5acd6fbc210d7b5546c0e879d157888455cd4a17a1d3f28f07c1c8a387019e0 F test/windowerr.test a8b752402109c15aa1c5efe1b93ccb0ce1ef84fa964ae1cd6684dd0b3cc1819b F test/windowfault.test 403693d7d951c5473f052f7ecddb61ed15ac9d212f238b8904ea270ba90f83e5 @@ -1838,7 +1838,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 17b3d2218c02a4005d4c96471c452105b54abb25901ae62990b01f0c955135fe -R 7416f927830c666df23d108179a39bca -U drh -Z 25c6c6c7c7a7a6e0ba13d172e2f31a7e +P 193c87fc96f964984a144c1a4506ef9db033ab2b80e64dd4d09d820fef466407 +R cf8e896490e535b9c5c16cd5f2f5a730 +U dan +Z 4641b29bc9e526bd7952f7ad9437097c diff --git a/manifest.uuid b/manifest.uuid index 27384e9f2c..31ed0bd0e7 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -193c87fc96f964984a144c1a4506ef9db033ab2b80e64dd4d09d820fef466407 \ No newline at end of file +8158d2aca68c5a253054376fdf1b8eaab2db874f4b93524742be7340e9c50dd5 \ No newline at end of file diff --git a/src/window.c b/src/window.c index 3b9af199eb..ebcb576bb5 100644 --- a/src/window.c +++ b/src/window.c @@ -934,11 +934,14 @@ int sqlite3WindowRewrite(Parse *pParse, Select *p){ ** redundant, remove the ORDER BY from the parent SELECT. */ pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0); pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1); - if( pSort && p->pOrderBy ){ + if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){ + int nSave = pSort->nExpr; + pSort->nExpr = p->pOrderBy->nExpr; if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){ sqlite3ExprListDelete(db, p->pOrderBy); p->pOrderBy = 0; } + pSort->nExpr = nSave; } /* Assign a cursor number for the ephemeral table used to buffer rows. diff --git a/test/window9.test b/test/window9.test index 65834ed89b..754761b78b 100644 --- a/test/window9.test +++ b/test/window9.test @@ -145,6 +145,32 @@ do_execsql_test 4.1.2 { SELECT b, b=count(*), group_concat(b) OVER () FROM t1 GROUP BY b; } {1 0 1,2 2 1 1,2} +#-------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(a, b, c, d, e); + CREATE INDEX i1 ON t1(a, b, c, d, e); +} + +foreach {tn sql} { + 1 { + SELECT + sum(e) OVER (), + sum(e) OVER (ORDER BY a), + sum(e) OVER (PARTITION BY a ORDER BY b), + sum(e) OVER (PARTITION BY a, b ORDER BY c), + sum(e) OVER (PARTITION BY a, b, c ORDER BY d) + FROM t1; + } + 2 { + SELECT sum(e) OVER (PARTITION BY a ORDER BY b) FROM t1 ORDER BY a; + } +} { + do_test 5.1.$tn { + execsql "EXPLAIN QUERY PLAN $sql" + } {~/ORDER/} +} + finish_test