Re: range_adjacent and discrete ranges

From: Tom Lane Florian Pflug Jeff Davis , pgsql-hackers(at)postgresql(dot)org Re: range_adjacent and discrete ranges 2011-11-23 02:40:48 11024.1322016048@sss.pgh.pa.us (view raw or flat) 2011-11-18 08:25:24 from Jeff Davis  2011-11-18 12:32:47 from Florian Pflug   2011-11-18 15:33:13 from Tom Lane    2011-11-18 16:56:06 from Jeff Davis     2011-11-18 19:47:46 from Tom Lane      2011-11-19 18:52:22 from Jeff Davis       2011-11-19 21:03:01 from Tom Lane        2011-11-20 00:41:31 from Florian Pflug   2011-11-18 16:58:49 from Jeff Davis   2011-11-23 02:40:48 from Tom Lane    2011-11-23 21:10:04 from Tom Lane     2011-11-23 21:39:35 from Tom Lane pgsql-hackers
Florian Pflug <fgp(at)phlo(dot)org> writes:
> More formally, let there be two arbitrary ranges
>   a,b,i_a,i_b
>   c,d,i_c,i_d
> where the first two parameters are the lower respectively upper bound, and
> the last two are booleans saying whether the lower respectively upper bound
> is inclusive (true) or exclusive (false).

> These ranges are then adjacent exactly if the range
>   b,c,!i_b,!i_c
> is empty.

I tried to implement this, and I think it has a small bug.  It works as
stated if we have b < c.  However, if we have b == c, then we want to
consider the ranges adjacent if i_b != i_c (ie, only one of them claims
the common boundary point).  But a singleton range is empty unless it's
inclusive on both sides.  So we have to have a special case when the
bounds are equal.

(If b > c, then of course we have to consider the two ranges in the
opposite order.)

Attached is a draft patch for this.  It passes regression tests but I've
not tried to exercise it with a canonical function that actually does
something different.  It's going to be a bit slower than Jeff's
original, because it does not only range_cmp_bound_values but also a
make_range cycle (in most cases).  So I guess the question is how much
we care about supporting canonical functions with non-default policies.
Thoughts?

regards, tom lane