pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedlooping over multirange segments?
3+ messages / 2 participants
[nested] [flat]
* looping over multirange segments?
@ 2021-07-14 23:19 Ben Chobot <bench@silentmedia.com>
2021-07-15 12:15 ` Re: looping over multirange segments? hubert depesz lubaczewski <depesz@depesz.com>
0 siblings, 1 reply; 3+ messages in thread
From: Ben Chobot @ 2021-07-14 23:19 UTC (permalink / raw)
To: pgsql-general
I'm really, really liking the multirange types in PG14. Thank you for
making them! Unfortunately I am struggling with how to loop over the
segments of a multirange. There doesn't seem to be a way to convert them
to arrays, and I can't just use plpgsql's FOREACH on one. Am I missing
something obvious? It seems like a reasonable thing to want to do.
FWIW, my actual end goal is to take something like this:
select int8range(1,10)::int8multirange - int8range(4,6)::int8multirange;
?column?
────────────────
{[1,4),[6,10)}
...and turn it into this:
select ???;
int8range │ ?column?
───────────┼──────────
[1,4) │ 0
[6,10) │ 0
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: looping over multirange segments?
2021-07-14 23:19 looping over multirange segments? Ben Chobot <bench@silentmedia.com>
@ 2021-07-15 12:15 ` hubert depesz lubaczewski <depesz@depesz.com>
2021-07-15 14:07 ` Re: looping over multirange segments? Ben Chobot <bench@silentmedia.com>
0 siblings, 1 reply; 3+ messages in thread
From: hubert depesz lubaczewski @ 2021-07-15 12:15 UTC (permalink / raw)
To: Ben Chobot <bench@silentmedia.com>; +Cc: pgsql-general
On Wed, Jul 14, 2021 at 04:19:48PM -0700, Ben Chobot wrote:
> I'm really, really liking the multirange types in PG14. Thank you for making
> them! Unfortunately I am struggling with how to loop over the segments of a
> multirange. There doesn't seem to be a way to convert them to arrays, and I
> can't just use plpgsql's FOREACH on one. Am I missing something obvious? It
> seems like a reasonable thing to want to do.
>
> FWIW, my actual end goal is to take something like this:
>
> select int8range(1,10)::int8multirange - int8range(4,6)::int8multirange;
> ?column?
> ────────────────
> {[1,4),[6,10)}
>
> ...and turn it into this:
>
> select ???;
> int8range │ ?column?
> ───────────┼──────────
> [1,4) │ 0
> [6,10) │ 0
There isn't anything nice now. There was, for a brief moment, unnest()
over multiranges, but it got reverted. While it's not there, you can use
regexps to split it. I know it's ugly, but it should work, for now.
For example you can:
select * from regexp_matches(_YOUR_MULTIRANGE_::text, '[\[(][^\])]+[\])]', 'g');
I wrote more, including explanation, and ready-to-use function, in here:
https://www.depesz.com/2021/07/15/how-to-get-list-of-elements-from-multiranges/
depesz
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: looping over multirange segments?
2021-07-14 23:19 looping over multirange segments? Ben Chobot <bench@silentmedia.com>
2021-07-15 12:15 ` Re: looping over multirange segments? hubert depesz lubaczewski <depesz@depesz.com>
@ 2021-07-15 14:07 ` Ben Chobot <bench@silentmedia.com>
0 siblings, 0 replies; 3+ messages in thread
From: Ben Chobot @ 2021-07-15 14:07 UTC (permalink / raw)
To: depesz@depesz.com; +Cc: pgsql-general
hubert depesz lubaczewski wrote on 7/15/21 5:15 AM:
> select * from regexp_matches(_YOUR_MULTIRANGE_::text, '[\[(][^\])]+[\])]', 'g');
>
> I wrote more, including explanation, and ready-to-use function, in here:
> https://www.depesz.com/2021/07/15/how-to-get-list-of-elements-from-multiranges/
>
So ugly.... and also so effective for my use case. Thanks!
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2021-07-15 14:07 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-07-14 23:19 looping over multirange segments? Ben Chobot <bench@silentmedia.com>
2021-07-15 12:15 ` hubert depesz lubaczewski <depesz@depesz.com>
2021-07-15 14:07 ` Ben Chobot <bench@silentmedia.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