BUG #19722: Window PARTITION BY numeric treats equal values with different scales as separate partitions

Started by PG Bug reporting form26 days ago2 messagesbugs
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19722
Logged by: N J
Email address: 1482694023@qq.com
PostgreSQL version: 18.6
Operating system: Windows 11 64-bit
Description:

Environment:
PostgreSQL: 18.6
OS: Windows 11 64-bit

Reproduction SQL:
CREATE TEMP TABLE decimal_source (raw_value text NOT NULL);
INSERT INTO decimal_source VALUES
('1.0'), ('1.00'), ('1.000');

SELECT amount, partition_size
FROM (
SELECT raw_value::numeric AS amount,
COUNT(*) OVER (PARTITION BY raw_value::numeric) AS partition_size
FROM decimal_source
) AS windowed
WHERE scale(amount) = 1;

Observed result:
amount | partition_size
--------+----------------
1.0 | 1

Expected result:
amount | partition_size
--------+----------------
1.0 | 3

Bug analysis:
All three text values convert to numerically equal numeric values (1.0,
1.00, 1.000 all represent the same number). According to standard SQL
semantics, PARTITION BY groups rows by value equality, so all three rows
should belong to the same window partition and COUNT(*) OVER should return 3
for every row.

The actual result shows partition_size = 1, which means values with
different decimal scales are incorrectly treated as distinct partition keys.
The window partition logic appears to use the internal representation
(including scale metadata) instead of logical numeric equality to determine
partition membership.

This is a correctness bug: numerically equal values must be grouped into the
same window partition regardless of their precision/scale.

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: PG Bug reporting form (#1)
Re: BUG #19722: Window PARTITION BY numeric treats equal values with different scales as separate partitions

PG Bug reporting form <noreply@postgresql.org> writes:

CREATE TEMP TABLE decimal_source (raw_value text NOT NULL);
INSERT INTO decimal_source VALUES
('1.0'), ('1.00'), ('1.000');

SELECT amount, partition_size
FROM (
SELECT raw_value::numeric AS amount,
COUNT(*) OVER (PARTITION BY raw_value::numeric) AS partition_size
FROM decimal_source
) AS windowed
WHERE scale(amount) = 1;

This is a near-duplicate of many recent reports[1]/messages/by-id/CA+COZaA0tnyza0pHx6-UEdM5dcLGP4RJJ0KOHFq+wVQCY2Jyiw@mail.gmail.com[2]/messages/by-id/19588-e32b82433b3660ea@postgresql.org[3]/messages/by-id/19619-fc646db1c6b2dc90@postgresql.org[4]/messages/by-id/19697-6e7ccba388bbe858@postgresql.org[5]/messages/by-id/19713-635b8656d07464df@postgresql.org.
Not one of them has offered a reason why they think that testing
scale() of a grouped numeric column is a useful or even well-defined
thing to do. Do you have a real-world use case? What is it?

regards, tom lane

[1]: /messages/by-id/CA+COZaA0tnyza0pHx6-UEdM5dcLGP4RJJ0KOHFq+wVQCY2Jyiw@mail.gmail.com
[2]: /messages/by-id/19588-e32b82433b3660ea@postgresql.org
[3]: /messages/by-id/19619-fc646db1c6b2dc90@postgresql.org
[4]: /messages/by-id/19697-6e7ccba388bbe858@postgresql.org
[5]: /messages/by-id/19713-635b8656d07464df@postgresql.org