Re: Needed function IF(expr, expr, expr) - Mailing list pgsql-general

From Rod Taylor
Subject Re: Needed function IF(expr, expr, expr)
Date
Msg-id 1062984540.62242.48.camel@jester
Whole thread Raw
In response to Re: Needed function IF(expr, expr, expr)  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
> 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.

Attachment

pgsql-general by date:

Previous
From: Bruno Wolff III
Date:
Subject: Re: why does count take so long?
Next
From: Tom Lane
Date:
Subject: Re: tsearch CVS and differences to version for 7.3.x