Re: BUG #18420: Unexpected values appeared in select query statements that should logically imply each other

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: 王子涵4620 <1290874854(at)qq(dot)com>
Cc: akuluasan <akuluasan(at)163(dot)com>, pgsql-bugs <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #18420: Unexpected values appeared in select query statements that should logically imply each other
Date: 2024-04-04 10:35:42
Message-ID: CAApHDvouSnn+LDUoGgpb2=eiN+a6CAPMrV=rw1VWr2d8EhOvhQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Thu, 4 Apr 2024 at 22:44, 王子涵4620 <1290874854(at)qq(dot)com> wrote:
> create table table_3_utf8_undef (
> id SERIAL PRIMARY KEY,
> "col_varchar(20)_undef_signed" varchar(20)
>
> ) ;
> insert into table_3_utf8_undef values (0,'3
> '),(1,'well'),(2,'-0');

What's going on here is just the same as the following.

postgres=# select '1'::numeric union select '1.00000'::numeric;
numeric
---------
1
(1 row)

postgres=# select '1.00000'::numeric union select '1'::numeric;
numeric
---------
1.00000
(1 row)

Essentially, numerics 1 and 1.00000 are equal but which one is output
from the query depends on which one was seen first by the code that
eliminates the duplicates.

Something similar can be seen with:

postgres=# select sum(n) from (values('1'::numeric),
('10.00000'::numeric), ('-10'::numeric)) n(n);
sum
---------
1.00000
(1 row)

You might expect this returns "1" rather than "1.00000". This is just
another example of us not reducing the numeric's dscale down to the
minimum value it can be without losing precision.

If we always reduced the numeric down to its minimum dscale, then the
following query would return "1" rather than "1.00000". The "n"
column type is numeric(8,5) so having 5 digits after the decimal point
seems correct here.

create table n (n numeric(8,5));
insert into n values('1'),('10'),('-10');
select sum(n) from n;
sum
---------
1.00000

I'm unsure how we'd change the behaviour of one without changing the
behaviour of the other.

David

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Peter Eisentraut 2024-04-04 13:56:25 Re: Building with musl in CI and the build farm
Previous Message Akshat Jaimini 2024-04-04 09:51:48 Re: BUG: deadlock between autovacuum worker and client backend during removal of orphan temp tables with sequences