polymorphic SQL functions has a problem with domains

Started by Pavel Stehulealmost 12 years ago10 messages
#1Pavel Stehule
pavel.stehule@gmail.com

Hello

I was informed about impossibility to use a polymorphic functions together
with domain types

see

create domain xx as numeric(15);

create or replace function g(anyelement, anyelement)
returns anyelement as
$$ select $1 + $2 $$
language sql immutable;

postgres=# select g(1::xx, 2::xx);
ERROR: return type mismatch in function declared to return xx
DETAIL: Actual return type is numeric.
CONTEXT: SQL function "g" during inlining

is this bug?

Regards

Pavel Stehule

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#1)
Re: polymorphic SQL functions has a problem with domains

Pavel Stehule <pavel.stehule@gmail.com> writes:

I was informed about impossibility to use a polymorphic functions together
with domain types

see

create domain xx as numeric(15);

create or replace function g(anyelement, anyelement)
returns anyelement as
$$ select $1 + $2 $$
language sql immutable;

postgres=# select g(1::xx, 2::xx);
ERROR: return type mismatch in function declared to return xx
DETAIL: Actual return type is numeric.
CONTEXT: SQL function "g" during inlining

That example doesn't say you can't use polymorphic functions with domains.
It says that this particular polymorphic function definition is wrong:
it is not making sure its result is of the expected data type. I don't
recall right now whether SQL functions will apply an implicit cast on the
result for you, but even if they do, an upcast from numeric to some domain
over numeric wouldn't be implicit.

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#2)
Re: polymorphic SQL functions has a problem with domains

2014-04-02 17:19 GMT+02:00 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

I was informed about impossibility to use a polymorphic functions

together

with domain types

see

create domain xx as numeric(15);

create or replace function g(anyelement, anyelement)
returns anyelement as
$$ select $1 + $2 $$
language sql immutable;

postgres=# select g(1::xx, 2::xx);
ERROR: return type mismatch in function declared to return xx
DETAIL: Actual return type is numeric.
CONTEXT: SQL function "g" during inlining

That example doesn't say you can't use polymorphic functions with domains.
It says that this particular polymorphic function definition is wrong:
it is not making sure its result is of the expected data type. I don't
recall right now whether SQL functions will apply an implicit cast on the
result for you, but even if they do, an upcast from numeric to some domain
over numeric wouldn't be implicit.

But I am not able to enforce a casting in polymorphic function

or there is some possibility?

Regards

Pavel

Show quoted text

regards, tom lane

