]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Remove a condition from query flattening that is now taken care of by materialize-order-by
authordrh <>
Wed, 2 Aug 2023 13:29:01 +0000 (13:29 +0000)
committerdrh <>
Wed, 2 Aug 2023 13:29:01 +0000 (13:29 +0000)
the caller.  Factor out the reverse_unordered_selects processing from the
main loop of sqlite3WhereBegin() for performance.

FossilOrigin-Name: f068f105fb158634321bf6401f0774c81059932d213a18b627ae98bcffc10912

manifest
manifest.uuid
src/select.c
src/where.c

index 043e5c2130637bdd147c8847d9c5d99b0e56e701..4664e580ebcdda3a5ef2b24c3ec01737504c6dff 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Simplify\sthe\sselect.c\slogic\sthat\senforces\sthe\soptimization\sfence\saround\na\sMATERIALIZED\scommon\stable\sexpression.\s\sDo\snot\sallow\sthe\nreverse_unordered_selects\ssetting\sto\saffect\sa\sfenced\sCTE\sthat\sitself\shas\nan\sORDER\sBY\sclause.
-D 2023-08-02T12:39:56.675
+C Remove\sa\scondition\sfrom\squery\sflattening\sthat\sis\snow\staken\scare\sof\sby\nthe\scaller.\s\sFactor\sout\sthe\sreverse_unordered_selects\sprocessing\sfrom\sthe\nmain\sloop\sof\ssqlite3WhereBegin()\sfor\sperformance.
+D 2023-08-02T13:29:01.398
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -643,7 +643,7 @@ F src/printf.c e3ba080e2f409f9bfcc8d34724e6fc160e9c718dc92d0548f6b71b8b6f860ce2
 F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c
 F src/resolve.c 37953a5f36c60bea413c3c04efcd433b6177009f508ef2ace0494728912fe2e9
 F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97
-F src/select.c 8a8b7117bed5b7ce8122b611a9a7ff9504c523de3793d3a26661bf8ad81577ef
+F src/select.c abf48be202d35c4f450325b61992e98ac4aa81ed1e29709069432877d3b555d3
 F src/shell.c.in 694aaf751f00610381533d4a31c83d142cfc83ef91ef65e2aa6912ace7c39b40
 F src/sqlite.h.in 7b07a33d2af82ee974aa91e6294abce0282b2f4c5934b291d2fff961810dd867
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
@@ -728,7 +728,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
 F src/wal.c dd843f619ac60d5dadab7109cf402432ba74dde0c301505fd1c202add07659e3
 F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a
 F src/walker.c 7c7ea0115345851c3da4e04e2e239a29983b61fb5b038b94eede6aba462640e2
-F src/where.c d4240edf4c78b94e705f774509f0552ebcf08a1d2ee169fe14d66eccb4dc910a
+F src/where.c b8917792f1e0dbfa28fb29e6cd3d560060d69667be0ba4c491cbc772363264f5
 F src/whereInt.h c7d19902863beadec1d04e66aca39c0bcd60b74f05f0eaa7422c7005dfc5d51a
 F src/wherecode.c 5d77db30a2a3dd532492ae882de114edba2fae672622056b1c7fd61f5917a8f1
 F src/whereexpr.c dc5096eca5ed503999be3bdee8a90c51361289a678d396a220912e9cb73b3c00
@@ -2049,8 +2049,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 b7ef9796f548ce43e06673f86bbec68157aae5e4ee8451e1d87ee5f70af1bb27
-R b2e3c3d0a4d53532b578742c3846e988
+P 165b9bda243777ae7fb6f51ee7ad58771c581b82ef9a70268eb9764e9e6eba6f
+R 2044877763bac67c01e0c206bcedee44
 U drh
-Z 94c05c39e8f919e5e81448014dcce915
+Z 1df9c853e697487617d65f29dca4002a
 # Remove this line to create a well-formed Fossil manifest.
