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

Function string_trim

datafusion/functions/src/string/common.rs:270–326  ·  view source on GitHub ↗

Applies the trim function to the given string array(s) and returns a new string array with the trimmed values. Pre-computes the pattern characters once for scalar patterns to avoid repeated allocations per row.

(args: &[ArrayRef])

Source from the content-addressed store, hash-verified

268/// Pre-computes the pattern characters once for scalar patterns to avoid
269/// repeated allocations per row.
270fn string_trim<T: OffsetSizeTrait, Tr: Trimmer>(args: &[ArrayRef]) -> Result<ArrayRef> {
271 let string_array = as_generic_string_array::<T>(&args[0])?;
272
273 match args.len() {
274 1 => {
275 // Trim spaces by default
276 let result = string_array
277 .iter()
278 .map(|string| string.map(|s| Tr::trim_ascii_char(s, b' ').0))
279 .collect::<GenericStringArray<T>>();
280
281 Ok(Arc::new(result) as ArrayRef)
282 }
283 2 => {
284 let characters_array = as_generic_string_array::<T>(&args[1])?;
285
286 if characters_array.len() == 1 {
287 // Scalar pattern - pre-compute pattern chars once
288 if characters_array.is_null(0) {
289 return Ok(new_null_array(
290 string_array.data_type(),
291 string_array.len(),
292 ));
293 }
294
295 let pattern: Vec<char> = characters_array.value(0).chars().collect();
296 let result = string_array
297 .iter()
298 .map(|item| item.map(|s| Tr::trim(s, &pattern).0))
299 .collect::<GenericStringArray<T>>();
300 return Ok(Arc::new(result) as ArrayRef);
301 }
302
303 // Per-row pattern - must compute pattern chars for each row
304 let mut pattern: Vec<char> = Vec::new();
305 let result = string_array
306 .iter()
307 .zip(characters_array.iter())
308 .map(|(string, characters)| match (string, characters) {
309 (Some(s), Some(c)) => {
310 pattern.clear();
311 pattern.extend(c.chars());
312 Some(Tr::trim(s, &pattern).0)
313 }
314 _ => None,
315 })
316 .collect::<GenericStringArray<T>>();
317
318 Ok(Arc::new(result) as ArrayRef)
319 }
320 other => {
321 exec_err!(
322 "Function TRIM was called with {other} arguments. It requires at least 1 and at most 2."
323 )
324 }
325 }
326}
327

Callers

nothing calls this directly

Calls 11

newFunction · 0.85
trimFunction · 0.85
collectMethod · 0.80
lenMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45
is_nullMethod · 0.45
data_typeMethod · 0.45
valueMethod · 0.45
clearMethod · 0.45
extendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…