#4David Johnston
polobo@yahoo.com
In reply to: Tom Lane (#2)
Re: polymorphic SQL functions has a problem with domains

Tom Lane-2 wrote

Pavel Stehule &lt;

pavel.stehule@

&gt; writes:

I was informed about impossibility to use a polymorphic functions
together
with domain types

see

create domain xx as numeric(15);

create or replace function g(anyelement, anyelement)
returns anyelement as
$$ select $1 + $2 $$
language sql immutable;

postgres=# select g(1::xx, 2::xx);
ERROR: return type mismatch in function declared to return xx
DETAIL: Actual return type is numeric.
CONTEXT: SQL function "g" during inlining

That example doesn't say you can't use polymorphic functions with domains.
It says that this particular polymorphic function definition is wrong:
it is not making sure its result is of the expected data type. I don't
recall right now whether SQL functions will apply an implicit cast on the
result for you, but even if they do, an upcast from numeric to some domain
over numeric wouldn't be implicit.

How would that be possible though? Since any number of domains could be
defined over numeric as soon as the "+" operator causes the domain to be
lost there is no way to get it back manually - you cannot just make it
"SELECT ($1 + $2)::xx".

Does something like:

SELECT ($1 + $2)::$1%TYPE

exist where you can explicitly cast to the type of the input argument?

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/polymorphic-SQL-functions-has-a-problem-with-domains-tp5798349p5798356.html
Sent from the PostgreSQL - hackers mailing list archive at Nabble.com.

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: David Johnston (#4)
Re: polymorphic SQL functions has a problem with domains

David Johnston <polobo@yahoo.com> writes:

Does something like:
SELECT ($1 + $2)::$1%TYPE
exist where you can explicitly cast to the type of the input argument?

I don't think SQL-language functions have such a notation, but it's
possible in plpgsql, if memory serves.

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#6Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#5)
Re: polymorphic SQL functions has a problem with domains

2014-04-02 18:27 GMT+02:00 Tom Lane <tgl@sss.pgh.pa.us>:

David Johnston <polobo@yahoo.com> writes:

Does something like:
SELECT ($1 + $2)::$1%TYPE
exist where you can explicitly cast to the type of the input argument?

I don't think SQL-language functions have such a notation, but it's
possible in plpgsql, if memory serves.

No, this possibility doesn't there, what I know.

but you can do assignment to some output variable - what is effective same

Pavel

Show quoted text

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#7Andres Freund
andres@2ndquadrant.com
In reply to: Tom Lane (#5)
Re: polymorphic SQL functions has a problem with domains

On 2014-04-02 12:27:30 -0400, Tom Lane wrote:

David Johnston <polobo@yahoo.com> writes:

Does something like:
SELECT ($1 + $2)::$1%TYPE
exist where you can explicitly cast to the type of the input argument?

I don't think SQL-language functions have such a notation, but it's
possible in plpgsql, if memory serves.

Sometimes you can play nasty tricks using COALESCE() to force a cast
like that.
E.g. SELECT COALESCE(NULLIF($1, $1), $1 + $2);

Greetings,

Andres Freund

--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#8David Johnston
polobo@yahoo.com
In reply to: Tom Lane (#5)
Re: polymorphic SQL functions has a problem with domains

Tom Lane-2 wrote

David Johnston &lt;

polobo@

&gt; writes:

Does something like:
SELECT ($1 + $2)::$1%TYPE
exist where you can explicitly cast to the type of the input argument?

I don't think SQL-language functions have such a notation, but it's
possible in plpgsql, if memory serves.

Indeed.

http://www.postgresql.org/docs/9.3/interactive/plpgsql-declarations.html#PLPGSQL-DECLARATION-TYPE

Section 40.3.3

You lose inlining but at least it (should) work.

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/polymorphic-SQL-functions-has-a-problem-with-domains-tp5798349p5798367.html
Sent from the PostgreSQL - hackers mailing list archive at Nabble.com.

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#9Pavel Stehule
pavel.stehule@gmail.com
In reply to: David Johnston (#8)
Re: polymorphic SQL functions has a problem with domains

2014-04-02 18:34 GMT+02:00 David Johnston <polobo@yahoo.com>:

Tom Lane-2 wrote

David Johnston <

polobo@

writes:
Does something like:
SELECT ($1 + $2)::$1%TYPE
exist where you can explicitly cast to the type of the input argument?

I don't think SQL-language functions have such a notation, but it's
possible in plpgsql, if memory serves.

Indeed.

http://www.postgresql.org/docs/9.3/interactive/plpgsql-declarations.html#PLPGSQL-DECLARATION-TYPE

This casting is indirect via assignment

Show quoted text

Section 40.3.3

You lose inlining but at least it (should) work.

David J.

--
View this message in context:
http://postgresql.1045698.n5.nabble.com/polymorphic-SQL-functions-has-a-problem-with-domains-tp5798349p5798367.html
Sent from the PostgreSQL - hackers mailing list archive at Nabble.com.

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#10Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#2)
Re: polymorphic SQL functions has a problem with domains

2014-04-02 17:19 GMT+02:00 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

I was informed about impossibility to use a polymorphic functions

together

with domain types

see

create domain xx as numeric(15);

create or replace function g(anyelement, anyelement)
returns anyelement as
$$ select $1 + $2 $$
language sql immutable;

postgres=# select g(1::xx, 2::xx);
ERROR: return type mismatch in function declared to return xx
DETAIL: Actual return type is numeric.
CONTEXT: SQL function "g" during inlining

That example doesn't say you can't use polymorphic functions with domains.
It says that this particular polymorphic function definition is wrong:
it is not making sure its result is of the expected data type. I don't
recall right now whether SQL functions will apply an implicit cast on the
result for you, but even if they do, an upcast from numeric to some domain
over numeric wouldn't be implicit.

I though about this issue again, and I am thinking so it is PostgreSQL bug

we can do safe transformation from Parent type -> domain.

and returning result require same transformation (in this case) - so
enforcing casting (not only binary casting) should be safe.

Otherwise - CAST(var AS var) should be useful and can helps too.

Regards

Pavel Stehule

Show quoted text

regards, tom lane