-
Notifications
You must be signed in to change notification settings - Fork 1.8k
minor: refactor Spark ascii function to reuse DataFusion ascii function code #17965
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -15,21 +15,23 @@ | |
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use arrow::array::{ArrayAccessor, ArrayIter, ArrayRef, AsArray, Int32Array}; | ||
| use arrow::datatypes::DataType; | ||
| use arrow::error::ArrowError; | ||
| use datafusion_common::{internal_err, plan_err, Result}; | ||
| use datafusion_common::Result; | ||
| use datafusion_expr::ColumnarValue; | ||
| use datafusion_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility}; | ||
| use datafusion_functions::string::ascii::ascii; | ||
| use datafusion_functions::utils::make_scalar_function; | ||
| use std::any::Any; | ||
| use std::sync::Arc; | ||
|
|
||
| /// <https://spark.apache.org/docs/latest/api/sql/index.html#ascii> | ||
| /// Spark compatible version of the [ascii] function. Differs from the [default ascii function] | ||
| /// in that it is more permissive of input types, for example casting numeric input to string | ||
| /// before executing the function (default version doesn't allow numeric input). | ||
| /// | ||
| /// [ascii]: https://spark.apache.org/docs/latest/api/sql/index.html#ascii | ||
| /// [default ascii function]: datafusion_functions::string::ascii::AsciiFunc | ||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| pub struct SparkAscii { | ||
|
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. I considered removing this entirely and making AsciiFunc have a toggleable behaviour for this, but keeping it like this was easier to work with considering existing macros used to export the udf's |
||
| signature: Signature, | ||
| aliases: Vec<String>, | ||
| } | ||
|
|
||
| impl Default for SparkAscii { | ||
|
|
@@ -42,7 +44,6 @@ impl SparkAscii { | |
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::user_defined(Volatility::Immutable), | ||
| aliases: vec![], | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -68,107 +69,7 @@ impl ScalarUDFImpl for SparkAscii { | |
| make_scalar_function(ascii, vec![])(&args.args) | ||
| } | ||
|
|
||
| fn aliases(&self) -> &[String] { | ||
| &self.aliases | ||
| } | ||
|
|
||
| fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> { | ||
| if arg_types.len() != 1 { | ||
| return plan_err!( | ||
| "The {} function requires 1 argument, but got {}.", | ||
| self.name(), | ||
| arg_types.len() | ||
| ); | ||
| } | ||
| fn coerce_types(&self, _arg_types: &[DataType]) -> Result<Vec<DataType>> { | ||
| Ok(vec![DataType::Utf8]) | ||
| } | ||
| } | ||
|
|
||
| fn calculate_ascii<'a, V>(array: V) -> Result<ArrayRef, ArrowError> | ||
| where | ||
| V: ArrayAccessor<Item = &'a str>, | ||
| { | ||
| let iter = ArrayIter::new(array); | ||
| let result = iter | ||
| .map(|string| { | ||
| string.map(|s| { | ||
| let mut chars = s.chars(); | ||
| chars.next().map_or(0, |v| v as i32) | ||
| }) | ||
| }) | ||
| .collect::<Int32Array>(); | ||
|
|
||
| Ok(Arc::new(result) as ArrayRef) | ||
| } | ||
|
|
||
| /// Returns the numeric code of the first character of the argument. | ||
| pub fn ascii(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
| match args[0].data_type() { | ||
| DataType::Utf8 => { | ||
| let string_array = args[0].as_string::<i32>(); | ||
| Ok(calculate_ascii(string_array)?) | ||
| } | ||
| DataType::LargeUtf8 => { | ||
| let string_array = args[0].as_string::<i64>(); | ||
| Ok(calculate_ascii(string_array)?) | ||
| } | ||
| DataType::Utf8View => { | ||
| let string_array = args[0].as_string_view(); | ||
| Ok(calculate_ascii(string_array)?) | ||
| } | ||
| _ => internal_err!("Unsupported data type"), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::function::string::ascii::SparkAscii; | ||
| use crate::function::utils::test::test_scalar_function; | ||
| use arrow::array::{Array, Int32Array}; | ||
| use arrow::datatypes::DataType::Int32; | ||
| use datafusion_common::{Result, ScalarValue}; | ||
| use datafusion_expr::{ColumnarValue, ScalarUDFImpl}; | ||
|
|
||
| macro_rules! test_ascii_string_invoke { | ||
| ($INPUT:expr, $EXPECTED:expr) => { | ||
| test_scalar_function!( | ||
| SparkAscii::new(), | ||
| vec![ColumnarValue::Scalar(ScalarValue::Utf8($INPUT))], | ||
| $EXPECTED, | ||
| i32, | ||
| Int32, | ||
| Int32Array | ||
| ); | ||
|
|
||
| test_scalar_function!( | ||
| SparkAscii::new(), | ||
| vec![ColumnarValue::Scalar(ScalarValue::LargeUtf8($INPUT))], | ||
| $EXPECTED, | ||
| i32, | ||
| Int32, | ||
| Int32Array | ||
| ); | ||
|
|
||
| test_scalar_function!( | ||
| SparkAscii::new(), | ||
| vec![ColumnarValue::Scalar(ScalarValue::Utf8View($INPUT))], | ||
| $EXPECTED, | ||
| i32, | ||
| Int32, | ||
| Int32Array | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_ascii_invoke() -> Result<()> { | ||
| test_ascii_string_invoke!(Some(String::from("x")), Ok(Some(120))); | ||
| test_ascii_string_invoke!(Some(String::from("a")), Ok(Some(97))); | ||
| test_ascii_string_invoke!(Some(String::from("")), Ok(Some(0))); | ||
| test_ascii_string_invoke!(Some(String::from("\n")), Ok(Some(10))); | ||
| test_ascii_string_invoke!(Some(String::from("\t")), Ok(Some(9))); | ||
| test_ascii_string_invoke!(None, Ok(None)); | ||
|
|
||
| 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
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.
Consolidating the tests here, removing from Spark unit test (Spark slt still remains)