Skip to content

Commit 729934c

Browse files
authored
Add doc example for creating FixedSizeListArray (#1426)
1 parent d9099c4 commit 729934c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

arrow/src/array/array_list.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,37 @@ pub type LargeListArray = GenericListArray<i64>;
343343
/// A list array where each element is a fixed-size sequence of values with the same
344344
/// type whose maximum length is represented by a i32.
345345
///
346+
/// # Example
347+
///
348+
/// ```
349+
/// # use arrow::array::{Array, ArrayData, FixedSizeListArray, Int32Array};
350+
/// # use arrow::datatypes::{DataType, Field};
351+
/// # use arrow::buffer::Buffer;
352+
/// // Construct a value array
353+
/// let value_data = ArrayData::builder(DataType::Int32)
354+
/// .len(9)
355+
/// .add_buffer(Buffer::from_slice_ref(&[0, 1, 2, 3, 4, 5, 6, 7, 8]))
356+
/// .build()
357+
/// .unwrap();
358+
/// let list_data_type = DataType::FixedSizeList(
359+
/// Box::new(Field::new("item", DataType::Int32, false)),
360+
/// 3,
361+
/// );
362+
/// let list_data = ArrayData::builder(list_data_type.clone())
363+
/// .len(3)
364+
/// .add_child_data(value_data.clone())
365+
/// .build()
366+
/// .unwrap();
367+
/// let list_array = FixedSizeListArray::from(list_data);
368+
/// let list0 = list_array.value(0);
369+
/// let list1 = list_array.value(1);
370+
/// let list2 = list_array.value(2);
371+
///
372+
/// assert_eq!( &[0, 1, 2], list0.as_any().downcast_ref::<Int32Array>().unwrap().values());
373+
/// assert_eq!( &[3, 4, 5], list1.as_any().downcast_ref::<Int32Array>().unwrap().values());
374+
/// assert_eq!( &[6, 7, 8], list2.as_any().downcast_ref::<Int32Array>().unwrap().values());
375+
/// ```
376+
///
346377
/// For non generic lists, you may wish to consider using
347378
/// [crate::array::FixedSizeBinaryArray]
348379
pub struct FixedSizeListArray {

0 commit comments

Comments
 (0)