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

Method transform

datafusion/sql/src/utils.rs:429–496  ·  view source on GitHub ↗
(
        &mut self,
        level: usize,
        alias_name: String,
        expr_in_unnest: &Expr,
        struct_allowed: bool,
    )

Source from the content-addressed store, hash-verified

427 }
428
429 fn transform(
430 &mut self,
431 level: usize,
432 alias_name: String,
433 expr_in_unnest: &Expr,
434 struct_allowed: bool,
435 ) -> Result<Vec<Expr>> {
436 let inner_expr_name = expr_in_unnest.schema_name().to_string();
437
438 // Full context, we are trying to plan the execution as InnerProjection->Unnest->OuterProjection
439 // inside unnest execution, each column inside the inner projection
440 // will be transformed into new columns. Thus we need to keep track of these placeholding column names
441 let placeholder_name = format!("{UNNEST_PLACEHOLDER}({inner_expr_name})");
442 let post_unnest_name =
443 format!("{UNNEST_PLACEHOLDER}({inner_expr_name},depth={level})");
444 // This is due to the fact that unnest transformation should keep the original
445 // column name as is, to comply with group by and order by
446 let placeholder_column = Column::from_name(placeholder_name.clone());
447 let field = expr_in_unnest.to_field(self.input_schema)?.1;
448 let data_type = field.data_type();
449
450 match data_type {
451 DataType::Struct(inner_fields) => {
452 assert_or_internal_err!(
453 struct_allowed,
454 "unnest on struct can only be applied at the root level of select expression"
455 );
456 push_projection_dedupl(
457 self.inner_projection_exprs,
458 expr_in_unnest.clone().alias(placeholder_name.clone()),
459 );
460 self.columns_unnestings
461 .insert(Column::from_name(placeholder_name.clone()), None);
462 Ok(get_struct_unnested_columns(&placeholder_name, inner_fields)
463 .into_iter()
464 .map(Expr::Column)
465 .collect())
466 }
467 DataType::List(_)
468 | DataType::FixedSizeList(_, _)
469 | DataType::LargeList(_)
470 | DataType::ListView(_)
471 | DataType::LargeListView(_) => {
472 push_projection_dedupl(
473 self.inner_projection_exprs,
474 expr_in_unnest.clone().alias(placeholder_name.clone()),
475 );
476
477 let post_unnest_expr = col(post_unnest_name.clone()).alias(alias_name);
478 let list_unnesting = self
479 .columns_unnestings
480 .entry(placeholder_column)
481 .or_insert(Some(vec![]));
482 let unnesting = ColumnUnnestList {
483 output_column: Column::from_name(post_unnest_name),
484 depth: level,
485 };
486 let list_unnestings = list_unnesting.as_mut().unwrap();

Callers 15

type_coercion_demoFunction · 0.45
analyzeMethod · 0.45
rewriteMethod · 0.45
replacementMethod · 0.45
rewriteMethod · 0.45
simplifyMethod · 0.45
case_transformFunction · 0.45
normalize_exprMethod · 0.45
test_transform_explainFunction · 0.45
normalize_colFunction · 0.45

Calls 15

push_projection_deduplFunction · 0.85
to_fieldMethod · 0.80
collectMethod · 0.80
colFunction · 0.50
to_stringMethod · 0.45
schema_nameMethod · 0.45
cloneMethod · 0.45
data_typeMethod · 0.45
aliasMethod · 0.45
insertMethod · 0.45
mapMethod · 0.45

Tested by 3

case_transformFunction · 0.36
test_transform_explainFunction · 0.36
rewriter_rewriteFunction · 0.36