pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
STRICT function returning a composite type
7+ messages / 3 participants
[nested] [flat]

* STRICT function returning a composite type
@ 2003-11-13 16:08  Alexander M. Pravking <fduch@antar.bryansk.ru>
  0 siblings, 1 reply; 7+ messages in thread

From: Alexander M. Pravking @ 2003-11-13 16:08 UTC (permalink / raw)
  To: pgsql-sql

I noted that such a function returns an empty rowset if a NULL value is
passed as an argument. Is it a bug or feature? I wish it was a feature,
because I probably want to use this behavour.

Here's an example:

CREATE TYPE ts_bounds AS (
        sdate   timestamptz,
        edate   timestamptz
);

CREATE OR REPLACE FUNCTION overlap_bounds(timestamptz, timestamptz, timestamptz, timestamptz)
RETURNS ts_bounds AS '
DECLARE
        sdate1  ALIAS FOR $1;
        edate1  ALIAS FOR $2;
        sdate2  ALIAS FOR $3;
        edate2  ALIAS FOR $4;
        res     ts_bounds%rowtype;
BEGIN
        res.sdate := CASE WHEN sdate1 > sdate2 THEN sdate1 ELSE sdate2 END;
        res.edate := CASE WHEN edate1 < edate2 THEN edate1 ELSE edate2 END;
        IF res.sdate > res.edate THEN
                res.sdate := NULL;
                res.edate := NULL;
        END IF;
        RETURN res;
END' LANGUAGE 'plPgSQL' STRICT;


fduch=# SELECT * from overlap_bounds('-infinity', 'today', 'yesterday', 'infinity');
         sdate          |         edate
------------------------+------------------------
 2003-11-12 00:00:00+03 | 2003-11-13 00:00:00+03
(1 row)

fduch=# SELECT * from overlap_bounds('-infinity', 'today', 'yesterday', null);
 sdate | edate
-------+-------
(0 rows)


What I want is to get no rows if given intervals don't overlap instead of:
fduch=# SELECT * from overlap_bounds('-infinity', 'yesterday', 'today', 'infinity');
 sdate | edate
-------+-------
       |
(1 row)

Is it possible without returning SETOF ts_bounds?


fduch=# SELECT version();
                               version
---------------------------------------------------------------------
 PostgreSQL 7.3.4 on i386-portbld-freebsd4.8, compiled by GCC 2.95.4


-- 
Fduch M. Pravking



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: STRICT function returning a composite type
@ 2003-11-13 17:14  Richard Huxton <dev@archonet.com>
  parent: Alexander M. Pravking <fduch@antar.bryansk.ru>
  0 siblings, 1 reply; 7+ messages in thread

From: Richard Huxton @ 2003-11-13 17:14 UTC (permalink / raw)
  To: Alexander M. Pravking <fduch@antar.bryansk.ru>; pgsql-sql

On Thursday 13 November 2003 16:08, Alexander M. Pravking wrote:
> I noted that such a function returns an empty rowset if a NULL value is
> passed as an argument. Is it a bug or feature? I wish it was a feature,
> because I probably want to use this behavour.



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: STRICT function returning a composite type
@ 2003-11-13 17:19  Alexander M. Pravking <fduch@antar.bryansk.ru>
  parent: Richard Huxton <dev@archonet.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Alexander M. Pravking @ 2003-11-13 17:19 UTC (permalink / raw)
  To: Richard Huxton <dev@archonet.com>; +Cc: pgsql-sql

On Thu, Nov 13, 2003 at 05:14:27PM +0000, Richard Huxton wrote:
> RETURNS NULL ON NULL INPUT or STRICT indicates that the function always 
> returns NULL whenever any of its arguments are NULL. If this parameter is 
> specified, the function is not executed when there are NULL arguments; 
> instead a NULL result is assumed automatically.

Does "NULL result" mean an empty rowset if the function returns a record?

