Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5ec6b7f

Browse files
committed
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
1 parent 878b74e commit 5ec6b7f

File tree

6 files changed

+59
-16
lines changed

6 files changed

+59
-16
lines changed

doc/src/sgml/ref/select.sgml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,9 @@ UNBOUNDED FOLLOWING
758758
If you do not specify a column name, a name is chosen automatically
759759
by <productname>PostgreSQL</productname>. If the column's expression
760760
is a simple column reference then the chosen name is the same as that
761-
column's name; in more complex cases a generated name looking like
762-
<literal>?column<replaceable>N</>?</literal> is usually chosen.
761+
column's name. In more complex cases a function or type name may be
762+
used, or the system may fall back on a generated name such as
763+
<literal>?column?</literal>.
763764
</para>
764765

765766
<para>

doc/src/sgml/syntax.sgml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,9 +2109,9 @@ SELECT ARRAY[]::integer[];
21092109
bracketed) subquery. For example:
21102110
<programlisting>
21112111
SELECT ARRAY(SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%');
2112-
?column?
2113-
-------------------------------------------------------------
2114-
{2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31}
2112+
array
2113+
-----------------------------------------------------------------------
2114+
{2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31,2412,2413}
21152115
(1 row)
21162116
</programlisting>
21172117
The subquery must return a single column. The resulting

src/backend/parser/parse_target.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,48 @@ FigureColnameInternal(Node *node, char **name)
16101610
break;
16111611
case T_CollateClause:
16121612
return FigureColnameInternal(((CollateClause *) node)->arg, name);
1613+
case T_SubLink:
1614+
switch (((SubLink *) node)->subLinkType)
1615+
{
1616+
case EXISTS_SUBLINK:
1617+
*name = "exists";
1618+
return 2;
1619+
case ARRAY_SUBLINK:
1620+
*name = "array";
1621+
return 2;
1622+
case EXPR_SUBLINK:
1623+
{
1624+
/* Get column name of the subquery's single target */
1625+
SubLink *sublink = (SubLink *) node;
1626+
Query *query = (Query *) sublink->subselect;
1627+
1628+
/*
1629+
* The subquery has probably already been transformed,
1630+
* but let's be careful and check that. (The reason
1631+
* we can see a transformed subquery here is that
1632+
* transformSubLink is lazy and modifies the SubLink
1633+
* node in-place.)
1634+
*/
1635+
if (IsA(query, Query))
1636+
{
1637+
TargetEntry *te = (TargetEntry *) linitial(query->targetList);
1638+
1639+
if (te->resname)
1640+
{
1641+
*name = te->resname;
1642+
return 2;
1643+
}
1644+
}
1645+
}
1646+
break;
1647+
/* As with other operator-like nodes, these have no names */
1648+
case ALL_SUBLINK:
1649+
case ANY_SUBLINK:
1650+
case ROWCOMPARE_SUBLINK:
1651+
case CTE_SUBLINK:
1652+
break;
1653+
}
1654+
break;
16131655
case T_CaseExpr:
16141656
strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult,
16151657
name);

src/test/regress/expected/aggregates.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ LINE 4: where sum(distinct a.four + b.four) = b.four)...
300300
select
301301
(select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1)))
302302
from tenk1 o;
303-
?column?
304-
----------
305-
9999
303+
max
304+
------
305+
9999
306306
(1 row)
307307

308308
--

src/test/regress/expected/subselect.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,20 +490,20 @@ select view_a from view_a;
490490
(1 row)
491491

492492
select (select view_a) from view_a;
493-
?column?
494-
----------
493+
view_a
494+
--------
495495
(42)
496496
(1 row)
497497

498498
select (select (select view_a)) from view_a;
499-
?column?
500-
----------
499+
view_a
500+
--------
501501
(42)
502502
(1 row)
503503

504504
select (select (a.*)::text) from view_a a;
505-
?column?
506-
----------
505+
a
506+
------
507507
(42)
508508
(1 row)
509509

src/test/regress/expected/with.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ with cte(foo) as ( select 42 ) select * from ((select foo from cte)) q;
10651065
select ( with cte(foo) as ( values(f1) )
10661066
select (select foo from cte) )
10671067
from int4_tbl;
1068-
?column?
1068+
foo
10691069
-------------
10701070
0
10711071
123456
@@ -1077,7 +1077,7 @@ from int4_tbl;
10771077
select ( with cte(foo) as ( values(f1) )
10781078
values((select foo from cte)) )
10791079
from int4_tbl;
1080-
?column?
1080+
column1
10811081
-------------
10821082
0
10831083
123456

0 commit comments

Comments
 (0)