/ Check-in [6b2ff26c]
Login

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

Overview
Comment:Move the generation of output column names earlier in the case of a CREATE TABLE AS. This is a fix for ticket [3b4450072511e62] and a continuation of check-in [ade7ddf1998190b2b63] that fixes cases of ticket [de3403bf5ae5f72ed6] that were missed previously.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6b2ff26c25bb9da344add79c93fb3e49fa034a89b38ef56e08e18d21de61f707
User & Date: drh 2017-12-23 12:33:40
References
2017-12-25
13:43
Fix an assertion fault on a syntax error input caused by check-in [6b2ff26c25bb9da3]. Problem discovered by OSSFuzz. check-in: 90d6e4f1 user: drh tags: trunk
2017-12-24
18:56
Fix a NULL pointer dereference after a syntax error that can occur as a result of check-in [6b2ff26c25bb9da3] yesterday. This problem was discovered by the OSSFuzz. check-in: d49afb8f user: drh tags: trunk
Context
2017-12-23
13:55
Improve the error message that comes back when sqlite3_load_extension() fails because the named file exists but is not a valid shared library. check-in: 05fee1a2 user: drh tags: trunk
12:33
Move the generation of output column names earlier in the case of a CREATE TABLE AS. This is a fix for ticket [3b4450072511e62] and a continuation of check-in [ade7ddf1998190b2b63] that fixes cases of ticket [de3403bf5ae5f72ed6] that were missed previously. check-in: 6b2ff26c user: drh tags: trunk
11:51
Add a SELECTTRACE() macro to indicate when column names are assigned to a SELECT statement. This helps with debugging for tickets like [de3403bf5ae5f72e] and [3b4450072511e621]. check-in: 8f194008 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/build.c.

  1961   1961         assert(pParse->nTab==1);
  1962   1962         sqlite3MayAbort(pParse);
  1963   1963         sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
  1964   1964         sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
  1965   1965         pParse->nTab = 2;
  1966   1966         addrTop = sqlite3VdbeCurrentAddr(v) + 1;
  1967   1967         sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
  1968         -      sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
  1969         -      sqlite3Select(pParse, pSelect, &dest);
  1970         -      sqlite3VdbeEndCoroutine(v, regYield);
  1971         -      sqlite3VdbeJumpHere(v, addrTop - 1);
  1972         -      if( pParse->nErr ) return;
  1973   1968         pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
  1974   1969         if( pSelTab==0 ) return;
  1975   1970         assert( p->aCol==0 );
  1976   1971         p->nCol = pSelTab->nCol;
  1977   1972         p->aCol = pSelTab->aCol;
  1978   1973         pSelTab->nCol = 0;
  1979   1974         pSelTab->aCol = 0;
  1980   1975         sqlite3DeleteTable(db, pSelTab);
         1976  +      sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
         1977  +      sqlite3Select(pParse, pSelect, &dest);
         1978  +      sqlite3VdbeEndCoroutine(v, regYield);
         1979  +      sqlite3VdbeJumpHere(v, addrTop - 1);
  1981   1980         addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
  1982   1981         VdbeCoverage(v);
  1983   1982         sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
  1984   1983         sqlite3TableAffinity(v, p, 0);
  1985   1984         sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
  1986   1985         sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
  1987   1986         sqlite3VdbeGoto(v, addrInsLoop);

