Skip to content

Commit

Permalink
Doctest for GenericListArray.
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberkilo committed Jun 20, 2021
1 parent 6e2f684 commit 4c5b2b4
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions arrow/src/array/array_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ impl OffsetSizeTrait for i64 {
}
}

/// Generic struct for a primitive Array
///
/// # Example
/// Creates a [`GenericListArray`] from an iterator of primitive values
/// ```
/// # use arrow::array::{Array, GenericListArray, Int32Array};
/// # use arrow::datatypes::{DataType, Int32Type};
/// let data = vec![
/// Some(vec![Some(0), Some(1), Some(2)]),
/// None,
/// Some(vec![Some(3), None, Some(5), Some(19)]),
/// Some(vec![Some(6), Some(7)]),
/// ];
/// let list_array = GenericListArray::<i32>::from_iter_primitive::<Int32Type, _, _>(data);
/// assert_eq!(DataType::Int32, list_array.value_type());
/// assert_eq!(4, list_array.len());
/// assert_eq!(1, list_array.null_count());
/// assert_eq!(3, list_array.value_length(0));
/// assert_eq!(0, list_array.value_length(1));
/// assert_eq!(4, list_array.value_length(2));
/// assert_eq!(2, list_array.value_length(3));
/// assert_eq!(
/// 0,
/// list_array
/// .value(0)
/// .as_any()
/// .downcast_ref::<Int32Array>()
/// .unwrap()
/// .value(0)
/// )
/// ```
pub struct GenericListArray<OffsetSize> {
data: ArrayData,
values: ArrayRef,
Expand Down Expand Up @@ -1053,4 +1084,32 @@ mod tests {
.build();
ListArray::from(list_data);
}

#[test]
fn test_generic_list_array() {
let data = vec![
Some(vec![Some(10), Some(1), Some(2)]),
None,
Some(vec![Some(3), None, Some(5), Some(19)]),
Some(vec![Some(6), Some(7)]),
];
let list_array = GenericListArray::<i32>::from_iter_primitive::<Int32Type, _, _>(data);
assert_eq!(DataType::Int32, list_array.value_type());
assert_eq!(4, list_array.len());
assert_eq!(1, list_array.null_count());

assert_eq!(3, list_array.value_length(0));
assert_eq!(0, list_array.value_length(1));
assert_eq!(4, list_array.value_length(2));
assert_eq!(2, list_array.value_length(3));
assert_eq!(
10,
list_array
.value(0)
.as_any()
.downcast_ref::<Int32Array>()
.unwrap()
.value(0)
);
}
}

0 comments on commit 4c5b2b4

Please sign in to comment.