Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id DB1E6B5DC02 for ; Wed, 20 Jul 2011 11:59:34 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 17534-05 for ; Wed, 20 Jul 2011 14:59:28 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 21701B5DC35 for ; Wed, 20 Jul 2011 11:59:28 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p6KExF4t011432; Wed, 20 Jul 2011 10:59:15 -0400 (EDT) To: depesz@depesz.com cc: Ludek Bouska , pgsql-bugs@postgresql.org Subject: Re: BUG #6124: overlaps In-reply-to: <20110720114312.GA27337@depesz.com> References: <201107200609.p6K69mNj055558@wwwmaster.postgresql.org> <20110720114312.GA27337@depesz.com> Comments: In-reply-to hubert depesz lubaczewski message dated "Wed, 20 Jul 2011 13:43:12 +0200" Date: Wed, 20 Jul 2011 10:59:15 -0400 Message-ID: <11431.1311173955@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-3.093 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-1.193 X-Spam-Level: X-Archive-Number: 201107/146 X-Sequence-Number: 30758 hubert depesz lubaczewski writes: > On Wed, Jul 20, 2011 at 06:09:48AM +0000, Ludek Bouska wrote: >> script >> select ('20110720'::date,'20110721'::date) overlaps >> ('20110721'::date,'20110721'::date) >> gives false. > docs clearly state the reason: > http://www.postgresql.org/docs/current/interactive/functions-datetime.html > This expression yields true when two time periods (defined by their > endpoints) overlap, false when they do not overlap. The endpoints can be > specified as pairs of dates, times, or time stamps; or as a date, time, or > time stamp followed by an interval. When a pair of values is provided, > either the start or the end can be written first; OVERLAPS automatically > takes the earlier value of the pair as the start. Each time period is > considered to represent the half-open interval start <= time < end, unless > start and end are equal in which case it represents that single time > instant. This means for instance that two time periods with only an endpoint > in common do not overlap. Yeah. This is an explanation in words of the behavior defined by the SQL spec. The actual spec text defines the result of (D1, E1) OVERLAPS (D2, E2) for the non-interval cases thus: 3) If D1 is the null value or if E1 < D1, then let S1 = E1 and let T1 = D1. Otherwise, let S1 = D1 and let T1 = E1. 5) If D2 is the null value or if E2 < D2, then let S2 = E2 and let T2 = D2. Otherwise, let S2 = D2 and let T2 = E2. 6) The result of the is the result of the following expression: ( S1 > S2 AND NOT ( S1 >= T2 AND T1 >= T2 ) ) OR ( S2 > S1 AND NOT ( S2 >= T1 AND T2 >= T1 ) ) OR ( S1 = S2 AND ( T1 <> T2 OR T1 = T2 ) ) If you work through that you'll see that indeed these values should produce a false result: we have S2 > S1, S2 >= T1, T2 >= T1. regards, tom lane