Returns an array of Int32/Int64 denoting the number of bits in each value in the array. this only accepts StringArray/Utf8, LargeString/LargeUtf8, StringViewArray/Utf8View, BinaryArray, LargeBinaryArray, BinaryViewArray, and FixedSizeBinaryArray, or DictionaryArray/REE with above Arrays as values bit_length of null is null. bit_length is in number of bits
(array: &dyn Array)
| 134 | /// * bit_length of null is null. |
| 135 | /// * bit_length is in number of bits |
| 136 | pub fn bit_length(array: &dyn Array) -> Result<ArrayRef, ArrowError> { |
| 137 | if let Some(d) = array.as_any_dictionary_opt() { |
| 138 | let lengths = bit_length(d.values().as_ref())?; |
| 139 | return Ok(d.with_values(lengths)); |
| 140 | } |
| 141 | if let Some(ree) = array.as_any_ree_opt() { |
| 142 | let lengths = bit_length(ree.values())?; |
| 143 | return Ok(ree.with_values(lengths)); |
| 144 | } |
| 145 | |
| 146 | match array.data_type() { |
| 147 | DataType::Utf8 => { |
| 148 | let list = array.as_string::<i32>(); |
| 149 | Ok(bit_length_impl::<Int32Type>(list.offsets(), list.nulls())) |
| 150 | } |
| 151 | DataType::LargeUtf8 => { |
| 152 | let list = array.as_string::<i64>(); |
| 153 | Ok(bit_length_impl::<Int64Type>(list.offsets(), list.nulls())) |
| 154 | } |
| 155 | DataType::Utf8View => { |
| 156 | let list = array.as_string_view(); |
| 157 | let values = list |
| 158 | .views() |
| 159 | .iter() |
| 160 | .map(|view| (*view as i32).wrapping_mul(8)) |
| 161 | .collect(); |
| 162 | Ok(Arc::new(Int32Array::try_new( |
| 163 | values, |
| 164 | array.nulls().cloned(), |
| 165 | )?)) |
| 166 | } |
| 167 | DataType::Binary => { |
| 168 | let list = array.as_binary::<i32>(); |
| 169 | Ok(bit_length_impl::<Int32Type>(list.offsets(), list.nulls())) |
| 170 | } |
| 171 | DataType::LargeBinary => { |
| 172 | let list = array.as_binary::<i64>(); |
| 173 | Ok(bit_length_impl::<Int64Type>(list.offsets(), list.nulls())) |
| 174 | } |
| 175 | DataType::FixedSizeBinary(len) => Ok(Arc::new(Int32Array::try_new( |
| 176 | vec![*len * 8; array.len()].into(), |
| 177 | array.nulls().cloned(), |
| 178 | )?)), |
| 179 | DataType::BinaryView => { |
| 180 | let list = array.as_binary_view(); |
| 181 | let values = list |
| 182 | .views() |
| 183 | .iter() |
| 184 | .map(|view| (*view as i32).wrapping_mul(8)) |
| 185 | .collect(); |
| 186 | Ok(Arc::new(Int32Array::try_new( |
| 187 | values, |
| 188 | array.nulls().cloned(), |
| 189 | )?)) |
| 190 | } |
| 191 | other => Err(ArrowError::ComputeError(format!( |
| 192 | "bit_length not supported for {other:?}" |
| 193 | ))), |