]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Call the query flattener while processing the parent query. Previously, it was called...
authordanielk1977 <danielk1977@noemail.net>
Mon, 30 Jun 2008 18:12:28 +0000 (18:12 +0000)
committerdanielk1977 <danielk1977@noemail.net>
Mon, 30 Jun 2008 18:12:28 +0000 (18:12 +0000)
FossilOrigin-Name: 6fcb3bffe26ae1c21c72ce9019f1db1c118094a4

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

index b4bdc68a0a6e95d54a59cbc55a4ea3624f8bf987..643ffdacdf47029d2bb83dfa795662bb8ffb57c4 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Changes\sto\sloadext.test\sso\sthat\sit\sworks\son\sosx\sas\swell\sas\slinux.\s(CVS\s5329)
-D 2008-06-30T15:09:29
+C Call\sthe\squery\sflattener\swhile\sprocessing\sthe\sparent\squery.\sPreviously,\sit\swas\scalled\swhile\sprocessing\sthe\ssub-queries.\s(CVS\s5330)
+D 2008-06-30T18:12:28
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in 325dfac0a0dd1cb4d975f1ace6453157892e6042
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -140,7 +140,7 @@ F src/pragma.c 9a95f5b3708f6d3ddd987eab5f369a19ffcb6795
 F src/prepare.c aba51dad52308e3d9d2074d8ff4e612e7f1cab51
 F src/printf.c 8b063da9dcde26b7c500a01444b718d86f21bc6e
 F src/random.c 5c754319d38abdd6acd74601ee0105504adc508a
-F src/select.c 5b213de002382aeaac5c9c2d3be7c98900bc3056
+F src/select.c afaac3f04b922edb8af07f05763b63c00ddc4a5a
 F src/shell.c 484e7297e066f22830f9c15d7abbcdd2acb097b0
 F src/sqlite.h.in 76c144d23f8824e8811e837e9396b9f1361f5902
 F src/sqlite3ext.h 1e3887c9bd3ae66cb599e922824b04cd0d0f2c3e
@@ -548,7 +548,7 @@ F test/vacuum2.test d3b9691541fe6ed5c711f547a1c7d70e9760ac6f
 F test/vacuum3.test 6149b24613f487915ca9cedfbb9147d5759714ec
 F test/varint.test ab7b110089a08b9926ed7390e7e97bdefeb74102
 F test/veryquick.test fdd8108de66eec378291d9a0e46e30d129eef1ca
-F test/view.test 4864e3841ab3213a95297718b94d5d6a8d3bc78b
+F test/view.test 041ccaf4c96f6c90d564c9ea37d1c1fa21fdb470
 F test/vtab1.test 47092e3ac59141f08e09a6c0cc0df8997242bc79
 F test/vtab2.test 1da49b015582965a8fc386aa23d051a5a622b08e
 F test/vtab3.test f38d6d7d19f08bffdadce4d5b8cba078f8118587
@@ -595,7 +595,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P ba8819a6f3dbc45d88346789de42971aacc0d246
-R d9ae76ff4c185320bbb89c9f9139919a
+P 189cd85413a2e00696752bb82e7a442e86e621ac
+R d620881f6cbbbbefdfa4e9725f1fbb87
 U danielk1977
-Z 00e6f564f2a763edcec24b97ea9019b8
+Z 6869d5a04e97a4a4d7edf1da8de9ca2a
index 1902737b61c5716cf70c1ca8ac3ba0c9e790bf24..a4cb46a6c734ed148384858cc63f00a8c4489306 100644 (file)
@@ -1 +1 @@
-189cd85413a2e00696752bb82e7a442e86e621ac
\ No newline at end of file
+6fcb3bffe26ae1c21c72ce9019f1db1c118094a4
\ No newline at end of file
index ddd03726c5c874c7ed8a6b7131def9387fa81956..55cf08e966d304569d44ed485c9f34341ffe2203 100644 (file)
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** to handle SELECT statements in SQLite.
 **
-** $Id: select.c,v 1.439 2008/06/27 00:52:45 drh Exp $
+** $Id: select.c,v 1.440 2008/06/30 18:12:28 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
@@ -3670,6 +3670,7 @@ int sqlite3Select(
   }
   p->pOrderBy = pOrderBy;
 
+
 #ifndef SQLITE_OMIT_COMPOUND_SELECT
   /* If there is are a sequence of queries, do the earlier ones first.
   */
