Skip to content

Fix Numeric signature coercion to properly handle null types - #24988

Open
Jefffrey wants to merge 3 commits into
apache:mainfrom
Jefffrey:numeric-null-coercion
Open

Fix Numeric signature coercion to properly handle null types#24988
Jefffrey wants to merge 3 commits into
apache:mainfrom
Jefffrey:numeric-null-coercion

Conversation

@Jefffrey

@Jefffrey Jefffrey commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • N/A

Rationale for this change

For UDFs using Numeric signature with more than one argument (currently mod & pmod from datafusion-spark), they cannot handle inputs Null, Int whereas they accept Int, Null. Fixing the coercion logic to ensure both are accepted.

What changes are included in this PR?

Modify numeric type coercion handling to ignore null types

What is the testing strategy for this PR?

Added unit & SLT tests

Are there any user-facing changes?

No

@github-actions github-actions Bot added logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt) labels Sep 6, 2026
// and their default type is double precision
.unwrap_or(DataType::Float64);
// Find common numeric type among given types except string
let mut valid_type = current_types.first().unwrap().to_owned();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if first argument was null type, valid_type is set to it, and below when we call binary_numeric_coercion it'll fail since it expects two numeric type arguments:

/// Coerce `lhs_type` and `rhs_type` to a common type where both are numeric
pub fn binary_numeric_coercion(
lhs_type: &DataType,
rhs_type: &DataType,
) -> Option<DataType> {
if !lhs_type.is_numeric() || !rhs_type.is_numeric() {
return None;
}

rather than fixing inside binary_numeric_coercion, decided it should be better to ensure we ignore null types initially, then run coercion on any non-null type, which is essentially what was happening in main (so long as the first type wasnt null)

// Fallback to default type if we don't know which type to coerced to
// f64 is chosen since most of the math functions utilize Signature::numeric,
// and their default type is double precision
if logical_data_type == NativeType::Null {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just moved this up; this would only happen if all input types were null type

query I
SELECT MOD(NULL, 3);
----
NULL

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently failing on main:

1. query failed: DataFusion error: Error during planning: For function 'mod' Null and Int64 are not coercible to a common numeric type. No function matches the given name and argument types 'mod(Null, Int64)'. You might need to add explicit type casts.
        Candidate functions:
        mod(Numeric(2))
[SQL] SELECT MOD(NULL, 3);

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.67%. Comparing base (5b389eb) to head (cd6278b).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main   #24988    +/-   ##
========================================
  Coverage   81.67%   81.67%            
========================================
  Files        1126     1126            
  Lines      414524   414782   +258     
  Branches   414524   414782   +258     
========================================
+ Hits       338558   338780   +222     
- Misses      56054    56070    +16     
- Partials    19912    19932    +20     

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants