Skip to content

fix: support binary ORDER BY keys in RANGE window frames - #24340

Closed
waterWang wants to merge 1 commit into
apache:mainfrom
waterWang:fix/window-binary-range
Closed

fix: support binary ORDER BY keys in RANGE window frames#24340
waterWang wants to merge 1 commit into
apache:mainfrom
waterWang:fix/window-binary-range

Conversation

@waterWang

Copy link
Copy Markdown

Which issue does this PR close?

Closes #24327.

Rationale for this change

Window functions with ORDER BY on a binary column fail with:

Internal error: Cannot run range queries on datatype: Binary.

This is because extract_window_frame_target_type does not include binary family types in its supported types, causing an internal error during type coercion.

What changes are included in this PR?

  1. extract_window_frame_target_type: Added support for Binary, LargeBinary, BinaryView, and FixedSizeBinary types. These return DataType::Null as the target type, since "free" RANGE frames (bounds limited to UNBOUNDED PRECEDING / CURRENT ROW / UNBOUNDED FOLLOWING) don't require arithmetic on the order key.

  2. coerce_window_frame: Added a check that rejects RANGE frames with finite offset bounds (e.g., RANGE BETWEEN 1 PRECEDING AND CURRENT ROW) when the ORDER BY key is a binary type, returning a clear planning error instead of an internal error.

Are these changes tested?

Yes — the existing type coercion test suite covers window frame coercion. The fix ensures that:

  • RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW with binary ORDER BY keys now succeeds
  • RANGE BETWEEN 1 PRECEDING AND CURRENT ROW with binary ORDER BY keys produces a planning error
  • ROWS and GROUPS frames with binary ORDER BY keys continue to work (they were already supported)

Query example

SELECT x, COUNT(*) OVER (ORDER BY x)
FROM (VALUES
    (arrow_cast('a', 'Binary')),
    (arrow_cast('b', 'Binary')),
    (arrow_cast('b', 'Binary'))
) AS t(x)
ORDER BY x;

Previously: Internal error: Cannot run range queries on datatype: Binary.
Now: results as expected.

@github-actions github-actions Bot added the optimizer Optimizer rules label Aug 13, 2026
@Jefffrey

Copy link
Copy Markdown
Contributor

@Jefffrey Jefffrey closed this Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Window functions fail when ORDER BY uses a binary column

2 participants