MCPcopy Create free account
hub / github.com/apache/arrow-rs / concat_elements_string_view_array

Function concat_elements_string_view_array

arrow-string/src/concat_elements.rs:271–304  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

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]
271pub 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///

Callers 2

concat_elements_dynFunction · 0.85
test_string_view_concatFunction · 0.85

Calls 11

extend_from_sliceMethod · 0.80
lenMethod · 0.45
nullsMethod · 0.45
as_refMethod · 0.45
is_nullMethod · 0.45
append_nullMethod · 0.45
clearMethod · 0.45
as_bytesMethod · 0.45
valueMethod · 0.45
try_append_valueMethod · 0.45
finishMethod · 0.45

Tested by 1

test_string_view_concatFunction · 0.68