Re: truncating timestamps on arbitrary intervals

From: Bauyrzhan Sakhariyev <baurzhansahariev(at)gmail(dot)com>
To: John Naylor <john(dot)naylor(at)enterprisedb(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: truncating timestamps on arbitrary intervals
Date: 2021-07-22 20:49:17
Message-ID: CAKpL73sE9SqFcdinwsm9xR9O9fFCMKhwP73kQr-Q3gu-EWoo2Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> No, the boundary is intentionally the earlier one:

I found that commit in GitHub, thanks for pointing it out.
When I test locally *origin_in_the_future *case I get different results for
positive and negative intervals (see queries #1 and #2 from above, they
have same timestamp, origin and interval magnitude, difference is only in
interval sign) - can it be that the version I downloaded from
https://www.enterprisedb.com/postgresql-early-experience doesn't include
commit with that improvement?

> I wonder if we should just disallow negative intervals here.

I cannot imagine somebody using negative as a constant argument but users
can pass another column as a first argument date or some function(ts) - not
likely but possible. A line in docs about the leftmost point of interval as
start of the bin could be helpful.

Not related to negative interval - I created a PR for adding zero check for
stride https://github.com/postgres/postgres/pull/67 and after getting it
closed I stopped right there - 1 line check doesn't worth going through the
patching process I'm not familiar with.

>In the case of full units (1 minute, 1 hour, etc.), it gives the same
result as the analogous date_trunc call,
Was not obvious to me that we need to supply Monday origin to make
date_bin(1 week, ts) produce same result with date_trunc

Sorry for the verbose report and thanks for the nice function - I know
it's not yet released, was just playing around with beta as I want to
align CrateDB
date_bin <https://github.com/crate/crate/issues/11310> with Postgresql

On Thu, Jul 22, 2021 at 7:28 PM John Naylor <john(dot)naylor(at)enterprisedb(dot)com>
wrote:

>
> On Thu, Jul 22, 2021 at 12:24 PM Bauyrzhan Sakhariyev <
> baurzhansahariev(at)gmail(dot)com> wrote:
> >
> > Is date_bin supposed to return the beginning of the bin?
>
> Thanks for testing! And yes.
>
> > And does the sign of an interval define the "direction" of the bin?
>
> No, the boundary is intentionally the earlier one:
>
> /*
> * Make sure the returned timestamp is at the start of the bin, even if
> * the origin is in the future.
> */
> if (origin > timestamp && stride_usecs > 1)
> tm_delta -= stride_usecs;
>
> I wonder if we should just disallow negative intervals here.
>
> --
> John Naylor
> EDB: http://www.enterprisedb.com
>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2021-07-22 20:54:58 Re: Autovacuum on partitioned table (autoanalyze)
Previous Message Robert Haas 2021-07-22 20:01:10 Re: Delegating superuser tasks to new security roles (Was: Granting control of SUSET gucs to non-superusers)