pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Francisco Olarte <folarte@peoplecall.com>
Cc: Chuck Martin <clmartin@theombudsman.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Query not producing expected result
Date: Thu, 2 May 2019 08:17:11 -0700
Message-ID: <b35aa052-12b4-bad0-5d3f-812e17499342@aklaver.com> (raw)
In-Reply-To: <CA+bJJbx_BLQ2DV3n_HXEKuSFJDX8NNnJ+Pyu0gh5AYNa4eTd=Q@mail.gmail.com>
References: <CAFw6=U2AisghJ2cOPOWopmk3NVVJ-WcSTNM+6K_-kQVBGxzMaA@mail.gmail.com>
	<25696.1556728884@sss.pgh.pa.us>
	<CAFw6=U3wV3NDt3c8sqHiE4QLRufy3s8d21BaLdkRLpPNUEh9ww@mail.gmail.com>
	<CA+bJJbyXzdBL7198YsJTjyWjtrZbrkfiw9w=cxERhQnZ=j8bww@mail.gmail.com>
	<f677424a-5902-63f2-608d-8b86412375c9@aklaver.com>
	<CAFw6=U3iPjbDykpPeuG09RHTrdhdUN-OMHksxmVwukzuL3dbZw@mail.gmail.com>
	<CA+bJJbxgDjLxQ9Mx8ipz29WT6_nfhbrh9+7gP0nZz4siBc29QQ@mail.gmail.com>
	<f7baf914-c756-e7b9-6afb-dce6beef014c@aklaver.com>
	<CA+bJJbzPv+Wa1CqhOm8ZtV8ZN6z2=OAF1wR=bB8xm0wXyR9QrA@mail.gmail.com>
	<1e416a02-099f-004b-aaeb-0521cd03e6bf@aklaver.com>
	<CA+bJJbx_BLQ2DV3n_HXEKuSFJDX8NNnJ+Pyu0gh5AYNa4eTd=Q@mail.gmail.com>

On 5/2/19 12:57 AM, Francisco Olarte wrote:
> Adrian:
> 
> On Wed, May 1, 2019 at 8:14 PM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
> .....
>>> select '2019-05-02'::timestamp <= '2019-05-01 24:00'::timestamp;
>>> And you'll see and out of range date selected.
>>
>> Technically it is correct as:
>> test_(postgres)# select '2019-05-02'::timestamp;
>>         timestamp
>> ---------------------
>>    2019-05-02 00:00:00
> 
> "Tecnhnically" is not an exact term in this context, so , ok, you right.

If you want exact:

test=> select '2019-05-01 24:00'::timestamp = '2019-05-02'::timestamp;
  ?column?
----------
  t

For the reason why see below.

> 
>> which is Midnight and is both the end of one day and start of another.
> 
> That's one definition. Of part a timestamp system on which timestamps
> belong to either one or two date. Use it at your own risk. I prefer to
> use one where, once the time zone is fixed ( as I see some tz stuff
> coming ), timestamps belong to exactly one date, I've founds it avoids
> problem, IANAL, YMMV, ....
> 
>> It comes down to where you want to draw the line between days.
> 
> Normally everybody wants "timestamps in 2019-04-01" to give
> "2019-04-01" when converted to date.
> 
> You can try all sort of technicisms and discussions, but the fact is
> if you want the simpler/original:
> 
>        dateTime::date <= '2019-05-01'::date ( second cast is implicit usually)
> 
> And you try to keep the <= but enable indexing by augmenting the
> constant to 24:00
> 
>       datetime <= '2019-05-01 24:00'::timestamp ( second cast auto )
> 
> You have changed the query, and it is the 24:00 which is nearly hiding
> it. Probably explain would shot it.
> 
> But if you get into the habit of working with <:
> 
>      datTime::date < '2019-05-02'   (::date implicit )
> 
> The transformation is straightforward:
> 
>     dateTime < '2019-05-02'   (::timestamp implicit )

Unless of course you are working with something that uses the standard 
below:

test=> select '2019-05-01 24:00'::timestamp < '2019-05-02'::timestamp; 
 

  ?column? 
 

---------- 
 

  f

I know a corner case. Still what all this points out is that it comes 
down to a definition on the part of a user/institution as to how they 
are going to define the day boundary.

> 
> Even if your constants come from user input and are complex,
> transformation works, as
> 
>     sometimestamp::date < somedate  ( somedate supposed to be date-typed)
> 
> Works if you just switch the casting side:
> 
>     sometimestamp< sometdate::timestamp
> 
> You know it. Playing with 24:00, which IIRC is just some exception,

It is not just some exception it is an ISO standard:

https://en.wikipedia.org/wiki/ISO_8601#Times

"Midnight is a special case and may be referred to as either "00:00" or 
"24:00". The notation "00:00" is used at the beginning of a calendar day 
and is the more frequently used. At the end of a day use "24:00". 
"2007-04-05T24:00" is the same instant as "2007-04-06T00:00" (see 
Combined date and time representations below). "


> and is a value that is never going to be produced on output, is nice
> for quickie handwritten queries, but not a path I would recommend. In
> fact if you have a date in an expression it's going to be really
> akward to use, you'll have to convert a date to timestamp by piping it
> through text conversions. It's, IMNSHO, much better to learn to do it
> in the typed world and avoid text conversions as much as possible,
> they are known to be responsible for lots of problems in the computer
> bussiness.
> 
> Francisco Olarte.
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (22+ messages)

Message-ID: <b35aa052-12b4-bad0-5d3f-812e17499342@aklaver.com>
Permalink:  ../b35aa052-12b4-bad0-5d3f-812e17499342@aklaver.com/
Also on:    postgresql.org/message-id/b35aa052-12b4-bad0-5d3f-812e17499342@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, folarte@peoplecall.com, clmartin@theombudsman.com
  Subject: Re: Query not producing expected result
  In-Reply-To: <b35aa052-12b4-bad0-5d3f-812e17499342@aklaver.com>
  References: <CAFw6=U2AisghJ2cOPOWopmk3NVVJ-WcSTNM+6K_-kQVBGxzMaA@mail.gmail.com> <25696.1556728884@sss.pgh.pa.us> <CAFw6=U3wV3NDt3c8sqHiE4QLRufy3s8d21BaLdkRLpPNUEh9ww@mail.gmail.com> <CA+bJJbyXzdBL7198YsJTjyWjtrZbrkfiw9w=cxERhQnZ=j8bww@mail.gmail.com> <f677424a-5902-63f2-608d-8b86412375c9@aklaver.com> <CAFw6=U3iPjbDykpPeuG09RHTrdhdUN-OMHksxmVwukzuL3dbZw@mail.gmail.com> <CA+bJJbxgDjLxQ9Mx8ipz29WT6_nfhbrh9+7gP0nZz4siBc29QQ@mail.gmail.com> <f7baf914-c756-e7b9-6afb-dce6beef014c@aklaver.com> <CA+bJJbzPv+Wa1CqhOm8ZtV8ZN6z2=OAF1wR=bB8xm0wXyR9QrA@mail.gmail.com> <1e416a02-099f-004b-aaeb-0521cd03e6bf@aklaver.com> <CA+bJJbx_BLQ2DV3n_HXEKuSFJDX8NNnJ+Pyu0gh5AYNa4eTd=Q@mail.gmail.com> <b35aa052-12b4-bad0-5d3f-812e17499342@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox