]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Fix a problem with using the push-down optimization on compound SELECTs where compone...
authordan <Dan Kennedy>
Fri, 14 Oct 2022 19:30:34 +0000 (19:30 +0000)
committerdan <Dan Kennedy>
Fri, 14 Oct 2022 19:30:34 +0000 (19:30 +0000)
FossilOrigin-Name: ed14863dd72e35fa3a23320c3d5a8166515faea39a555c28a27b2d35e701eac4

manifest
manifest.uuid
src/select.c
test/collate5.test

index 7c2b8ac79b394ad2dae4719a720ca0fe6fdb6989..df43c1449f1f81397ee1d3714abf0936af6d82f6 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\smissing\sinitializer\sfrom\sthe\sextension\sloader.\s\sFix\sfor\s\ncheck-in\s[d6d449978245b4fa].
-D 2022-10-14T19:21:12.300
+C Fix\sa\sproblem\swith\susing\sthe\spush-down\soptimization\son\scompound\sSELECTs\swhere\scomponent\sSELECTs\suse\sdifferent\scollation\ssequences.\sdbsqlfuzz\s11516f050100243e5a845f5a2b48a90ed2efaf2e.
+D 2022-10-14T19:30:34.777
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -617,7 +617,7 @@ F src/printf.c e99ee9741e79ae3873458146f59644276657340385ade4e76a5f5d1c25793764
 F src/random.c 546d6feb15ec69c1aafe9bb351a277cbb498fd5410e646add673acb805714960
 F src/resolve.c efea4e5fbecfd6d0a9071b0be0d952620991673391b6ffaaf4c277b0bb674633
 F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
-F src/select.c bb18acf4eded647fef88d4d543c673874dbebff516fbeba90a85e6c13f2a58cd
+F src/select.c b795f31a326564ffc448bb26f863c64f0796b2a5e0585a3358aff7877b72ed82
 F src/shell.c.in 2915eaf22bda89ad6533851a051de4773c249185360fe1fc7b4477b8f9063b2c
 F src/sqlite.h.in d9c8a6243fc0a1c270d69db33758e34b810af3462f9bc5b4af113b347e07c69d
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
@@ -848,7 +848,7 @@ F test/collate1.test 71a6f27fdc93a92f14d8ab80c05e1937656a5a03197e1a10157314554d6
 F test/collate2.test 471c6f74573382b89b0f8b88a05256faa52f7964f9e4799e76708a3b1ece6ba4
 F test/collate3.test 89defc49983ddfbf0a0555aca8c0521a676f56a5
 F test/collate4.test c953715fb498b87163e3e73dd94356bff1f317bd
-F test/collate5.test 65d928034d30d2d263a80f6359f7549ee1598ec6
+F test/collate5.test b1dfeff239ea69ee9225832553f423d37a6184eb730cee06f6846ab4e3c6dbef
 F test/collate6.test 8be65a182abaac8011a622131486dafb8076e907
 F test/collate7.test 8ec29d98f3ee4ccebce6e16ce3863fb6b8c7b868
 F test/collate8.test cd9b3d3f999b8520ffaa7cc1647061fc5bab1334
@@ -2031,8 +2031,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 a4423ca234453c14eb40db7fe5943f63b30fd9dc2207388e8a2966733a004e9d
-R 657c639cfdb29ba1717741ed5fd9ad00
-U drh
-Z ac4a2afa493b2e100552976a88fb6b99
+P 565d74c36f2dd4612860c7ee561682c50db28cfa5384346528292019dbfdf86c
+R 75e7f9b91b017a681d1ae11ac5186379
+U dan
+Z 42c84fae9e00b1c8a3d198c324edcd95
 # Remove this line to create a well-formed Fossil manifest.
index 0fb2923565fcaf7f6a5f378e1a8f828319943b38..65ef760f0e4dbe3e3497c3e76da49b0f4ef56492 100644 (file)
@@ -1 +1 @@
-565d74c36f2dd4612860c7ee561682c50db28cfa5384346528292019dbfdf86c
\ No newline at end of file
+ed14863dd72e35fa3a23320c3d5a8166515faea39a555c28a27b2d35e701eac4
\ No newline at end of file
index 2876f48ab8bb89a980391f8b5094b951d3329605..e4424024c83223d7014af2fd73788c4716291cec 100644 (file)
@@ -3750,6 +3750,7 @@ typedef struct SubstContext {
   int iNewTable;            /* New table number */
   int isOuterJoin;          /* Add TK_IF_NULL_ROW opcodes on each replacement */
   ExprList *pEList;         /* Replacement expressions */
+  ExprList *pCList;         /* Collation sequences for replacement expr */
 } SubstContext;
 
 /* Forward Declarations */
