MCPcopy Create free account
hub / github.com/apache/datafusion / general_replace

Function general_replace

datafusion/functions-nested/src/replace.rs:305–413  ·  view source on GitHub ↗

For each element of `list_array[i]`, replaces up to `arr_n[i]` occurrences of `from_array[i]`, `to_array[i]`. The type of each **element** in `list_array` must be the same as the type of `from_array` and `to_array`. This function also handles nested arrays (\[`ListArray`\] of \[`ListArray`\]s) For example, when called to replace a list array (where each element is a list of int32s, the second a

(
    list_array: &GenericListArray<O>,
    from_array: &ArrayRef,
    to_array: &ArrayRef,
    arr_n: &[i64],
)

Source from the content-addressed store, hash-verified

303/// )
304/// ```
305fn general_replace<O: OffsetSizeTrait>(
306 list_array: &GenericListArray<O>,
307 from_array: &ArrayRef,
308 to_array: &ArrayRef,
309 arr_n: &[i64],
310) -> Result<ArrayRef> {
311 // Build up the offsets for the final output array
312 let mut offsets: Vec<O> = vec![O::usize_as(0)];
313 let values = list_array.values();
314 let original_data = values.to_data();
315 let to_data = to_array.to_data();
316 let capacity = Capacities::Array(original_data.len());
317
318 // First array is the original array, second array is the element to replace with.
319 let mut mutable = MutableArrayData::with_capacities(
320 vec![&original_data, &to_data],
321 false,
322 capacity,
323 );
324
325 let mut valid = NullBufferBuilder::new(list_array.len());
326
327 for (row_index, offset_window) in list_array.offsets().windows(2).enumerate() {
328 if list_array.is_null(row_index) {
329 offsets.push(offsets[row_index]);
330 valid.append_null();
331 continue;
332 }
333
334 let start = offset_window[0];
335 let end = offset_window[1];
336
337 let list_array_row = list_array.value(row_index);
338
339 // Compute all positions in list_row_array (that is itself an
340 // array) that are equal to `from_array_row`
341 let eq_array =
342 compare_element_to_list(&list_array_row, &from_array, row_index, true)?;
343
344 let original_idx = O::usize_as(0);
345 let replace_idx = O::usize_as(1);
346 let n = arr_n[row_index];
347 let mut counter = 0;
348
349 // All elements are false, no need to replace, just copy original data
350 if n <= 0 || !eq_array.has_true() {
351 mutable.extend(
352 original_idx.to_usize().unwrap(),
353 start.to_usize().unwrap(),
354 end.to_usize().unwrap(),
355 );
356 offsets.push(offsets[row_index] + (end - start));
357 valid.append_non_null();
358 continue;
359 }
360
361 let mut pending_retain: Option<O> = None;
362 for (i, to_replace) in eq_array.iter().enumerate() {

Callers

nothing calls this directly

Calls 15

newFunction · 0.85
compare_element_to_listFunction · 0.85
make_arrayFunction · 0.85
offsetsMethod · 0.80
is_noneMethod · 0.80
value_typeMethod · 0.80
valuesMethod · 0.45
lenMethod · 0.45
is_nullMethod · 0.45
pushMethod · 0.45
append_nullMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…