-C Performance\soptimization\sfor\sJSON\srendering\slogic.
-D 2023-08-02T16:06:02.660
+C If\sa\squery\shas\san\sORDER\sBY\sclause\sthat\sonly\srefers\sto\sresult\scolumns\sof\sthe\nleft-most\stable\sand\sthe\sleft\smost\stable\sis\sa\sMATERIALIZED\scommon\stable\nexpresion,\sthen\sattempt\sto\spush\sthe\sORDER\sBY\sclause\sdown\sinto\sthe\ssubquery.
+D 2023-08-02T18:20:10.121
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c
F src/resolve.c 37953a5f36c60bea413c3c04efcd433b6177009f508ef2ace0494728912fe2e9
F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97
-F src/select.c abf48be202d35c4f450325b61992e98ac4aa81ed1e29709069432877d3b555d3
+F src/select.c 5d94f21834e0b5960617f5bef266a01675428679422124a0015ff2ed18de0086
F src/shell.c.in 694aaf751f00610381533d4a31c83d142cfc83ef91ef65e2aa6912ace7c39b40
F src/sqlite.h.in 7b07a33d2af82ee974aa91e6294abce0282b2f4c5934b291d2fff961810dd867
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P c4347e4400e96f932ac12f8f22484a2ebce2a578d1b2181977954c432f117bfd
-R a3168d15b44149a629121fdff1d59acd
+P ea0b9aecbaca9a8e784fd2bcb50f78cbdcf4c5cfb45a7700bb222e4cc104c644
+R ee3670aab2a8384c34615ac09a8a53a3
+T *branch * order-by-push-down
+T *sym-order-by-push-down *
+T -sym-trunk *
U drh
-Z 37bd268b83df81194e3e4e7be36a0655
+Z 30f1c0db753d338fceeb02d267e9d680
# Remove this line to create a well-formed Fossil manifest.
return 0;
}
+/*
+** If the ORDER BY clause on the outer query pOuter only refers directly
+** to columns of the subquery pSub, then drop the ORDER BY clause on the
+** outer query and create a new equivalent ORDER BY clause on the
+** subquery.
+**
+** Return non-zero on success. Return zero if the ORDER BY transfer
+** could not occur, either because conditions were not met or because
+** of an OOM.
+*/
+static SQLITE_NOINLINE int transferOrderByIntoSubquery(
+ Parse *pParse, /* Parsing context */
+ int iCursor, /* Cursor number for the subquery */
+ Select *pSub, /* The sub query */
+ Select *pOuter /* The outer query */
+){
+ int i;
+ ExprList *pEL = pOuter->pOrderBy;
+ ExprList *pNew;
+
+ for(i=pEL->nExpr-1; i>=0; i--){
+ Expr *pExpr = pEL->a[i].pExpr;
+ if( pExpr->op!=TK_COLUMN ) return 0;
+ if( pExpr->iTable!=iCursor ) return 0;
+ }
+ /* If we get to this point, it means that the ORDER BY should be
+ ** transferred. */
+ pNew = 0;
+ for(i=0; i<pEL->nExpr; i++){
+ int iCol = pEL->a[0].pExpr->iColumn;
+ pNew = sqlite3ExprListAppend(pParse, pNew,
+ sqlite3ExprDup(pParse->db, pSub->pEList->a[iCol].pExpr, 0));
+ if( pParse->nErr ) break;
+ pNew->a[i].u.x.iOrderByCol = iCol+1;
+ }
+ if( pParse->nErr ){
+ sqlite3ExprListDelete(pParse->db, pNew);
+ return 0;
+ }
+ sqlite3ParserAddCleanup(pParse,
+ (void(*)(sqlite3*,void*))sqlite3ExprListDelete,
+ pOuter->pOrderBy);
+ pOuter->pOrderBy = 0;
+ pSub->pOrderBy = pNew;
+ return 1;
+}
+
+
/*
** Return TRUE (non-zero) if the i-th entry in the pTabList SrcList can
** be implemented as a co-routine. The i-th entry is guaranteed to be
** a MATERIALIZED common table expression is an optimization fence.
*/
if( pItem->fg.isCte && pItem->u2.pCteUse->eM10d==M10d_Yes ){
+ /* If the MATERIALIZED common table expression is the left-most
+ ** term of the outer query, and if it has no ORDER BY clause, but
+ ** there is an ORDER BY clause on the outer query that only references
+ ** the common table expression, then transfer the ORDER BY clause over
+ ** to the common table expression.
+ */
+ if( i==0
+ && pSub->pOrderBy==0
+ && sSort.pOrderBy!=0
+ && transferOrderByIntoSubquery(pParse, pItem->iCursor, pSub, p)
+ ){
+ sSort.pOrderBy = 0;
+ TREETRACE(0x800,pParse,p,
+ ("transfer ORDER BY into left-most subquery"));
+ }
continue;
}