proposal: type info support functions for functions that use "any" type

Started by Pavel Stehuleabout 7 years ago20 messageshackers
Jump to latest
#1Pavel Stehule
pavel.stehule@gmail.com

Hi,

Tom introduced supported functions for calculation function's selectivity.
Still I have similar idea to use supported function for calculation
function's parameter's types and function return type.

Motivation:

Reduce a necessity of overloading of functions. My motivation is related
primary to Orafce, but this feature should be helpful for anybody with
similar goals. The function's overloading is great functionality but it is
hard for maintenance.

My idea to enhance a CREATE FUNCTION command to be able do

CREATE FUCNTION foo("any")
RETURNS "any" AS ...
TYPEINFO foo_typeinfo

CREATE FUNCTION decode(VARIADIC "any")
RETURNS "any" AS ...
TYPEINFO decode_typeinfo.

The typeinfo functions returns a pointer tu structure with param types and
result type. Only function with "any" parameters or "any" result can use
TYPEINFO supported function. This functionality should not be allowed for
common functions.

This functionality is limited just for C coders. But I expect so typical
application coder doesn't need it. It doesn't replace my proposal of
introduction other polymorphic type - now named "commontype" (can be named
differently). The commontype is good enough solution for application
coders, developers.

Comments, notes?

Regards

Pavel

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#1)
Re: proposal: type info support functions for functions that use "any" type

Hi

so 9. 3. 2019 v 7:22 odesílatel Pavel Stehule <pavel.stehule@gmail.com>
napsal:

Hi,

Tom introduced supported functions for calculation function's selectivity.
Still I have similar idea to use supported function for calculation
function's parameter's types and function return type.

Motivation:

Reduce a necessity of overloading of functions. My motivation is related
primary to Orafce, but this feature should be helpful for anybody with
similar goals. The function's overloading is great functionality but it is
hard for maintenance.

My idea to enhance a CREATE FUNCTION command to be able do

CREATE FUCNTION foo("any")
RETURNS "any" AS ...
TYPEINFO foo_typeinfo

CREATE FUNCTION decode(VARIADIC "any")
RETURNS "any" AS ...
TYPEINFO decode_typeinfo.

The typeinfo functions returns a pointer tu structure with param types and
result type. Only function with "any" parameters or "any" result can use
TYPEINFO supported function. This functionality should not be allowed for
common functions.

This functionality is limited just for C coders. But I expect so typical
application coder doesn't need it. It doesn't replace my proposal of
introduction other polymorphic type - now named "commontype" (can be named
differently). The commontype is good enough solution for application
coders, developers.

Comments, notes?

here is a patch

I have not a plan to push decode function to upstream. Patch contains it
just as demo.

Regards

Pavel

Show quoted text

Regards

Pavel

Attachments:

parse_func-support-func.patchtext/x-patch; charset=US-ASCII; name=parse_func-support-func.patchDownload+433-3
#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#2)
Re: proposal: type info support functions for functions that use "any" type

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

so 9. 3. 2019 v 7:22 odesílatel Pavel Stehule <pavel.stehule@gmail.com>
napsal:

Tom introduced supported functions for calculation function's selectivity.
Still I have similar idea to use supported function for calculation
function's parameter's types and function return type.
Motivation:
Reduce a necessity of overloading of functions. My motivation is related
primary to Orafce, but this feature should be helpful for anybody with
similar goals. The function's overloading is great functionality but it is
hard for maintenance.

here is a patch

TBH, I don't like this proposal one bit. As far as I can see, the idea
is to let a function's support function redefine the function's declared
argument and result types on-the-fly according to no predetermined rules,
and that seems to me like it's a recipe for disaster. How will anyone
understand which function(s) are candidates to match a query, or why one
particular candidate got selected over others? It's already hard enough
to understand the behavior of polymorphic functions in complex cases,
and those are much more constrained than this would be.

Moreover, I don't think you've even provided a compelling example
case. What's this doing that you couldn't do with existing polymorphic
types or the anycompatibletype proposal?

I also strongly suspect that this would break pieces of the system
that expect that the stored pg_proc.prorettype has something to do
with reality. At minimum, you'd need to fix a number of places you
haven't touched here that have their own knowledge of function type
resolution, such as enforce_generic_type_consistency,
resolve_polymorphic_argtypes, resolve_aggregate_transtype. Probably
anyplace that treats polymorphics as being any sort of special case
would have to be taught to re-call the support function to find out
what it should think the relevant types are.

