Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1semyc-00C230-6y for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Aug 2024 02:46:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1semyZ-002e3l-TJ for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Aug 2024 02:46:03 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1semyZ-002e3d-KP for pgsql-bugs@lists.postgresql.org; Fri, 16 Aug 2024 02:46:03 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1semyW-004xIK-T1 for pgsql-bugs@lists.postgresql.org; Fri, 16 Aug 2024 02:46:02 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=momjian.us; s=2024011501; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description; bh=iB/V6yUKMG+hlePdcbbM4fTsIxr8RpF8nzTT9k3Re2k=; b=YLR3J aecyOCt6O7bwNC6oPky+4U0t9aju4VuGNj7clgsw9gH80ONuyAERpJd2otO76X07l8bPRcrKAV1j6 vzKLBdDGObYR335A5wM+s3NiNf6qt+Pg2o9d8SQys6uKq/oEYvkHzsjEE2/mFyFPgNQuj7szZL2Ik cSvIyoR+w/RFnT5tiV6nQY1ijikI4KovZiQJtxAJaUGs27qLBAehey5j0dttWREN2T0R7XaKs7oHG pMHHbfDhqZPF5IiQxnxu9zwucLlS10gSxj21F0B3yfvAsl/bt2JkeP2ctcB5iJA3SI7CMqGgo6f27 61jIsOKlYHJl4K2DSOs3YCt0bZhuQ==; Received: from bruce by momjian.us with local (Exim 4.96) (envelope-from ) id 1semyU-00HOGs-0D; Thu, 15 Aug 2024 22:45:58 -0400 Date: Thu, 15 Aug 2024 22:45:58 -0400 From: Bruce Momjian To: Tom Lane Cc: jian he , Francisco Olarte , Michael Bondarenko , pgsql-bugs@lists.postgresql.org, dgrowleyml@gmail.com, Peter Eisentraut Subject: Re: BUG #18348: Inconsistency with EXTRACT([field] from INTERVAL); Message-ID: References: <3749306.1708222748@sss.pgh.pa.us> <222870.1708458150@sss.pgh.pa.us> <270582.1708462564@sss.pgh.pa.us> <166456.1715117228@sss.pgh.pa.us> <476283.1715177448@sss.pgh.pa.us> <1079670.1720458208@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Q/H+2X3Ci5E6Bip5" Content-Disposition: inline In-Reply-To: <1079670.1720458208@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Q/H+2X3Ci5E6Bip5 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 https://momjian.us EDB https://enterprisedb.com Only you can decide what is important to you. --Q/H+2X3Ci5E6Bip5 Content-Type: application/sql Content-Disposition: attachment; filename="extract.sql" Content-Transfer-Encoding: quoted-printable SELECT extract(quarter FROM interval '18 months');=0ASELECT extract(quarter= FROM interval '-18 months');=0ASELECT extract(quarter FROM interval '-18 m= onths -600 days');=0ASELECT extract(quarter FROM interval '-600 days');=0A --Q/H+2X3Ci5E6Bip5 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="master.out" 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 --Q/H+2X3Ci5E6Bip5 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch2.out" 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 --Q/H+2X3Ci5E6Bip5 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch3.out" 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 --Q/H+2X3Ci5E6Bip5--