@@ -3791,9 +3792,10 @@ static Expr *substExpr(
 #endif
     {
       Expr *pNew;
-      Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
+      int iColumn = pExpr->iColumn;
+      Expr *pCopy = pSubst->pEList->a[iColumn].pExpr;
       Expr ifNullRow;
-      assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
+      assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr );
       assert( pExpr->pRight==0 );
       if( sqlite3ExprIsVector(pCopy) ){
         sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
@@ -3831,11 +3833,16 @@ static Expr *substExpr(
 
         /* Ensure that the expression now has an implicit collation sequence,
         ** just as it did when it was a column of a view or sub-query. */
-        if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
-          CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
-          pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr, 
-              (pColl ? pColl->zName : "BINARY")
+        {
+          CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
+          CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
+                pSubst->pCList->a[iColumn].pExpr
           );
+          if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
+            pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
+                (pColl ? pColl->zName : "BINARY")
+            );
+          }
         }
         ExprClearProperty(pExpr, EP_Collate);
       }
@@ -4028,6 +4035,18 @@ static void renumberCursors(
 }
 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
 
+/*
+** If pSel is not part of a compound SELECT, return a pointer to its
+** expression list. Otherwise, return a pointer to the expression list
+** of the leftmost SELECT in the compound.
+*/
+static ExprList *findLeftmostExprlist(Select *pSel){
+  while( pSel->pPrior ){
+    pSel = pSel->pPrior;
+  }
+  return pSel->pEList;
+}
+
 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
 /*
 ** This routine attempts to flatten subqueries as a performance optimization.
@@ -4580,6 +4599,7 @@ static int flattenSubquery(
       x.iNewTable = iNewParent;
       x.isOuterJoin = isOuterJoin;
       x.pEList = pSub->pEList;
+      x.pCList = findLeftmostExprlist(pSub);
       substSelect(&x, pParent, 0);
     }
   
@@ -5062,6 +5082,7 @@ static int pushDownWhereTerms(
       x.iNewTable = pSrc->iCursor;
       x.isOuterJoin = 0;
       x.pEList = pSubq->pEList;
+      x.pCList = findLeftmostExprlist(pSubq);
       pNew = substExpr(&x, pNew);
 #ifndef SQLITE_OMIT_WINDOWFUNC
       if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
index 5f8697ea51ee34c39ee7028cac52c0d4893efe5b..71d4efe2559b0cb414b6990a8afeb9ac6f15cf03 100644 (file)
@@ -19,6 +19,8 @@
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
 
+set testprefix collate5
+
 
 #
 # Tests are organised as follows:
@@ -26,6 +28,7 @@ source $testdir/tester.tcl
 # collate5-2.* - Compound SELECT
 # collate5-3.* - ORDER BY on compound SELECT
 # collate5-4.* - GROUP BY
+# collate5-5.* - Collation sequence cases
 
 # Create the collation sequence 'TEXT', purely for asthetic reasons. The
 # test cases in this script could just as easily use BINARY.
@@ -289,4 +292,37 @@ do_test collate5-4.3 {
   }
 } {}
 
+#-------------------------------------------------------------------------
+reset_db
+
+do_execsql_test 5.0 {
+  CREATE TABLE t1(a, b COLLATE nocase);
+  CREATE TABLE t2(c, d);
+  INSERT INTO t2 VALUES(1, 'bbb');
+}
+do_execsql_test 5.1 {
+  SELECT * FROM (
+      SELECT a, b FROM t1 UNION ALL SELECT c, d FROM t2
+  ) WHERE b='BbB';
+} {1 bbb}
+
+reset_db
+do_execsql_test 5.2 {
+  CREATE TABLE t1(a,b,c COLLATE NOCASE);
+  INSERT INTO t1 VALUES(NULL,'C','c');
+  CREATE VIEW v2 AS
+    SELECT a,b,c FROM t1 INTERSECT SELECT a,b,b FROM t1
+    WHERE 'eT"3qRkL+oJMJjQ9z0'>=b
+    ORDER BY a,b,c;
+}
+
+do_execsql_test 5.3 {
+  SELECT * FROM v2;
+} { {} C c }
+
+do_execsql_test 5.4 {
+  SELECT * FROM v2 WHERE c='c';
+} { {} C c }
+
+
 finish_test