Thread: Table function support

Table function support

From
"Pavel Stehule"
Date:
Hello

this patch allows using SQL2003 syntax for set returning functions. It is
based on using new type of argmode - PROARGMODE_TABLE.

Proposal: http://archives.postgresql.org/pgsql-hackers/2007-02/msg00318.php

Sample:

CREATE FUNCTION foof(a int)
RETURNS TABLE(a int, b int) AS
$$ SELECT x, y FROM Foo WHERE x < a $$ LANGUAGE sql;

CREATE FUNCTION fooff(a int)
RETURNS TABLE(a int, b int) AS $$
BEGIN
  RETURN TABLE(SELECT * FRON Foo WHERE x < a);
END; $$ LANGUAGE plpgsql;

This patch enhance plpgsql stmt return too (table expression support).

Conformance with SQL2003:
T326     Table functions

Description: SIGMOD Record, Vol. 33, No. 1, March 2004

Regards
Pavel Stehule

_________________________________________________________________
Emotikony a pozadi programu MSN Messenger ozivi vasi konverzaci.
http://messenger.msn.cz/

Attachment

Re: Table function support

From
Bruce Momjian
Date:
Your patch has been added to the PostgreSQL unapplied patches list at:

    http://momjian.postgresql.org/cgi-bin/pgpatches

It will be applied as soon as one of the PostgreSQL committers reviews
and approves it.

---------------------------------------------------------------------------


Pavel Stehule wrote:
> Hello
>
> this patch allows using SQL2003 syntax for set returning functions. It is
> based on using new type of argmode - PROARGMODE_TABLE.
>
> Proposal: http://archives.postgresql.org/pgsql-hackers/2007-02/msg00318.php
>
> Sample:
>
> CREATE FUNCTION foof(a int)
> RETURNS TABLE(a int, b int) AS
> $$ SELECT x, y FROM Foo WHERE x < a $$ LANGUAGE sql;
>
> CREATE FUNCTION fooff(a int)
> RETURNS TABLE(a int, b int) AS $$
> BEGIN
>   RETURN TABLE(SELECT * FRON Foo WHERE x < a);
> END; $$ LANGUAGE plpgsql;
>
> This patch enhance plpgsql stmt return too (table expression support).
>
> Conformance with SQL2003:
> T326     Table functions
>
> Description: SIGMOD Record, Vol. 33, No. 1, March 2004
>
> Regards
> Pavel Stehule
>
> _________________________________________________________________
> Emotikony a pozadi programu MSN Messenger ozivi vasi konverzaci.
> http://messenger.msn.cz/

[ Attachment, skipping... ]

>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: You can help support the PostgreSQL project by donating at
>
>                 http://www.postgresql.org/about/donate

--
  Bruce Momjian  <bruce@momjian.us>          http://momjian.us
  EnterpriseDB                               http://www.enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +

Re: Table function support

From
Tom Lane
Date:
"Pavel Stehule" <pavel.stehule@hotmail.com> writes:
> this patch allows using SQL2003 syntax for set returning functions. It is
> based on using new type of argmode - PROARGMODE_TABLE.

I've been looking at this, and my feeling is that we should drop the
PROARGMODE_TABLE business and just define RETURNS TABLE(x int, y int)
as exactly equivalent to RETURNS SETOF RECORD with x and y treated as
OUT parameters.  There isn't any advantage to distinguishing the cases
that outweighs breaking client code that looks at pg_proc.proargmodes.
I don't believe that the SQL spec prevents us from exposing those
parameter names to PL functions, especially since none of our PLs are
in the standard at all.

            regards, tom lane

Re: Table function support

From
"Pavel Stehule"
Date:
>I've been looking at this, and my feeling is that we should drop the
>PROARGMODE_TABLE business and just define RETURNS TABLE(x int, y int)
>as exactly equivalent to RETURNS SETOF RECORD with x and y treated as
>OUT parameters.  There isn't any advantage to distinguishing the cases
>that outweighs breaking client code that looks at pg_proc.proargmodes.
>I don't believe that the SQL spec prevents us from exposing those
>parameter names to PL functions, especially since none of our PLs are
>in the standard at all.
>

