Concatenates two `StringViewArray`s element-wise. If either element is `Null`, the result element is also `Null`. # Errors - Returns an error if the input arrays have different lengths. - Returns an error if any concatenated value exceeds `u32::MAX` in length. - Returns an error if concatenated strings do not result in a proper UTF-8 string Cannot reuse code with `GenericByteViewBuilder` since `t
(
left: &StringViewArray,
right: &StringViewArray,
)
| 269 | // Cannot reuse code with `GenericByteViewBuilder` since `try_append_value` works with |
| 270 | // `AsRef<T::Native>`, and there is no conversion from `ByteViewType` to this or [u8] |
| 271 | pub fn concat_elements_string_view_array( |
| 272 | left: &StringViewArray, |
| 273 | right: &StringViewArray, |
| 274 | ) -> Result<StringViewArray, ArrowError> { |
| 275 | if left.len() != right.len() { |
| 276 | return Err(ArrowError::ComputeError(format!( |
| 277 | "Arrays must have the same length: {} != {}", |
| 278 | left.len(), |
| 279 | right.len() |
| 280 | ))); |
| 281 | } |
| 282 | |
| 283 | let mut result = StringViewBuilder::with_capacity(left.len()); |
| 284 | |
| 285 | // Avoid reallocations by writing to a reused buffer |
| 286 | let mut buffer: Vec<u8> = Vec::new(); |
| 287 | |
| 288 | let nulls = NullBuffer::union(left.nulls(), right.nulls()); |
| 289 | |
| 290 | for i in 0..left.len() { |
| 291 | if nulls.as_ref().is_some_and(|n| n.is_null(i)) { |
| 292 | result.append_null(); |
| 293 | } else { |
| 294 | buffer.clear(); |
| 295 | buffer.extend_from_slice(left.value(i).as_bytes()); |
| 296 | buffer.extend_from_slice(right.value(i).as_bytes()); |
| 297 | let s = std::str::from_utf8(&buffer).map_err(|_| { |
| 298 | ArrowError::ComputeError("Concatenated values are not valid UTF-8".into()) |
| 299 | })?; |
| 300 | result.try_append_value(s)?; |
| 301 | } |
| 302 | } |
| 303 | Ok(result.finish()) |
| 304 | } |
| 305 | |
| 306 | /// Returns the elementwise concatenation of [`Array`]s. |
| 307 | /// |