Обсуждение: [PATCH] Generate column names for subquery expressions

Поиск
Список
Период
Сортировка

[PATCH] Generate column names for subquery expressions

От
Marti Raudsepp
Дата:
Hi list,

In current PostgreSQL versions, subquery expressions in the SELECT list always generate columns with name "?column?"

postgres=# select (select 1 as foo);
?column?
--------
       1

This patch improves on that:
  select (SELECT 1 AS foo) => foo
  select exists(SELECT 1)  => exists
  select array(SELECT 1)   => array

The "array" one is now consistent with an array literal: select array[1];

Other subquery types (=ALL(), =ANY() and row comparison) don't change because they act more like operators.

I guess it's fairly unlikely that users rely on column names being "?column?", but it does change the name of some expressions, for example:
  select (select 1 foo)::int;
  select case when true then 1 else (select 1 as foo) end;

Previously these returned column names "int4" and "case", now they would return "foo". Personally I prefer it this way, but if it is considered a compatibility problem, lowering the strength of subquery names in FigureColnameInternal would resort to the old behavior.

How this affects different queries can be seen from the regression diffs.

Does this sound like a good idea?
Should I submit this to the CommitFest?


Regards,
Marti Raudsepp

Вложения

Re: [PATCH] Generate column names for subquery expressions

От
Tom Lane
Дата:
Marti Raudsepp <marti@juffo.org> writes:
> In current PostgreSQL versions, subquery expressions in the SELECT list
> always generate columns with name "?column?"
> ...
> This patch improves on that:
>   select (SELECT 1 AS foo) => foo
>   select exists(SELECT 1)  => exists
>   select array(SELECT 1)   => array

> Does this sound like a good idea?

Seems like a lot of room for bikeshedding here, but we could certainly
consider doing something.

> Should I submit this to the CommitFest?

Please.
        regards, tom lane


Re: [PATCH] Generate column names for subquery expressions

От
Marti Raudsepp
Дата:
Here's version 2 of the patch, mainly to silence compiler warnings
about missing "case" statements from a switch over SubLinkType enum.

Also expanded commit message a little and changed whitespace to be
more consistent with nearby code.

Regards,
Marti

Вложения

[REVIEW] Generate column names for subquery expressions

От
Kyotaro HORIGUCHI
Дата:
This is a review for the patch `Generate column names for
subquery expressions'
(https://commitfest.postgresql.org/action/patch_view?id=632)



Summary
====================
Patch format is in context diff format.
This patch applies cleanly on HEAD and make check suceeded.
It seems have no problem to apply.
Documents is needed to modify.


Purpose and function of this patch
====================

This patch intends to name a part of the columns in the outmost
SELECT caluse currently left unnamed - seen as `?column?' - and
fix `unnatural' naming - seen as `int4', `case'.

This patch figures column name after T_SubLink parse nodes
corresponding to EXISTS, ARRAY, and subquery in addition to
currently processed parse node types.

It seems reasonable that (ALL|ANY|ROWCOMPARE|CTE)_SUBLINK is left
unnnamed.


Patch application, regression test
====================
The patch applies cleanly onto HEAD. make check yiels no error.
This patch adds no additional test case and it seems ok.
The coding style in this patch seems according to the convention.


Behavior changes
====================
The behavior of column naming changes as following.
 STATEMENT                    AFTER       BEFORE
-----------------------------------------------------
select (select 1 as foo)       foo         ?column?
select (exists (select 1))     exists      ?column?
select (array (select 1 as x)) array       ?column?
select (select 1 as aaa)::int  aaa         int4

select case when true then 1 else (select 1 as foo) end;                              foo         case

Aboves are same as described. But the following expression
returns somewhat confising outcome.

> select case when true then (select 2 as bar) else (select 1 as foo) end;
>  foo 
> -----
>    2
> (1 row)

But this patch is not to blame for the behavior. The following is
seen for unpatched pg.

> # create table foo (a int, b int, c int);
> # insert into foo values (1, 100, -100), (0, 10, -10), (-1, 25, -25);
> # select case when a < 0 then b else c end from foo;
>   c   
> ------
(snipped)

> # select case a when -1 then c when 1 then a else b end from foo;
>   b  
> -----
(snipped)

Nevertheless this behavior seems a bit unnatural, it is not the
issue for this patch.



Performance
====================

This patch adds no extra load such as loops, recursive calls or
deep calls. Added code runs for exists(), array() and subquery
appear in the column list of select clause. So I think this patch
can put only negligible impact on performance.


Gleaning
====================
This patch assumes node(subLinkType==EXPR_SUBLINK)->subselect is
not null, and it seems that gram.y(c) says the assumption is
correct.

I think this patch needs no documentation, but it is needed to
edit the changed behaviors quoted in document. Maybe only one
change as far as I have seen.

http://www.postgresql.org/docs/9.0/static/sql-expressions.html

> 4.2.11
.. 
> SELECT ARRAY(SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%');
>                           ?column?
> -------------------------------------------------------------
>  {2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31}



Sincerely,

-- 
Kyotaro Horiguchi


Re: [REVIEW] Generate column names for subquery expressions

От
Marti Raudsepp
Дата:
On Wed, Sep 14, 2011 at 05:26, Kyotaro HORIGUCHI
<horiguchi.kyotaro@oss.ntt.co.jp> wrote:
> This is a review for the patch `Generate column names for
> subquery expressions'
> (https://commitfest.postgresql.org/action/patch_view?id=632)

Thanks for the review. :)

PS: When you send a review, you should add the author's email to the
"To:" line to make sure they see it. I noticed your email only today
because it was in a new thread and not addressed to me directly.

> I think this patch needs no documentation, but it is needed to
> edit the changed behaviors quoted in document. Maybe only one
> change as far as I have seen.
>
> http://www.postgresql.org/docs/9.0/static/sql-expressions.html

>> SELECT ARRAY(SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%');
>>                           ?column?
>> -------------------------------------------------------------
>>  {2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31}

Good catch, a new patch is attached. Apparently the results of this
query have also changed in recent versions, but I didn't touch that.

Regards,
Marti

Вложения

Re: [REVIEW] Generate column names for subquery expressions

От
Kyotaro HORIGUCHI
Дата:
Hi, 

> PS: When you send a review, you should add the author's email to the
> "To:" line to make sure they see it. I noticed your email only today
> because it was in a new thread and not addressed to me directly.
Thanks for the advise. I will do so after this.

> Good catch, a new patch is attached. Apparently the results of this
> query have also changed in recent versions, but I didn't touch that.
I've comfirmed that is fixed.I'll send this comitter review.

-- 
Kyotaro Horiguchi
NTT Open Source Software Center


Re: [REVIEW] Generate column names for subquery expressions

От
Tom Lane
Дата:
Marti Raudsepp <marti@juffo.org> writes:
> On Wed, Sep 14, 2011 at 05:26, Kyotaro HORIGUCHI
> <horiguchi.kyotaro@oss.ntt.co.jp> wrote:
>> This is a review for the patch `Generate column names for
>> subquery expressions'
>> (https://commitfest.postgresql.org/action/patch_view?id=632)

> Thanks for the review. :)

Applied with minor adjustments.
        regards, tom lane