From: Bruce Momjian <bruce@momjian.us>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: jian he <jian.universality@gmail.com>
Cc: Francisco Olarte <folarte@peoplecall.com>
Cc: Michael Bondarenko <work.michael.2956@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org, dgrowleyml@gmail.com, Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Subject: Re: BUG #18348: Inconsistency with EXTRACT([field] from INTERVAL);
Date: Thu, 15 Aug 2024 22:45:58 -0400
Message-ID: <Zr69ZnXWlS_2_bvI@momjian.us> (raw)
In-Reply-To: <1079670.1720458208@sss.pgh.pa.us>
References: <CACJufxGB4MD6vRz3jVGK6kza-6fuDgfW2jC_1S2+MwcQYGkgSA@mail.gmail.com>
<3749306.1708222748@sss.pgh.pa.us>
<CACJufxHL0g73FPaRS+OGSYWJ0YbP6nEdVK79=wjL-TKU0_heDw@mail.gmail.com>
<222870.1708458150@sss.pgh.pa.us>
<270582.1708462564@sss.pgh.pa.us>
<CACJufxFOjGpAQJ5EJ0=a1geERgdHW77SEdRyK8A_qwB3CVdwhQ@mail.gmail.com>
<166456.1715117228@sss.pgh.pa.us>
<CACJufxGFuZsj+31a204fFpxv_RCHA-u1h6WEBZ6qpjBVoio5dQ@mail.gmail.com>
<476283.1715177448@sss.pgh.pa.us>
<1079670.1720458208@sss.pgh.pa.us>
On Mon, Jul 8, 2024 at 01:03:28PM -0400, Tom Lane wrote:
> I took another look at this issue and got annoyed by the fact that the
> proposed coding for "quarter" still doesn't satisfy the rule that
> the output for a negative interval should be the negative of the
> output for the sign-reversed interval. Specifically, if the month
> field is zero, the v2 patch always emits 1:
>
> regression=# select extract(quarter from interval '1 day');
> extract
> ---------
> 1
> (1 row)
>
> regression=# select extract(quarter from interval '-1 day');
> extract
> ---------
> 1
> (1 row)
>
> We could fix that by examining the sign of the lower-order fields
> when month is zero, as in the v3 patch attached. However, I'm not
> at all sure this is really better than v2. Notably, it makes the
> documentation's statement that the result is "the month field
> divided by 3 plus 1" even more incomplete. I still don't really
> want to go into details about the behavior for negative intervals.
> OTOH if we did do that, I'd rather write a blanket statement
> about the result being the negative of the result for a positive
> interval.
>
> Thoughts?
I tested master, patch version 2 and patch version 3 with some sample
extract() queires, attached. I like patch version 2. Patch version 3
bothers me because "-600 days" is ignored if months is non-zero, and
used for its sign for zero month values, which seems odd to me; better
to ignore it.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
Only you can decide what is important to you.
SELECT extract(quarter FROM interval '18 months');
extract
---------
3
SELECT extract(quarter FROM interval '-18 months');
extract
---------
-1
SELECT extract(quarter FROM interval '-18 months -600 days');
extract
---------
-1
SELECT extract(quarter FROM interval '-600 days');
extract
---------
1
SELECT extract(quarter FROM interval '18 months');
extract
---------
3
SELECT extract(quarter FROM interval '-18 months');
extract
---------
-3
SELECT extract(quarter FROM interval '-18 months -600 days');
extract
---------
-3
SELECT extract(quarter FROM interval '-600 days');
extract
---------
1
SELECT extract(quarter FROM interval '18 months');
extract
---------
3
SELECT extract(quarter FROM interval '-18 months');
extract
---------
-3
SELECT extract(quarter FROM interval '-18 months -600 days');
extract
---------
-3
SELECT extract(quarter FROM interval '-600 days');
extract
---------
-1
Attachments:
[application/sql] extract.sql (216B, ../Zr69ZnXWlS_2_bvI@momjian.us/2-extract.sql)
download
[text/plain] master.out (334B, ../Zr69ZnXWlS_2_bvI@momjian.us/3-master.out)
download | inline:
SELECT extract(quarter FROM interval '18 months');
extract
---------
3
SELECT extract(quarter FROM interval '-18 months');
extract
---------
-1
SELECT extract(quarter FROM interval '-18 months -600 days');
extract
---------
-1
SELECT extract(quarter FROM interval '-600 days');
extract
---------
1
[text/plain] patch2.out (334B, ../Zr69ZnXWlS_2_bvI@momjian.us/4-patch2.out)
download | inline:
SELECT extract(quarter FROM interval '18 months');
extract
---------
3
SELECT extract(quarter FROM interval '-18 months');
extract
---------
-3
SELECT extract(quarter FROM interval '-18 months -600 days');
extract
---------
-3
SELECT extract(quarter FROM interval '-600 days');
extract
---------
1
[text/plain] patch3.out (334B, ../Zr69ZnXWlS_2_bvI@momjian.us/5-patch3.out)
download | inline:
SELECT extract(quarter FROM interval '18 months');
extract
---------
3
SELECT extract(quarter FROM interval '-18 months');
extract
---------
-3
SELECT extract(quarter FROM interval '-18 months -600 days');
extract
---------
-3
SELECT extract(quarter FROM interval '-600 days');
extract
---------
-1
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 using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: bruce@momjian.us, tgl@sss.pgh.pa.us, jian.universality@gmail.com, folarte@peoplecall.com, work.michael.2956@gmail.com, peter.eisentraut@enterprisedb.com
Subject: Re: BUG #18348: Inconsistency with EXTRACT([field] from INTERVAL);
In-Reply-To: <Zr69ZnXWlS_2_bvI@momjian.us>
* 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