Skip to content

fix: ignore ordering in approx_median - #27159

Open
Phoenix500526 wants to merge 1 commit into
apache:mainfrom
Phoenix500526:issue/27157
Open

Phoenix500526 wants to merge 1 commit into
apache:mainfrom
Phoenix500526:issue/27157

Conversation

@Phoenix500526

@Phoenix500526 Phoenix500526 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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?

Rationale for this change

APPROX_MEDIAN(x ORDER BY y) can fail with a misleading error:

Execution error: Percentile value for APPROX_MEDIAN must be Float32 or Float64 (got Int64)

The query has no percentile argument, so this message gives users little guidance. We should explicitly report that ORDER BY is not supported for APPROX_MEDIAN.

What changes are included in this PR?

Check for ORDER BY when creating the APPROX_MEDIAN accumulator 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.slt check the error message and verify that APPROX_MEDIAN without ORDER BY still 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, and cargo clippy --all-targets --all-features -- -D warnings pass.

Are there any user-facing changes?

Calls to APPROX_MEDIAN with an aggregate ORDER BY clause now return:

This feature is not implemented: ORDER BY is not supported for APPROX_MEDIAN

Additional context

In #17805, @Jefffrey proposed following PostgreSQL and rejecting inline ORDER BY for 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_CONT treats inline ORDER BY as WITHIN GROUP. It does not support computing a percentile of x while using an independent y to control input order. Since APPROX_MEDIAN is the p = 0.5 case, adding that behavior only to APPRO_MEDIAN would 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.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Nov 6, 2026
@Phoenix500526
Phoenix500526 marked this pull request as ready for review November 7, 2026 12:29

@getChan getChan 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.

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 alamb 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.

@Phoenix500526
Phoenix500526 marked this pull request as draft November 9, 2026 07:17
@Phoenix500526
Phoenix500526 force-pushed the issue/27157 branch 2 times, most recently from 679d166 to 2020a74 Compare November 10, 2026 08:01
@Phoenix500526
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

APPROX_MEDIAN with ORDER BY incorrectly treats the sort key as a percentile argument

3 participants