| 132 | } |
| 133 | |
| 134 | fn character_length<'a, V>(array: &V) -> datafusion_common::Result<ArrayRef> |
| 135 | where |
| 136 | V: StringArrayType<'a>, |
| 137 | { |
| 138 | // String characters are variable length encoded in UTF-8, counting the |
| 139 | // number of chars requires expensive decoding, however checking if the |
| 140 | // string is ASCII only is relatively cheap. |
| 141 | // If strings are ASCII only, count bytes instead. |
| 142 | let is_array_ascii_only = array.is_ascii(); |
| 143 | let nulls = array.nulls().cloned(); |
| 144 | let array = { |
| 145 | if is_array_ascii_only { |
| 146 | let values: Vec<_> = (0..array.len()) |
| 147 | .map(|i| { |
| 148 | // Safety: we are iterating with array.len() so the index is always valid |
| 149 | let value = unsafe { array.value_unchecked(i) }; |
| 150 | value.len() as i32 |
| 151 | }) |
| 152 | .collect(); |
| 153 | PrimitiveArray::<Int32Type>::new(values.into(), nulls) |
| 154 | } else { |
| 155 | let values: Vec<_> = (0..array.len()) |
| 156 | .map(|i| { |
| 157 | // Safety: we are iterating with array.len() so the index is always valid |
| 158 | if array.is_null(i) { |
| 159 | i32::default() |
| 160 | } else { |
| 161 | let value = unsafe { array.value_unchecked(i) }; |
| 162 | if value.is_empty() { |
| 163 | i32::default() |
| 164 | } else if value.is_ascii() { |
| 165 | value.len() as i32 |
| 166 | } else { |
| 167 | value.chars().count() as i32 |
| 168 | } |
| 169 | } |
| 170 | }) |
| 171 | .collect(); |
| 172 | PrimitiveArray::<Int32Type>::new(values.into(), nulls) |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | Ok(Arc::new(array)) |
| 177 | } |
| 178 | |
| 179 | fn byte_length<'a, V>(array: &V) -> datafusion_common::Result<ArrayRef> |
| 180 | where |