@@ -3696,11 +3697,7 @@ int sqlite3Select(
   /* Make local copies of the parameters for this query.
   */
   pTabList = p->pSrc;
-  pWhere = p->pWhere;
-  pGroupBy = p->pGroupBy;
-  pHaving = p->pHaving;
   isAgg = p->isAgg;
-  isDistinct = p->isDistinct;
   pEList = p->pEList;
   if( pEList==0 ) goto select_end;
 
@@ -3734,19 +3731,21 @@ int sqlite3Select(
   */
 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
   for(i=0; i<pTabList->nSrc; i++){
-    const char *zSavedAuthContext = 0;
-    int needRestoreContext;
     struct SrcList_item *pItem = &pTabList->a[i];
     SelectDest dest;
+    Select *pSub = pItem->pSelect;
 
-    if( pItem->pSelect==0 || pItem->isPopulated ) continue;
-    if( pItem->zName!=0 ){
-      zSavedAuthContext = pParse->zAuthContext;
+    if( pSub==0 || pItem->isPopulated ) continue;
+    if( pItem->zName!=0 ){   /* An sql view */
+      const char *zSavedAuthContext = pParse->zAuthContext;
       pParse->zAuthContext = pItem->zName;
-      needRestoreContext = 1;
-    }else{
-      needRestoreContext = 0;
+      rc = sqlite3SelectResolve(pParse, pSub, 0);
+      pParse->zAuthContext = zSavedAuthContext;
+      if( rc ){
+        goto select_end;
+      }
     }
+
     /* Increment Parse.nHeight by the height of the largest expression
     ** tree refered to by this, the parent select. The child select
     ** may contain expression trees of at most
@@ -3755,36 +3754,32 @@ int sqlite3Select(
     ** an exact limit.
     */
     pParse->nHeight += sqlite3SelectExprHeight(p);
-    sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
-    sqlite3Select(pParse, pItem->pSelect, &dest, p, i, &isAgg, 0);
+
+    /* Check to see if the subquery can be absorbed into the parent. */
+    if( !pSub->pPrior && flattenSubquery(db, p, i, isAgg, pSub->isAgg) ){
+      if( pSub->isAgg ){
+        p->isAgg = isAgg = 1;
+      }
+      i = -1;
+    }else{
+      sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
+      sqlite3Select(pParse, pSub, &dest, p, i, &isAgg, 0);
+    }
     if( db->mallocFailed ){
       goto select_end;
     }
     pParse->nHeight -= sqlite3SelectExprHeight(p);
-    if( needRestoreContext ){
-      pParse->zAuthContext = zSavedAuthContext;
-    }
     pTabList = p->pSrc;
-    pWhere = p->pWhere;
     if( !IgnorableOrderby(pDest) ){
       pOrderBy = p->pOrderBy;
     }
-    pGroupBy = p->pGroupBy;
-    pHaving = p->pHaving;
-    isDistinct = p->isDistinct;
-  }
-#endif
-
-  /* Check to see if this is a subquery that can be "flattened" into its parent.
-  ** If flattening is a possiblity, do so and return immediately.  
-  */
-#ifndef SQLITE_OMIT_VIEW
-  if( pParent && pParentAgg &&
-      flattenSubquery(db, pParent, parentTab, *pParentAgg, isAgg) ){
-    if( isAgg ) *pParentAgg = 1;
-    goto select_end;
   }
+  pEList = p->pEList;
 #endif
+  pWhere = p->pWhere;
+  pGroupBy = p->pGroupBy;
+  pHaving = p->pHaving;
+  isDistinct = p->isDistinct;
 
   /* If possible, rewrite the query to use GROUP BY instead of DISTINCT.
   ** GROUP BY may use an index, DISTINCT never does.
index edc37c392753871e64676693e0a58fec3f2e5131..f2f2ab68f82b579f915408c4f06f679642679034 100644 (file)
@@ -11,7 +11,7 @@
 # This file implements regression tests for SQLite library.  The
 # focus of this file is testing VIEW statements.
 #
-# $Id: view.test,v 1.35 2008/01/25 15:04:50 drh Exp $
+# $Id: view.test,v 1.36 2008/06/30 18:12:28 danielk1977 Exp $
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
 
@@ -514,5 +514,23 @@ do_test view-17.2 {
   }
 } {1 {no such view: main.nosuchview}}
 
+do_test view-18.1 {
+  execsql {
+    DROP VIEW t1;
+    DROP TABLE t1;
+    CREATE TABLE t1(a, b, c);
+    INSERT INTO t1 VALUES(1, 2, 3);
+    INSERT INTO t1 VALUES(4, 5, 6);
+
+    CREATE VIEW vv1 AS SELECT * FROM t1;
+    CREATE VIEW vv2 AS SELECT * FROM vv1;
+    CREATE VIEW vv3 AS SELECT * FROM vv2;
+    CREATE VIEW vv4 AS SELECT * FROM vv3;
+    CREATE VIEW vv5 AS SELECT * FROM vv4;
+
+    SELECT * FROM vv5;
+  }
+} {1 2 3 4 5 6}
+
 
 finish_test