-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Fix Numeric signature coercion to properly handle null types
#24988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jefffrey
wants to merge
6
commits into
apache:main
Choose a base branch
from
Jefffrey:numeric-null-coercion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+54
−14
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7e87d45
Fix `Numeric` signature coercion to properly handle null types
Jefffrey f3b9eb9
fix test
Jefffrey cd6278b
fix test
Jefffrey eec8f6c
Merge branch 'main' into numeric-null-coercion
Jefffrey 93354e5
fix test
Jefffrey 913c8c6
Merge branch 'main' into numeric-null-coercion
Jefffrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
| for t in current_types.iter().skip(1) { | ||
| for &t in non_nulls.iter().skip(1) { | ||
| let logical_data_type: NativeType = t.into(); | ||
| if logical_data_type == NativeType::Null { | ||
| continue; | ||
| } | ||
|
|
||
| if !logical_data_type.is_numeric() { | ||
| return plan_err!( | ||
|
|
@@ -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 { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| valid_type = DataType::Float64; | ||
| } else if !logical_data_type.is_numeric() { | ||
| if !logical_data_type.is_numeric() { | ||
| return plan_err!( | ||
| "Function '{function_name}' expects Numeric but received {logical_data_type}" | ||
| ); | ||
|
|
@@ -1451,6 +1454,13 @@ mod tests { | |
| ); | ||
| assert_eq!(got, [DataType::Float64]); | ||
|
|
||
| let got = get_valid_types_flatten( | ||
| "test", | ||
| &TypeSignature::Numeric(2), | ||
| &[DataType::Null, DataType::Null], | ||
| ); | ||
| assert_eq!(got, [DataType::Float64, DataType::Float64]); | ||
|
|
||
| // Rejects non-numeric arg. | ||
| let got = get_valid_types( | ||
| "test", | ||
|
|
@@ -1463,6 +1473,21 @@ mod tests { | |
| "Function 'test' expects Numeric but received Timestamp(s)" | ||
| ); | ||
|
|
||
| // Nulls should get ignored among other valid types | ||
| let got = get_valid_types_flatten( | ||
| "test", | ||
| &TypeSignature::Numeric(2), | ||
| &[DataType::Null, DataType::Int32], | ||
| ); | ||
| assert_eq!(got, [DataType::Int32, DataType::Int32]); | ||
|
|
||
| let got = get_valid_types_flatten( | ||
| "test", | ||
| &TypeSignature::Numeric(2), | ||
| &[DataType::Int32, DataType::Null], | ||
| ); | ||
| assert_eq!(got, [DataType::Int32, DataType::Int32]); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,6 +102,21 @@ SELECT MOD(NULL::int, NULL::int) as mod_null_3; | |
| ---- | ||
| NULL | ||
|
|
||
| query I | ||
| SELECT MOD(NULL, 3); | ||
| ---- | ||
| NULL | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); |
||
|
|
||
| query I | ||
| SELECT MOD(10, NULL); | ||
| ---- | ||
| NULL | ||
|
|
||
| query R | ||
| SELECT MOD(NULL, NULL); | ||
| ---- | ||
| NULL | ||
|
|
||
| # Special values: NaN and Infinity | ||
| query R | ||
| SELECT MOD(5.0::float8, 'NaN'::float8) as mod_nan_1; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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_typeis set to it, and below when we callbinary_numeric_coercionit'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)