-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Refactor substr signature #18653
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
Merged
Merged
Refactor substr signature #18653
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -27,9 +27,13 @@ use arrow::array::{ | |
| use arrow::buffer::ScalarBuffer; | ||
| use arrow::datatypes::DataType; | ||
| use datafusion_common::cast::as_int64_array; | ||
| use datafusion_common::{exec_err, plan_err, Result}; | ||
| use datafusion_common::types::{ | ||
| logical_int32, logical_int64, logical_string, NativeType, | ||
| }; | ||
| use datafusion_common::{exec_err, Result}; | ||
| use datafusion_expr::{ | ||
| ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility, | ||
| Coercion, ColumnarValue, Documentation, ScalarUDFImpl, Signature, TypeSignature, | ||
| TypeSignatureClass, Volatility, | ||
| }; | ||
| use datafusion_macros::user_doc; | ||
|
|
||
|
|
@@ -44,7 +48,7 @@ use datafusion_macros::user_doc; | |
| | substr(Utf8("datafusion"),Int64(5),Int64(3)) | | ||
| +----------------------------------------------+ | ||
| | fus | | ||
| +----------------------------------------------+ | ||
| +----------------------------------------------+ | ||
| ```"#, | ||
| standard_argument(name = "str", prefix = "String"), | ||
| argument( | ||
|
|
@@ -70,14 +74,30 @@ impl Default for SubstrFunc { | |
|
|
||
| impl SubstrFunc { | ||
| pub fn new() -> Self { | ||
| let string = Coercion::new_exact(TypeSignatureClass::Native(logical_string())); | ||
| let int64 = Coercion::new_implicit( | ||
| TypeSignatureClass::Native(logical_int64()), | ||
| vec![TypeSignatureClass::Native(logical_int32())], | ||
| NativeType::Int64, | ||
| ); | ||
| Self { | ||
| signature: Signature::user_defined(Volatility::Immutable) | ||
| .with_parameter_names(vec![ | ||
| "str".to_string(), | ||
| "start_pos".to_string(), | ||
| "length".to_string(), | ||
| ]) | ||
| .expect("valid parameter names"), | ||
| signature: Signature::one_of( | ||
| vec![ | ||
| TypeSignature::Coercible(vec![string.clone(), int64.clone()]), | ||
| TypeSignature::Coercible(vec![ | ||
| string.clone(), | ||
| int64.clone(), | ||
| int64.clone(), | ||
| ]), | ||
| ], | ||
| Volatility::Immutable, | ||
| ) | ||
| .with_parameter_names(vec![ | ||
|
Contributor
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. would be even nice to refactor such blocks in future so param names and types put together like
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. Raised for tracking: #18685 |
||
| "str".to_string(), | ||
| "start_pos".to_string(), | ||
| "length".to_string(), | ||
| ]) | ||
| .expect("valid parameter names"), | ||
| aliases: vec![String::from("substring")], | ||
| } | ||
| } | ||
|
|
@@ -112,72 +132,6 @@ impl ScalarUDFImpl for SubstrFunc { | |
| &self.aliases | ||
| } | ||
|
|
||
| fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> { | ||
| if arg_types.len() < 2 || arg_types.len() > 3 { | ||
| return plan_err!( | ||
| "The {} function requires 2 or 3 arguments, but got {}.", | ||
| self.name(), | ||
| arg_types.len() | ||
| ); | ||
| } | ||
| let first_data_type = match &arg_types[0] { | ||
| DataType::Null => Ok(DataType::Utf8), | ||
| DataType::LargeUtf8 | DataType::Utf8View | DataType::Utf8 => Ok(arg_types[0].clone()), | ||
| DataType::Dictionary(key_type, value_type) => { | ||
| if key_type.is_integer() { | ||
| match value_type.as_ref() { | ||
| DataType::Null => Ok(DataType::Utf8), | ||
| DataType::LargeUtf8 | DataType::Utf8View | DataType::Utf8 => Ok(*value_type.clone()), | ||
| _ => plan_err!( | ||
| "The first argument of the {} function can only be a string, but got {:?}.", | ||
| self.name(), | ||
| arg_types[0] | ||
| ), | ||
| } | ||
| } else { | ||
| plan_err!( | ||
| "The first argument of the {} function can only be a string, but got {:?}.", | ||
| self.name(), | ||
| arg_types[0] | ||
| ) | ||
| } | ||
| } | ||
| _ => plan_err!( | ||
| "The first argument of the {} function can only be a string, but got {:?}.", | ||
| self.name(), | ||
| arg_types[0] | ||
| ) | ||
| }?; | ||
|
|
||
| if ![DataType::Int64, DataType::Int32, DataType::Null].contains(&arg_types[1]) { | ||
| return plan_err!( | ||
| "The second argument of the {} function can only be an integer, but got {:?}.", | ||
| self.name(), | ||
| arg_types[1] | ||
| ); | ||
| } | ||
|
|
||
| if arg_types.len() == 3 | ||
| && ![DataType::Int64, DataType::Int32, DataType::Null].contains(&arg_types[2]) | ||
| { | ||
| return plan_err!( | ||
| "The third argument of the {} function can only be an integer, but got {:?}.", | ||
| self.name(), | ||
| arg_types[2] | ||
| ); | ||
| } | ||
|
|
||
| if arg_types.len() == 2 { | ||
| Ok(vec![first_data_type.to_owned(), DataType::Int64]) | ||
| } else { | ||
| Ok(vec![ | ||
| first_data_type.to_owned(), | ||
| DataType::Int64, | ||
| DataType::Int64, | ||
| ]) | ||
| } | ||
| } | ||
|
|
||
| fn documentation(&self) -> Option<&Documentation> { | ||
| self.doc() | ||
| } | ||
|
|
||
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
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
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.
👍