Re: anonymous composite types for Table Functions (aka

From: Joe Conway <mail(at)joeconway(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, pgsql-patches(at)postgresql(dot)org
Subject: Re: anonymous composite types for Table Functions (aka
Date: 2002-08-05 15:12:27
Message-ID: 3D4E95DB.8060104@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-patches

Tom Lane wrote:
> In the cold light of morning I started to wonder what should happen if
> you write "from foo() as z" when foo returns a tuple. It would probably
> be peculiar for the z to overwrite the column name of just the first
> column --- there is no such column renaming for an ordinary table alias.
>
> My current thought: z becomes the table alias, and it also becomes the
> column alias *if* the function returns scalar. For a function returning
> tuple, this syntax doesn't affect the column names. (In any case this
> syntax is disallowed for functions returning RECORD.)

I think the one liner patch I sent in last night does exactly what you
describe -- so I guess we're in complete agreement ;-)

See below:

test=# \d foo
Table "foo"
Column | Type | Modifiers
--------+---------+-----------
f1 | bytea |
f2 | integer |
Indexes: foo_idx1 btree (f1)

test=# create function foo1() returns setof int as 'select f2 from foo'
language sql;
CREATE FUNCTION
test=# create function foo2() returns setof foo as 'select * from foo'
language sql;
CREATE FUNCTION
test=# select * from foo1() as z where z.z = 1;
z
---
1
(1 row)

test=# select * from foo1() as z(a) where z.a = 1;
a
---
1
(1 row)

test=# select * from foo2() as z where z.f2 = 1;
f1 | f2
------------------------+----
\237M(at)y[J\272z\304\003 | 1
(1 row)

test=# select * from foo2() as z(a) where z.f2 = 1;
a | f2
------------------------+----
\237M(at)y[J\272z\304\003 | 1
(1 row)

test=# create function foo3() returns setof record as 'select * from
foo' language sql;
CREATE FUNCTION
test=# select * from foo3() as z where z.f2 = 1;
ERROR: A column definition list is required for functions returning RECORD
test=# select * from foo3() as z(a bytea, b int) where z.f2 = 1;
ERROR: No such attribute z.f2
test=# select * from foo3() as z(a bytea, b int) where z.b = 1;
a | b
------------------------+---
\237M(at)y[J\272z\304\003 | 1
(1 row)

Joe

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Joe Conway 2002-08-05 15:18:38 Re: FUNC_MAX_ARGS benchmarks
Previous Message Tom Lane 2002-08-05 14:58:26 Re: Did someone break CVS?

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2002-08-05 16:23:02 Re: anonymous composite types for Table Functions (aka SRFs)
Previous Message Tom Lane 2002-08-05 13:37:10 Re: anonymous composite types for Table Functions (aka SRFs)