Re: Why overlaps is not working - Mailing list pgsql-general

From Andrus
Subject Re: Why overlaps is not working
Date
Msg-id ej4e7v$30mc$1@news.hub.org
Whole thread Raw
In response to Re: Why overlaps is not working  (Matthias.Pitzl@izb.de)
Responses Re: Why overlaps is not working  (Jorge Godoy <jgodoy@gmail.com>)
Re: Why overlaps is not working  (Richard Huxton <dev@archonet.com>)
Re: Why overlaps is not working  (Richard Broersma Jr <rabroersma@yahoo.com>)
List pgsql-general
> CREATE OR REPLACE FUNCTION f_v_same_day_overlaps(date,
>       date, date, date, out overlaps bool) as
> $_$
> SELECT (($3 between $1 and $2) or ($4 between $1 and $2));
> $_$ language sql;

Thank you.
In my application second and fourth parameters can be NULL which means
forever.
So I tried the code:

CREATE OR REPLACE FUNCTION public.doverlaps(date,
        date, date, date, out bool) IMMUTABLE AS
$_$
IF $1 is NULL OR $3 IS NULL THEN
  RAISE EXCEPTION 'doverlaps: first or third parameter is NULL % %',$1,$3;
  END IF;

IF $2 is null and $4 is null THEN
  SELECT true;
  RETURN;
END IF;

IF $2 is null THEN
  SELECT $1<=$4;
  RETURN;
END IF;

IF $4 is null THEN
  SELECT $2>=$3;
  RETURN;
END IF;

SELECT ($3 between $1 and $2) or ($4 between $1 and $2);

$_$ language sql;

This causes error

ERROR: syntax error at or near "IF"
SQL state: 42601
Character: 109

So I changed code to

CREATE OR REPLACE FUNCTION public.doverlaps(date,
        date, date, date, out bool) IMMUTABLE AS
$_$
SELECT ($3 between $1 and coalesce($2, '99991231')) or
       (coalesce($4, '99991231') between $1 and coalesce($2, '99991231'));
$_$ language sql;

It this best solution ?
How many times this is slower than expression in where clause?

Andrus.



pgsql-general by date:

Previous
From: Richard Huxton
Date:
Subject: Re: how & from where to start & admin pgsql on red hat
Next
From: Jorge Godoy
Date:
Subject: Re: Why overlaps is not working