CREATE TABLE ... AS ... uses short names for columns.  Ticket #1036. (CVS 2232)

FossilOrigin-Name: b1d4c42d2be07adda68d31c570ba7cf8b115c3ad
diff --git a/src/expr.c b/src/expr.c
index 36cfa28..88ca91f 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -12,7 +12,7 @@
 ** This file contains routines used for analyzing expressions and
 ** for generating VDBE code that evaluates expressions in SQLite.
 **
-** $Id: expr.c,v 1.180 2005/01/18 04:00:44 drh Exp $
+** $Id: expr.c,v 1.181 2005/01/18 17:20:10 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -826,7 +826,7 @@
     ** Note that the expression in the result set should have already been
     ** resolved by the time the WHERE clause is resolved.
     */
-    if( cnt==0 && pEList!=0 ){
+    if( cnt==0 && pEList!=0 && zTab==0 ){
       for(j=0; j<pEList->nExpr; j++){
         char *zAs = pEList->a[j].zName;
         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
diff --git a/src/select.c b/src/select.c
index e3ee9f6..278416e 100644
--- a/src/select.c
+++ b/src/select.c
@@ -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.226 2005/01/18 16:02:40 drh Exp $
+** $Id: select.c,v 1.227 2005/01/18 17:20:10 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -829,32 +829,50 @@
   assert( pTab->nCol>0 );
   pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
   for(i=0, pCol=aCol; i<pTab->nCol; i++, pCol++){
-    Expr *pR;
+    Expr *p, *pR;
     char *zType;
     char *zName;
-    Expr *p = pEList->a[i].pExpr;
+    char *zBasename;
+    int cnt;
+    
+    /* Get an appropriate name for the column
+    */
+    p = pEList->a[i].pExpr;
     assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 );
     if( (zName = pEList->a[i].zName)!=0 ){
+      /* If the column contains an "AS <name>" phrase, use <name> as the name */
       zName = sqliteStrDup(zName);
     }else if( p->op==TK_DOT 
-               && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){
-      int cnt;
+              && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){
+      /* For columns of the from A.B use B as the name */
       zName = sqlite3MPrintf("%T", &pR->token);
-      for(j=cnt=0; j<i; j++){
-        if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
-          sqliteFree(zName);
-          zName = sqlite3MPrintf("%T_%d", &pR->token, ++cnt);
-          j = -1;
-        }
-      }
     }else if( p->span.z && p->span.z[0] ){
+      /* Use the original text of the column expression as its name */
       zName = sqlite3MPrintf("%T", &p->span);
     }else{
+      /* If all else fails, make up a name */
       zName = sqlite3MPrintf("column%d", i+1);
     }
     sqlite3Dequote(zName);
+
+    /* Make sure the column name is unique.  If the name is not unique,
+    ** append a integer to the name so that it becomes unique.
+    */
+    zBasename = zName;
+    for(j=cnt=0; j<i; j++){
+      if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
+        zName = sqlite3MPrintf("%s:%d", zBasename, ++cnt);
+        j = -1;
+      }
+    }
+    if( zBasename!=zName ){
+      sqliteFree(zBasename);
+    }
     pCol->zName = zName;
 
+    /* Get the typename, type affinity, and collating sequence for the
+    ** column.
+    */
     zType = sqliteStrDup(columnType(pParse, pSelect->pSrc ,p));
     pCol->zType = zType;
     pCol->affinity = SQLITE_AFF_NUMERIC;
@@ -1059,7 +1077,7 @@
               pExpr = pRight;
               pExpr->span = pExpr->token;
             }
-            pNew = sqlite3ExprListAppend(pNew, pExpr, 0);
+            pNew = sqlite3ExprListAppend(pNew, pExpr, &pRight->token);
           }
         }
         if( !tableSeen ){