Re: [GENERAL] Absolute value of intervals

2009-11-16 Thread Jasen Betts
On 2009-11-07, Joshua Berry wrote: > On Mon, Nov 2, 2009 at 8:35 AM, Jasen Betts wrote: >| >| for this case: convert to seconds and then do abs. >| >|  select * from enviados e, recibidos r where abs(extract ( epoch from >|  (e.fecha - r.fecha) )) < 1 > > Cheers for that. The query cost is pretty

Re: [GENERAL] Absolute value of intervals

2009-11-10 Thread Bruce Momjian
Joshua Berry wrote: > On Mon, Nov 2, 2009 at 8:35 AM, Jasen Betts wrote: > | > | for this case: convert to seconds and then do abs. > | > | ?select * from enviados e, recibidos r where abs(extract ( epoch from > | ?(e.fecha - r.fecha) )) < 1 > > Cheers for that. The query cost is pretty heavy, bu

Re: [GENERAL] Absolute value of intervals

2009-11-02 Thread Alvaro Herrera
Jasen Betts wrote: > On 2009-11-01, Lew wrote: > > I'm writing this at about 8:35 p.m. New York time on October 31, 2009. > > From > > now, adding interval '1 day -25 hours' yields right now, New York time. > > This year halloween is the longest night. Interesting. Not the first time this h

Re: [GENERAL] Absolute value of intervals

2009-11-02 Thread CaT
On Mon, Nov 02, 2009 at 10:52:40AM +, Jasen Betts wrote: > what's the absolute value of '1month -30 days'::interval > > if I add it to the first of march it goes forwards if I add it to the In march a month has 31 days. > first of february if goes backwards. if I add it to the first of april

Re: [GENERAL] Absolute value of intervals

2009-11-02 Thread Ron Mayer
On Mon, Nov 02, 2009 at 10:52:40AM +, Jasen Betts wrote: > what's the absolute value of '1month -30 days'::interval Curious what a use case for taking the absolute value of such mixed intervals might be. I could imagine such intervals being used for stuff like "XXX is due in Y months but need

Re: [GENERAL] Absolute value of intervals

2009-11-02 Thread Sam Mason
On Mon, Nov 02, 2009 at 10:52:40AM +, Jasen Betts wrote: > what's the absolute value of '1month -30 days'::interval > > if I add it to the first of march it goes forwards if I add it to the > first of february if goes backwards. if I add it to the first of april > it goes nowhere. > > select

Re: [GENERAL] Absolute value of intervals

2009-11-02 Thread Sam Mason
On Mon, Nov 02, 2009 at 11:22:00AM +, Jasen Betts wrote: > On 2009-10-27, Sam Mason wrote: > > On Tue, Oct 27, 2009 at 03:25:02PM +, Sam Mason wrote: > >> If the absolute value of an interval was defined to strip out all the > >> negation signs you'd get the "wrong" answers out. > > > > Oo

Re: [GENERAL] Absolute value of intervals

2009-11-02 Thread Jasen Betts
On 2009-11-01, Lew wrote: > Tom Lane wrote: >> Sam Mason writes: >>> On Tue, Oct 27, 2009 at 11:27:17AM -0300, Joshua Berry wrote: I couldn't find the operator '@' for intervals >> >>> A simple SQL implementation would look like: >> >>> CREATE FUNCTION absinterval(interval) RETURNS inte

Re: [GENERAL] Absolute value of intervals

2009-11-02 Thread Jasen Betts
On 2009-10-27, Joshua Berry wrote: > I couldn't find the operator '@' for intervals and found this thread > from over six years ago: > http://archives.postgresql.org/pgsql-general/2003-09/msg00292.php > >| "Claudio Lapidus" writes: >| > Bruce Momjian wrote: >| >> Why would you want an abolute val

Re: [GENERAL] Absolute value of intervals

2009-11-02 Thread Jasen Betts
On 2009-10-27, Sam Mason wrote: > On Tue, Oct 27, 2009 at 03:25:02PM +, Sam Mason wrote: >> If the absolute value of an interval was defined to strip out all the >> negation signs you'd get the "wrong" answers out. > > Oops, forgot another reason! For maths to work (n) and (-(-n)) should > ev

Re: [GENERAL] Absolute value of intervals

2009-11-02 Thread Jasen Betts
On 2009-10-27, Sam Mason wrote: > On Tue, Oct 27, 2009 at 10:55:31AM -0400, Tom Lane wrote: >> Sam Mason writes: >> > On Tue, Oct 27, 2009 at 11:27:17AM -0300, Joshua Berry wrote: >> >> I couldn't find the operator '@' for intervals >> >> > A simple SQL implementation would look like: >> >> >

