Improve generated column names for cases involving sub-SELECTs.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 1 Oct 2011 18:01:46 +0000 (14:01 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 1 Oct 2011 18:01:46 +0000 (14:01 -0400)
We'll now use "exists" for EXISTS(SELECT ...), "array" for ARRAY(SELECT
...), or the sub-select's own result column name for a simple expression
sub-select.  Previously, you usually got "?column?" in such cases.

Marti Raudsepp, reviewed by Kyotaro Horiugchi

doc/src/sgml/ref/select.sgml
doc/src/sgml/syntax.sgml
src/backend/parser/parse_target.c
src/test/regress/expected/aggregates.out
src/test/regress/expected/subselect.out
src/test/regress/expected/with.out

index 7fb52833e8abc1051ea1e39977a9440165be3a34..636435fe1d8b94adffc1edc4307f8f7ce4d05600 100644 (file)
@@ -758,8 +758,9 @@ UNBOUNDED FOLLOWING
     If you do not specify a column name, a name is chosen automatically
     by <productname>PostgreSQL</productname>.  If the column's expression
     is a simple column reference then the chosen name is the same as that
-    column's name; in more complex cases a generated name looking like
-    <literal>?column<replaceable>N</>?</literal> is usually chosen.
+    column's name.  In more complex cases a function or type name may be
+    used, or the system may fall back on a generated name such as
+    <literal>?column?</literal>.
    </para>
 
    <para>
index 9119b60792e50e1ae8924f42dbde11b6ebcd0b98..5ea755c57256a8e95171e772bc7a40bc816a8c49 100644 (file)
@@ -2109,9 +2109,9 @@ SELECT ARRAY[]::integer[];
    bracketed) subquery. For example:
 <programlisting>
 SELECT ARRAY(SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%');
-                          ?column?
--------------------------------------------------------------
- {2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31}
+                                 array
+-----------------------------------------------------------------------
+ {2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31,2412,2413}
 (1 row)
 </programlisting>
    The subquery must return a single column. The resulting
index 9d4e580e47699219973ba0c6613c23749577ffcc..2f2e87fd698219e0241e30e598b0b4adf8d5900a 100644 (file)
@@ -1610,6 +1610,48 @@ FigureColnameInternal(Node *node, char **name)
            break;
        case T_CollateClause:
            return FigureColnameInternal(((CollateClause *) node)->arg, name);
+       case T_SubLink:
+           switch (((SubLink *) node)->subLinkType)
+           {
+               case EXISTS_SUBLINK:
+                   *name = "exists";
+                   return 2;
+               case ARRAY_SUBLINK:
+                   *name = "array";
+                   return 2;
+               case EXPR_SUBLINK:
+                   {
+                       /* Get column name of the subquery's single target */
+                       SubLink    *sublink = (SubLink *) node;
+                       Query      *query = (Query *) sublink->subselect;
+
+                       /*
+                        * The subquery has probably already been transformed,
+                        * but let's be careful and check that.  (The reason
+                        * we can see a transformed subquery here is that
+                        * transformSubLink is lazy and modifies the SubLink
+                        * node in-place.)
+                        */
+                       if (IsA(query, Query))
+                       {
+                           TargetEntry *te = (TargetEntry *) linitial(query->targetList);
+
+                           if (te->resname)
+                           {
+                               *name = te->resname;
+                               return 2;
+                           }
+                       }
+                   }
+                   break;
+               /* As with other operator-like nodes, these have no names */
+               case ALL_SUBLINK:
+               case ANY_SUBLINK:
+               case ROWCOMPARE_SUBLINK:
+               case CTE_SUBLINK:
+                   break;
+           }
+           break;
        case T_CaseExpr:
            strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult,
                                             name);
index 48610066bbd83eeda39f7dacc39c06589bde1b73..69926f7b79ec22bd9645d73d452a782863abb78b 100644 (file)
@@ -300,9 +300,9 @@ LINE 4:                where sum(distinct a.four + b.four) = b.four)...
 select
   (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1)))
 from tenk1 o;
- ?column? 
-----------
    9999
+ max  
+------
+ 9999
 (1 row)
 
 --
index e638f0a60c30bdde922fd0d09b0582f5d612557b..4ea8211c692f6d37e20b68f18b0eb8da0ad7c371 100644 (file)
@@ -490,20 +490,20 @@ select view_a from view_a;
 (1 row)
 
 select (select view_a) from view_a;
- ?column? 
-----------
+ view_a 
+--------
  (42)
 (1 row)
 
 select (select (select view_a)) from view_a;
- ?column? 
-----------
+ view_a 
+--------
  (42)
 (1 row)
 
 select (select (a.*)::text) from view_a a;
- ?column? 
-----------
+  a   
+------
  (42)
 (1 row)
 
index a1b089921d362787cf1ca69a5e66d0a6fad14875..c4b045604b604043fcab40dcb172ea7cb7d88c25 100644 (file)
@@ -1065,7 +1065,7 @@ with cte(foo) as ( select 42 ) select * from ((select foo from cte)) q;
 select ( with cte(foo) as ( values(f1) )
          select (select foo from cte) )
 from int4_tbl;
-  ?column?   
+     foo     
 -------------
            0
       123456
@@ -1077,7 +1077,7 @@ from int4_tbl;
 select ( with cte(foo) as ( values(f1) )
           values((select foo from cte)) )
 from int4_tbl;
-  ?column?   
+   column1   
 -------------
            0
       123456