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])
| 268 | /// Pre-computes the pattern characters once for scalar patterns to avoid |
| 269 | /// repeated allocations per row. |
| 270 | fn 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…