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

Method add_missing_columns

datafusion/expr/src/logical_plan/builder.rs:710–753  ·  view source on GitHub ↗

Add missing sort columns to all downstream projection Thus, if you have a LogicalPlan that selects A and B and have not requested a sort by C, this code will add C recursively to all input projections. Adding a new column is not correct if there is a `Distinct` node, which produces only distinct values of its inputs. Adding a new column to its input will result in potentially different results t

(
        curr_plan: LogicalPlan,
        missing_cols: &IndexSet<Column>,
        is_distinct: bool,
    )

Source from the content-addressed store, hash-verified

708 ///
709 /// See <https://github.com/apache/datafusion/issues/5065> for more details
710 fn add_missing_columns(
711 curr_plan: LogicalPlan,
712 missing_cols: &IndexSet<Column>,
713 is_distinct: bool,
714 ) -> Result<LogicalPlan> {
715 match curr_plan {
716 LogicalPlan::Projection(Projection {
717 input,
718 mut expr,
719 schema: _,
720 }) if missing_cols.iter().all(|c| input.schema().has_column(c)) => {
721 let mut missing_exprs = missing_cols
722 .iter()
723 .map(|c| normalize_col(Expr::Column(c.clone()), &input))
724 .collect::<Result<Vec<_>>>()?;
725
726 // Do not let duplicate columns to be added, some of the
727 // missing_cols may be already present but without the new
728 // projected alias.
729 missing_exprs.retain(|e| !expr.contains(e));
730 if is_distinct {
731 Self::ambiguous_distinct_check(&missing_exprs, missing_cols, &expr)?;
732 }
733 expr.extend(missing_exprs);
734 project(Arc::unwrap_or_clone(input), expr)
735 }
736 _ => {
737 let is_distinct =
738 is_distinct || matches!(curr_plan, LogicalPlan::Distinct(_));
739 let new_inputs = curr_plan
740 .inputs()
741 .into_iter()
742 .map(|input_plan| {
743 Self::add_missing_columns(
744 (*input_plan).clone(),
745 missing_cols,
746 is_distinct,
747 )
748 })
749 .collect::<Result<Vec<_>>>()?;
750 curr_plan.with_new_exprs(curr_plan.expressions(), new_inputs)
751 }
752 }
753 }
754
755 fn ambiguous_distinct_check(
756 missing_exprs: &[Expr],

Callers

nothing calls this directly

Calls 15

normalize_colFunction · 0.85
projectFunction · 0.85
allMethod · 0.80
has_columnMethod · 0.80
with_new_exprsMethod · 0.80
ColumnClass · 0.50
iterMethod · 0.45
schemaMethod · 0.45
mapMethod · 0.45
cloneMethod · 0.45
containsMethod · 0.45
extendMethod · 0.45

Tested by

no test coverage detected