You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "Jefffrey (via GitHub)" <gi...@apache.org> on 2026/09/06 15:58:03 UTC
[PR] Fix `Numeric` signature coercion to properly handle null types [datafusion]
Jefffrey opened a new pull request, #24988:
URL: https://github.com/apache/datafusion/pull/24988
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
- N/A
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
Please explain the problem you are trying to solve in terms of the user-visible
behavior, rather than the implementation.
For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the
implementation. "COUNT(DISTINCT) returns wrong results when the column contains
nulls" is the user-visible problem.
-->
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?
<!--
There is no need to duplicate the description in the issue here, but it is sometimes worth providing a summary of the individual changes in this PR.
-->
Modify numeric type coercion handling to ignore null types
## What is the testing strategy for this PR?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
Briefly describe how this PR is tested, and point to the specific tests you added. For example: 'This new feature is covered by the `sqllogictest` cases added in `foo.slt`'.
If this PR does not add tests, explain why. For example, if the change is already covered by existing tests, please mention it.
You should also check the `codecov` bot reply on this PR to confirm the changed code is exercised.
-->
Added unit & SLT tests
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
If there are any breaking changes to public APIs, please add the `api change` label.
-->
No
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix `Numeric` signature coercion to properly handle null types [datafusion]
Posted by "AdamGS (via GitHub)" <gi...@apache.org>.
AdamGS merged PR #24988:
URL: https://github.com/apache/datafusion/pull/24988
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix `Numeric` signature coercion to properly handle null types [datafusion]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #24988:
URL: https://github.com/apache/datafusion/pull/24988#issuecomment-5560696310
## [Codecov](https://app.codecov.io/gh/apache/datafusion/pull/24988?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 81.67%. Comparing base ([`5b389eb`](https://app.codecov.io/gh/apache/datafusion/commit/5b389eb23cb4793d2550d87c249febc2e03e85f6?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`cd6278b`](https://app.codecov.io/gh/apache/datafusion/commit/cd6278bb211aefa21a8d43ca4d42605d59effe2c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
:warning: Report is 2 commits behind head on main.
<details><summary>Additional details and impacted files</summary>
```diff
@@ 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
```
</details>
[:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/datafusion/pull/24988?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix `Numeric` signature coercion to properly handle null types [datafusion]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #24988:
URL: https://github.com/apache/datafusion/pull/24988#discussion_r3944496441
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -840,12 +848,7 @@ fn get_valid_types(
}
let logical_data_type: NativeType = valid_type.clone().into();
- // 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 {
Review Comment:
just moved this up; this would only happen if all input types were null type
##########
datafusion/sqllogictest/test_files/spark/math/mod.slt:
##########
@@ -102,6 +102,21 @@ SELECT MOD(NULL::int, NULL::int) as mod_null_3;
----
NULL
+query I
+SELECT MOD(NULL, 3);
+----
+NULL
Review Comment:
currently failing on main:
```sql
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);
```
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -816,13 +816,21 @@ fn get_valid_types(
TypeSignature::Numeric(number) => {
function_length_check(function_name, current_types.len(), *number)?;
+ let non_nulls = current_types
+ .iter()
+ .filter(|&t| NativeType::from(t) != NativeType::Null)
+ .collect::<Vec<_>>();
+ let mut valid_type = non_nulls
+ .first()
+ .copied()
+ .cloned()
+ // 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
+ .unwrap_or(DataType::Float64);
// Find common numeric type among given types except string
- let mut valid_type = current_types.first().unwrap().to_owned();
Review Comment:
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:
https://github.com/apache/datafusion/blob/5b389eb23cb4793d2550d87c249febc2e03e85f6/datafusion/expr-common/src/type_coercion/binary.rs#L1047-L1054
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)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org