-- 
Fduch M. Pravking



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: STRICT function returning a composite type
@ 2003-11-13 17:27  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Alexander M. Pravking <fduch@antar.bryansk.ru>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 2003-11-13 17:27 UTC (permalink / raw)
  To: Alexander M. Pravking <fduch@antar.bryansk.ru>; +Cc: Richard Huxton <dev@archonet.com>; pgsql-sql

"Alexander M. Pravking" <fduch@antar.bryansk.ru> writes:
> On Thu, Nov 13, 2003 at 05:14:27PM +0000, Richard Huxton wrote:
>> RETURNS NULL ON NULL INPUT or STRICT indicates that the function always 
>> returns NULL whenever any of its arguments are NULL.

> Does "NULL result" mean an empty rowset if the function returns a record?

No, it means a null record.  "Empty rowset" would apply to a function
declared to return SETOF something.   (I believe that is how we
interpret the concept of strictness for functions returning sets.)

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: STRICT function returning a composite type
@ 2003-11-13 17:32  Alexander M. Pravking <fduch@antar.bryansk.ru>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Alexander M. Pravking @ 2003-11-13 17:32 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Richard Huxton <dev@archonet.com>; pgsql-sql

On Thu, Nov 13, 2003 at 12:27:58PM -0500, Tom Lane wrote:
> "Alexander M. Pravking" <fduch@antar.bryansk.ru> writes:
> > Does "NULL result" mean an empty rowset if the function returns a record?
> 
> No, it means a null record.  "Empty rowset" would apply to a function
> declared to return SETOF something.   (I believe that is how we
> interpret the concept of strictness for functions returning sets.)

Very well then... Can I return a null record from such function
explicitly? Sorry, I could't find it anywhere in docs or examples.


-- 
Fduch M. Pravking



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: STRICT function returning a composite type
@ 2003-11-13 17:35  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Alexander M. Pravking <fduch@antar.bryansk.ru>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 2003-11-13 17:35 UTC (permalink / raw)
  To: Alexander M. Pravking <fduch@antar.bryansk.ru>; +Cc: Richard Huxton <dev@archonet.com>; pgsql-sql

"Alexander M. Pravking" <fduch@antar.bryansk.ru> writes:
> Very well then... Can I return a null record from such function
> explicitly? Sorry, I could't find it anywhere in docs or examples.

Not sure.  Seems like you should be able to, but I've never tried it.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: STRICT function returning a composite type
@ 2003-11-13 17:42  Alexander M. Pravking <fduch@antar.bryansk.ru>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 7+ messages in thread

From: Alexander M. Pravking @ 2003-11-13 17:42 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Richard Huxton <dev@archonet.com>; pgsql-sql

On Thu, Nov 13, 2003 at 12:35:41PM -0500, Tom Lane wrote:
> "Alexander M. Pravking" <fduch@antar.bryansk.ru> writes:
> > Very well then... Can I return a null record from such function
> > explicitly? Sorry, I could't find it anywhere in docs or examples.
> 
> Not sure.  Seems like you should be able to, but I've never tried it.

Thanks for a quick response, guys. I'll try to find it myself and
will let you know if I did ;)

-- 
Fduch M. Pravking



^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2003-11-13 17:42 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-11-13 16:08 STRICT function returning a composite type Alexander M. Pravking <fduch@antar.bryansk.ru>
2003-11-13 17:14 ` Richard Huxton <dev@archonet.com>
2003-11-13 17:19   ` Alexander M. Pravking <fduch@antar.bryansk.ru>
2003-11-13 17:27     ` Tom Lane <tgl@sss.pgh.pa.us>
2003-11-13 17:32       ` Alexander M. Pravking <fduch@antar.bryansk.ru>
2003-11-13 17:35         ` Tom Lane <tgl@sss.pgh.pa.us>
2003-11-13 17:42           ` Alexander M. Pravking <fduch@antar.bryansk.ru>

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