Skip to content

Commit 6eea180

Browse files
alambcomphead
andauthored
Improve documentation on StringArrayType trait (#12027)
* Improve documentation on `StringArrayType` trait * tweaks * Update datafusion/functions/src/string/common.rs Co-authored-by: Oleks V <[email protected]> --------- Co-authored-by: Oleks V <[email protected]>
1 parent 902f1c6 commit 6eea180

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

datafusion/functions/src/string/common.rs

+64
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
//! Common utilities for implementing string functions
19+
1820
use std::fmt::{Display, Formatter};
1921
use std::sync::Arc;
2022

@@ -252,7 +254,69 @@ impl<'a> ColumnarValueRef<'a> {
252254
}
253255
}
254256

257+
/// Abstracts iteration over different types of string arrays.
258+
///
259+
/// The [`StringArrayType`] trait helps write generic code for string functions that can work with
260+
/// different types of string arrays.
261+
///
262+
/// Currently three types are supported:
263+
/// - [`StringArray`]
264+
/// - [`LargeStringArray`]
265+
/// - [`StringViewArray`]
266+
///
267+
/// It is inspired / copied from [arrow-rs].
268+
///
269+
/// [arrow-rs]: https://github.com/apache/arrow-rs/blob/bf0ea9129e617e4a3cf915a900b747cc5485315f/arrow-string/src/like.rs#L151-L157
270+
///
271+
/// # Examples
272+
/// Generic function that works for [`StringArray`], [`LargeStringArray`]
273+
/// and [`StringViewArray`]:
274+
/// ```
275+
/// # use arrow::array::{StringArray, LargeStringArray, StringViewArray};
276+
/// # use datafusion_functions::string::common::StringArrayType;
277+
///
278+
/// /// Combines string values for any StringArrayType type. It can be invoked on
279+
/// /// and combination of `StringArray`, `LargeStringArray` or `StringViewArray`
280+
/// fn combine_values<'a, S1, S2>(array1: S1, array2: S2) -> Vec<String>
281+
/// where S1: StringArrayType<'a>, S2: StringArrayType<'a>
282+
/// {
283+
/// // iterate over the elements of the 2 arrays in parallel
284+
/// array1
285+
/// .iter()
286+
/// .zip(array2.iter())
287+
/// .map(|(s1, s2)| {
288+
/// // if both values are non null, combine them
289+
/// if let (Some(s1), Some(s2)) = (s1, s2) {
290+
/// format!("{s1}{s2}")
291+
/// } else {
292+
/// "None".to_string()
293+
/// }
294+
/// })
295+
/// .collect()
296+
/// }
297+
///
298+
/// let string_array = StringArray::from(vec!["foo", "bar"]);
299+
/// let large_string_array = LargeStringArray::from(vec!["foo2", "bar2"]);
300+
/// let string_view_array = StringViewArray::from(vec!["foo3", "bar3"]);
301+
///
302+
/// // can invoke this function a string array and large string array
303+
/// assert_eq!(
304+
/// combine_values(&string_array, &large_string_array),
305+
/// vec![String::from("foofoo2"), String::from("barbar2")]
306+
/// );
307+
///
308+
/// // Can call the same function with string array and string view array
309+
/// assert_eq!(
310+
/// combine_values(&string_array, &string_view_array),
311+
/// vec![String::from("foofoo3"), String::from("barbar3")]
312+
/// );
313+
/// ```
314+
///
315+
/// [`LargeStringArray`]: arrow::array::LargeStringArray
255316
pub trait StringArrayType<'a>: ArrayAccessor<Item = &'a str> + Sized {
317+
/// Return an [`ArrayIter`] over the values of the array.
318+
///
319+
/// This iterator iterates returns `Option<&str>` for each item in the array.
256320
fn iter(&self) -> ArrayIter<Self>;
257321
}
258322

0 commit comments

Comments
 (0)