#27347·risingwave

sum() over a DISTINCT ON subquery gives different last bits in two identical materialized views, at parallelism 1

Author: sumaiyaazadCreated Sep 29, 2026Updated Sep 29, 2026

Version: RisingWave v2.8.4 (a95b9d3b4d5a0e1d7ab36c50d6d72484680be487), Docker image, single node.

What happens

Two materialized views have the same definition and read the same table. Each one removes duplicates with DISTINCT ON and then sums per group. Their sums differ in the last few bits.

Without DISTINCT ON, the same sum taken straight from the table is identical in both views.

The differences are tiny, about one part in 10^15. Every result is still a correct sum of the inputs, just added in a different order. So this is about repeatability, not correctness: the same query on the same data does not give the same bits twice.

How to reproduce

Run this in psql against a single-node RisingWave:

sql
SET streaming_parallelism = 1;
CREATE TABLE t (event_id int, grp int, value double precision);

-- Two identical views: deduplicate by event_id, then sum per group.
CREATE MATERIALIZED VIEW dedup_a AS
  WITH d AS (SELECT DISTINCT ON (event_id) event_id, grp, value
             FROM t ORDER BY event_id)
  SELECT grp, sum(value) AS s FROM d GROUP BY grp;
CREATE MATERIALIZED VIEW dedup_b AS
  WITH d AS (SELECT DISTINCT ON (event_id) event_id, grp, value
             FROM t ORDER BY event_id)
  SELECT grp, sum(value) AS s FROM d GROUP BY grp;

-- Two identical control views: the same sum with no deduplication.
CREATE MATERIALIZED VIEW plain_a AS SELECT grp, sum(value) AS s FROM t GROUP BY grp;
CREATE MATERIALIZED VIEW plain_b AS SELECT grp, sum(value) AS s FROM t GROUP BY grp;

-- 20,000 rows in 50 groups, with values of mixed size so that the order
-- of addition changes the rounding.
INSERT INTO t
  SELECT i, i % 50, (i % 1000) * 0.001 + (i % 7) * 1000.0
  FROM generate_series(1, 20000) AS g(i);
FLUSH;

SELECT count(*) AS dedup_groups_that_differ
  FROM (SELECT grp, s FROM dedup_a EXCEPT SELECT grp, s FROM dedup_b) x;
SELECT count(*) AS plain_groups_that_differ
  FROM (SELECT grp, s FROM plain_a EXCEPT SELECT grp, s FROM plain_b) x;
SELECT a.grp, a.s, b.s FROM dedup_a a JOIN dedup_b b USING (grp)
  WHERE a.s <> b.s ORDER BY a.grp LIMIT 3;

Desired, for bit reproducibility

Both counts are 0, so two views with the same definition over the same data hold the same bits. RisingWave does not promise this, so the gap below is a reproducibility limit rather than a wrong result.

Actual

Query Groups that differ
dedup_a against dedup_b 20 of 50
plain_a against plain_b 0 of 50

Example rows from the last query:

 grp |         a.s        |         b.s
-----+--------------------+--------------------
   0 | 1198190.0000000002 | 1198190.0
   1 | 1198190.399999999  | 1198190.3999999992

The count of differing groups changes from run to run. That is the problem itself.

Why parallelism 1: at streaming_parallelism = 4 the plain views differ too, because partial sums are then combined in a changing order. Parallelism 1 removes that effect, so only the DISTINCT ON effect is left.

Likely cause

DISTINCT ON runs as the group Top-N operator. In src/stream/src/executor/top_n/group_top_n.rs (v2.8.4):

  1. apply_chunk collects each chunk's changes in a HashMap<K, TopNStaging> (line 173).
  2. It builds the output chunk by walking that map withstagings.into_values() (line 234).
  3. A standard HashMap walks its entries in a randomly seeded order, so the rows reach the downstream sum in a different order each time.
  4. Floating-point addition rounds differently in a different order, so the last bits change.

The append-only version, src/stream/src/executor/top_n/group_top_n_appendonly.rs, does the same at lines 154 and 203.

What we ask

This may be intended. If so, please document that floating-point aggregates after Top-N or DISTINCT ON are not bit-reproducible. If not, emitting each chunk's changes in a fixed order, for example, sorted by group key, would make repeated runs match at parallelism 1.

Source: risingwavelabs/risingwave