Re: truncating timestamps on arbitrary intervals - Mailing list pgsql-hackers

From Bauyrzhan Sakhariyev
Subject Re: truncating timestamps on arbitrary intervals
Date
Msg-id CAKpL73sE9SqFcdinwsm9xR9O9fFCMKhwP73kQr-Q3gu-EWoo2Q@mail.gmail.com
Whole thread Raw
In response to Re: truncating timestamps on arbitrary intervals  (John Naylor <john.naylor@enterprisedb.com>)
Responses Re: truncating timestamps on arbitrary intervals
Re: truncating timestamps on arbitrary intervals
List 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 with Postgresql

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

On Thu, Jul 22, 2021 at 12:24 PM Bauyrzhan Sakhariyev <baurzhansahariev@gmail.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

pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: Delegating superuser tasks to new security roles (Was: Granting control of SUSET gucs to non-superusers)
Next
From: Andres Freund
Date:
Subject: Re: Autovacuum on partitioned table (autoanalyze)