Re: BUG #15551: Date/Time comparison not correct when the comparison is inside join clause and involves "+" or "-"

From: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
To: whereverlei(at)gmail(dot)com
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #15551: Date/Time comparison not correct when the comparison is inside join clause and involves "+" or "-"
Date: 2018-12-14 11:32:31
Message-ID: 87o99oz6w8.fsf@news-spur.riddles.org.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

>>>>> "PG" == PG Bug reporting form <noreply(at)postgresql(dot)org> writes:

PG> Note: the fourth quarter (*-12-31) of all years are missing.

So the problem here is that you're trying to identify quarters by the
_last_ date, not by the _first_ date.

If you add 3 months to Sept 30th, you get Dec 30th, NOT Dec 31st.
It so happens that adding 3 months to Mar 31st gives Jun 30th, and
adding 3 months to Jun 30th gives Sep 30th, and adding 3 months to
Dec 31st gives Mar 31st, so it's only the one case that fails here.

But if you identify the quarter by its _first_ date, you have no
problem.

date_trunc('quarter', somedate::timestamp) can help with this, but
remember to cast the date to timestamp (without time zone) in the call,
otherwise you'll get incorrect results due to timezone issues.

PG> The only difference among these query lies in the join clause
PG> involving date / time comparison having date / time operators being
PG> used:

PG> 1st: ct1."time" = ct2."time" + interval '3 months'
PG> 2nd: ct2."time" = ct1."time" - interval '3 months'
PG> 3rd: ct1."time" = ct2."time" + interval '3 months' or ct2."time" =
PG> ct1."time" - interval '3 months'

PG> It seems to me all the 3 conditions are logically same and should
PG> have the same result.

Well, in the presence of months of variable lengths, they clearly are
not equivalent and cannot be. So no bug here.

--
Andrew (irc:RhodiumToad)

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2018-12-14 19:31:28 BUG #15553: "ERROR: cache lookup failed for type 2" with a function the first time it run.
Previous Message PG Bug reporting form 2018-12-14 10:42:02 BUG #15552: Unexpected error in COPY to a foreign table in a transaction