Changes to src/select.c.

  1377   1377     char const *zOrigDb = 0;
  1378   1378     char const *zOrigTab = 0;
  1379   1379     char const *zOrigCol = 0;
  1380   1380   #endif
  1381   1381   
  1382   1382     assert( pExpr!=0 );
  1383   1383     assert( pNC->pSrcList!=0 );
         1384  +  assert( pExpr->op!=TK_AGG_COLUMN );  /* This routine runes before aggregates
         1385  +                                       ** are processed */
  1384   1386     switch( pExpr->op ){
  1385         -    case TK_AGG_COLUMN:
  1386   1387       case TK_COLUMN: {
  1387   1388         /* The expression is a column. Locate the table the column is being
  1388   1389         ** extracted from in NameContext.pSrcList. This table may be real
  1389   1390         ** database table or a subquery.
  1390   1391         */
  1391   1392         Table *pTab = 0;            /* Table structure column is extracted from */
  1392   1393         Select *pS = 0;             /* Select the column is extracted from */
  1393   1394         int iCol = pExpr->iColumn;  /* Index of column in pTab */
  1394         -      testcase( pExpr->op==TK_AGG_COLUMN );
  1395         -      testcase( pExpr->op==TK_COLUMN );
  1396   1395         while( pNC && !pTab ){
  1397   1396           SrcList *pTabList = pNC->pSrcList;
  1398   1397           for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
  1399   1398           if( j<pTabList->nSrc ){
  1400   1399             pTab = pTabList->a[j].pTab;
  1401   1400             pS = pTabList->a[j].pSelect;
  1402   1401           }else{
................................................................................
  1701   1700         /* If the column contains an "AS <name>" phrase, use <name> as the name */
  1702   1701       }else{
  1703   1702         Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
  1704   1703         while( pColExpr->op==TK_DOT ){
  1705   1704           pColExpr = pColExpr->pRight;
  1706   1705           assert( pColExpr!=0 );
  1707   1706         }
  1708         -      if( (pColExpr->op==TK_COLUMN || pColExpr->op==TK_AGG_COLUMN)
  1709         -       && pColExpr->pTab!=0 
  1710         -      ){
         1707  +      assert( pColExpr->op!=TK_AGG_COLUMN );
         1708  +      if( pColExpr->op==TK_COLUMN ){
  1711   1709           /* For columns use the column name name */
  1712   1710           int iCol = pColExpr->iColumn;
  1713   1711           Table *pTab = pColExpr->pTab;
         1712  +        assert( pTab!=0 );
  1714   1713           if( iCol<0 ) iCol = pTab->iPKey;
  1715   1714           zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
  1716   1715         }else if( pColExpr->op==TK_ID ){
  1717   1716           assert( !ExprHasProperty(pColExpr, EP_IntValue) );
  1718   1717           zName = pColExpr->u.zToken;
  1719   1718         }else{
  1720   1719           /* Use the original text of the column expression as its name */

Changes to test/colname.test.

   373    373   } {a 1 n 3}
   374    374   do_test colname-9.211 {
   375    375     execsql2 {SELECT t1.a AS n, v3.a FROM t1 JOIN v3}
   376    376   } {n 1 a 3}
   377    377   do_test colname-9.210 {
   378    378     execsql2 {SELECT t1.a, v3.a AS n FROM t1 JOIN v3}
   379    379   } {a 1 n 3}
          380  +
          381  +# 2017-12-23:  Ticket https://www.sqlite.org/src/info/3b4450072511e621
          382  +# Inconsistent column names in CREATE TABLE AS
          383  +#
          384  +# Verify that the names of columns in the created table of a CREATE TABLE AS
          385  +# are the same as the names of result columns in the SELECT statement.
          386  +#
          387  +do_execsql_test colname-9.300 {
          388  +  DROP TABLE IF EXISTS t1;
          389  +  DROP TABLE IF EXISTS t2;
          390  +  CREATE TABLE t1(aaa INT);
          391  +  INSERT INTO t1(aaa) VALUES(123);
          392  +}
          393  +do_test colname-9.310 {
          394  +  execsql2 {SELECT BBb FROM (SELECT aaa AS Bbb FROM t1)}
          395  +} {Bbb 123}
          396  +do_execsql_test colname-9.320 {
          397  +  CREATE TABLE t2 AS SELECT BBb FROM (SELECT aaa AS Bbb FROM t1);
          398  +  SELECT name FROM pragma_table_info('t2');
          399  +} {Bbb}
          400  +
   380    401   
   381    402   # Make sure the quotation marks get removed from the column names
   382    403   # when constructing a new table from an aggregate SELECT.
   383    404   # Email from Juergen Palm on 2017-07-11.
   384    405   #
   385    406   do_execsql_test colname-10.100 {
   386    407     DROP TABLE IF EXISTS t1;