Re: Range Types - symmetric

Lists: pgsql-hackers
From: "Erik Rijkers" <er(at)xs4all(dot)nl>
To: "Jeff Davis" <pgsql(at)j-davis(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Range Types - symmetric
Date: 2011-09-13 16:08:14
Message-ID: 8eece6ec9500b1d4382f06f07dcedd7f.squirrel@webmail.xs4all.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

Just a thought:

select int4range(5,2);
ERROR: range lower bound must be less than or equal to range upper bound

Of course, I won't argue this is a bug, but I was wondering if it wouldn't be handy to allow a
'symmetric' mode in range construction, where, if the first of the pair is higher than the second,
they are automatically swapped, similar to SYMMETRIC in the BETWEEN clause.

Admittedly, I don't have a specific 'use case' -- it might just often prevent 'manual' swapping
before range construction calls.

Thanks,

Erik Rijkers


From: Christopher Browne <cbbrowne(at)gmail(dot)com>
To: Erik Rijkers <er(at)xs4all(dot)nl>
Cc: Jeff Davis <pgsql(at)j-davis(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Range Types - symmetric
Date: 2011-09-13 16:34:27
Message-ID: CAFNqd5XPgd0oHg3gtk2wPYZYFSR76jx382EKqwmc_OgVySD_Fw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Sep 13, 2011 at 12:08 PM, Erik Rijkers <er(at)xs4all(dot)nl> wrote:
> Just a thought:
>
> select int4range(5,2);
> ERROR:  range lower bound must be less than or equal to range upper bound
>
> Of course, I won't argue this is a bug, but I was wondering if it wouldn't be handy to allow a
> 'symmetric' mode in range construction, where, if the first of the pair is higher than the second,
> they are automatically swapped, similar to SYMMETRIC in the BETWEEN clause.
>
> Admittedly, I don't have a specific 'use case' -- it might just often prevent 'manual' swapping
> before range construction calls.

I'll buy that this is a plausible feature, but suggest an opposite
perspective, namely that this "DWIM" means that you can't notice
'getting things backwards' in your application as a bug anymore.

If you have a computation that gets a "backwards" range, then it is
more than possible that what you've got isn't an error of getting the
range backwards, but rather the error that your data is
overconstraining, and that you don't actually have a legitimate range.

So, if I decide that I want a range that expresses:
- Dates before 2012-01-01
and
- Dates after 2012-02-01

Which smells like (2012-02-01,2012-01-01).

It is NOT proper to turn that into the range (2012-01-01,2012-02-01) -
that's definitely not consistent with the facts I started with.

If you want to create your own range constructor function where you'll
take 2 values and reorder as needed to get a feasible range, that's
fine.

I think I rather oppose doing the swap automagically, by default,
because, in the case described above, it gives a WRONG range.
--
When confronted by a difficult problem, solve it by reducing it to the
question, "How would the Lone Ranger handle this?"


From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Christopher Browne <cbbrowne(at)gmail(dot)com>
Cc: Erik Rijkers <er(at)xs4all(dot)nl>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Range Types - symmetric
Date: 2011-09-13 16:55:24
Message-ID: 1315932924.7281.112.camel@jdavis
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, 2011-09-13 at 12:34 -0400, Christopher Browne wrote:
> > select int4range(5,2);
> > ERROR: range lower bound must be less than or equal to range upper bound
> >
> > Of course, I won't argue this is a bug, but I was wondering if it wouldn't be handy to allow a
> > 'symmetric' mode in range construction, where, if the first of the pair is higher than the second,
> > they are automatically swapped, similar to SYMMETRIC in the BETWEEN clause.

...

> If you have a computation that gets a "backwards" range, then it is
> more than possible that what you've got isn't an error of getting the
> range backwards, but rather the error that your data is
> overconstraining, and that you don't actually have a legitimate range.

Agreed. On balance, it's just as likely that you miss an error as save a
few keystrokes.

I'll add that it would also cause a little confusion with inclusivity.
What if you do: '[5,2)'::int4range? Is that really '[2,5)' or '(2,5]'?

Regards,
Jeff Davis


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Jeff Davis <pgsql(at)j-davis(dot)com>
Cc: Christopher Browne <cbbrowne(at)gmail(dot)com>, Erik Rijkers <er(at)xs4all(dot)nl>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Range Types - symmetric
Date: 2011-09-24 14:49:54
Message-ID: 201109241449.p8OEnsh26358@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Jeff Davis wrote:
> On Tue, 2011-09-13 at 12:34 -0400, Christopher Browne wrote:
> > > select int4range(5,2);
> > > ERROR: range lower bound must be less than or equal to range upper bound
> > >
> > > Of course, I won't argue this is a bug, but I was wondering if it wouldn't be handy to allow a
> > > 'symmetric' mode in range construction, where, if the first of the pair is higher than the second,
> > > they are automatically swapped, similar to SYMMETRIC in the BETWEEN clause.
>
> ...
>
> > If you have a computation that gets a "backwards" range, then it is
> > more than possible that what you've got isn't an error of getting the
> > range backwards, but rather the error that your data is
> > overconstraining, and that you don't actually have a legitimate range.
>
> Agreed. On balance, it's just as likely that you miss an error as save a
> few keystrokes.
>
> I'll add that it would also cause a little confusion with inclusivity.
> What if you do: '[5,2)'::int4range? Is that really '[2,5)' or '(2,5]'?

Reminder: BETWEEEN supports the SYMMETRIC keyword, so there is
a precedent for this.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +


From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Christopher Browne <cbbrowne(at)gmail(dot)com>, Erik Rijkers <er(at)xs4all(dot)nl>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Range Types - symmetric
Date: 2011-09-25 18:30:54
Message-ID: 1316975454.7281.234.camel@jdavis
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, 2011-09-24 at 10:49 -0400, Bruce Momjian wrote:
> > I'll add that it would also cause a little confusion with inclusivity.
> > What if you do: '[5,2)'::int4range? Is that really '[2,5)' or '(2,5]'?
>
> Reminder: BETWEEEN supports the SYMMETRIC keyword, so there is
> a precedent for this.

And I don't see it as valuable enough to justify changing the grammar.

Also, that still leaves confusion about inclusivity when applied to
range types.

Regards,
Jeff Davis


From: Joshua Berkus <josh(at)agliodbs(dot)com>
To: Jeff Davis <pgsql(at)j-davis(dot)com>
Cc: Christopher Browne <cbbrowne(at)gmail(dot)com>, Erik Rijkers <er(at)xs4all(dot)nl>, pgsql-hackers(at)postgresql(dot)org, Bruce Momjian <bruce(at)momjian(dot)us>
Subject: Re: Range Types - symmetric
Date: 2011-09-25 19:04:49
Message-ID: 786273042.50673.1316977489707.JavaMail.root@mail-1.01.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


> > Reminder: BETWEEEN supports the SYMMETRIC keyword, so there is
> > a precedent for this.
>
> And I don't see it as valuable enough to justify changing the
> grammar.

I agree that we should leave symmetry until 9.3.

--Josh Berkus