Repository navigation
fix: ignore ordering in approx_median - #27159
Open
Phoenix500526 wants to merge 1 commit into
Open
Phoenix500526 wants to merge 1 commit into
Phoenix500526 wants to merge 1 commit into
Conversation
Phoenix500526
marked this pull request as ready for review
November 7, 2026 12:29
Phoenix500526
force-pushed
the
issue/27157
branch
from
November 7, 2026 15:23
64b7cb3 to
5eba493
Compare
getChan
approved these changes
Nov 7, 2026
Contributor
There was a problem hiding this comment.
Declaring APPROX_MEDIAN as Insensitive looks appropriate, since it does not depend on input ordering.
Although APPROX_MEDIAN itself does not need a second argument, it shares its accumulator with the percentile aggregate. Removing unnecessary ordering keys through the existing framework looks like an appropriate fix that keeps the scope of the change small. The existing MEDIAN implementation also declares itself Insensitive to handle this in the same way, so this keeps the implementations consistent.
alamb
reviewed
Nov 8, 2026
alamb
left a comment
Contributor
There was a problem hiding this comment.
thanks @Phoenix500526 and @getChan
Phoenix500526
marked this pull request as draft
November 9, 2026 07:17
Phoenix500526
force-pushed
the
issue/27157
branch
2 times, most recently
from
November 10, 2026 08:01
679d166 to
2020a74
Compare
Phoenix500526
marked this pull request as ready for review
November 12, 2026 02:38
Reject unsupported ORDER BY before creating the shared accumulator so sort keys cannot produce misleading percentile parameter errors. CLOSES apache#27157
Phoenix500526
force-pushed
the
issue/27157
branch
from
November 12, 2026 02:38
2020a74 to
34d94d8
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.
Important
Fork CI setup: For pull requests from a fork to
main, enable workflows in your fork's Actions tab before you push.See the fork CI setup guide.
Which issue does this PR close?
APPROX_MEDIANwithORDER BYincorrectly treats the sort key as a percentile argument #27157.Rationale for this change
APPROX_MEDIAN(x ORDER BY y)can fail with a misleading error:The query has no percentile argument, so this message gives users little guidance. We should explicitly report that
ORDER BYis not supported forAPPROX_MEDIAN.What changes are included in this PR?
Check for
ORDER BYwhen creating theAPPROX_MEDIANaccumulator and return a clear error before passing inputs to the shared percentile accumulator.Add regression tests covering integer and floating-point sort keys, NULL input, and grouped aggregation.
What is the testing strategy for this PR?
The regression tests in
datafusion/sqllogictest/test_files/aggregate.sltcheck the error message and verify thatAPPROX_MEDIANwithoutORDER BYstill works. The four rejection tests fail with the previous order-insensitive implementation and pass with this change.The aggregate SQLLogicTests, workspace extended tests,
cargo fmt --all, andcargo clippy --all-targets --all-features -- -D warningspass.Are there any user-facing changes?
Calls to
APPROX_MEDIANwith an aggregateORDER BYclause now return:Additional context
In #17805, @Jefffrey proposed following PostgreSQL and rejecting inline
ORDER BYfor ordered-set aggregates. In #20421, existing inline syntax was retained for backward compatibility. As @alamb noted in that discussion, preserving existing syntax does not require extending it to other functions.Currently,
APPROX_PERCENTILE_CONTtreats inlineORDER BYasWITHIN GROUP. It does not support computing a percentile ofxwhile using an independentyto control input order. SinceAPPROX_MEDIANis thep = 0.5case, adding that behavior only toAPPRO_MEDIANwould leave the two functions inconsistent.This PR therefore returns a clear unsupported-operation error. Supporting independent input ordering for both functions would require a separate change that also addresses compatibility with the existing APC syntax.