Compare commits

...

2 Commits

Author SHA1 Message Date
drh 86e2e4cf54 Use the subquery column name, not the original SQL statement text, as the
added AS clause in the query flattener.

FossilOrigin-Name: 5df7f0e6a1fbc770a68830ce88e78ecccbf023557ea446ce312ab53d5b32a6a9
2017-07-29 14:56:53 +00:00
drh f76f7c2e2b In the query flattener, only add AS clauses to output columns of the outer
query that are copied directly from the inner query.  Formerly, all columns
of the outer query received an AS clause if they did not have one already.
This is a proposed fix for ticket [de3403bf5ae5f72].

FossilOrigin-Name: 439cc5c52cbe6e67bbf0b6de0610f7d95ca9eb994f032547dc3535fd2c9dfc78
2017-07-29 03:33:21 +00:00
3 changed files with 48 additions and 17 deletions
+7 -7
View File
@@ -1,5 +1,5 @@
C Update\sTcl\sversion\sused\sby\sthe\sTclKit\sbatch\stool\sfor\sMSVC.
D 2017-07-28T22:22:15.250
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/printf.c 8757834f1b54dae512fb25eb1acc8e94a0d15dd2290b58f2563f65973265adb2
F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
F src/resolve.c 4324a94573b1e29286f8121e4881db59eaedc014afeb274c8d3e07ed282e0e20
F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac
F src/select.c c6bf96a7f9d7d68f929de84738c599a30d0a725ab0b54420e70545743cd5ee7b
F src/select.c ef0be59b8394507c139678a8b6f1c9e8ea028b12200b5e38bc0992560341a035
F src/shell.c bd6a37cbe8bf64ef6a6a74fdc50f067d3148149b4ce2b4d03154663e66ded55f
F src/shell.c.in b5725acacba95ccefa57b6d068f710e29ba8239c3aa704628a1902a1f729c175
F src/sqlite.h.in 0e2603c23f0747c5660669f946e231730af000c76d1653b153dcf2c26fce0a6b
@@ -1637,7 +1637,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 3286e1a07b0693049a07f0865bf93749c461ea8f6d1175ec2d1642886673d8ac
R fa880debd22b5643c43c3f5ac12562f1
U mistachkin
Z 2ba3113508e5372bd91f947e270302b1
P 439cc5c52cbe6e67bbf0b6de0610f7d95ca9eb994f032547dc3535fd2c9dfc78
R 958b536ad819a7cfdc8789f87ebdcbab
U drh
Z 4ab6903a815161b50ba952b68c66e144
+1 -1
View File
@@ -1 +1 @@
bcec155e0d6c6b17ae09d5a366c080723d01ff40dbc1a0ad0bb669a91db1b850
5df7f0e6a1fbc770a68830ce88e78ecccbf023557ea446ce312ab53d5b32a6a9
+40 -9
View File
@@ -3769,7 +3769,46 @@ static int flattenSubquery(
memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
}
pSrc->a[iFrom].fg.jointype = jointype;
/* 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 by adding an AS clause.
** This prevents the outer query column from taking on a name derived
** from inner query column name.
**
** Example:
** CREATE TABLE t1(a,b);
** CREATE VIEW v1(x,y) AS SELECT a,b FROM t1;
** SELECT x,y FROM v1;
**
** The inner "v1" subquery will get flattened into the outer query. After
** flattening, the outer query becomes: "SELECT a,b FROM t1". But the
** new query gives column names of "a" and "b", not the "x" and "y" that
** the programmer expected. This step adds AS clauses so that the
** flattened query becomes: "SELECT a AS x, b AS y FROM t1".
**
** 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++){
Expr *p;
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, p->pTab->aCol[p->iColumn].zName);
pList->a[i].zName = zName;
}
}
/* Now begin substituting subquery result set expressions for
** references to the iParent in the outer query.
**
@@ -3782,14 +3821,6 @@ static int flattenSubquery(
** We look at every expression in the outer query and every place we see
** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
*/
pList = pParent->pEList;
for(i=0; i<pList->nExpr; i++){
if( pList->a[i].zName==0 ){
char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
sqlite3Dequote(zName);
pList->a[i].zName = zName;
}
}
if( pSub->pOrderBy ){
/* At this point, any non-zero iOrderByCol values indicate that the
** ORDER BY column expression is identical to the iOrderByCol'th