Re: Cast as compound type

Lists: pgsql-hackers
From: David Fetter <david(at)fetter(dot)org>
To: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Cast as compound type
Date: 2008-03-30 18:15:13
Message-ID: 20080330181513.GA12289@fetter.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Folks,

I'd like to take a whack at making set-returning functions returning
SETOF RECORD a little more fun to use. Let's imagine that we have a
table foo and a function returning SETOF RECORD that can return foos.
The call might look something like:

SELECT a, b, c
FROM f(ROW OF foo)
WHERE ...;

This would make it much easier and less error-prone to use SETOF
RECORD.

Would others like to see such a feature?

If so, what pieces of the code would I be touching for the first
patch?

Cheers,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david(dot)fetter(at)gmail(dot)com

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate


From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "David Fetter" <david(at)fetter(dot)org>
Cc: "PG Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Cast as compound type
Date: 2008-03-30 20:00:33
Message-ID: 162867790803301300p44b6b276g4cd9a44773b96f1c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello

maybe I don't understand well your idea. There exist simple syntax -
table function

http://archives.postgresql.org/pgsql-patches/2007-02/msg00341.php

and it is standard

regards
Pavel Stehule

On 30/03/2008, David Fetter <david(at)fetter(dot)org> wrote:
> Folks,
>
> I'd like to take a whack at making set-returning functions returning
> SETOF RECORD a little more fun to use. Let's imagine that we have a
> table foo and a function returning SETOF RECORD that can return foos.
> The call might look something like:
>
> SELECT a, b, c
> FROM f(ROW OF foo)
> WHERE ...;
>
> This would make it much easier and less error-prone to use SETOF
> RECORD.
>
> Would others like to see such a feature?
>
> If so, what pieces of the code would I be touching for the first
> patch?
>
> Cheers,
> David.
> --
> David Fetter <david(at)fetter(dot)org> http://fetter.org/
> Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
> Skype: davidfetter XMPP: david(dot)fetter(at)gmail(dot)com
>
> Remember to vote!
> Consider donating to Postgres: http://www.postgresql.org/about/donate
>
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>


From: David Fetter <david(at)fetter(dot)org>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Cast as compound type
Date: 2008-03-30 20:15:24
Message-ID: 20080330201524.GB12289@fetter.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sun, Mar 30, 2008 at 10:00:33PM +0200, Pavel Stehule wrote:
> Hello
>
> maybe I don't understand well your idea. There exist simple syntax -
> table function
>
> http://archives.postgresql.org/pgsql-patches/2007-02/msg00341.php
>
> and it is standard

It's completely different from your patch, which specifies the return
type at the time you create the function.

This idea takes functions which return SETOF RECORD, that is functions
which determine their return type at run time instead of create time,
and short-cuts the cast that you need to do at run time.

Cheers,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david(dot)fetter(at)gmail(dot)com

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate


From: "korry" <korry(dot)douglas(at)enterprisedb(dot)com>
To: "David Fetter" <david(at)fetter(dot)org>
Cc: "PG Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Cast as compound type
Date: 2008-03-31 23:18:43
Message-ID: 47F17153.60507@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

David Fetter wrote:
> I'd like to take a whack at making set-returning functions returning
> SETOF RECORD a little more fun to use. Let's imagine that we have a
> table foo and a function returning SETOF RECORD that can return foos.
> The call might look something like:
>
> SELECT a, b, c
> FROM f(ROW OF foo)
> WHERE ...;
>
> This would make it much easier and less error-prone to use SETOF
> RECORD.
>
David, it sounds like you really want to declare the return type of the
function? In your above example, you want to say that, in this
particular invocation, function f() returns a SETOF foo's. Is that correct?

If you were to create function that returns a RECORD (not a SETOF
RECORD), you would call it like this:

SELECT * FROM f() AS (column1 type1, column2 type2, column3 type3);

In your case, I think you want to declare the return type using an
explicitly defined composite type (possibly a table row); which would
imply syntax such as:

SELECT * FROM f() AS (foo);
or
SELECT * FROM f() AS (foo.*);

So, it seems like you want the syntax to look more like:

SELECT a,b,c, FROM f() AS (SETOF foo);

Does that make sense to you? Your original syntax implied that the "ROW
OF foo" was somehow related to the function arguments.

-- Korry

--

Korry Douglas <korryd(at)enterprisedb(dot)com>
EnterpriseDB http://www.enterprisedb.com


From: David Fetter <david(at)fetter(dot)org>
To: korry <korry(dot)douglas(at)enterprisedb(dot)com>
Cc: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Cast as compound type
Date: 2008-04-01 16:35:03
Message-ID: 20080401163502.GA25159@fetter.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Mar 31, 2008 at 07:18:43PM -0400, Korry Douglas wrote:
> David Fetter wrote:
>> I'd like to take a whack at making set-returning functions
>> returning SETOF RECORD a little more fun to use. Let's imagine
>> that we have a table foo and a function returning SETOF RECORD that
>> can return foos. The call might look something like:
>>
>> SELECT a, b, c
>> FROM f(ROW OF foo)
>> WHERE ...;
>>
>> This would make it much easier and less error-prone to use SETOF
>> RECORD.
>>
> David, it sounds like you really want to declare the return type of
> the function? In your above example, you want to say that, in this
> particular invocation, function f() returns a SETOF foo's. Is that
> correct?

Yes.

> If you were to create function that returns a RECORD (not a SETOF RECORD),
> you would call it like this:
>
> SELECT * FROM f() AS (column1 type1, column2 type2, column3 type3);
>
> In your case, I think you want to declare the return type using an
> explicitly defined composite type (possibly a table row); which would imply
> syntax such as:
>
> SELECT * FROM f() AS (foo);
> or
> SELECT * FROM f() AS (foo.*);
>
> So, it seems like you want the syntax to look more like:
>
> SELECT a,b,c, FROM f() AS (SETOF foo);
>
> Does that make sense to you? Your original syntax implied that the
> "ROW OF foo" was somehow related to the function arguments.
> -- Korry

I see.

Thinking a little further, it seems we could do this a little more
generally. Here's what it could look like.

AS (<column_set_description> {, <column_set_description})

<column_set_description> =
<column_name> <simple_data_type_name> |
[ <compound_data_type_prefix> ] <compound_data_type_name>;

<compound_data_type_prefix> would be prepended to each column in the
output, so for a compound type foo(i int, t text, p point), AS (f foo)
would produce output columns f.i, f.t and f.p. Typical uses for this
would be to keep a set of column names distinct.

Cheers,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david(dot)fetter(at)gmail(dot)com

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate