Re: idea: allow AS label inside ROW constructor

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: idea: allow AS label inside ROW constructor
Date: 2014-10-23 09:34:04
Message-ID: CAFj8pRDEsqKFZPK3G_4SyYWhxZcZZo07-7QFPK4kKEHqzaBi=Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi

here is a prototype

postgres=# select row_to_json(row(10 as A, row(30 as c, 20 AS B) as x));
row_to_json
------------------------------
{"a":10,"x":{"c":30,"b":20}}
(1 row)

postgres=# select row_to_json(row(10, row(30, 20)));
row_to_json
----------------------------------
{"f1":10,"f2":{"f1":30,"f2":20}}
(1 row)

Regards

Pavel

2014-10-22 19:09 GMT+02:00 Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>:

>
>
> 2014-10-22 18:35 GMT+02:00 Merlin Moncure <mmoncure(at)gmail(dot)com>:
>
>> On Wed, Oct 22, 2014 at 11:21 AM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
>> wrote:
>> > Hi
>> >
>> > with new functions row_to_json(b), there is more often usage of ROW
>> > constructor. Using names in fields is relative difficult. Because ROW
>> has
>> > special clause in parser, I am thinking so we can enable labeling
>> inside ROW
>> > constructor
>> >
>> > so instead currently supported:
>> >
>> > select row_to_json(r) from (select 10 as a, 20 as b) r;
>> >
>> > users can to write:
>> >
>> > select row_to_json(row(10 as a,20 as b));
>> >
>> > labeling will be enabled "only" inside ROW constructor. I don't propose
>> > enable it everywhere.
>> >
>> > What do you think about it?
>>
>> It's a neat idea -- maybe a better alternative to what I was thinking
>> here:
>> http://postgresql.1045698.n5.nabble.com/Support-UPDATE-table-SET-tp5823073p5823944.html
>>
>> Some questions:
>> *) What would the parser transformation resolve to
>>
>
> row: ROW '(' expr_list ')' { $$
> = $3; }
> | ROW '('
> ')' { $$ = NIL; }
> | '(' expr_list ',' a_expr ')' {
> $$ = lappend($2, $4); }
> ;
>
> we can replace a expr_list by target_list. I know only so it doesn't
> enforce a problems with gramatic - bison doesn't raise any warning.
>
>
> *) Are we ok with SQL standard
>>
>
> SQL standard doesn't think named attributes in row - so it is out of range
> ANSI. But it is not in conflict with standard. "AS name" is used more in
> SQL/MM, SQL/XML -- and function named parameters has different syntax
> "parameter_name <= value" - I checked it against SQL99.
>
>
>> *) Do you think this (or some similar variant) would work?
>>
>> select row_to_json(row(foo.*)) from foo;
>>
>
> It looks like independent feature and can work too - it is more natural
> for user.
>
>
>>
>> merlin
>>
>
>

Attachment Content-Type Size
row_fields_labeling-prototype.patch text/x-patch 6.8 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David Rowley 2014-10-23 09:41:33 Re: [Windows,PATCH] Use faster, higher precision timer API
Previous Message Etsuro Fujita 2014-10-23 09:29:07 Incorrect comment in tablecmds.c