Re: Needed function IF(expr, expr, expr)

From: Rod Taylor <rbt(at)rbt(dot)ca>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, Marek Lewczuk <newsy(at)lewczuk(dot)com>, pgsql-general(at)postgresql(dot)org, PostgreSQL Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Needed function IF(expr, expr, expr)
Date: 2003-09-08 01:29:01
Message-ID: 1062984540.62242.48.camel@jester
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

> I don't feel that the anyelement in -> anyelement out mechanism is the
> last word in polymorphism, though. Care to propose additional features
> of the same kind? If you can find a way to describe the behavior you
> want in terms of the function signature, it'd be worth considering ...

For my immediate purposes the output is a known type. It is the input
that would be useful if it was passed through as unknown, or effectively
function as a placeholder if a stronger match cannot be found.

Due to inherited poorly typed data I find myself doing quite a bit of
"X_orNULL(anyelement) returns X". This takes quite a bit of interesting
structure to make it work on the current system.

CASE WHEN $1 IS OF (X) then $1
WHEN $1 IS OF (unknown) AND cancastX($1) THEN $1::X
ELSE NULL::X
END

Another useful function would be an extension of IS OF with output
somewhat like format_type. Returning a string of the datatype based upon
the value passed to it.

getType(anyelement) RETURNS text

Order of type match for unknown:
- Exact match first -- function(unknown) returns <type>
- Cast match second
- Anyelement match with defined return type should be supplied as
UNKNOWN (per function examples above)

If wanted, Anyelement match with anyelement return type could be
converted to text. Perhaps this is best described as a fallback cast
(when anyelement is unknown, autocast to X)

CREATE FUNCTION x(anyelement) RETURNS anyelement
....
LANGUAGE SQL RETURNS TYPE text ON UNKNOWN OUTPUT;

Without this clause an error would be thrown as unknown is not a valid
output.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruno Wolff III 2003-09-08 01:31:08 Re: why does count take so long?
Previous Message Tom Lane 2003-09-08 01:14:49 Re: [GENERAL] Needed function IF(expr, expr, expr)

Browse pgsql-hackers by date

  From Date Subject
Next Message Larry Douzie 2003-09-08 02:45:53 row level lock and table level locks
Previous Message Tom Lane 2003-09-08 01:14:49 Re: [GENERAL] Needed function IF(expr, expr, expr)