diff options
author | drh <drh@noemail.net> | 2015-11-14 20:52:43 +0000 |
---|---|---|
committer | drh <drh@noemail.net> | 2015-11-14 20:52:43 +0000 |
commit | 0315e3cc140683a814f9d0069d6972669332388c (patch) | |
tree | ae1194d10dfb7599c2cd28cf472544a23a2c9b15 /src/select.c | |
parent | ebed3fa3e1193352caa581181332c9b26c007004 (diff) | |
download | sqlite-0315e3cc140683a814f9d0069d6972669332388c.tar.gz sqlite-0315e3cc140683a814f9d0069d6972669332388c.zip |
Use a hash table to improve the preformance of column name uniqueness checking.
FossilOrigin-Name: 5b08f29f458c600401860c7d70d8174cf61e69f8
Diffstat (limited to 'src/select.c')
-rw-r--r-- | src/select.c | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/src/select.c b/src/select.c index afebf87ba..802e75eec 100644 --- a/src/select.c +++ b/src/select.c @@ -1602,7 +1602,9 @@ int sqlite3ColumnsFromExprList( Expr *p; /* Expression for a single result column */ char *zName; /* Column name */ int nName; /* Size of name in zName[] */ + Hash ht; /* Hash table of column names */ + sqlite3HashInit(&ht); if( pEList ){ nCol = pEList->nExpr; aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol); @@ -1614,7 +1616,7 @@ int sqlite3ColumnsFromExprList( *pnCol = nCol; *paCol = aCol; - for(i=0, pCol=aCol; i<nCol; i++, pCol++){ + for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){ /* Get an appropriate name for the column */ p = sqlite3ExprSkipCollate(pEList->a[i].pExpr); @@ -1643,32 +1645,28 @@ int sqlite3ColumnsFromExprList( zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan); } } - if( db->mallocFailed ){ - sqlite3DbFree(db, zName); - break; - } /* Make sure the column name is unique. If the name is not unique, ** append an integer to the name so that it becomes unique. */ - nName = sqlite3Strlen30(zName); - for(j=cnt=0; j<i; j++){ - if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){ - char *zNewName; - int k; - for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){} - if( k>=0 && zName[k]==':' ) nName = k; - zName[nName] = 0; - zNewName = sqlite3MPrintf(db, "%s:%u", zName, ++cnt); - sqlite3DbFree(db, zName); - zName = zNewName; - j = -1; - if( zName==0 ) break; - if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt); - } + cnt = 0; + while( zName && sqlite3HashFind(&ht, zName)!=0 ){ + char *zNewName; + nName = sqlite3Strlen30(zName); + for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){} + if( zName[j]==':' ) nName = j; + zName[nName] = 0; + zNewName = sqlite3MPrintf(db, "%s:%u", zName, ++cnt); + sqlite3DbFree(db, zName); + zName = zNewName; + if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt); } pCol->zName = zName; + if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){ + db->mallocFailed = 1; + } } + sqlite3HashClear(&ht); if( db->mallocFailed ){ for(j=0; j<i; j++){ sqlite3DbFree(db, aCol[j].zName); |