Skip to content

Sliding RANGE aggregates retain stale state after an empty frame #24184

Description

@lyne7-sc

Describe the bug

Sliding aggregate window functions can return incorrect results when a bounded RANGE frame transitions from non-empty to empty and then back to non-empty.

The empty frame returns the correct default value, but the accumulator retains values from the previous frame. These stale values are included in subsequent results.

To Reproduce

SELECT k,
       SUM(v) OVER w,
       COUNT(v) OVER w,
       MIN(v) OVER w,
       MAX(v) OVER w
FROM (VALUES
    (0, 100),
    (10, 90),
    (30, 10),
    (40, 20)
) AS t(k, v)
WINDOW w AS (
    ORDER BY k
    RANGE BETWEEN 10 PRECEDING AND 5 PRECEDING
);

Current result:

0  NULL 0 NULL NULL
10 100  1 100  100
30 NULL 0 NULL NULL
40 110  2 10   100

Expected behavior

0  NULL 0 NULL NULL
10 100  1 100  100
30 NULL 0 NULL NULL
40 10   1 10   10

Additional context

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions