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

Function inject_column_aliases

datafusion/sql/src/unparser/rewrite.rs:464–492  ·  view source on GitHub ↗

Injects column aliases into the projection of a logical plan by wrapping expressions with `Expr::Alias` using the provided list of aliases. Example: - `SELECT col1, col2 FROM table` with aliases `["alias_1", "some_alias_2"]` will be transformed to - `SELECT col1 AS alias_1, col2 AS some_alias_2 FROM table`

(
    projection: &Projection,
    aliases: impl IntoIterator<Item = Ident>,
)

Source from the content-addressed store, hash-verified

462/// - `SELECT col1, col2 FROM table` with aliases `["alias_1", "some_alias_2"]` will be transformed to
463/// - `SELECT col1 AS alias_1, col2 AS some_alias_2 FROM table`
464pub(super) fn inject_column_aliases(
465 projection: &Projection,
466 aliases: impl IntoIterator<Item = Ident>,
467) -> LogicalPlan {
468 let mut updated_projection = projection.clone();
469
470 let new_exprs = updated_projection
471 .expr
472 .into_iter()
473 .zip(aliases)
474 .map(|(expr, col_alias)| {
475 let relation = match &expr {
476 Expr::Column(col) => col.relation.clone(),
477 _ => None,
478 };
479
480 Expr::Alias(Alias {
481 expr: Box::new(expr.clone()),
482 relation,
483 name: col_alias.value,
484 metadata: None,
485 })
486 })
487 .collect::<Vec<_>>();
488
489 updated_projection.expr = new_exprs;
490
491 LogicalPlan::Projection(updated_projection)
492}
493
494fn find_projection(logical_plan: &LogicalPlan) -> Option<&Projection> {
495 match logical_plan {

Callers 1

Calls 6

AliasClass · 0.85
newFunction · 0.85
ProjectionClass · 0.85
cloneMethod · 0.45
mapMethod · 0.45
into_iterMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…