Skip to content

[CALCITE-7864] RANGE window frames should handle NULL ORDER BY keys - #5337

Merged
mihaibudiu merged 1 commit into
apache:mainfrom
letian-jiang:codex/calcite-7864-range-null-order-key
Oct 18, 2026
Merged

mihaibudiu merged 1 commit into
apache:mainfrom
letian-jiang:codex/calcite-7864-range-null-order-key

Conversation

@letian-jiang

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7864

Changes Proposed

Fix a NullPointerException when evaluating RANGE window frames with offset bounds and NULL ORDER BY keys.

Guard boundary arithmetic for nullable keys and preserve NULL as the boundary search key. Explicitly box the non-NULL result to prevent implicit unboxing in the conditional expression.

For NULL keys, offset lower and upper bounds resolve to the first and last NULL peers, respectively. UNBOUNDED endpoints retain their partition-boundary behavior.

For example:

SELECT k,
       SUM(v) OVER (
         ORDER BY k NULLS LAST
         RANGE BETWEEN 1 PRECEDING AND CURRENT ROW
       ) AS s
FROM (VALUES (1, 10), (2, 20),
             (CAST(NULL AS INTEGER), 100), (NULL, 200)) AS t(k, v)

Previously, this query threw a NullPointerException. It now returns (1, 10), (2, 30), (NULL, 300), (NULL, 300), ignoring row order.

Add 28 parameterized regression cases covering INTEGER/BIGINT, NULLS FIRST/LAST, offsets at both frame endpoints, and combinations with CURRENT ROW and UNBOUNDED bounds.

for (String type : new String[] {"integer", "bigint"}) {
for (String nulls : new String[] {"first", "last"}) {
// Cover PRECEDING and FOLLOWING for both the lower and upper bounds.
cases.add(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did you validate these expected results?
Please add a comment explaining.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. I ran all 28 SQL cases against PostgreSQL 14.24 and confirmed that all 140 result rows match the expected results, ignoring row order while preserving duplicate NULL-key rows. I also added a comment documenting the verification method and PostgreSQL version in d6df043.

@mihaibudiu mihaibudiu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can squash the commits for merging.

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Oct 18, 2026
Handle nullable ORDER BY keys in offset RANGE boundaries and add 28 regression cases. Verify expected results against PostgreSQL 14.24.
@letian-jiang
letian-jiang force-pushed the codex/calcite-7864-range-null-order-key branch from d6df043 to c1916eb Compare October 18, 2026 05:16
@letian-jiang

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I’ve squashed the commits into a single commit: c1916eb.

@sonarqubecloud

Copy link
Copy Markdown

@mihaibudiu
mihaibudiu merged commit 4681d4d into apache:main Oct 18, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants