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

Function simplify_concat

datafusion/functions/src/string/concat.rs:310–392  ·  view source on GitHub ↗
(args: Vec<Expr>)

Source from the content-addressed store, hash-verified

308}
309
310pub(crate) fn simplify_concat(args: Vec<Expr>) -> Result<ExprSimplifyResult> {
311 let mut new_args = Vec::with_capacity(args.len());
312 let mut contiguous_scalar = "".to_string();
313
314 let return_type = {
315 let data_types: Vec<_> = args
316 .iter()
317 .filter_map(|expr| match expr {
318 Expr::Literal(l, _) => Some(l.data_type()),
319 _ => None,
320 })
321 .collect();
322 ConcatFunc::new().return_type(&data_types)
323 }?;
324
325 for arg in args.clone() {
326 match arg {
327 Expr::Literal(ScalarValue::Utf8(None), _) => {}
328 Expr::Literal(ScalarValue::LargeUtf8(None), _) => {}
329 Expr::Literal(ScalarValue::Utf8View(None), _) => {}
330
331 // filter out `null` args
332 // All literals have been converted to Utf8 or LargeUtf8 in type_coercion.
333 // Concatenate it with the `contiguous_scalar`.
334 Expr::Literal(ScalarValue::Utf8(Some(v)), _) => {
335 contiguous_scalar += &v;
336 }
337 Expr::Literal(ScalarValue::LargeUtf8(Some(v)), _) => {
338 contiguous_scalar += &v;
339 }
340 Expr::Literal(ScalarValue::Utf8View(Some(v)), _) => {
341 contiguous_scalar += &v;
342 }
343
344 Expr::Literal(x, _) => {
345 return internal_err!(
346 "The scalar {x} should be casted to string type during the type coercion."
347 );
348 }
349 // If the arg is not a literal, we should first push the current `contiguous_scalar`
350 // to the `new_args` (if it is not empty) and reset it to empty string.
351 // Then pushing this arg to the `new_args`.
352 arg => {
353 if !contiguous_scalar.is_empty() {
354 match return_type {
355 DataType::Utf8 => new_args.push(lit(contiguous_scalar)),
356 DataType::LargeUtf8 => new_args
357 .push(lit(ScalarValue::LargeUtf8(Some(contiguous_scalar)))),
358 DataType::Utf8View => new_args
359 .push(lit(ScalarValue::Utf8View(Some(contiguous_scalar)))),
360 _ => unreachable!(),
361 }
362 contiguous_scalar = "".to_string();
363 }
364 new_args.push(arg);
365 }
366 }
367 }

Callers 2

simplify_concat_wsFunction · 0.85
simplifyMethod · 0.85

Calls 14

newFunction · 0.85
ScalarFunctionClass · 0.85
collectMethod · 0.80
concatFunction · 0.70
litFunction · 0.50
lenMethod · 0.45
to_stringMethod · 0.45
iterMethod · 0.45
data_typeMethod · 0.45
return_typeMethod · 0.45
cloneMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…