(I don't even want to think about what happens if the support function's
behavior changes between original parsing and these re-checking spots.)

Another thing that's very much less than compelling about your example
is that your support function seems to be happy to throw errors
if the argument types don't match what it's expecting. That seems
quite unacceptable, since it would prevent the parser from moving on
to consider other possibly-matching functions. Maybe that's just
because it's a quick hack not a polished example, but it doesn't
seem like a good precedent.

In short, I think the added complexity and bug potential outweigh
any possible gain from this.

regards, tom lane

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#3)
Re: proposal: type info support functions for functions that use "any" type

I wrote:

TBH, I don't like this proposal one bit. As far as I can see, the idea
is to let a function's support function redefine the function's declared
argument and result types on-the-fly according to no predetermined rules,
and that seems to me like it's a recipe for disaster. How will anyone
understand which function(s) are candidates to match a query, or why one
particular candidate got selected over others? It's already hard enough
to understand the behavior of polymorphic functions in complex cases,
and those are much more constrained than this would be.

After thinking about this a bit more, it seems like you could avoid
a lot of problems if you restricted what the support function call
does to be potentially replacing the result type of a function
declared to return ANY with some more-specific type (computed from
examination of the actual arguments). That would make it act much
more like a traditional polymorphic function. It'd remove the issues
about interactions among multiple potentially-matching functions,
since we'd only call a single support function for an already-identified
target function.

You'd still need to touch everyplace that knows about polymorphic
type resolution, since this would essentially be another form of
polymorphic function. And I'm still very dubious that it's worth
the trouble. But it would be a lot more controllable than the
proposal as it stands.

regards, tom lane

#5Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#3)
Re: proposal: type info support functions for functions that use "any" type

pá 26. 7. 2019 v 22:03 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

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

so 9. 3. 2019 v 7:22 odesílatel Pavel Stehule <pavel.stehule@gmail.com>
napsal:

Tom introduced supported functions for calculation function's

selectivity.

Still I have similar idea to use supported function for calculation
function's parameter's types and function return type.
Motivation:
Reduce a necessity of overloading of functions. My motivation is related
primary to Orafce, but this feature should be helpful for anybody with
similar goals. The function's overloading is great functionality but it

is

hard for maintenance.

here is a patch

TBH, I don't like this proposal one bit. As far as I can see, the idea
is to let a function's support function redefine the function's declared
argument and result types on-the-fly according to no predetermined rules,
and that seems to me like it's a recipe for disaster. How will anyone
understand which function(s) are candidates to match a query, or why one
particular candidate got selected over others? It's already hard enough
to understand the behavior of polymorphic functions in complex cases,
and those are much more constrained than this would be.

I quietly expect so this feature will be used without combination with
overloading. But the combination of support function and overloading can be
explicitly disabled - (in runtime for simple implementation).

Moreover, I don't think you've even provided a compelling example
case. What's this doing that you couldn't do with existing polymorphic
types or the anycompatibletype proposal?

There are two cases of usage

a) combination of polymorphic types - fx(t1, t1, t2, t1, t2, t1, t2, ...)
b) forcing types fx(t1, t2) t1 force explicit cast for t2 to t1
c) optimization of repeated call of functions like fx("any", "any", "any",
...)

It is pretty hard to create simple non-procedural language to describe
syntaxes like @a. But with procedural code it is easy.

@c is special case, that we can do already. But we cannot to push casting
outside function, and inside function, there is a overhead with casting.
With implementing type case inside function, then we increase startup time
and it is overhead for function started by plpgsql runtime.

I also strongly suspect that this would break pieces of the system
that expect that the stored pg_proc.prorettype has something to do
with reality. At minimum, you'd need to fix a number of places you
haven't touched here that have their own knowledge of function type
resolution, such as enforce_generic_type_consistency,
resolve_polymorphic_argtypes, resolve_aggregate_transtype. Probably
anyplace that treats polymorphics as being any sort of special case
would have to be taught to re-call the support function to find out
what it should think the relevant types are.

(I don't even want to think about what happens if the support function's
behavior changes between original parsing and these re-checking spots.)

The helper function should be immutable - what I know, is not possible to
change data types dynamically, so repeated call should not be effective,
but should to produce same result, so it should not be a problem.

