Skip to content

fix: median returns Float64 for integer inputs to avoid truncation#21988

Open
CuteChuanChuan wants to merge 1 commit intoapache:mainfrom
CuteChuanChuan:raymond/issue-19536-median-truncate-integer
Open

fix: median returns Float64 for integer inputs to avoid truncation#21988
CuteChuanChuan wants to merge 1 commit intoapache:mainfrom
CuteChuanChuan:raymond/issue-19536-median-truncate-integer

Conversation

@CuteChuanChuan
Copy link
Copy Markdown
Contributor

@CuteChuanChuan CuteChuanChuan commented May 2, 2026

Which issue does this PR close?

Rationale for this change

MEDIAN over integer columns previously returned the input integer type, which truncates the .5 produced when averaging the two middle values of an even-length input.

For example:

SELECT median(v) FROM (VALUES (arrow_cast(-128, 'Int8')),
                              (arrow_cast(127,  'Int8'))) AS t(v);
-- Old: 0    (truncated from -0.5)
-- New: -0.5

What changes are included in this PR?

  • Replace Median's Signature::numeric with Signature::one_of containing three coercible arms:
    • Decimal → preserved exactly
    • Float → preserved exactly
    • Integer → coerced implicitly to Float64
  • Update SLT and insta snapshot expectations affected by the new return type (column type indicators I → R, previously truncated values such as -17 → -17.5).

Are these changes tested?

Yes. Includes:

  • Existing SLT cases for median over Int8/16/32/64 and UInt8/16/32/64
  • Boundary cases that previously truncated.
  • insta snapshot tests.

Are there any user-facing changes?

Yes — this is a breaking change for median(<integer_column>): Return type changes from the input integer type to Float64.

@github-actions github-actions Bot added core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Median() truncates integers

1 participant