Dandandan commented on a change in pull request #9376:
URL: https://github.com/apache/arrow/pull/9376#discussion_r571419483
##########
File path: rust/datafusion/src/physical_plan/string_expressions.rs
##########
@@ -17,27 +17,108 @@
//! String expressions
-use crate::error::{DataFusionError, Result};
-use arrow::array::{
- Array, ArrayRef, GenericStringArray, StringArray, StringBuilder,
- StringOffsetSizeTrait,
+use std::sync::Arc;
+
+use crate::{
+ error::{DataFusionError, Result},
+ scalar::ScalarValue,
+};
+use arrow::{
+ array::{Array, GenericStringArray, StringArray, StringOffsetSizeTrait},
+ datatypes::DataType,
};
-macro_rules! downcast_vec {
- ($ARGS:expr, $ARRAY_TYPE:ident) => {{
- $ARGS
- .iter()
- .map(|e| match e.as_any().downcast_ref::<$ARRAY_TYPE>() {
- Some(array) => Ok(array),
- _ => Err(DataFusionError::Internal("failed to
downcast".to_string())),
- })
- }};
+use super::ColumnarValue;
+
+/// applies a unary expression to `args[0]` that is expected to be
downcastable to
+/// a `GenericStringArray` and returns a `GenericStringArray` (which may have
a different offset)
+/// # Errors
+/// This function errors when:
+/// * the number of arguments is not 1
+/// * the first argument is not castable to a `GenericStringArray`
+pub(crate) fn unary_string_function<'a, T, O, F, R>(
+ args: &[&'a dyn Array],
+ op: F,
+ name: &str,
+) -> Result<GenericStringArray<O>>
+where
+ R: AsRef<str>,
+ O: StringOffsetSizeTrait,
+ T: StringOffsetSizeTrait,
+ F: Fn(&'a str) -> R,
+{
+ if args.len() != 1 {
+ return Err(DataFusionError::Internal(format!(
+ "{:?} args were supplied but {} takes exactly one argument",
+ args.len(),
+ name,
+ )));
+ }
+
+ let array = args[0]
+ .as_any()
+ .downcast_ref::<GenericStringArray<T>>()
+ .ok_or_else(|| {
+ DataFusionError::Internal("failed to downcast to
string".to_string())
+ })?;
+
+ // first map is the iterator, second is for the `Option<_>`
+ Ok(array.iter().map(|x| x.map(|x| op(x))).collect())
Review comment:
```suggestion
Ok(array.iter().map(|x| x.map(op).collect())
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]