/ Check-in [92724260]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Code simplification in sqlite3ColumnsFromExprList(). Update the %z format code so that it works with buffers obtained from sqlite3DbMalloc(). Add a testcase for the slow column name uniquifier.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9272426057b6cb2d913519ff4c97aa6e211f7d51
User & Date: drh 2015-11-14 22:04:22
Context
2015-11-15
11:13
Fix the column name uniquifier so that it works with zero-length column names. check-in: 791761eb user: drh tags: trunk
2015-11-14
22:04
Code simplification in sqlite3ColumnsFromExprList(). Update the %z format code so that it works with buffers obtained from sqlite3DbMalloc(). Add a testcase for the slow column name uniquifier. check-in: 92724260 user: drh tags: trunk
20:52
Use a hash table to improve the preformance of column name uniqueness checking. check-in: 5b08f29f user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/printf.c.

   722    722       */
   723    723       width -= length;
   724    724       if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
   725    725       sqlite3StrAccumAppend(pAccum, bufpt, length);
   726    726       if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
   727    727   
   728    728       if( zExtra ){
   729         -      sqlite3_free(zExtra);
          729  +      sqlite3DbFree(pAccum->db, zExtra);
   730    730         zExtra = 0;
   731    731       }
   732    732     }/* End for loop over the format string */
   733    733   } /* End of function */
   734    734   
   735    735   /*
   736    736   ** Enlarge the memory allocation on a StrAccum object so that it is

Changes to src/select.c.

  1618   1618   
  1619   1619     for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
  1620   1620       /* Get an appropriate name for the column
  1621   1621       */
  1622   1622       p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
  1623   1623       if( (zName = pEList->a[i].zName)!=0 ){
  1624   1624         /* If the column contains an "AS <name>" phrase, use <name> as the name */
  1625         -      zName = sqlite3DbStrDup(db, zName);
  1626   1625       }else{
  1627   1626         Expr *pColExpr = p;  /* The expression that is the result column name */
  1628   1627         Table *pTab;         /* Table associated with this expression */
  1629   1628         while( pColExpr->op==TK_DOT ){
  1630   1629           pColExpr = pColExpr->pRight;
  1631   1630           assert( pColExpr!=0 );
  1632   1631         }
  1633   1632         if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
  1634   1633           /* For columns use the column name name */
  1635   1634           int iCol = pColExpr->iColumn;
  1636   1635           pTab = pColExpr->pTab;
  1637   1636           if( iCol<0 ) iCol = pTab->iPKey;
  1638         -        zName = sqlite3MPrintf(db, "%s",
  1639         -                 iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
         1637  +        zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
  1640   1638         }else if( pColExpr->op==TK_ID ){
  1641   1639           assert( !ExprHasProperty(pColExpr, EP_IntValue) );
  1642         -        zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
         1640  +        zName = pColExpr->u.zToken;
  1643   1641         }else{
  1644   1642           /* Use the original text of the column expression as its name */
  1645         -        zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
         1643  +        zName = pEList->a[i].zSpan;
  1646   1644         }
  1647   1645       }
         1646  +    zName = sqlite3MPrintf(db, "%s", zName);
  1648   1647   
  1649   1648       /* Make sure the column name is unique.  If the name is not unique,
  1650   1649       ** append an integer to the name so that it becomes unique.
  1651   1650       */
  1652   1651       cnt = 0;
  1653   1652       while( zName && sqlite3HashFind(&ht, zName)!=0 ){
  1654         -      char *zNewName;
  1655   1653         nName = sqlite3Strlen30(zName);
  1656   1654         for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
  1657   1655         if( zName[j]==':' ) nName = j;
  1658         -      zName[nName] = 0;
  1659         -      zNewName = sqlite3MPrintf(db, "%s:%u", zName, ++cnt);
  1660         -      sqlite3DbFree(db, zName);
  1661         -      zName = zNewName;
         1656  +      zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
  1662   1657         if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
  1663   1658       }
  1664   1659       pCol->zName = zName;
  1665   1660       if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
  1666   1661         db->mallocFailed = 1;
  1667   1662       }
  1668   1663     }

Changes to test/misc1.test.

   696    696   
   697    697   # At one point, running this would read one byte passed the end of a 
   698    698   # buffer, upsetting valgrind.
   699    699   #
   700    700   do_test misc1-24.0 {
   701    701     list [catch { sqlite3_prepare_v2 db ! -1 dummy } msg] $msg
   702    702   } {1 {(1) unrecognized token: "!}}
          703  +
          704  +# The following query (provided by Kostya Serebryany) used to take 25
          705  +# minutes to prepare.  This has been speeded up to about 250 milliseconds.
          706  +#
          707  +do_catchsql_test misc1-25.0 {
          708  +SELECT-1 UNION  SELECT 5 UNION SELECT 0 UNION SElECT*from(SELECT-5) UNION SELECT*from(SELECT-0) UNION  SELECT:SELECT-0 UNION SELECT-1 UNION SELECT 1 UNION SELECT 1 ORDER BY S  in(WITH K AS(WITH K AS(select'CREINDERcharREADEVIRTUL5TABLECONFLICT !1 USIN'' MFtOR(b38q,eWITH K AS(selectCREATe TABLE t0(a,b,c,d,e, PRIMARY KEY(a,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,b,c,d,c,a,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b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