From: drh Date: Tue, 18 Jan 2005 17:20:10 +0000 (+0000) Subject: CREATE TABLE ... AS ... uses short names for columns. Ticket #1036. (CVS 2232) X-Git-Tag: version-3.6.10~3920 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=79d5f63fa2b969bc6aed59e48fb781e340782878;p=thirdparty%2Fsqlite.git CREATE TABLE ... AS ... uses short names for columns. Ticket #1036. (CVS 2232) FossilOrigin-Name: b1d4c42d2be07adda68d31c570ba7cf8b115c3ad --- diff --git a/manifest b/manifest index 874a3ad7e4..ad1af6b95f 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Column\snames\scoming\sback\sfrom\sa\sSELECT\sare\snow\sjust\sthe\sname\sof\sthe\nsource\scolumn\swithout\sthe\s"table."\sprefix.\sIn\sother\swords,\n"PRAGMA\sshort_column_names=ON"\sis\snow\sthe\sdefault.\nThis\smakes\sthe\snames\sof\scolumns\sbehave\smore\slike\sother\sSQL\sengines.\nThe\sold\sbehavior\scan\sbe\srestored\sby\ssetting\s"PRAGMA\sshort_column_names=OFF".\s(CVS\s2231) -D 2005-01-18T16:02:40 +C CREATE\sTABLE\s...\sAS\s...\suses\sshort\snames\sfor\scolumns.\s\sTicket\s#1036.\s(CVS\s2232) +D 2005-01-18T17:20:10 F Makefile.in ffd81f5e926d40b457071b4de8d7c1fa18f39b5a F Makefile.linux-gcc a9e5a0d309fa7c38e7c14d3ecf7690879d3a5457 F README a01693e454a00cc117967e3f9fdab2d4d52e9bc1 @@ -35,7 +35,7 @@ F src/build.c 608ea54aab8f561da99c8a40c3ab6b26b7fc91e9 F src/cursor.c f883813759742068890b1f699335872bfa8fdf41 F src/date.c f3d1f5cd1503dabf426a198f3ebef5afbc122a7f F src/delete.c 728a02e5b5c62d294f8cdbdb21dbaa3e188983ab -F src/expr.c fa983460a064ee9ba55a0b3be1bd7db6b0545622 +F src/expr.c fdacfb27a5803eadda1a14980553ca394d1d5612 F src/func.c dc188d862d7276ea897655b248e2cb17022686e3 F src/hash.c a97721a55440b7bea31ffe471bb2f6b4123cddd5 F src/hash.h 1b0c445e1c89ff2aaad9b4605ba61375af001e84 @@ -57,7 +57,7 @@ F src/parse.y ceba179b9703657180963568f54b0e75f33e36e1 F src/pragma.c ac594f74c90ffec043c43e49358719ffeb491eec F src/printf.c 3d20b21cfecadacecac3fb7274e746cb81d3d357 F src/random.c eff68e3f257e05e81eae6c4d50a51eb88beb4ff3 -F src/select.c 56f018292ce467c276440916d455671d884e9141 +F src/select.c f9239c5936598de7f20710d82b024ff0a1582cd3 F src/shell.c 591364a0e9ca4ce53873e21e0294476c0c2b4770 F src/sqlite.h.in 0d5e48e506845b74a845c9470e01d3f472b59611 F src/sqliteInt.h c6414179a23cab108b4b07e8665f30829ce47f2a @@ -156,7 +156,7 @@ F test/minmax.test e7048476940df0af11d0f2cf687572f557cd0b29 F test/misc1.test 744f60d1025fa978708b96cb222a07a1feb1524a F test/misc2.test bc852f1622d98b610d1f3e2ceb36ed7271256050 F test/misc3.test 928a2f1e1189924ed14e1ae074e34f40688bdf94 -F test/misc4.test 33fd2ef6e9443acd73257a8f49aee0baec125888 +F test/misc4.test 74e6b9e8f96ec60ad0afa61f7fad681f88daa473 F test/misuse.test 600738a8e611989bc5f544303f5e311c5f228084 F test/notnull.test 7a08117a71e74b0321aaa937dbeb41a09d6eb1d0 F test/null.test 5a945790ef21b24fd602fe2c7a23847b903f8687 @@ -173,7 +173,7 @@ F test/reindex.test 3552c6b944a3fab28cfd3049c04c65cb79419757 F test/rollback.test 94cd981ee3a627d9f6466f69dcf1f7dbfe695d7a F test/rowid.test 1ce3f1520d2082b0363e7d9bdef904cb72b9efe8 F test/safety.test 907b64fee719554a3622853812af3886fddbbb4f -F test/select1.test 0b4d3883a9c4e4b78e9f81b4f5d339e4e1494d26 +F test/select1.test 6d4a42c2fb7fca7196435d6bc90aac5b180e8d94 F test/select2.test 91a2225926039b0d1687840735c284dbbf89f0bc F test/select3.test 9de435aa84fc406708cd8dc1b1d60e7f27cea685 F test/select4.test 1ae6f2bd6177d4428df8644eb95d09f4568fb149 @@ -269,7 +269,7 @@ F www/tclsqlite.tcl e73f8f8e5f20e8277619433f7970060ab01088fc F www/vdbe.tcl 095f106d93875c94b47367384ebc870517431618 F www/version3.tcl 092a01f5ef430d2c4acc0ae558d74c4bb89638a0 F www/whentouse.tcl c3b50d3ac31c54be2a1af9b488a89d22f1e6e746 -P 88d4834fec7583a9b6400a1ce5b928c1f8a390dd -R b82899e5e20c2e403cd6bfa3ba754b75 +P 9295050af1bf2d9d4dc63adc225a2848d67cbe17 +R a088aa4bf87c4c0ebda525aa7fbfbf2e U drh -Z fb3a884afe1488504ff81cd6dd7f3e7e +Z 81817808d93adb08227cfb0c57264748 diff --git a/manifest.uuid b/manifest.uuid index 2db0b31d92..6f76adb1cd 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -9295050af1bf2d9d4dc63adc225a2848d67cbe17 \ No newline at end of file +b1d4c42d2be07adda68d31c570ba7cf8b115c3ad \ No newline at end of file diff --git a/src/expr.c b/src/expr.c index 36cfa2821d..88ca91ff17 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 @@ -826,7 +826,7 @@ static int lookupName( ** 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; jnExpr; 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 e3ee9f6559..278416e171 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 @@ Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ assert( pTab->nCol>0 ); pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); for(i=0, pCol=aCol; inCol; 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 " phrase, use 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; jtoken, ++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; jzName = 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 @@ static int prepSelectStmt(Parse *pParse, Select *p){ pExpr = pRight; pExpr->span = pExpr->token; } - pNew = sqlite3ExprListAppend(pNew, pExpr, 0); + pNew = sqlite3ExprListAppend(pNew, pExpr, &pRight->token); } } if( !tableSeen ){ diff --git a/test/misc4.test b/test/misc4.test index cc8e0f09d8..d098608432 100644 --- a/test/misc4.test +++ b/test/misc4.test @@ -13,7 +13,7 @@ # This file implements tests for miscellanous features that were # left out of other test files. # -# $Id: misc4.test,v 1.10 2005/01/18 14:45:49 drh Exp $ +# $Id: misc4.test,v 1.11 2005/01/18 17:20:10 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -119,4 +119,27 @@ do_test misc4-4.1 { } } {01 data01 01 3.0 +1 data+1 +1 7.0} +# Ticket #1036. When creating tables from a SELECT on a view, use the +# short names of columns. +# +do_test misc4-5.1 { + execsql { + create table t4(a,b); + create table t5(a,c); + insert into t4 values (1,2); + insert into t5 values (1,3); + create view myview as select t4.a a from t4 inner join t5 on t4.a=t5.a; + create table problem as select * from myview; + } + execsql2 { + select * FROM problem; + } +} {a 1} +do_test misc4-5.2 { + execsql2 { + create table t6 as select * from t4, t5; + select * from t6; + } +} {a 1 b 2 a:1 1 c 3} + finish_test diff --git a/test/select1.test b/test/select1.test index 45a907d86c..dd00484c56 100644 --- a/test/select1.test +++ b/test/select1.test @@ -11,7 +11,7 @@ # This file implements regression tests for SQLite library. The # focus of this file is testing the SELECT statement. # -# $Id: select1.test,v 1.39 2005/01/18 16:02:41 drh Exp $ +# $Id: select1.test,v 1.40 2005/01/18 17:20:10 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -385,12 +385,12 @@ do_test select1-6.1.2 { do_test select1-6.1.3 { set v [catch {execsql2 {SELECT * FROM test1 WHERE f1==11}} msg] lappend v $msg -} {0 {test1.f1 11 test1.f2 22}} +} {0 {f1 11 f2 22}} do_test select1-6.1.4 { set v [catch {execsql2 {SELECT DISTINCT * FROM test1 WHERE f1==11}} msg] execsql {PRAGMA full_column_names=off} lappend v $msg -} {0 {test1.f1 11 test1.f2 22}} +} {0 {f1 11 f2 22}} do_test select1-6.1.5 { set v [catch {execsql2 {SELECT * FROM test1 WHERE f1==11}} msg] lappend v $msg