Repository navigation
fix: propagate field metadata through coalesce, nvl, and nvl2 #24865
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
james-willis
wants to merge
9
commits into
apache:main
Choose a base branch
from
james-willis:coalesce-metadata
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1ea88c0
fix: propagate field metadata through coalesce, nvl, and nvl2
james-willis b48d6ba
Merge branch 'main' into coalesce-metadata
james-willis 33fe79d
Address review: propagate metadata leniently, move helper to datafusi…
james-willis dde66de
Merge remote-tracking branch 'origin/main' into coalesce-metadata
james-willis c2449d1
Merge remote-tracking branch 'fork/coalesce-metadata' into coalesce-m…
james-willis e76a9b8
Address review: merge metadata, error on conflicting extension names
james-willis d3ece60
Merge remote-tracking branch 'origin/main' into coalesce-metadata
james-willis f70a43c
Address review: inline test field construction in coalesce and nvl2
james-willis eb5f143
Merge branch 'main' into coalesce-metadata
james-willis 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
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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| // under the License. | ||
|
|
||
| use arrow::datatypes::{DataType, Field, FieldRef}; | ||
| use datafusion_common::metadata::coerced_fields_metadata; | ||
| use datafusion_common::{Result, exec_err, internal_err, plan_err}; | ||
| use datafusion_expr::binary::try_type_union_resolution; | ||
| use datafusion_expr::conditional_expressions::CaseBuilder; | ||
|
|
@@ -86,7 +87,14 @@ impl ScalarUDFImpl for CoalesceFunc { | |
| .find_or_first(|d| !d.is_null()) | ||
| .unwrap() | ||
| .clone(); | ||
| Ok(Field::new(self.name(), return_type, nullable).into()) | ||
| // Propagate field metadata (e.g. Arrow extension types) from the | ||
| // arguments that can supply a value; see `coerced_fields_metadata` | ||
| // for the merge semantics. | ||
| let metadata = | ||
| coerced_fields_metadata(args.arg_fields.iter().map(|f| f.as_ref()))?; | ||
| Ok(metadata | ||
| .add_to_field(Field::new(self.name(), return_type, nullable)) | ||
| .into()) | ||
|
Comment on lines
+90
to
+97
Member
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. Happy to hear another opinion, but I do think this is a bug worth fixing regardless of whether #26145 is a good idea. |
||
| } | ||
|
|
||
| fn simplify( | ||
|
|
@@ -146,3 +154,142 @@ impl ScalarUDFImpl for CoalesceFunc { | |
| self.doc() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
|
|
||
| const EXTENSION_KEY: &str = "ARROW:extension:name"; | ||
|
|
||
| #[test] | ||
| fn propagates_metadata_when_arguments_agree() { | ||
| let fields = [ | ||
| Arc::new(Field::new("a", DataType::Binary, true).with_metadata( | ||
| HashMap::from([(EXTENSION_KEY.to_string(), "geoarrow.wkb".to_string())]), | ||
| )), | ||
| Arc::new(Field::new("b", DataType::Binary, true).with_metadata( | ||
| HashMap::from([(EXTENSION_KEY.to_string(), "geoarrow.wkb".to_string())]), | ||
| )), | ||
| ]; | ||
| let ret = CoalesceFunc::new() | ||
| .return_field_from_args(ReturnFieldArgs { | ||
| arg_fields: &fields, | ||
| scalar_arguments: &[None, None], | ||
| }) | ||
| .unwrap(); | ||
| assert_eq!(ret.data_type(), &DataType::Binary); | ||
| assert!(ret.is_nullable()); | ||
| assert_eq!( | ||
| ret.metadata().get(EXTENSION_KEY).map(String::as_str), | ||
| Some("geoarrow.wkb") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn null_literal_argument_does_not_block_metadata() { | ||
| // An untyped NULL argument carries no metadata but cannot contribute | ||
| // a typed value either, so it is skipped when picking the metadata. | ||
| let fields = [ | ||
| Arc::new(Field::new("null", DataType::Null, true)), | ||
| Arc::new(Field::new("b", DataType::Binary, true).with_metadata( | ||
| HashMap::from([(EXTENSION_KEY.to_string(), "geoarrow.wkb".to_string())]), | ||
| )), | ||
| ]; | ||
| let ret = CoalesceFunc::new() | ||
| .return_field_from_args(ReturnFieldArgs { | ||
| arg_fields: &fields, | ||
| scalar_arguments: &[None, None], | ||
| }) | ||
| .unwrap(); | ||
| assert_eq!(ret.data_type(), &DataType::Binary); | ||
| assert_eq!( | ||
| ret.metadata().get(EXTENSION_KEY).map(String::as_str), | ||
| Some("geoarrow.wkb") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn errors_on_conflicting_extension_types() { | ||
| let fields = [ | ||
| Arc::new(Field::new("a", DataType::Binary, true).with_metadata( | ||
| HashMap::from([(EXTENSION_KEY.to_string(), "geoarrow.wkb".to_string())]), | ||
| )), | ||
| Arc::new(Field::new("b", DataType::Binary, true).with_metadata( | ||
| HashMap::from([( | ||
| EXTENSION_KEY.to_string(), | ||
| "unrelated.type".to_string(), | ||
| )]), | ||
| )), | ||
| ]; | ||
| let err = CoalesceFunc::new() | ||
| .return_field_from_args(ReturnFieldArgs { | ||
| arg_fields: &fields, | ||
| scalar_arguments: &[None, None], | ||
| }) | ||
| .unwrap_err(); | ||
| assert!(err.to_string().contains("geoarrow.wkb"), "{err}"); | ||
| assert!(err.to_string().contains("unrelated.type"), "{err}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn plain_argument_does_not_block_later_metadata() { | ||
| let fields = [ | ||
| Arc::new(Field::new("plain", DataType::Binary, true)), | ||
| Arc::new(Field::new("b", DataType::Binary, true).with_metadata( | ||
| HashMap::from([(EXTENSION_KEY.to_string(), "geoarrow.wkb".to_string())]), | ||
| )), | ||
| ]; | ||
| let ret = CoalesceFunc::new() | ||
| .return_field_from_args(ReturnFieldArgs { | ||
| arg_fields: &fields, | ||
| scalar_arguments: &[None, None], | ||
| }) | ||
| .unwrap(); | ||
| assert_eq!(ret.data_type(), &DataType::Binary); | ||
| assert_eq!( | ||
| ret.metadata().get(EXTENSION_KEY).map(String::as_str), | ||
| Some("geoarrow.wkb") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn no_metadata_when_all_arguments_are_null() { | ||
| let fields = [ | ||
| Arc::new(Field::new("a", DataType::Null, true)), | ||
| Arc::new(Field::new("b", DataType::Null, true)), | ||
| ]; | ||
| let ret = CoalesceFunc::new() | ||
| .return_field_from_args(ReturnFieldArgs { | ||
| arg_fields: &fields, | ||
| scalar_arguments: &[None, None], | ||
| }) | ||
| .unwrap(); | ||
| assert_eq!(ret.data_type(), &DataType::Null); | ||
| assert!(ret.metadata().is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nullability_is_unchanged_by_metadata_propagation() { | ||
| let fields = [ | ||
| Arc::new(Field::new("a", DataType::Binary, true).with_metadata( | ||
| HashMap::from([(EXTENSION_KEY.to_string(), "geoarrow.wkb".to_string())]), | ||
| )), | ||
| Arc::new(Field::new("b", DataType::Binary, false).with_metadata( | ||
| HashMap::from([(EXTENSION_KEY.to_string(), "geoarrow.wkb".to_string())]), | ||
| )), | ||
| ]; | ||
| let ret = CoalesceFunc::new() | ||
| .return_field_from_args(ReturnFieldArgs { | ||
| arg_fields: &fields, | ||
| scalar_arguments: &[None, None], | ||
| }) | ||
| .unwrap(); | ||
| assert!(!ret.is_nullable()); | ||
| assert_eq!( | ||
| ret.metadata().get(EXTENSION_KEY).map(String::as_str), | ||
| Some("geoarrow.wkb") | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
I recently discovered
intersect_metadata_for_union(), which silently discards non-matching metadata keys. I like this version better (I want a louder error when the types don't match), but either is OK with me if we want to keep the existing logic for consistency.