Skip to content

feat: decimal support for percentile_cont and median - #24419

Open
theirix wants to merge 4 commits into
apache:mainfrom
theirix:percentile-decimal
Open

feat: decimal support for percentile_cont and median#24419
theirix wants to merge 4 commits into
apache:mainfrom
theirix:percentile-decimal

Conversation

@theirix

@theirix theirix commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

UDF percentile_cont uses floating-point arithmetic to find a percentile, which doesn't work well for decimals. The median UDF already supports decimals. Let's implement decimals in percentile_cont and build median on top of it.

A follow-up to #21988, refines #23954.

What changes are included in this PR?

  • Exact decimal support for percentile_cont, without float coercion
  • Integer support for percentile_cont (backported from median UDF)
  • Linear interpolation for both float (old code), decimal and integer code paths (new code)
  • Implement median on top of percentile_cont UDF by prepending a 0.5f argument
  • Add SLTs to verify code
  • Make percentile_cont(NULL) return NULL (see changes below), under a new SLT test case

Are these changes tested?

  • Extend SLTs
  • Unit test to verify integer inputs (cannot be done in SLT)
  • Unit tests for percentile_cont

Are there any user-facing changes?

  • percentile_cont(NULL) now returns a NULL type instead of a Float64 type, aligning with other aggregate UDFs

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Aug 16, 2026
@codecov-commenter

codecov-commenter commented Aug 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.02439% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.25%. Comparing base (bbf739a) to head (df896d2).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...afusion/functions-aggregate/src/percentile_cont.rs 88.29% 9 Missing and 15 partials ⚠️
datafusion/functions-aggregate/src/median.rs 92.68% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24419      +/-   ##
==========================================
+ Coverage   81.23%   81.25%   +0.01%     
==========================================
  Files        1111     1112       +1     
  Lines      390208   390464     +256     
  Branches   390208   390464     +256     
==========================================
+ Hits       316990   317257     +267     
+ Misses      54591    54556      -35     
- Partials    18627    18651      +24     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@theirix
theirix marked this pull request as ready for review August 16, 2026 23:24
@theirix

theirix commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

CC @neilconway

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

Integer support for percentile_cont (backported from median UDF)

this doesnt sound right, since this is continuous and not discrete so it must always be a float (or decimal)

#[derive(PartialEq, Eq, Hash, Debug)]
pub struct Median {
signature: Signature,
percentile_cont: PercentileCont,

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.

i do wonder if we should implement simplify for median to make it an unconditional rewrite to percentile_cont

Comment on lines +144 to +148
Coercion::new_implicit(
TypeSignatureClass::Native(logical_float64()),
vec![TypeSignatureClass::Numeric],
NativeType::Float64,
),

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.

Suggested change
Coercion::new_implicit(
TypeSignatureClass::Native(logical_float64()),
vec![TypeSignatureClass::Numeric],
NativeType::Float64,
),
Coercion::new_implicit_native(
logical_float64(),
vec![TypeSignatureClass::Numeric],
),

}

fn accumulator(&self, args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
let percentile = get_percentile(&args)?;

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.

i think its intentional to check the percentile up here, so that we still error on invalid percentiles even if input is null

}

downcast_integer! {
input_dt => (integer_helper, input_dt),

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.

are integer types supported in the signature? is this code being reached?

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.

Align median and percentile_cont implementations (preserve Decimal in percentile_cont, alias median)

3 participants