Suppress superfluous OP_OpenTemps when flattening subqueries. (CVS 412)

FossilOrigin-Name: 000441c8fec48cc172894eb767ae9549b8ed8c34
diff --git a/manifest b/manifest
index 17db1ac..690f569 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Bug\sfixes\sand\sadditional\stests\sfor\sthe\ssubquery\sflattener.\s(CVS\s411)
-D 2002-03-03T02:49:51
+C Suppress\ssuperfluous\sOP_OpenTemps\swhen\sflattening\ssubqueries.\s(CVS\s412)
+D 2002-03-03T03:03:53
 F Makefile.in 50f1b3351df109b5774771350d8c1b8d3640130d
 F Makefile.template 89e373b2dad0321df00400fa968dc14b61a03296
 F README a4c0ba11354ef6ba0776b400d057c59da47a4cc0
@@ -37,11 +37,11 @@
 F src/parse.y d62960cdee2d2e7821f277d2fe63d823c86602ba
 F src/printf.c 300a90554345751f26e1fc0c0333b90a66110a1d
 F src/random.c 19e8e00fe0df32a742f115773f57651be327cabe
-F src/select.c 285a9cfa670c3fc47d8f42c9873be88860a015a1
+F src/select.c 68d4ee84e6b6202a15fff60e0eaaf20983525d54
 F src/shell.c 9f8249ca5b8f8aad40becd778c151b58c0d6109e
 F src/shell.tcl 27ecbd63dd88396ad16d81ab44f73e6c0ea9d20e
 F src/sqlite.h.in a9b5772604265f98f3120573ef29e37b9d917216
-F src/sqliteInt.h 272c356245e3fdb33e91e87a3f7c0324d118cac9
+F src/sqliteInt.h cd2c2fcc50950ab384de8e09fa818fd329288148
 F src/table.c 203a09d5d0009eeeb1f670370d52b4ce163a3b52
 F src/tclsqlite.c b9cf346e95291cb4c4f1bf5ac1d77db6b8ad023d
 F src/test1.c 33efd350dca27c52c58c553c04fd3a6a51f13c1f
@@ -87,7 +87,7 @@
 F test/select3.test 9469c332250a75a0ef1771fb5da62dc04ec77f18
 F test/select4.test 29a2ffb187f3d8b6ca42a0a6b619e9cabe12e228
 F test/select5.test c2a6c4a003316ee42cbbd689eebef8fdce0db2ac
-F test/select6.test 8ccbcd3b53df4735a5bb3a8cd4bf4e495cae26c0
+F test/select6.test 522b4865bfdd433b7745cb45e8df1dbc53a4eaf3
 F test/sort.test 3b996ce7ca385f9cd559944ac0f4027a23aa546b
 F test/subselect.test 335d3dad8d585726c447dfee8d9c4f7383c76b78
 F test/table.test 17b0b6eafa3faaee5545b7a94e6c1ff73f0880f3
@@ -127,7 +127,7 @@
 F www/sqlite.tcl 8b5884354cb615049aed83039f8dfe1552a44279
 F www/tclsqlite.tcl 829b393d1ab187fd7a5e978631b3429318885c49
 F www/vdbe.tcl 2013852c27a02a091d39a766bc87cff329f21218
-P cf1ebcfb741786f84a596c406f4c492f68cbe881
-R 15fdb1a16caac3bbe9fb9691324e3176
+P 2c05389eda391e38894fc6969e29766df82a8fec
+R 5b2798dd95ce729696863e062872c5f6
 U drh
-Z 2b80ac05372abd52845156b2368b8bf6
+Z 5e040130dc2022feb4cc36a0d9eb62b1
diff --git a/manifest.uuid b/manifest.uuid
index e74ffae..079d233 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-2c05389eda391e38894fc6969e29766df82a8fec
\ No newline at end of file
+000441c8fec48cc172894eb767ae9549b8ed8c34
\ No newline at end of file
diff --git a/src/select.c b/src/select.c
index fb55a3f..305c3e3 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.72 2002/03/03 02:49:51 drh Exp $
+** $Id: select.c,v 1.73 2002/03/03 03:03:53 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -163,7 +163,7 @@
 
   /* Store the result as data using a unique key.
   */
-  if( eDest==SRT_Table ){
+  if( eDest==SRT_Table || eDest==SRT_TempTable ){
     sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
     sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
     sqliteVdbeAddOp(v, OP_Pull, 1, 0);
@@ -1283,8 +1283,7 @@
   */
   for(i=0; i<pTabList->nId; i++){
     if( pTabList->a[i].pSelect==0 ) continue;
-    sqliteVdbeAddOp(v, OP_OpenTemp, base+i, 0);
-    sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_Table, base+i,
+    sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable, base+i,
                  p, i, &isAgg);
     pTabList = p->pSrc;
     pWhere = p->pWhere;
@@ -1303,6 +1302,12 @@
     return rc;
   }
 
+  /* If the output is destined for a temporary table, open that table.
+  */
+  if( eDest==SRT_TempTable ){
+    sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
+  }
+
   /* Do an analysis of aggregate expressions.
   */
   sqliteAggregateInfoReset(pParse);
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index bb6ee1a..28b106e 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.98 2002/03/03 02:49:51 drh Exp $
+** @(#) $Id: sqliteInt.h,v 1.99 2002/03/03 03:03:53 drh Exp $
 */
 #include "sqlite.h"
 #include "hash.h"
@@ -475,6 +475,7 @@
 #define SRT_Union        5  /* Store result as keys in a table */
 #define SRT_Except       6  /* Remove result from a UNION table */
 #define SRT_Table        7  /* Store result as data with a unique key */
+#define SRT_TempTable    8  /* Store result in a trasient table */
 
 /*
 ** When a SELECT uses aggregate functions (like "count(*)" or "avg(f1)")
diff --git a/test/select6.test b/test/select6.test
index afbff90..43ed5b8 100644
--- a/test/select6.test
+++ b/test/select6.test
@@ -12,7 +12,7 @@
 # focus of this file is testing SELECT statements that contain
 # subqueries in their FROM clause.
 #
-# $Id: select6.test,v 1.3 2002/03/03 02:49:52 drh Exp $
+# $Id: select6.test,v 1.4 2002/03/03 03:03:54 drh Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -263,6 +263,16 @@
     SELECT DISTINCT y FROM (SELECT y FROM t1) WHERE y<5 ORDER BY y
   }
 } {1 2 3 4}
+do_test select6-4.4 {
+  execsql {
+    SELECT avg(y) FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
+  }
+} {2.5}
+do_test select6-4.5 {
+  execsql {
+    SELECT avg(y) FROM (SELECT DISTINCT y FROM t1 WHERE y<5) ORDER BY y
+  }
+} {2.5}
 
 
 finish_test