Returns an [`ArrayRef`] with substrings of all the elements in `array`. # Arguments `start` - The start index of all substrings. If `start >= 0`, then count from the start of the string, otherwise count from the end of the string. `length`(option) - The length of all substrings. If `length` is [None], then the substring is from `start` to the end of the string. Attention: Both `start` and `len
(
array: &dyn Array,
start: i64,
length: Option<u64>,
)
| 69 | /// assert!(error.contains("invalid utf-8 boundary")); |
| 70 | /// ``` |
| 71 | pub fn substring( |
| 72 | array: &dyn Array, |
| 73 | start: i64, |
| 74 | length: Option<u64>, |
| 75 | ) -> Result<ArrayRef, ArrowError> { |
| 76 | macro_rules! substring_dict { |
| 77 | ($kt: ident, $($t: ident: $gt: ident), *) => { |
| 78 | match $kt.as_ref() { |
| 79 | $( |
| 80 | &DataType::$t => { |
| 81 | let dict = array |
| 82 | .as_any() |
| 83 | .downcast_ref::<DictionaryArray<$gt>>() |
| 84 | .unwrap_or_else(|| { |
| 85 | panic!("Expect 'DictionaryArray<{}>' but got array of data type {:?}", |
| 86 | stringify!($gt), array.data_type()) |
| 87 | }); |
| 88 | let values = substring(dict.values(), start, length)?; |
| 89 | Ok(Arc::new(dict.with_values(values))) |
| 90 | }, |
| 91 | )* |
| 92 | t => panic!("Unsupported dictionary key type: {}", t) |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | match array.data_type() { |
| 98 | DataType::Dictionary(kt, _) => { |
| 99 | substring_dict!( |
| 100 | kt, |
| 101 | Int8: Int8Type, |
| 102 | Int16: Int16Type, |
| 103 | Int32: Int32Type, |
| 104 | Int64: Int64Type, |
| 105 | UInt8: UInt8Type, |
| 106 | UInt16: UInt16Type, |
| 107 | UInt32: UInt32Type, |
| 108 | UInt64: UInt64Type |
| 109 | ) |
| 110 | } |
| 111 | DataType::LargeBinary => byte_substring( |
| 112 | array |
| 113 | .as_any() |
| 114 | .downcast_ref::<LargeBinaryArray>() |
| 115 | .expect("A large binary is expected"), |
| 116 | start, |
| 117 | length.map(|e| e as i64), |
| 118 | ), |
| 119 | DataType::Binary => byte_substring( |
| 120 | array |
| 121 | .as_any() |
| 122 | .downcast_ref::<BinaryArray>() |
| 123 | .expect("A binary is expected"), |
| 124 | start as i32, |
| 125 | length.map(|e| e as i32), |
| 126 | ), |
| 127 | DataType::FixedSizeBinary(old_len) => { |
| 128 | let old_len: usize = (*old_len) |