Re: [REVIEW] Generate column names for subquery expressions

Lists: pgsql-hackers
From: Marti Raudsepp <marti(at)juffo(dot)org>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: [PATCH] Generate column names for subquery expressions
Date: 2011-08-31 22:36:53
Message-ID: CABRT9RCGKfCwOLCQEgo7O+jnOTro-oZB=j7E_qiA0s=x4AgaYA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

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

Attachment Content-Type Size
0001-Generate-column-names-for-subquery-expressions.patch text/x-patch 3.4 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Marti Raudsepp <marti(at)juffo(dot)org>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [PATCH] Generate column names for subquery expressions
Date: 2011-09-01 01:48:20
Message-ID: 9810.1314841700@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Marti Raudsepp <marti(at)juffo(dot)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


From: Marti Raudsepp <marti(at)juffo(dot)org>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [PATCH] Generate column names for subquery expressions
Date: 2011-09-06 18:28:10
Message-ID: CABRT9RB4muob24k=cGux5ETvdTc5NAfGu3VGCckZwq-V4V9tAQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

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

Attachment Content-Type Size
0001-Generate-column-names-for-subquery-expressions-inste.patch text/x-patch 4.0 KB

From: Kyotaro HORIGUCHI <horiguchi(dot)kyotaro(at)oss(dot)ntt(dot)co(dot)jp>
To: pgsql-hackers(at)postgresql(dot)org
Subject: [REVIEW] Generate column names for subquery expressions
Date: 2011-09-14 02:26:51
Message-ID: 20110914.112651.19132478.horiguchi.kyotaro@oss.ntt.co.jp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

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


From: Marti Raudsepp <marti(at)juffo(dot)org>
To: Kyotaro HORIGUCHI <horiguchi(dot)kyotaro(at)oss(dot)ntt(dot)co(dot)jp>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: [REVIEW] Generate column names for subquery expressions
Date: 2011-09-18 23:28:14
Message-ID: CABRT9RCm=rDPogJ8+v0maHPEisUuDf2=02K0=sGCq2iwWjtqeg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Sep 14, 2011 at 05:26, Kyotaro HORIGUCHI
<horiguchi(dot)kyotaro(at)oss(dot)ntt(dot)co(dot)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

Attachment Content-Type Size
Generate-column-names-for-subquery-expressions-v3.patch text/x-patch 4.5 KB

From: Kyotaro HORIGUCHI <horiguchi(dot)kyotaro(at)oss(dot)ntt(dot)co(dot)jp>
To: marti(at)juffo(dot)org
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: [REVIEW] Generate column names for subquery expressions
Date: 2011-09-29 08:26:31
Message-ID: 20110929.172631.124462348.horiguchi.kyotaro@oss.ntt.co.jp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

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


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Marti Raudsepp <marti(at)juffo(dot)org>
Cc: Kyotaro HORIGUCHI <horiguchi(dot)kyotaro(at)oss(dot)ntt(dot)co(dot)jp>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [REVIEW] Generate column names for subquery expressions
Date: 2011-10-01 18:02:52
Message-ID: 29014.1317492172@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Marti Raudsepp <marti(at)juffo(dot)org> writes:
> On Wed, Sep 14, 2011 at 05:26, Kyotaro HORIGUCHI
> <horiguchi(dot)kyotaro(at)oss(dot)ntt(dot)co(dot)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