From: Sebastien Flaesch <sebastien.flaesch@4js.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: SUM() of INTERVAL type produces INTERVAL with no precision
Date: Fri, 3 Dec 2021 16:12:36 +0000
Message-ID: <DBAP191MB128944EC48D51CB1C34D3EB8B06A9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <DBAP191MB1289BC291027FD78F77B2737B06A9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM>
References: <DBAP191MB1289209E98AA6F340651F7A3B06A9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM>
<2292656.1638544536@sss.pgh.pa.us>
<DBAP191MB1289BC291027FD78F77B2737B06A9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM>
Hum....
test1=> create table tab2 ( i interval hour to minute );
CREATE TABLE
test1=> insert into tab2 values ( interval '9999-10 555 11:22:33' );
INSERT 0 1
test1=> select * from tab2;
i
--------------------------------------
9999 years 10 mons 555 days 11:22:00
(1 row)
Expected?
Qualifiers "hour to minute" seem just to be some decoration...
Seb
________________________________
From: Sebastien Flaesch <sebastien.flaesch@4js.com>
Sent: Friday, December 3, 2021 5:08 PM
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: SUM() of INTERVAL type produces INTERVAL with no precision
EXTERNAL: Do not click links or open attachments if you do not recognize the sender.
Hi Tom,
I do care because I wrote a database driver/interface that needs to do automatic conversions from native PostgreSQL types, to our programming language types (we sell a compile/runtime system).
I need introspection of the fetched data. The more exact type info I get, the better.
As our software is a programming language runtime system, I don't know in advance the SQL statement and column types.
It's all generic code.
We have features/APIs that return the type of a SELECT item, like ODBC's SQLDescribeCol() ...
Our programming language supports Informix-style INTERVALs where you have 2 classes:
INTERVAL year-to-month
INTERVAL day-to-second[.fractions]
It's not easy to explain all the constraints here in a few lines but basically, I can better handle the values returned from PQgetvalue(), when I know exactly when is the original type from PQftype() and PQfmode()
We could help our customer by using a CAST():
SELECT CAST( SUM(col) AS INTERVAL HOUR TO MINUTE) ...
After more tests:
I have some doubts about the values that can be inserted into a PostgreSQL INTERVAL.
Sorry if I am missing something, but it's quite strange to me that one can insert various "classes" of interval values:
test1=> create table tab1 ( i interval );
CREATE TABLE
test1=> insert into tab1 values ( interval '12434:12:11' );
INSERT 0 1
test1=> select * from tab1;
i
-------------
12434:12:11
(1 row)
test1=> insert into tab1 values ( interval '99999 12:11:10' );
INSERT 0 1
test1=> select * from tab1;
i
---------------------
12434:12:11
99999 days 12:11:10
(2 rows)
test1=> insert into tab1 values ( interval '9999-10 12:11:10' );
INSERT 0 1
test1=> select * from tab1;
i
-----------------------------
12434:12:11
99999 days 12:11:10
9999 years 10 mons 12:11:10
(3 rows)
test1=> insert into tab1 values ( interval '9999-10 55 12:11:10' );
INSERT 0 1
test1=> select * from tab1;
i
-------------------------------------
12434:12:11
99999 days 12:11:10
9999 years 10 mons 12:11:10
9999 years 10 mons 55 days 12:11:10
(4 rows)
How much days is is 9999 years 10 months 55 days?
A month can have 30, 31, 28 or 29 days ...
I must carefully read the doc again.
Thanks for you answer(s)
Seb
________________________________
From: Tom Lane <tgl@sss.pgh.pa.us>
Sent: Friday, December 3, 2021 4:15 PM
To: Sebastien Flaesch <sebastien.flaesch@4js.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: SUM() of INTERVAL type produces INTERVAL with no precision
EXTERNAL: Do not click links or open attachments if you do not recognize the sender.
Sebastien Flaesch <sebastien.flaesch@4js.com> writes:
> When doing a SUM( ) aggregate on an INTERVAL HOUR TO SECOND(0) column, the resulting type loses the interval type qualifiers...
We don't generally attribute a typmod (which is what interval field
specs are) to the output of any function, other than the ones that are
specifically identified in the catalogs as cast-to-a-particular-typmod
infrastructure. Why do you care? The value is the same regardless.
> The type of a SELECT item is important to us because we have generic C code that identifies the PQfmod() type, to properly interpret the string value returned by PQgetvalue().
I think you have vastly overestimated the value of PQfmod. Moreover,
if you think you need it to interpret the output data, you are mistaken.
There is no situation in which Postgres output formats are
typmod-specific; in fact, the output function API doesn't even pass
the typmod.
regards, tom lane
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: sebastien.flaesch@4js.com, tgl@sss.pgh.pa.us
Subject: Re: SUM() of INTERVAL type produces INTERVAL with no precision
In-Reply-To: <DBAP191MB128944EC48D51CB1C34D3EB8B06A9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM>
References: <DBAP191MB1289209E98AA6F340651F7A3B06A9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM> <2292656.1638544536@sss.pgh.pa.us> <DBAP191MB1289BC291027FD78F77B2737B06A9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM> <DBAP191MB128944EC48D51CB1C34D3EB8B06A9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM>
* 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