The following bug has been logged on the website:
Bug reference: 19588
Logged by: Yuxiao Guo
Email address: dllggyx(at)outlook(dot)com
PostgreSQL version: 18.4
Operating system: Ubuntu 20.04 x86-64, docker image postgres:18.4
Description:
The two forms are semantically equivalent, but I observed different results.
The first version returned 1.0, while the second version returned empty set.
PoC:
CREATE TABLE t0(c0 numeric);
INSERT INTO t0 VALUES (1.0), (1.00);
-- Query A, result: {1.0}
SELECT * FROM (
SELECT DISTINCT ON (c0) c0
FROM t0
ORDER BY c0, scale(c0) DESC
) t1
WHERE scale(c0) = 1;
-- Query B, result: empty set
WITH t1 AS MATERIALIZED (
SELECT DISTINCT ON (c0) c0
FROM t0
ORDER BY c0, scale(c0) DESC
)
SELECT * FROM t1 WHERE scale(c0) = 1;