Re: Extract epoch from Interval weird behavior

From: Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com>
To: Joseph Koshakow <koshy44(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Extract epoch from Interval weird behavior
Date: 2022-04-08 11:38:48
Message-ID: 3e0b6f70-1098-f895-76f7-883fe0a3c267@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 24.02.22 03:35, Joseph Koshakow wrote:
>> However when executing EXTRACT we first truncate
>> DAYS_PER_YEAR to an integer, and then multiply it
>> by the total years in the Interval
>> /* this always fits into int64 */
>>> secs_from_day_month = ((int64) DAYS_PER_YEAR * (interval->month / MONTHS_PER_YEAR) +
>>> (int64) DAYS_PER_MONTH * (interval->month % MONTHS_PER_YEAR) +
>>> interval->day) * SECS_PER_DAY;
>> Is this truncation on purpose? It seems like
>> EXTRACT is not accounting for leap years in
>> it's calculation.

This was not intentional. The cast is only to make the multiplication
happen in int64; it didn't mean to drop any fractional parts.

> Oops I sent that to the wrong email. If this isn't intented I've created a patch
> that fixes it, with the following two open questions
> * DAYS_PER_YEAR_NUM is recalculated every time. Is there anyway
> to convert a float directly to a numeric to avoid this?

We really wanted to avoid doing calculations in numeric as much as
possible. So we should figure out a different way to write this. The
attached patch works for me. It's a bit ugly since it hardcodes some
factors. Maybe we can rephrase it a bit more elegantly.

Attachment Content-Type Size
v1-0001-Fix-extract-epoch-from-interval-calculation.patch text/plain 4.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Langote 2022-04-08 11:45:37 Re: generic plans and "initial" pruning
Previous Message Matthias van de Meent 2022-04-08 11:38:22 Re: Lowering the ever-growing heap->pd_lower