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

Function replace

datafusion/functions/src/string/replace.rs:205–244  ·  view source on GitHub ↗

Replaces all occurrences in string of substring from with substring to. replace('abcdefabcdef', 'cd', 'XX') = 'abXXefabXXef'

(args: &[ArrayRef])

Source from the content-addressed store, hash-verified

203/// Replaces all occurrences in string of substring from with substring to.
204/// replace('abcdefabcdef', 'cd', 'XX') = 'abXXefabXXef'
205fn replace<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
206 let string_array = as_generic_string_array::<T>(&args[0])?;
207 let from_array = as_generic_string_array::<T>(&args[1])?;
208 let to_array = as_generic_string_array::<T>(&args[2])?;
209
210 let len = string_array.len();
211 let mut builder = GenericStringArrayBuilder::<T>::with_capacity(len, 0);
212 let nulls = NullBuffer::union_many([
213 string_array.nulls(),
214 from_array.nulls(),
215 to_array.nulls(),
216 ]);
217
218 // Hoist the nulls.is_some() check out of the loop. LLVM unswitches this
219 // automatically today, but kept explicit so the no-nulls fast path is not
220 // contingent on the optimizer's cost heuristic.
221 if let Some(nulls_ref) = nulls.as_ref() {
222 for i in 0..len {
223 if nulls_ref.is_null(i) {
224 builder.append_placeholder();
225 continue;
226 }
227 // SAFETY: union of input nulls is non-null at i, so each input is too.
228 let string = unsafe { string_array.value_unchecked(i) };
229 let from = unsafe { from_array.value_unchecked(i) };
230 let to = unsafe { to_array.value_unchecked(i) };
231 apply_replace(&mut builder, string, from, to);
232 }
233 } else {
234 for i in 0..len {
235 // SAFETY: i < len, and no input has a null buffer.
236 let string = unsafe { string_array.value_unchecked(i) };
237 let from = unsafe { from_array.value_unchecked(i) };
238 let to = unsafe { to_array.value_unchecked(i) };
239 apply_replace(&mut builder, string, from, to);
240 }
241 }
242
243 Ok(Arc::new(builder.finish(nulls)?) as ArrayRef)
244}
245
246#[inline]
247fn apply_replace<B: BulkNullStringArrayBuilder>(

Callers 15

take_n_innerMethod · 0.85
flush_in_progressMethod · 0.85
split_vec_min_allocFunction · 0.85
vectorized_equal_toMethod · 0.85
nextMethod · 0.85
poll_next_innerMethod · 0.85
initiate_fallbackMethod · 0.85
split_filesMethod · 0.85

Calls 8

apply_replaceFunction · 0.85
newFunction · 0.85
append_placeholderMethod · 0.80
lenMethod · 0.45
nullsMethod · 0.45
as_refMethod · 0.45
is_nullMethod · 0.45
finishMethod · 0.45

Tested by 3

test_fn_replaceFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…