Another thing that's very much less than compelling about your example
is that your support function seems to be happy to throw errors
if the argument types don't match what it's expecting. That seems
quite unacceptable, since it would prevent the parser from moving on
to consider other possibly-matching functions. Maybe that's just
because it's a quick hack not a polished example, but it doesn't
seem like a good precedent.

In this case it is decision, because I don't expect overloading.

I understand to your objections about mixing parser helper functions and
overloading. Currently it is pretty hard to understand what will be
expected behave when somebody overload function with polymorphic function.

With parser helper function the overloading is not necessary and can be
disabled.

Show quoted text

In short, I think the added complexity and bug potential outweigh
any possible gain from this.

regards, tom lane

#6Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#4)
Re: proposal: type info support functions for functions that use "any" type

pá 26. 7. 2019 v 22:53 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

I wrote:

TBH, I don't like this proposal one bit. As far as I can see, the idea
is to let a function's support function redefine the function's declared
argument and result types on-the-fly according to no predetermined rules,
and that seems to me like it's a recipe for disaster. How will anyone
understand which function(s) are candidates to match a query, or why one
particular candidate got selected over others? It's already hard enough
to understand the behavior of polymorphic functions in complex cases,
and those are much more constrained than this would be.

After thinking about this a bit more, it seems like you could avoid
a lot of problems if you restricted what the support function call
does to be potentially replacing the result type of a function
declared to return ANY with some more-specific type (computed from
examination of the actual arguments). That would make it act much
more like a traditional polymorphic function. It'd remove the issues
about interactions among multiple potentially-matching functions,
since we'd only call a single support function for an already-identified
target function.

I am not sure if I understand well - so I repeat it with my words.

So calculation of result type (replace ANY by some specific) can be ok?

I am able to do it if there will be a agreement.

I wrote a possibility to specify argument types as optimization as
protection against repeated type identification and casting (that can be
done in planning time, and should not be repeated).

This feature should be used only for functions with types fx("any", "any",
..) returns "any". So it is very probable so in execution type you should
to do some work with parameter type identification.

But if we find a agreement just on work with return type, then it is good
enough solution. The practical overhead of type cache inside function
should not be dramatic.

You'd still need to touch everyplace that knows about polymorphic
type resolution, since this would essentially be another form of
polymorphic function. And I'm still very dubious that it's worth
the trouble. But it would be a lot more controllable than the
proposal as it stands.

ok

Show quoted text

regards, tom lane

#7Thomas Munro
thomas.munro@gmail.com
In reply to: Pavel Stehule (#6)
Re: proposal: type info support functions for functions that use "any" type

On Sat, Jul 27, 2019 at 5:45 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:

pá 26. 7. 2019 v 22:53 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

I wrote:

TBH, I don't like this proposal one bit. As far as I can see, the idea
is to let a function's support function redefine the function's declared
argument and result types on-the-fly according to no predetermined rules,
and that seems to me like it's a recipe for disaster. How will anyone
understand which function(s) are candidates to match a query, or why one
particular candidate got selected over others? It's already hard enough
to understand the behavior of polymorphic functions in complex cases,
and those are much more constrained than this would be.

After thinking about this a bit more, it seems like you could avoid
a lot of problems if you restricted what the support function call
does to be potentially replacing the result type of a function
declared to return ANY with some more-specific type (computed from
examination of the actual arguments). That would make it act much
more like a traditional polymorphic function. It'd remove the issues
about interactions among multiple potentially-matching functions,
since we'd only call a single support function for an already-identified
target function.

I am not sure if I understand well - so I repeat it with my words.

So calculation of result type (replace ANY by some specific) can be ok?

I am able to do it if there will be a agreement.

...

Hi Pavel,

I see that this is an active project with an ongoing discussion, but
we have run out of July so I have moved this to the September CF and
set it to "Waiting on Author".

--
Thomas Munro
https://enterprisedb.com

#8Pavel Stehule
pavel.stehule@gmail.com
In reply to: Thomas Munro (#7)
Re: proposal: type info support functions for functions that use "any" type

čt 1. 8. 2019 v 11:01 odesílatel Thomas Munro <thomas.munro@gmail.com>
napsal:

On Sat, Jul 27, 2019 at 5:45 PM Pavel Stehule <pavel.stehule@gmail.com>
wrote:

pá 26. 7. 2019 v 22:53 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

I wrote:

TBH, I don't like this proposal one bit. As far as I can see, the

idea

is to let a function's support function redefine the function's

declared

argument and result types on-the-fly according to no predetermined

rules,

and that seems to me like it's a recipe for disaster. How will anyone
understand which function(s) are candidates to match a query, or why

one

particular candidate got selected over others? It's already hard

enough

to understand the behavior of polymorphic functions in complex cases,
and those are much more constrained than this would be.

After thinking about this a bit more, it seems like you could avoid
a lot of problems if you restricted what the support function call
does to be potentially replacing the result type of a function
declared to return ANY with some more-specific type (computed from
examination of the actual arguments). That would make it act much
more like a traditional polymorphic function. It'd remove the issues
about interactions among multiple potentially-matching functions,
since we'd only call a single support function for an already-identified
target function.

I am not sure if I understand well - so I repeat it with my words.

So calculation of result type (replace ANY by some specific) can be ok?

I am able to do it if there will be a agreement.

...

Hi Pavel,

I see that this is an active project with an ongoing discussion, but
we have run out of July so I have moved this to the September CF and
set it to "Waiting on Author".

sure

Pavel

Show quoted text

--
Thomas Munro
https://enterprisedb.com

#9Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#4)
Re: proposal: type info support functions for functions that use "any" type

Hi

pá 26. 7. 2019 v 22:53 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

I wrote:

TBH, I don't like this proposal one bit. As far as I can see, the idea
is to let a function's support function redefine the function's declared
argument and result types on-the-fly according to no predetermined rules,
and that seems to me like it's a recipe for disaster. How will anyone
understand which function(s) are candidates to match a query, or why one
particular candidate got selected over others? It's already hard enough
to understand the behavior of polymorphic functions in complex cases,
and those are much more constrained than this would be.

After thinking about this a bit more, it seems like you could avoid
a lot of problems if you restricted what the support function call
does to be potentially replacing the result type of a function
declared to return ANY with some more-specific type (computed from
examination of the actual arguments). That would make it act much
more like a traditional polymorphic function. It'd remove the issues
about interactions among multiple potentially-matching functions,
since we'd only call a single support function for an already-identified
target function.

You'd still need to touch everyplace that knows about polymorphic
type resolution, since this would essentially be another form of
polymorphic function. And I'm still very dubious that it's worth
the trouble. But it would be a lot more controllable than the
proposal as it stands.

I am sending reduced version of previous patch. Now, support function is
used just for replacement of returned type "any" by some other.

The are two patches - shorter with only support function, larger with demo
"decode" function. I don't expect so the "decode" extension should be
pushed to master. It is just demo of usage.

Regards

Pavel

Show quoted text

regards, tom lane

Attachments:

parser-support-function.patchtext/x-patch; charset=US-ASCII; name=parser-support-function.patchDownload+65-6
parser-support-function-with-demo.patchtext/x-patch; charset=US-ASCII; name=parser-support-function-with-demo.patchDownload+632-6
#10Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#9)
Re: proposal: type info support functions for functions that use "any" type

Hi

rebase

Pavel

Attachments:

parser-support-function-with-demo.patchtext/x-patch; charset=US-ASCII; name=parser-support-function-with-demo.patchDownload+632-6
#11Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#10)
Re: proposal: type info support functions for functions that use "any" type

Hi

pá 16. 8. 2019 v 8:41 odesílatel Pavel Stehule <pavel.stehule@gmail.com>
napsal:

Hi

rebase

another rebase

Regards

Pavel

Show quoted text

Pavel

Attachments:

parser-support-function-with-demo-20191128.patchtext/x-patch; charset=US-ASCII; name=parser-support-function-with-demo-20191128.patchDownload+633-6
#12Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#11)
Re: proposal: type info support functions for functions that use "any" type

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

[ parser-support-function-with-demo-20191128.patch ]

TBH, I'm still not convinced that this is a good idea. Restricting
the support function to only change the function's return type is
safer than the original proposal, but it's still not terribly safe.
If you change the support function's algorithm in any way, how do
you know whether you've broken existing stored queries? If the
support function consults external resources to make its choice
(perhaps checking the existence of a cast), where could we record
that the query depends on the existence of that cast? There'd be
no visible trace of that in the query parsetree.

I'm also still not convinced that this idea allows doing anything
that can't be done just as well with polymorphism. It would be a
really bad idea for the support function to be examining the values
of the arguments (else what happens when they're not constants?).
So all you can do is look at their types, and then it seems like
the things you can usefully do are pretty much like polymorphism,
i.e. select some one of the input types, or a related type such
as an array type or element type. If there are gaps in what you
can express with polymorphism, I'd much rather spend effort on
improving that facility than in adding something that is only
accessible to advanced C coders. (Yes, I know I've been slacking
on reviewing [1]https://commitfest.postgresql.org/26/1911/.)

Lastly, I still think that this patch doesn't begin to address
all the places that would have to know about the feature. There's
a lot of places that know about polymorphism --- if this is
polymorphism on steroids, which it is, then why don't all of those
places need to be touched?

On the whole I think we should reject this idea.

regards, tom lane

[1]: https://commitfest.postgresql.org/26/1911/

#13Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#12)
Re: proposal: type info support functions for functions that use "any" type

út 14. 1. 2020 v 22:09 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

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

[ parser-support-function-with-demo-20191128.patch ]

TBH, I'm still not convinced that this is a good idea. Restricting
the support function to only change the function's return type is
safer than the original proposal, but it's still not terribly safe.
If you change the support function's algorithm in any way, how do
you know whether you've broken existing stored queries? If the
support function consults external resources to make its choice
(perhaps checking the existence of a cast), where could we record
that the query depends on the existence of that cast? There'd be
no visible trace of that in the query parsetree.

This risk is real and cannot be simply solved without more complications.

Can be solution to limit and enforce this functionality only for extensions
that be initialized from shared_preload_libraries or
local_preload_libraries?

I'm also still not convinced that this idea allows doing anything
that can't be done just as well with polymorphism. It would be a
really bad idea for the support function to be examining the values
of the arguments (else what happens when they're not constants?).
So all you can do is look at their types, and then it seems like
the things you can usefully do are pretty much like polymorphism,
i.e. select some one of the input types, or a related type such
as an array type or element type. If there are gaps in what you
can express with polymorphism, I'd much rather spend effort on
improving that facility than in adding something that is only
accessible to advanced C coders. (Yes, I know I've been slacking
on reviewing [1].)

For my purpose critical information is type. I don't need to work with
constant, but I can imagine, so some API can be nice to work with constant
value.
Yes, I can solve lot of things by patch [1], but not all, and this patch
shorter, and almost trivial.

Lastly, I still think that this patch doesn't begin to address
all the places that would have to know about the feature. There's
a lot of places that know about polymorphism --- if this is
polymorphism on steroids, which it is, then why don't all of those
places need to be touched?

I am sorry, I don't understand last sentence?

Show quoted text

On the whole I think we should reject this idea.

regards, tom lane

[1] https://commitfest.postgresql.org/26/1911/

#14Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#13)
Re: proposal: type info support functions for functions that use "any" type

st 15. 1. 2020 v 11:04 odesílatel Pavel Stehule <pavel.stehule@gmail.com>
napsal:

út 14. 1. 2020 v 22:09 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

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

[ parser-support-function-with-demo-20191128.patch ]

TBH, I'm still not convinced that this is a good idea. Restricting
the support function to only change the function's return type is
safer than the original proposal, but it's still not terribly safe.
If you change the support function's algorithm in any way, how do
you know whether you've broken existing stored queries? If the
support function consults external resources to make its choice
(perhaps checking the existence of a cast), where could we record
that the query depends on the existence of that cast? There'd be
no visible trace of that in the query parsetree.

This risk is real and cannot be simply solved without more complications.

Can be solution to limit and enforce this functionality only for
extensions that be initialized from shared_preload_libraries or
local_preload_libraries?

When we check, so used function is started from dynamic loaded extension,
we can raise a error. It's not too great for upgrades, but I expect upgrade
of this kind extension is very similar like Postgres - and the restart can
be together.

I'm also still not convinced that this idea allows doing anything
that can't be done just as well with polymorphism. It would be a
really bad idea for the support function to be examining the values
of the arguments (else what happens when they're not constants?).
So all you can do is look at their types, and then it seems like
the things you can usefully do are pretty much like polymorphism,
i.e. select some one of the input types, or a related type such
as an array type or element type. If there are gaps in what you
can express with polymorphism, I'd much rather spend effort on
improving that facility than in adding something that is only
accessible to advanced C coders. (Yes, I know I've been slacking
on reviewing [1].)

For my purpose critical information is type. I don't need to work with
constant, but I can imagine, so some API can be nice to work with constant
value.
Yes, I can solve lot of things by patch [1], but not all, and this patch
shorter, and almost trivial.

All this discussion is motivated by my work on Orafce extension -
https://github.com/orafce/orafce

Unfortunately implementation of "decode" functions is not possible with
patch [1]. Now I have 55 instances of "decode" function and I am sure, I
don't cover all.

With this patch (polymorphism on stereoids :)), I can do it very simple,
and quickly. This functions and other similar.

The patch was very simple, so I think, maybe wrongly, so it is acceptable
way.

Our polymorphism is strong, and if I design code natively for Postgres,
than it is perfect. But It doesn't allow to implement some simple functions
that are used in other databases. With this small patch I can cover almost
all situations - and very simply.

I don't want to increase complexity of polymorphism rules more - [1] is
maximum, what we can implement with acceptable costs, but this generic
system is sometimes not enough.

But I invite any design, how this problem can be solved.

Any ideas?

Show quoted text

Lastly, I still think that this patch doesn't begin to address
all the places that would have to know about the feature. There's
a lot of places that know about polymorphism --- if this is
polymorphism on steroids, which it is, then why don't all of those
places need to be touched?

I am sorry, I don't understand last sentence?

On the whole I think we should reject this idea.

regards, tom lane

[1] https://commitfest.postgresql.org/26/1911/

#15Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#12)
Re: proposal: type info support functions for functions that use "any" type

Hi

út 14. 1. 2020 v 22:09 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

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

[ parser-support-function-with-demo-20191128.patch ]

TBH, I'm still not convinced that this is a good idea. Restricting
the support function to only change the function's return type is
safer than the original proposal, but it's still not terribly safe.
If you change the support function's algorithm in any way, how do
you know whether you've broken existing stored queries? If the
support function consults external resources to make its choice
(perhaps checking the existence of a cast), where could we record
that the query depends on the existence of that cast? There'd be
no visible trace of that in the query parsetree.

I reread all related mails and I think so it should be safe - or there is
same risk like using any C extensions for functions or hooks.

I use a example from demo

+CREATE FUNCTION decode_support(internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+--
+-- decode function - example of function that returns "any" type
+--
+CREATE FUNCTION decode(variadic "any")
+RETURNS "any"
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE SUPPORT decode_support;

The support function (and implementation) is joined with "decode" function.
So I cannot to change the behave of support function without reloading a
extension, and it needs typically session reconnect.

I'm also still not convinced that this idea allows doing anything
that can't be done just as well with polymorphism. It would be a
really bad idea for the support function to be examining the values
of the arguments (else what happens when they're not constants?).
So all you can do is look at their types, and then it seems like
the things you can usefully do are pretty much like polymorphism,
i.e. select some one of the input types, or a related type such
as an array type or element type. If there are gaps in what you
can express with polymorphism, I'd much rather spend effort on
improving that facility than in adding something that is only
accessible to advanced C coders. (Yes, I know I've been slacking
on reviewing [1].)

The design is based not on values, just on types. I don't need to know a
value, I need to know a type.

Currently our polymorphism is not enough - and is necessary to use "any"
datatype. This patch just add a possibility to use "any" as return type.

I spent on this topic lot of time and one result is patch [1]. This patch
increase situation lot of, but cannot to cover all. There are strong limits
for variadic usage.

This patch is really not about values, it is about types - and about more
possibility (and more elasticity) to control result type.

Lastly, I still think that this patch doesn't begin to address
all the places that would have to know about the feature. There's
a lot of places that know about polymorphism --- if this is
polymorphism on steroids, which it is, then why don't all of those
places need to be touched?

It is working with "any" type, and then it can be very small, because the
all work with this type is moved to extension.

On the whole I think we should reject this idea.

I will accept any your opinion. Please, try to understand to me as Orafce
developer, maintainer. I would to clean this extension, and current state
of polymorphism (with patch [1]) doesn't allow it.

I am open to any proposals, ideas.

Regards

Pavel

Show quoted text

regards, tom lane

[1] https://commitfest.postgresql.org/26/1911/

#16Daniel Gustafsson
daniel@yesql.se
In reply to: Pavel Stehule (#15)
Re: proposal: type info support functions for functions that use "any" type

On 26 Jan 2020, at 16:33, Pavel Stehule <pavel.stehule@gmail.com> wrote:

I reread all related mails and I think so it should be safe - or there is same risk like using any C extensions for functions or hooks.

This patch has been bumped in CFs for the past year, with the thread stalled
and the last review comment being in support of rejection. Tom, do you still
feel it should be rejected in light of Pavel's latest posts?

cheers ./daniel

#17Tom Lane
tgl@sss.pgh.pa.us
In reply to: Daniel Gustafsson (#16)
Re: proposal: type info support functions for functions that use "any" type

Daniel Gustafsson <daniel@yesql.se> writes:

This patch has been bumped in CFs for the past year, with the thread stalled
and the last review comment being in support of rejection. Tom, do you still
feel it should be rejected in light of Pavel's latest posts?

I have seen no convincing response to the concerns I raised in my
last message in the thread [1]/messages/by-id/31501.1579036195@sss.pgh.pa.us, to wit that

1. I think the "flexibility" of letting a support function resolve the
output type in some unspecified way is mostly illusory, because if it
doesn't do it in a way that's morally equivalent to polymorphism, it's
doing it wrong. Also, I'm not that excited about improving polymorphism
in a way that is only accessible with specialized C code. The example of
Oracle-like DECODE() could be handled about as well if we had a second set
of anycompatible-style polymorphic types, that is something like

decode(expr anycompatible,
search1 anycompatible, result1 anycompatible2,
search2 anycompatible, result2 anycompatible2,
search3 anycompatible, result3 anycompatible2,
...
) returns anycompatible2;

Admittedly, you'd need to write a separate declaration for each number of
arguments you wanted to support, but they could all point at the same C
function --- which'd be a lot simpler than in this patch, since it would
not need to deal with any type coercions, only comparisons.

I also argue that to the extent that the support function is reinventing
polymorphism internally, it's going to be inferior to the parser's
version. As an example, with Pavel's sample implementation, if a
particular query needs a coercion from type X to type Y, that's nowhere
visible in the parse tree. So you could drop the cast without being told
that view so-and-so depends on it, leading to a run-time failure next time
you try to use that view. Doing the same thing with normal polymorphism,
the X-to-Y cast function would be used in the parse tree and so we'd know
about the dependency.

2. I have no faith that the proposed implementation is correct or
complete. As I complained earlier, a lot of places have special-case
handling for polymorphism, and it seems like every one of them would
need to know about this feature too. That is, to the extent that
this patch's footprint is smaller than commit 24e2885ee -- which it
is, by a lot -- I think those are bugs of omission. It will not work
to have a situation where some parts of the backend resolve a function's
result type as one thing and others resolve it as something else thanks to
failure to account for this new feature. As a concrete example, it looks
like we'd fail pretty hard if someone tried to use this facility in an
aggregate support function.

So my opinion is still what it was in January.

regards, tom lane

[1]: /messages/by-id/31501.1579036195@sss.pgh.pa.us

#18Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#17)
Re: proposal: type info support functions for functions that use "any" type

pá 31. 7. 2020 v 2:32 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

Daniel Gustafsson <daniel@yesql.se> writes:

This patch has been bumped in CFs for the past year, with the thread

stalled

and the last review comment being in support of rejection. Tom, do you

still

feel it should be rejected in light of Pavel's latest posts?

I have seen no convincing response to the concerns I raised in my
last message in the thread [1], to wit that

1. I think the "flexibility" of letting a support function resolve the
output type in some unspecified way is mostly illusory, because if it
doesn't do it in a way that's morally equivalent to polymorphism, it's
doing it wrong. Also, I'm not that excited about improving polymorphism
in a way that is only accessible with specialized C code. The example of
Oracle-like DECODE() could be handled about as well if we had a second set
of anycompatible-style polymorphic types, that is something like

decode(expr anycompatible,
search1 anycompatible, result1 anycompatible2,
search2 anycompatible, result2 anycompatible2,
search3 anycompatible, result3 anycompatible2,
...
) returns anycompatible2;

With this proposal I can write a good enough implementation of the "decode"
function, although it cannot be 100% compatible. It can cover probably
almost all use cases.

But this design doesn't help with ANSI compatible LEAD, LAG functions.
There is a different strategy - optional argument is implicitly casted to
type of first argument.

Admittedly, you'd need to write a separate declaration for each number of
arguments you wanted to support, but they could all point at the same C
function --- which'd be a lot simpler than in this patch, since it would
not need to deal with any type coercions, only comparisons.

This patch is reduced - first version allowed similar argument list
transformations like parser does with COALESCE or CASE expressions.

When arguments are transformed early, then the body of function can be thin.

I also argue that to the extent that the support function is reinventing
polymorphism internally, it's going to be inferior to the parser's
version. As an example, with Pavel's sample implementation, if a
particular query needs a coercion from type X to type Y, that's nowhere
visible in the parse tree. So you could drop the cast without being told
that view so-and-so depends on it, leading to a run-time failure next time
you try to use that view. Doing the same thing with normal polymorphism,
the X-to-Y cast function would be used in the parse tree and so we'd know
about the dependency.

It is by reduced design. First implementation did a transformation of the
argument list too. Then the cast was visible in the argument list.

It is true, so this patch implements an alternative way to polymorphic
types. I don't think it is necessarily bad (and this functionality is
available only for C language). We do it for COALESCE, CASE, GREATEST,
LEAST functions and minimally due lazy evaluation we don't try to rewrite
these functionality to usual functions. I would not increase the complexity
of Postgres type systems or introduce some specific features used just by
me. When people start to write an application on Postgres, then the current
system is almost good enough. But a different situation is when a
significant factor is compatibility - this is a topic that I have to solve
in Orafce or issue with LAG, LEAD functions. Introducing a special
polymorphic type for some specific behavior is hard and maybe unacceptable
work. For me (as extension author) it can be nice to have some possibility
to modify a parse tree - without useless overhead. With this possibility,
some functions can be lighter and faster - because casting will be outside
the function.

Regards

Pavel

Show quoted text

2. I have no faith that the proposed implementation is correct or
complete. As I complained earlier, a lot of places have special-case
handling for polymorphism, and it seems like every one of them would
need to know about this feature too. That is, to the extent that
this patch's footprint is smaller than commit 24e2885ee -- which it
is, by a lot -- I think those are bugs of omission. It will not work
to have a situation where some parts of the backend resolve a function's
result type as one thing and others resolve it as something else thanks to
failure to account for this new feature. As a concrete example, it looks
like we'd fail pretty hard if someone tried to use this facility in an
aggregate support function.

So my opinion is still what it was in January.

regards, tom lane

[1] /messages/by-id/31501.1579036195@sss.pgh.pa.us

#19David Steele
david@pgmasters.net
In reply to: Pavel Stehule (#18)
Re: proposal: type info support functions for functions that use "any" type

pá 31. 7. 2020 v 2:32 odesílatel Tom Lane <tgl@sss.pgh.pa.us
<mailto:tgl@sss.pgh.pa.us>> napsal:

So my opinion is still what it was in January.

Since there does not appear to be support for this patch and it has not
attracted any new review or comment in the last year I'm planning to
close it on MAR 8 unless there are arguments to the contrary.

Regards,
--
-David
david@pgmasters.net

#20Pavel Stehule
pavel.stehule@gmail.com
In reply to: David Steele (#19)
Re: proposal: type info support functions for functions that use "any" type

st 3. 3. 2021 v 18:12 odesílatel David Steele <david@pgmasters.net> napsal:

pá 31. 7. 2020 v 2:32 odesílatel Tom Lane <tgl@sss.pgh.pa.us
<mailto:tgl@sss.pgh.pa.us>> napsal:

So my opinion is still what it was in January.

Since there does not appear to be support for this patch and it has not
attracted any new review or comment in the last year I'm planning to
close it on MAR 8 unless there are arguments to the contrary.

This feature is very specific. The benefit is mostly for authors of
extensions that try to emulate some other RDBMS. For me - it can reduce
thousands of lines of Orafce source code.

But Tom has a strong negative option on this feature. But on second hand, I
am accepting so this feature is specific and an benefit is specific subset
of users. So there are no strong reasons for hard pushing from me. There
are other similar proposals, so we will see.

Regards

Pavel

Show quoted text

Regards,
--
-David
david@pgmasters.net