/ Check-in [3cacc4b9]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Renumber the Select.selId values in the copies of SELECT statements that implement VIEWs when the VIEW is expanded, so that when the same VIEW is used twice in the same join, each expansion as a distinct selId. This fixes ticket [ce823231949d3abf42453c8].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3cacc4b940fd69776d930deec9512df47a2f22cb04fb955e354a0b25bdec287c
User & Date: drh 2019-05-22 22:49:23
Context
2019-05-22
23:12
New test case for check-in [74ef97bf51dd531a] that takes the fix in the previous check-in into account. check-in: cb1d0652 user: drh tags: trunk
22:49
Renumber the Select.selId values in the copies of SELECT statements that implement VIEWs when the VIEW is expanded, so that when the same VIEW is used twice in the same join, each expansion as a distinct selId. This fixes ticket [ce823231949d3abf42453c8]. check-in: 3cacc4b9 user: drh tags: trunk
14:35
New dbsqlfuzz find added to test/fuzzdata8.db. check-in: 42af7c81 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/select.c.

  4839   4839     if( db->mallocFailed  ){
  4840   4840       return WRC_Abort;
  4841   4841     }
  4842   4842     assert( p->pSrc!=0 );
  4843   4843     if( (selFlags & SF_Expanded)!=0 ){
  4844   4844       return WRC_Prune;
  4845   4845     }
         4846  +  if( pWalker->eCode ){
         4847  +    /* Renumber selId because it has been copied from a view */
         4848  +    p->selId = ++pParse->nSelect;
         4849  +  }
  4846   4850     pTabList = p->pSrc;
  4847   4851     pEList = p->pEList;
  4848   4852     sqlite3WithPush(pParse, p->pWith, 0);
  4849   4853   
  4850   4854     /* Make sure cursor numbers have been assigned to all entries in
  4851   4855     ** the FROM clause of the SELECT statement.
  4852   4856     */
................................................................................
  4888   4892         pTab->nTabRef++;
  4889   4893         if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
  4890   4894           return WRC_Abort;
  4891   4895         }
  4892   4896   #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
  4893   4897         if( IsVirtual(pTab) || pTab->pSelect ){
  4894   4898           i16 nCol;
         4899  +        u8 eCodeOrig = pWalker->eCode;
  4895   4900           if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
  4896   4901           assert( pFrom->pSelect==0 );
  4897   4902           pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
  4898   4903           nCol = pTab->nCol;
  4899   4904           pTab->nCol = -1;
         4905  +        pWalker->eCode = 1;  /* Turn on Select.selId renumbering */
  4900   4906           sqlite3WalkSelect(pWalker, pFrom->pSelect);
         4907  +        pWalker->eCode = eCodeOrig;
  4901   4908           pTab->nCol = nCol;
  4902   4909         }
  4903   4910   #endif
  4904   4911       }
  4905   4912   
  4906   4913       /* Locate the index named by the INDEXED BY clause, if any. */
  4907   4914       if( sqlite3IndexedByLookup(pParse, pFrom) ){
................................................................................
  5143   5150     if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
  5144   5151       w.xSelectCallback = convertCompoundSelectToSubquery;
  5145   5152       w.xSelectCallback2 = 0;
  5146   5153       sqlite3WalkSelect(&w, pSelect);
  5147   5154     }
  5148   5155     w.xSelectCallback = selectExpander;
  5149   5156     w.xSelectCallback2 = selectPopWith;
         5157  +  w.eCode = 0;
  5150   5158     sqlite3WalkSelect(&w, pSelect);
  5151   5159   }
  5152   5160   
  5153   5161   
  5154   5162   #ifndef SQLITE_OMIT_SUBQUERY
  5155   5163   /*
  5156   5164   ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()

Changes to test/with1.test.

  1087   1087           SELECT  2 FROM c,c,c,c,c,c,c,c,c
  1088   1088        )
  1089   1089        SELECT 3 FROM c,c,c,c,c,c,c,c,c
  1090   1090     )
  1091   1091     SELECT 4 FROM c,c,c,c,c,c,c,c,c;
  1092   1092   } {1 {too many FROM clause terms, max: 200}}
  1093   1093   
         1094  +# 2019-05-22
         1095  +# ticket https://www.sqlite.org/src/tktview/ce823231949d3abf42453c8f20
         1096  +#
         1097  +sqlite3 db :memory:
         1098  +do_execsql_test 23.1 {
         1099  +  CREATE TABLE t1(id INTEGER NULL PRIMARY KEY, name Text);
         1100  +  INSERT INTO t1 VALUES (1, 'john');
         1101  +  INSERT INTO t1 VALUES (2, 'james');
         1102  +  INSERT INTO t1 VALUES (3, 'jingle');
         1103  +  INSERT INTO t1 VALUES (4, 'himer');
         1104  +  INSERT INTO t1 VALUES (5, 'smith');
         1105  +  CREATE VIEW v2 AS
         1106  +    WITH t4(Name) AS (VALUES ('A'), ('B'))
         1107  +    SELECT Name Name FROM t4;
         1108  +  CREATE VIEW v3 AS
         1109  +    WITH t4(Att, Val, Act) AS (VALUES
         1110  +      ('C', 'D', 'E'),
         1111  +      ('F', 'G', 'H')
         1112  +    )
         1113  +    SELECT D.Id Id, P.Name Protocol, T.Att Att, T.Val Val, T.Act Act
         1114  +    FROM t1 D
         1115  +    CROSS JOIN v2 P
         1116  +    CROSS JOIN t4 T;
         1117  +  SELECT * FROM v3;
         1118  +} {1 A C D E 1 A F G H 1 B C D E 1 B F G H 2 A C D E 2 A F G H 2 B C D E 2 B F G H 3 A C D E 3 A F G H 3 B C D E 3 B F G H 4 A C D E 4 A F G H 4 B C D E 4 B F G H 5 A C D E 5 A F G H 5 B C D E 5 B F G H}
         1119  +
  1094   1120   finish_test