fix: Support RANGE window frames over binary ORDER BY keys - #24357
Open
fornwall wants to merge 1 commit into
Open
fix: Support RANGE window frames over binary ORDER BY keys#24357fornwall wants to merge 1 commit into
fornwall wants to merge 1 commit into
Conversation
fornwall
force-pushed
the
range-window-bytes
branch
2 times, most recently
from
August 14, 2026 06:50
ce39143 to
5951847
Compare
fornwall
force-pushed
the
range-window-bytes
branch
2 times, most recently
from
August 14, 2026 08:13
cbcb22f to
e0663ae
Compare
Closes apache#24327. A window with an `ORDER BY` over a binary column and no explicit frame gets the default `RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`. Computing that frame's bounds goes through `extract_window_frame_target_type`, which had no arm for the binary types, so any window function whose leading `ORDER BY` key was `Binary`/`LargeBinary`/`BinaryView`/`FixedSizeBinary` failed with: Internal error: Cannot run range queries on datatype: Binary Binary is orderable, so peer/range comparison is well defined for it in exactly the same way it already is for `Utf8`. Accept the binary types alongside the string types. A finite RANGE offset such as `1 PRECEDING` asks for more than ordering: the bound is computed as `current_value - 1`, so the order key type must also support arithmetic. Binary does not, and neither do the `Utf8`, `Boolean`, `List` and `Null` order keys that `extract_window_frame_target_type` already accepted. For those, `ScalarValue::sub_checked` failed during execution and the error was swallowed by the overflow handling from apache#22140, which collapses a failed bound to the partition edge. So SELECT COUNT(*) OVER (ORDER BY x RANGE BETWEEN 1 PRECEDING AND CURRENT ROW) FROM (VALUES ('a'), ('b'), ('c')) t(x) silently returned the running count 1, 2, 3 -- the whole partition -- rather than an error. Reject the offset form during planning for every target type that has no arithmetic. For the string, boolean, list and null order keys this turns a wrong result into a plan error, which matches PostgreSQL's "RANGE with offset PRECEDING/FOLLOWING is not supported for column type ...". `WindowFrame::free_range()` becomes public so that check can be expressed in the vocabulary the crate already uses for "bounds that need no arithmetic". Document the restriction in the window functions user guide, and the changed SQL behavior in the 55.0.0 upgrade guide. Signed-off-by: Fredrik Fornwall <fredrik@fornwall.net>
fornwall
force-pushed
the
range-window-bytes
branch
from
August 14, 2026 12:47
e0663ae to
146f516
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
ORDER BYuses a binary column #24327.Rationale for this change
A window function over a binary
ORDER BYkey fails even with no frame clause:ORDER BYwithout a frame defaults toRANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, andextract_window_frame_target_typehad no arm for the binary types.LargeBinary,BinaryView,FixedSizeBinaryand dictionary-wrapped binary are affected too. Those bounds only need to compare order key values, which binary supports just asUtf8does.A finite offset like
1 PRECEDINGis different: the bound is computed ascurrent_value - 1, which needs arithmetic on the order key type. Binary has none, and neither doUtf8,Boolean,ListandNull-- yet offset frames overUtf8,BooleanandListkeys were accepted, and since 54.0.0 the failing arithmetic is swallowed by the overflow handling from #22140, silently collapsing the bound to the partition edge:returns the running count
1,2,3-- a silently widened frame -- instead of an error. This PR rejects such frames at planning time instead, matching PostgreSQL.What changes are included in this PR?
extract_window_frame_target_type. The existing dictionary arm already recurses into the value type.Interval. The message follows PostgreSQL'sRANGE with offset PRECEDING/FOLLOWING is not supported for column type ....WindowFrame::free_range()pub, so the check reuses the crate's existing notion of "bounds that are UNBOUNDED or CURRENT ROW".55.0.0upgrade guide.Are these changes tested?
Yes, in
window.slt: all four binary types plus dictionary-wrapped binary, ascending and descending, an aggregate andRANK, with duplicate values asserting peer semantics rather than positional counting. Planning errors for offsets overBinary,Utf8,Boolean,List(Int64)andNull, on the end bound as well as the start. Free range frames overUtf8andBooleanstill return results, andDecimal128keeps its offsets.The full sqllogictest suite passes.
Are there any user-facing changes?
Yes, one breaking SQL change:
RANGE BETWEEN <offset> PRECEDING/FOLLOWINGover aUtf8,Binary,Boolean,ListorNullORDER BYkey now fails at planning time withRANGE with offset PRECEDING/FOLLOWING is not supported for ORDER BY type .... OverUtf8,BooleanandListkeys this replaces the silently widened frames described above; overNulland binary keys such queries already failed at planning time, so only the error message changes. The55.0.0upgrade guide entry covers the per-type history and the migration paths: use aROWSframe to count rows, or stateUNBOUNDED PRECEDING/UNBOUNDED FOLLOWINGexplicitly to compare against the partition edge.In the other direction, binary order keys now work with free range frames, where they previously raised the internal error above.
WindowFrame::free_range()going from private topubis additive, so not a breaking Rust API change.AI usage: Created with Claude Code and Opus 5. I have reviewed the code and made modifications where it made sense. I have also tested this end to end in an internal project.