Use the subquery column name, not the original SQL statement text, as the
added AS clause in the query flattener.

FossilOrigin-Name: 5df7f0e6a1fbc770a68830ce88e78ecccbf023557ea446ce312ab53d5b32a6a9
diff --git a/manifest b/manifest
index 79354a3..1a8ff03 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C In\sthe\squery\sflattener,\sonly\sadd\sAS\sclauses\sto\soutput\scolumns\sof\sthe\souter\nquery\sthat\sare\scopied\sdirectly\sfrom\sthe\sinner\squery.\s\sFormerly,\sall\scolumns\nof\sthe\souter\squery\sreceived\san\sAS\sclause\sif\sthey\sdid\snot\shave\sone\salready.\nThis\sis\sa\sproposed\sfix\sfor\sticket\s[de3403bf5ae5f72].
-D 2017-07-29T03:33:21.465
+C Use\sthe\ssubquery\scolumn\sname,\snot\sthe\soriginal\sSQL\sstatement\stext,\sas\sthe\nadded\sAS\sclause\sin\sthe\squery\sflattener.
+D 2017-07-29T14:56:53.996
 F Makefile.in d9873c9925917cca9990ee24be17eb9613a668012c85a343aef7e5536ae266e8
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc 02b469e9dcd5b7ee63fc1fb05babc174260ee4cfa4e0ef2e48c3c6801567a016
@@ -452,7 +452,7 @@
 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
 F src/resolve.c 4324a94573b1e29286f8121e4881db59eaedc014afeb274c8d3e07ed282e0e20
 F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac
-F src/select.c 2da6bc6f1dc48ee6f1b7278fefec51185f583e260b326f191e331b0d7faa3272
+F src/select.c ef0be59b8394507c139678a8b6f1c9e8ea028b12200b5e38bc0992560341a035
 F src/shell.c bd6a37cbe8bf64ef6a6a74fdc50f067d3148149b4ce2b4d03154663e66ded55f
 F src/shell.c.in b5725acacba95ccefa57b6d068f710e29ba8239c3aa704628a1902a1f729c175
 F src/sqlite.h.in 0e2603c23f0747c5660669f946e231730af000c76d1653b153dcf2c26fce0a6b
@@ -1637,10 +1637,7 @@
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P bcec155e0d6c6b17ae09d5a366c080723d01ff40dbc1a0ad0bb669a91db1b850
-R 45c8dd957f6aab7de79a5415e3464bd4
-T *branch * flattener-column-names
-T *sym-flattener-column-names *
-T -sym-trunk *
+P 439cc5c52cbe6e67bbf0b6de0610f7d95ca9eb994f032547dc3535fd2c9dfc78
+R 958b536ad819a7cfdc8789f87ebdcbab
 U drh
-Z 62bad34cdd55bc284474093ae2886434
+Z 4ab6903a815161b50ba952b68c66e144
diff --git a/manifest.uuid b/manifest.uuid
index 677dd10..41dfdaf 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-439cc5c52cbe6e67bbf0b6de0610f7d95ca9eb994f032547dc3535fd2c9dfc78
\ No newline at end of file
+5df7f0e6a1fbc770a68830ce88e78ecccbf023557ea446ce312ab53d5b32a6a9
\ No newline at end of file
diff --git a/src/select.c b/src/select.c
index 79c23df..05784af 100644
--- a/src/select.c
+++ b/src/select.c
@@ -3772,8 +3772,7 @@
 
     /* For every result column in the outer query that does not have an AS
     ** clause, if that column is a reference to an output column from the
-    ** inner query, then preserve the name of the column as it was written
-    ** in the original SQL text of the outer query by added an AS clause.
+    ** inner query, then preserve the name of the column by adding an AS clause.
     ** This prevents the outer query column from taking on a name derived
     ** from inner query column name.
     **
@@ -3788,18 +3787,13 @@
     ** the programmer expected.  This step adds AS clauses so that the
     ** flattened query becomes:  "SELECT a AS x, b AS y FROM t1".
     **
-    ** This is not a perfect solution.  The added AS clause is the same text as
-    ** the original input SQL.  So if the input SQL used goofy column names
-    ** like "SELECT v1.X,(y) FROM v1", then the added AS clauses will be those
-    ** same goofy colum names "v1.X" and "(y)", not just "x" and "y".  We could
-    ** improve that, but doing so might break lots of legacy code that depends
-    ** on the current behavior which dates back to around 2004.
-    **
-    ** Update on 2017-07-29:  The AS clause is only inserted into outer query
-    ** result columns that get substituted for inner query columns.  Formerly
-    ** an AS clause was added to *all* columns in the outer query that did not
-    ** already have one, even columns that had nothing to do with the inner
-    ** query.
+    ** Update on 2017-07-29:  The current implementation only adds AS clauses
+    ** to outer query result columns that are substituted directly for
+    ** columns of the inner query.  Formerly, all result columns in the outer
+    ** query got new AS clauses if they didn't have them all ready.  Also,
+    ** the name of the AS clause is taken from the result column name of
+    ** the inner query.  Formerly, the name was a copy of the text of the
+    ** original SQL statement that specified the column.
     */
     pList = pParent->pEList;
     for(i=0; i<pList->nExpr; i++){
@@ -3807,9 +3801,10 @@
       if( pList->a[i].zName==0
        && (p = pList->a[i].pExpr)->op==TK_COLUMN
        && p->iTable==iParent
+       && p->iColumn>=0
+       && ALWAYS(p->pTab!=0)
       ){
-        char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
-        sqlite3Dequote(zName);
+        char *zName = sqlite3DbStrDup(db, p->pTab->aCol[p->iColumn].zName);
         pList->a[i].zName = zName;
       }
     }