Returns an array of Int32/Int64 denoting the length of each value in the array. For list array, length is the number of elements in each list. For string array and binary array, length is the number of bytes of each value. this only accepts ListArray/LargeListArray, StringArray/LargeStringArray/StringViewArray, BinaryArray/LargeBinaryArray, FixedSizeListArray, and ListViewArray/LargeListViewArra
(array: &dyn Array)
| 54 | /// RunEndEncoded arrays with above arrays as values |
| 55 | /// * length of null is null. |
| 56 | pub fn length(array: &dyn Array) -> Result<ArrayRef, ArrowError> { |
| 57 | if let Some(d) = array.as_any_dictionary_opt() { |
| 58 | let lengths = length(d.values().as_ref())?; |
| 59 | return Ok(d.with_values(lengths)); |
| 60 | } |
| 61 | if let Some(ree) = array.as_any_ree_opt() { |
| 62 | let lengths = length(ree.values())?; |
| 63 | return Ok(ree.with_values(lengths)); |
| 64 | } |
| 65 | match array.data_type() { |
| 66 | DataType::List(_) => { |
| 67 | let list = array.as_list::<i32>(); |
| 68 | Ok(length_impl::<Int32Type>(list.offsets(), list.nulls())) |
| 69 | } |
| 70 | DataType::LargeList(_) => { |
| 71 | let list = array.as_list::<i64>(); |
| 72 | Ok(length_impl::<Int64Type>(list.offsets(), list.nulls())) |
| 73 | } |
| 74 | DataType::ListView(_) => { |
| 75 | let list = array.as_list_view::<i32>(); |
| 76 | Ok(Arc::new(Int32Array::new( |
| 77 | list.sizes().clone(), |
| 78 | list.nulls().cloned(), |
| 79 | ))) |
| 80 | } |
| 81 | DataType::LargeListView(_) => { |
| 82 | let list = array.as_list_view::<i64>(); |
| 83 | Ok(Arc::new(Int64Array::new( |
| 84 | list.sizes().clone(), |
| 85 | list.nulls().cloned(), |
| 86 | ))) |
| 87 | } |
| 88 | DataType::Utf8 => { |
| 89 | let list = array.as_string::<i32>(); |
| 90 | Ok(length_impl::<Int32Type>(list.offsets(), list.nulls())) |
| 91 | } |
| 92 | DataType::LargeUtf8 => { |
| 93 | let list = array.as_string::<i64>(); |
| 94 | Ok(length_impl::<Int64Type>(list.offsets(), list.nulls())) |
| 95 | } |
| 96 | DataType::Utf8View => { |
| 97 | let list = array.as_string_view(); |
| 98 | let v = list.views().iter().map(|v| *v as i32).collect::<Vec<_>>(); |
| 99 | Ok(Arc::new(PrimitiveArray::<Int32Type>::try_new( |
| 100 | v.into(), |
| 101 | list.nulls().cloned(), |
| 102 | )?)) |
| 103 | } |
| 104 | DataType::Binary => { |
| 105 | let list = array.as_binary::<i32>(); |
| 106 | Ok(length_impl::<Int32Type>(list.offsets(), list.nulls())) |
| 107 | } |
| 108 | DataType::LargeBinary => { |
| 109 | let list = array.as_binary::<i64>(); |
| 110 | Ok(length_impl::<Int64Type>(list.offsets(), list.nulls())) |
| 111 | } |
| 112 | DataType::FixedSizeBinary(len) | DataType::FixedSizeList(_, len) => Ok(Arc::new( |
| 113 | Int32Array::try_new(vec![*len; array.len()].into(), array.nulls().cloned())?, |