index 9736ae771325e38436eb7bbdd4a12d18c2b74067..bed6656874e872ba73456c783ede2a7841101851 100644 (file)
@@ -1 +1 @@
-165b9bda243777ae7fb6f51ee7ad58771c581b82ef9a70268eb9764e9e6eba6f
\ No newline at end of file
+f068f105fb158634321bf6401f0774c81059932d213a18b627ae98bcffc10912
\ No newline at end of file
index 482d7283c3f9563f3dff9f6f1c94dcb2ed5d7fdf..41e42b68085f7def5a19477380bb4ddf03156719 100644 (file)
@@ -4292,7 +4292,8 @@ static int compoundHasDifferentAffinities(Select *p){
 **        (27b) the subquery is a compound query and the RIGHT JOIN occurs
 **              in any arm of the compound query.  (See also (17g).)
 **
-**  (28)  The subquery is not a MATERIALIZED CTE.
+**  (28)  The subquery is not a MATERIALIZED CTE.  (This is handled
+**        in the caller before ever reaching this routine.)
 **
 **
 ** In this routine, the "p" parameter is a pointer to the outer query.
@@ -4402,9 +4403,9 @@ static int flattenSubquery(
   if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
     return 0;   /* Restriction (27a) */
   }
-  if( pSubitem->fg.isCte && pSubitem->u2.pCteUse->eM10d==M10d_Yes ){
-    return 0;       /* (28) */
-  }
+
+  /* Condition (28) is blocked by the caller */
+  assert( !pSubitem->fg.isCte || pSubitem->u2.pCteUse->eM10d!=M10d_Yes );
 
   /* Restriction (17): If the sub-query is a compound SELECT, then it must
   ** use only the UNION ALL operator. And none of the simple select queries
index 9992b2053f4681fe76f01a175b95e31e545903bb..213df4223024002c4d38c1db13b0561cbd8c46ef 100644 (file)
@@ -5758,6 +5758,28 @@ static SQLITE_NOINLINE void whereAddIndexedExpr(
   }
 }
 
+/*
+** Set the reverse-scan order mask to one for all tables in the query
+** with the exception of MATERIALIZED common table expressions that have
+** their own internal ORDER BY clauses.
+**
+** This implements the PRAGMA reverse_unordered_selects=ON setting.
+** (Also SQLITE_DBCONFIG_REVERSE_SCANORDER).
+*/
+static SQLITE_NOINLINE void whereReverseScanOrder(WhereInfo *pWInfo){
+  int ii;
+  for(ii=0; ii<pWInfo->pTabList->nSrc; ii++){
+    SrcItem *pItem = &pWInfo->pTabList->a[ii];
+    if( !pItem->fg.isCte
+     || pItem->u2.pCteUse->eM10d!=M10d_Yes
+     || NEVER(pItem->pSelect==0)
+     || pItem->pSelect->pOrderBy==0
+    ){
+      pWInfo->revMask |= MASKBIT(ii);
+    }
+  }
+}
+
 /*
 ** Generate the beginning of the loop used for WHERE clause processing.
 ** The return value is a pointer to an opaque structure that contains
@@ -6124,25 +6146,7 @@ WhereInfo *sqlite3WhereBegin(
   }
   assert( pWInfo->pTabList!=0 );
   if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
-    for(ii=0; ii<pWInfo->pTabList->nSrc; ii++){
-      /* The PRAGMA reverse_unordered_selects=ON setting (also accessible
-      ** using SQLITE_DBCONFIG_REVERSE_SCANORDER) means to reverse the scan
-      ** order for any table that is part of a query that does not have an
-      ** ORDER BY clause.
-      **
-      ** Except, do not reverse the output from MATERIALIZED common table
-      ** expression that has an internal ORDER BY clause, because a
-      ** MATERIALIZED common table expression is an optimization fence.
-      */
-      SrcItem *pItem = &pWInfo->pTabList->a[ii];
-      if( !pItem->fg.isCte
-       || pItem->u2.pCteUse->eM10d!=M10d_Yes
-       || NEVER(pItem->pSelect==0)
-       || pItem->pSelect->pOrderBy==0
-      ){
-        pWInfo->revMask |= MASKBIT(ii);
-      }
-    }
+    whereReverseScanOrder(pWInfo);
   }
   if( pParse->nErr ){
     goto whereBeginError;