Skip to content

Commit

Permalink
add doc test
Browse files Browse the repository at this point in the history
Signed-off-by: jayzhan211 <[email protected]>
  • Loading branch information
jayzhan211 committed Oct 30, 2023
1 parent a9a0c2f commit 5a95567
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions datafusion/common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

//! This module provides the bisect function, which implements binary search.
use crate::error::_internal_err;
use crate::{DataFusionError, Result, ScalarValue};
use arrow::array::{ArrayRef, PrimitiveArray};
use arrow::buffer::OffsetBuffer;
Expand Down Expand Up @@ -337,12 +338,28 @@ pub fn longest_consecutive_prefix<T: Borrow<usize>>(
}

/// Wrap arrays into a single element `ListArray`.
/// For example `[1, 2, 3]` would be converted into `[[1, 2, 3]]`
///
/// Example:
/// ```
/// use arrow::array::{Int32Array, ListArray};
/// use arrow::datatypes::{Int32Type, Field};
///
/// let arr1 = Int32Array::from(vec![1, 2, 3]);
/// let arr2 = Int32Array::from(vec![4, 5, 6]);
///
/// let list_arr = datafusion_common::utils::wrap_into_list_array(&[&arr1, &arr2]).unwrap();
///
/// let expected = ListArray::from_iter_primitive::<Int32Type, _, _>(
/// vec![
/// Some(vec![Some(1), Some(2), Some(3)]),
/// Some(vec![Some(4), Some(5), Some(6)]),
/// ]
/// );
///
/// assert_eq!(list_arr, expected);
pub fn wrap_into_list_array(arr: &[&dyn Array]) -> Result<ListArray> {
if arr.is_empty() {
return Err(DataFusionError::Internal(
"Cannot wrap empty array into list array".to_owned(),
));
return _internal_err!("Cannot wrap empty array into list array");
}

let lens = arr.iter().map(|x| x.len()).collect::<Vec<_>>();
Expand Down

0 comments on commit 5a95567

Please sign in to comment.