Re: [GENERAL] Absolute value of intervals

2009-11-02 Thread Jasen Betts
On 2009-10-27, Joshua Berry wrote: > I couldn't find the operator '@' for intervals and found this thread > from over six years ago: > http://archives.postgresql.org/pgsql-general/2003-09/msg00292.php > >| "Claudio Lapidus" writes: >| > Bruce Momjian wrote: >| >> Why would you want an abolute val

Re: [GENERAL] Absolute value of intervals

2009-10-31 Thread Lew
Jeff Davis wrote: On Thu, 2009-10-29 at 16:39 -0700, Scott Bailey wrote: But there is some ambiguity around the length of a month. So INTERVAL '1 month - 30 days' = INTERVAL '0 days' = INTERVAL '-1 month +30 days'. But when added to a date, it makes no change for months with 30 days, adds 1 d

Re: [GENERAL] Absolute value of intervals

2009-10-31 Thread Lew
Tom Lane wrote: Sam Mason writes: On Tue, Oct 27, 2009 at 11:27:17AM -0300, Joshua Berry wrote: I couldn't find the operator '@' for intervals A simple SQL implementation would look like: CREATE FUNCTION absinterval(interval) RETURNS interval IMMUTABLE LANGUAGE sql AS 'SELECT great

Re: [GENERAL] Absolute value of intervals

2009-10-31 Thread Sam Mason
On Fri, Oct 30, 2009 at 01:09:30PM -0700, Scott Bailey wrote: > Sam Mason wrote: > >My personal feeling is that when you provide any ordering operator and > >negation you can easily provide an absolute value operator. We've > >already (somewhat arbitrarily) decided that one of '1month -30days' and

Re: [GENERAL] Absolute value of intervals

2009-10-31 Thread Alban Hertroys
On 30 Oct 2009, at 21:09, Scott Bailey wrote: My personal feeling is that when you provide any ordering operator and negation you can easily provide an absolute value operator. We've already (somewhat arbitrarily) decided that one of '1month -30days' and '-1month 30days) is "greater" than

Re: [GENERAL] Absolute value of intervals

2009-10-30 Thread Scott Bailey
My personal feeling is that when you provide any ordering operator and negation you can easily provide an absolute value operator. We've already (somewhat arbitrarily) decided that one of '1month -30days' and '-1month 30days) is "greater" than the other, so why not provide an operator that retu

Re: [GENERAL] Absolute value of intervals

2009-10-30 Thread Sam Mason
On Fri, Oct 30, 2009 at 11:39:26AM -0300, Alvaro Herrera wrote: > Sam Mason wrote: > > + Datum > > + interval_abs(PG_FUNCTION_ARGS) > > + { > > + Interval *interval1 = PG_GETARG_INTERVAL_P(0); > > + Interval *interval2 = PG_GETARG_INTERVAL_P(1); > > Surely it must receive a single argument

Re: [GENERAL] Absolute value of intervals

2009-10-30 Thread Alvaro Herrera
Sam Mason wrote: > + Datum > + interval_abs(PG_FUNCTION_ARGS) > + { > + Interval *interval1 = PG_GETARG_INTERVAL_P(0); > + Interval *interval2 = PG_GETARG_INTERVAL_P(1); Surely it must receive a single argument? -- Alvaro Herrerahttp://www.CommandPro

Re: [GENERAL] Absolute value of intervals

2009-10-30 Thread Sam Mason
On Fri, Oct 30, 2009 at 02:14:31PM +0200, Marko Kreen wrote: > Slightly makes sense, but only slightly. We deterministically know, > that we dont have certain timestamp, thus we need to use some default > values. We already have situation that does that: > > extract(epoch from interval) You'r

Re: [GENERAL] Absolute value of intervals

2009-10-30 Thread Marko Kreen
On 10/30/09, Sam Mason wrote: > On Fri, Oct 30, 2009 at 01:45:24PM +0200, Marko Kreen wrote: > > On 10/30/09, Tom Lane wrote: > > > > That was the point of my '1 day -25 hours' example. Whether you > > > consider that positive or negative seems mighty arbitrary. > > > > If I can add it to a

Re: [GENERAL] Absolute value of intervals

2009-10-30 Thread Sam Mason
On Fri, Oct 30, 2009 at 01:45:24PM +0200, Marko Kreen wrote: > On 10/30/09, Tom Lane wrote: > > That was the point of my '1 day -25 hours' example. Whether you > > consider that positive or negative seems mighty arbitrary. > > If I can add it to a timestamp and get a deterministic result, > the

Re: [GENERAL] Absolute value of intervals

2009-10-30 Thread Marko Kreen
On 10/30/09, Tom Lane wrote: > Jeff Davis writes: > > Yes, that is a strange case. When you can't tell if an interval is > > positive or negative, how do you define the absolute value? > > That was the point of my '1 day -25 hours' example. Whether you > consider that positive or negative see

Re: [GENERAL] Absolute value of intervals

2009-10-30 Thread Sam Mason
On Fri, Oct 30, 2009 at 12:55:51AM -0400, Tom Lane wrote: > Jeff Davis writes: > > Yes, that is a strange case. When you can't tell if an interval is > > positive or negative, how do you define the absolute value? > > That was the point of my '1 day -25 hours' example. Whether you > consider tha

Re: [GENERAL] Absolute value of intervals

2009-10-29 Thread Tom Lane
Jeff Davis writes: > Yes, that is a strange case. When you can't tell if an interval is > positive or negative, how do you define the absolute value? That was the point of my '1 day -25 hours' example. Whether you consider that positive or negative seems mighty arbitrary.

Re: [GENERAL] Absolute value of intervals

2009-10-29 Thread Jeff Davis
On Thu, 2009-10-29 at 16:39 -0700, Scott Bailey wrote: > But there is some ambiguity around the length of a month. So INTERVAL '1 > month - 30 days' = INTERVAL '0 days' = INTERVAL '-1 month +30 days'. > But when added to a date, it makes no change for months with 30 days, > adds 1 day for month

Re: [GENERAL] Absolute value of intervals

2009-10-29 Thread Scott Bailey
I think this came up again recently and somebody pointed out that the correct definition isn't as obvious as all that. The components of an interval can have different signs, so should abs('-1 day 1 hour') be '1 day -1 hour' or '1 day 1 hour'? Or what about corner cases like '1 day -25 hours'?

Re: [GENERAL] Absolute value of intervals

2009-10-27 Thread Scott Bailey
Joshua Berry wrote: I couldn't find the operator '@' for intervals and found this thread from over six years ago: http://archives.postgresql.org/pgsql-general/2003-09/msg00292.php | "Claudio Lapidus" writes: | > Bruce Momjian wrote: | >> Why would you want an abolute value of a negative interva

Re: [GENERAL] Absolute value of intervals

2009-10-27 Thread Sam Mason
On Tue, Oct 27, 2009 at 03:25:02PM +, Sam Mason wrote: > If the absolute value of an interval was defined to strip out all the > negation signs you'd get the "wrong" answers out. Oops, forgot another reason! For maths to work (n) and (-(-n)) should evaluate to the same value. Inverting all t

Re: [GENERAL] Absolute value of intervals

2009-10-27 Thread Sam Mason
On Tue, Oct 27, 2009 at 10:55:31AM -0400, Tom Lane wrote: > Sam Mason writes: > > On Tue, Oct 27, 2009 at 11:27:17AM -0300, Joshua Berry wrote: > >> I couldn't find the operator '@' for intervals > > > A simple SQL implementation would look like: > > > CREATE FUNCTION absinterval(interval) RE

Re: [GENERAL] Absolute value of intervals

2009-10-27 Thread Tom Lane
Sam Mason writes: > On Tue, Oct 27, 2009 at 11:27:17AM -0300, Joshua Berry wrote: >> I couldn't find the operator '@' for intervals > A simple SQL implementation would look like: > CREATE FUNCTION absinterval(interval) RETURNS interval > IMMUTABLE LANGUAGE sql AS 'SELECT greatest($1,-$1)'

Re: [GENERAL] Absolute value of intervals

2009-10-27 Thread Sam Mason
On Tue, Oct 27, 2009 at 11:27:17AM -0300, Joshua Berry wrote: > I couldn't find the operator '@' for intervals A simple SQL implementation would look like: CREATE FUNCTION absinterval(interval) RETURNS interval IMMUTABLE LANGUAGE sql AS 'SELECT greatest($1,-$1)'; CREATE OPERATOR @ ( PROC

[GENERAL] Absolute value of intervals

2009-10-27 Thread Joshua Berry
I couldn't find the operator '@' for intervals and found this thread from over six years ago: http://archives.postgresql.org/pgsql-general/2003-09/msg00292.php | "Claudio Lapidus" writes: | > Bruce Momjian wrote: | >> Why would you want an abolute value of a negative interval? | | > Because I'm t