Reason for PROARGMODE_TABLE was protection before name's collision, and x,
and y are table attributies (not variables) and then we are protected before
collision. It's shortcut for

create function foo() returns setof record as ...
select * from foo() as (x int, y int);

Regards
Pavel Stehule

_________________________________________________________________
Najdete si svou lasku a nove pratele na Match.com. http://www.msn.cz/


Re: Table function support

From
"Pavel Stehule"
Date:
>I've been looking at this, and my feeling is that we should drop the
>PROARGMODE_TABLE business and just define RETURNS TABLE(x int, y int)
>as exactly equivalent to RETURNS SETOF RECORD with x and y treated as
>OUT parameters.  There isn't any advantage to distinguishing the cases
>that outweighs breaking client code that looks at pg_proc.proargmodes.
>I don't believe that the SQL spec prevents us from exposing those
>parameter names to PL functions, especially since none of our PLs are
>in the standard at all.
>

Reason for PROARGMODE_TABLE was protection before name's collision, and x,
and y are table attributies (not variables) and then we are protected before
collision. It's shortcut for

create function foo() returns setof record as ...
select * from foo() as (x int, y int);

Regards
Pavel Stehule

_________________________________________________________________
Najdete si svou lasku a nove pratele na Match.com. http://www.msn.cz/


Re: Table function support

From
"Pavel Stehule"
Date:
Hello

I searched some notes about this topic. I didn't find any usefull sample.
Lot of samples are about external stored procedures and others about using
table expression like

create function foo(i1)
returns table (a1 int)
as
  return table(select a1 from tab)

isn't clear if table attributes are related to output variables, but nobody
join it together.

SQL/PSM sample:
create function accounts_of (customer_name char(20))
     returns table (       account_number char(10),
                           branch_name char(15)
                           balance numeric(12,2))
return table
     (select account_number, branch_name, balance
      from account A
      where exists (
          select *
          from depositor D
          where D.customer_name = accounts_of.customer_name
                    and D.account_number = A.account_number ))


correct calling of it is:
select *
from table (accounts_of (�Smith�))

next sample:
CREATE FUNCTION filmtyp (art CHAR(2))
  RETURNS TABLE (titel VARCHAR(75), jahr INTEGER)
  LANGUAGE SQL
  READS SQL DATA
  NO EXTERNAL ACTION
  DETERMINISTIC
  RETURN
     SELECT titel, jahr
     FROM film
     WHERE film.art = filmtyp.art


Table functions are named as parametrised views too. I don't thing using OUT
variables is good idea, because you will have problems with colum's names,
which is problem for plpgsql.

http://www.wiscorp.com/SQL2003Features.pdf

http://wwwdvs.informatik.uni-kl.de/courses/NEDM/SS2004/Vorlesungsunterlagen/NEDM.Chapter.03.User-defined_Routines_and_Object_Behavior.pdf

Regards
Pavel Stehule



>From: Tom Lane <tgl@sss.pgh.pa.us>
>To: "Pavel Stehule" <pavel.stehule@hotmail.com>
>CC: pgsql-patches@postgresql.org
>Subject: Re: [PATCHES] Table function support Date: Tue, 10 Apr 2007
>18:17:14 -0400
>
>"Pavel Stehule" <pavel.stehule@hotmail.com> writes:
> > this patch allows using SQL2003 syntax for set returning functions. It
>is
> > based on using new type of argmode - PROARGMODE_TABLE.
>
>I've been looking at this, and my feeling is that we should drop the
>PROARGMODE_TABLE business and just define RETURNS TABLE(x int, y int)
>as exactly equivalent to RETURNS SETOF RECORD with x and y treated as
>OUT parameters.  There isn't any advantage to distinguishing the cases
>that outweighs breaking client code that looks at pg_proc.proargmodes.
>I don't believe that the SQL spec prevents us from exposing those
>parameter names to PL functions, especially since none of our PLs are
>in the standard at all.
>
>            regards, tom lane

_________________________________________________________________
Najdete si svou lasku a nove pratele na Match.com. http://www.msn.cz/