Repository navigation
Fix Numeric signature coercion to properly handle null types - #24988
Conversation
| // 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(); |
There was a problem hiding this 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:
datafusion/datafusion/expr-common/src/type_coercion/binary.rs
Lines 1047 to 1054 in 5b389eb
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 { |
There was a problem hiding this comment.
just moved this up; this would only happen if all input types were null type
| query I | ||
| SELECT MOD(NULL, 3); | ||
| ---- | ||
| NULL |
There was a problem hiding this comment.
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 Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #24988 +/- ##
==========================================
- Coverage 81.72% 81.72% -0.01%
==========================================
Files 1127 1127
Lines 416519 416540 +21
Branches 416519 416540 +21
==========================================
+ Hits 340401 340418 +17
Misses 56115 56115
- Partials 20003 20007 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
AdamGS
left a comment
There was a problem hiding this comment.
Looks reasonable to me, also verified the spark results
…e#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 apache#123` indicates that this PR will close issue apache#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
Which issue does this PR close?
Rationale for this change
For UDFs using
Numericsignature with more than one argument (currently mod & pmod from datafusion-spark), they cannot handle inputsNull, Intwhereas they acceptInt, 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