pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedUser-defined function with anyrange[]
3+ messages / 2 participants
[nested] [flat]
* User-defined function with anyrange[]
@ 2018-07-05 23:44 Paul A Jungwirth <pj@illuminatedcomputing.com>
0 siblings, 1 reply; 3+ messages in thread
From: Paul A Jungwirth @ 2018-07-05 23:44 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hello,
I want to make an aggregate function range_agg(anyrange) that returns
anyrange[]. But when I try to define it, Postgres tells me it doesn't
know what an anyrange[] is. I get this error:
ERROR: type anyrange[] does not exist
I also tried taking an anyrange and returning an anyarray, which does
let me define the function, but upon calling it I get an error. For
example:
paul=# CREATE OR REPLACE FUNCTION range_agg4(anyrange)
RETURNS anyarray
AS $$
BEGIN
RETURN ARRAY[$1];
END;
$$
LANGUAGE plpgsql;
paul=# select range_agg4(daterange('2016-05-04', '2016-07-01'));
ERROR: invalid input syntax for type date: "[2016-05-04,2016-07-01)"
CONTEXT: PL/pgSQL function range_agg4(anyrange) while casting
return value to function's return type
So I guess it thinks that daterange in means date[] out (not daterange[] out).
The best I can think of is to define range_agg(anyelement) that
returns anyarray. That actually works how I hope:
paul=# CREATE OR REPLACE FUNCTION range_agg3(anyelement)
RETURNS anyarray
AS $$
BEGIN
RETURN ARRAY[$1];
END;
$$
LANGUAGE plpgsql;
paul=# select range_agg3(daterange('2016-05-04', '2016-07-01'));
range_agg3
-----------------------------
{"[2016-05-04,2016-07-01)"}
But of course that is not as restricted as I'd like. Are there any
better options?
Thanks,
Paul
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: User-defined function with anyrange[]
@ 2018-07-06 21:51 Tom Lane <tgl@sss.pgh.pa.us>
parent: Paul A Jungwirth <pj@illuminatedcomputing.com>
0 siblings, 1 reply; 3+ messages in thread
From: Tom Lane @ 2018-07-06 21:51 UTC (permalink / raw)
To: Paul A Jungwirth <pj@illuminatedcomputing.com>; +Cc: pgsql-general@lists.postgresql.org
Paul A Jungwirth <pj@illuminatedcomputing.com> writes:
> I want to make an aggregate function range_agg(anyrange) that returns
> anyrange[]. But when I try to define it, Postgres tells me it doesn't
> know what an anyrange[] is. I get this error:
Yeah, there is no such thing as anyrange[], nor arrays over any other
pseudo-type either.
> I also tried taking an anyrange and returning an anyarray, which does
> let me define the function, but upon calling it I get an error. For
> example:
> paul=# CREATE OR REPLACE FUNCTION range_agg4(anyrange)
> RETURNS anyarray
> AS $$
> BEGIN
> RETURN ARRAY[$1];
> END;
> $$
> LANGUAGE plpgsql;
The trouble with this is that anyarray and anyrange are both implicitly
related to the "anyelement" pseudo-type, which represents their element
type. So if anyrange is associated with daterange in a particular
function call, then anyelement is associated with date, and then anyarray
is associated with date[] not daterange[].
I don't think there's any way to get what you want using a single
polymorphic function, at least not without some sort of extension to
the polymorphism rules. However, you can use overloading to define
several functions of the same name, and just write out one for each
range type you actually need this functionality for. I haven't really
seen applications that need so many range types that this'd be
intolerable.
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: User-defined function with anyrange[]
@ 2018-07-08 23:56 Paul A Jungwirth <pj@illuminatedcomputing.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 3+ messages in thread
From: Paul A Jungwirth @ 2018-07-08 23:56 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general@lists.postgresql.org
> you can use overloading to define
> several functions of the same name, and just write out one for each
> range type you actually need this functionality for.
Thanks! I was hoping to avoid that, but it's what I wound up doing
after all, as you can see here:
https://github.com/pjungwir/range_agg
In the README I give instructions to create a new aggregate with a
custom range type, and it is actually not bad at all, since the same C
function can serve as implementation in all cases.
Thanks again,
Paul
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2018-07-08 23:56 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-07-05 23:44 User-defined function with anyrange[] Paul A Jungwirth <pj@illuminatedcomputing.com>
2018-07-06 21:51 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-07-08 23:56 ` Paul A Jungwirth <pj@illuminatedcomputing.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox