pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Philippe Lang <philippe.lang@attiksystem.ch>
Cc: pgsql-sql@postgresql.org
Subject: Re: function expression in FROM may not refer to other relations of same query level
Date: Thu, 12 Aug 2004 10:30:41 -0400
Message-ID: <28661.1092321041@sss.pgh.pa.us> (raw)
In-Reply-To: <6C0CF58A187DA5479245E0830AF84F420803B7@poweredge.attiksystem.ch>
References: <6C0CF58A187DA5479245E0830AF84F420803B7@poweredge.attiksystem.ch>

"Philippe Lang" <philippe.lang@attiksystem.ch> writes:
> I wish there was a way to run the query like this:

> select
>   id,
>   usr,
>   code,
>   CAST(get_lines(code) as lines)
> from tbl;

You can do something like this:

regression=# create type complex as (r float8, i float8);
CREATE TYPE
regression=# create function fooey(float8) returns complex as
regression-# 'select $1 + 1, $1 + 2' language sql;
CREATE FUNCTION
regression=# select f1, (fooey).r, (fooey).i from
regression-# (select f1, fooey(f1) as fooey from float8_tbl) ss;
          f1           |           r           |           i
-----------------------+-----------------------+-----------------------
                     0 |                     1 |                     2
                -34.84 |                -33.84 |                -32.84
               -1004.3 |               -1003.3 |               -1002.3
 -1.2345678901234e+200 | -1.2345678901234e+200 | -1.2345678901234e+200
 -1.2345678901234e-200 |                     1 |                     2
(5 rows)

Note the odd-looking parenthesization --- you can't write just "fooey.r"
because that looks like it should be a table and field name, not a field
name that is selected from.

If the sub-select is too simple, as it is in this case, the planner is
likely to "flatten out" the query into

select f1, (fooey(f1)).r, (fooey(f1)).i from float8_tbl;

thus defeating your purpose of not calling the function twice.  The
currently best hack for preventing this is to add "OFFSET 0" to the
sub-select:

select f1, (fooey).r, (fooey).i from
(select f1, fooey(f1) as fooey from float8_tbl offset 0) ss;

			regards, tom lane



view thread (9+ messages)  latest in thread

Message-ID: <28661.1092321041@sss.pgh.pa.us>
Permalink:  ../28661.1092321041@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/28661.1092321041@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, philippe.lang@attiksystem.ch
  Subject: Re: function expression in FROM may not refer to other relations of same query level
  In-Reply-To: <28661.1092321041@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox