Commit 5ec6b7f1 authored by Tom Lane's avatar Tom Lane

Improve generated column names for cases involving sub-SELECTs.

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
parent 878b74e0
...@@ -758,8 +758,9 @@ UNBOUNDED FOLLOWING ...@@ -758,8 +758,9 @@ UNBOUNDED FOLLOWING
If you do not specify a column name, a name is chosen automatically If you do not specify a column name, a name is chosen automatically
by <productname>PostgreSQL</productname>. If the column's expression by <productname>PostgreSQL</productname>. If the column's expression
is a simple column reference then the chosen name is the same as that 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 column's name. In more complex cases a function or type name may be
<literal>?column<replaceable>N</>?</literal> is usually chosen. used, or the system may fall back on a generated name such as
<literal>?column?</literal>.
</para> </para>
<para> <para>
......
...@@ -2109,9 +2109,9 @@ SELECT ARRAY[]::integer[]; ...@@ -2109,9 +2109,9 @@ SELECT ARRAY[]::integer[];
bracketed) subquery. For example: bracketed) subquery. For example:
<programlisting> <programlisting>
SELECT ARRAY(SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%'); SELECT ARRAY(SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%');
?column? array
------------------------------------------------------------- -----------------------------------------------------------------------
{2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31} {2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31,2412,2413}
(1 row) (1 row)
</programlisting> </programlisting>
The subquery must return a single column. The resulting The subquery must return a single column. The resulting
......
...@@ -1610,6 +1610,48 @@ FigureColnameInternal(Node *node, char **name) ...@@ -1610,6 +1610,48 @@ FigureColnameInternal(Node *node, char **name)
break; break;
case T_CollateClause: case T_CollateClause:
return FigureColnameInternal(((CollateClause *) node)->arg, name); 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: case T_CaseExpr:
strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult, strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult,
name); name);
......
...@@ -300,8 +300,8 @@ LINE 4: where sum(distinct a.four + b.four) = b.four)... ...@@ -300,8 +300,8 @@ LINE 4: where sum(distinct a.four + b.four) = b.four)...
select select
(select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1))) (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1)))
from tenk1 o; from tenk1 o;
?column? max
---------- ------
9999 9999
(1 row) (1 row)
......
...@@ -490,20 +490,20 @@ select view_a from view_a; ...@@ -490,20 +490,20 @@ select view_a from view_a;
(1 row) (1 row)
select (select view_a) from view_a; select (select view_a) from view_a;
?column? view_a
---------- --------
(42) (42)
(1 row) (1 row)
select (select (select view_a)) from view_a; select (select (select view_a)) from view_a;
?column? view_a
---------- --------
(42) (42)
(1 row) (1 row)
select (select (a.*)::text) from view_a a; select (select (a.*)::text) from view_a a;
?column? a
---------- ------
(42) (42)
(1 row) (1 row)
......
...@@ -1065,7 +1065,7 @@ with cte(foo) as ( select 42 ) select * from ((select foo from cte)) q; ...@@ -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 ( with cte(foo) as ( values(f1) )
select (select foo from cte) ) select (select foo from cte) )
from int4_tbl; from int4_tbl;
?column? foo
------------- -------------
0 0
123456 123456
...@@ -1077,7 +1077,7 @@ from int4_tbl; ...@@ -1077,7 +1077,7 @@ from int4_tbl;
select ( with cte(foo) as ( values(f1) ) select ( with cte(foo) as ( values(f1) )
values((select foo from cte)) ) values((select foo from cte)) )
from int4_tbl; from int4_tbl;
?column? column1
------------- -------------
0 0
123456 123456
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment