xudong963 commented on a change in pull request #1415: URL: https://github.com/apache/arrow-datafusion/pull/1415#discussion_r764527294
########## File path: datafusion/src/logical_plan/builder.rs ########## @@ -466,12 +467,126 @@ impl LogicalPlanBuilder { }))) } + /// Add missing sort columns to downstream projection + fn add_missing_columns( + &self, + curr_plan: LogicalPlan, + missing_cols: &[Column], + ) -> LogicalPlan { + match curr_plan.clone() { + LogicalPlan::Projection(Projection { + input, + expr, + schema: _, + alias, + }) => { + let input_schema = input.schema(); + if missing_cols + .iter() + .all(|c| input_schema.field_from_column(c).is_ok()) + { + let mut new_expr = Vec::new(); + new_expr.extend(expr); + missing_cols.iter().for_each(|c| { + new_expr.push( + normalize_col(Expr::Column(c.clone()), &input).unwrap(), + ); + }); + + let new_inputs = + curr_plan.inputs().into_iter().cloned().collect::<Vec<_>>(); + + let new_schema = DFSchema::new( + exprlist_to_fields(&new_expr, input_schema).unwrap(), + ) + .unwrap(); + + LogicalPlan::Projection(Projection { + expr: new_expr, + input: Arc::new(new_inputs.get(0).unwrap().clone()), + schema: DFSchemaRef::new(new_schema), + alias, + }) + } else { + let inputs = curr_plan.inputs(); + let new_inputs = inputs + .iter() + .map(|input_plan| { + self.add_missing_columns((*input_plan).clone(), missing_cols) + }) + .collect::<Vec<_>>(); + + let expr = curr_plan.expressions(); + utils::from_plan(&curr_plan, &expr, &new_inputs).unwrap() + } + } + _ => { + let inputs = curr_plan.inputs(); + let new_inputs = inputs + .iter() + .map(|input_plan| { + self.add_missing_columns((*input_plan).clone(), missing_cols) + }) + .collect::<Vec<_>>(); + + let expr = curr_plan.expressions(); + utils::from_plan(&curr_plan, &expr, &new_inputs).unwrap() + } + } + } + /// Apply a sort - pub fn sort(&self, exprs: impl IntoIterator<Item = impl Into<Expr>>) -> Result<Self> { - Ok(Self::from(LogicalPlan::Sort(Sort { - expr: normalize_cols(exprs, &self.plan)?, - input: Arc::new(self.plan.clone()), - }))) + pub fn sort( + &self, + exprs: impl IntoIterator<Item = impl Into<Expr>> + Clone, + ) -> Result<Self> { + let schema = self.plan.schema(); + + // Collect sort columns that are missing in the input plan's schema + let mut missing_cols: Vec<Column> = vec![]; + exprs + .clone() + .into_iter() + .try_for_each::<_, Result<()>>(|expr| { + let mut columns: HashSet<Column> = HashSet::new(); + utils::expr_to_columns(&expr.into(), &mut columns)?; + + columns.into_iter().for_each(|c| { + if schema.field_from_column(&c).is_err() { + missing_cols.push(c); + } + }); + + Ok(()) + })?; + + if !missing_cols.is_empty() { + let plan = self.add_missing_columns(self.plan.clone(), &missing_cols); + let sort_plan = LogicalPlan::Sort(Sort { + expr: normalize_cols(exprs, &plan)?, + input: Arc::new(plan.clone()), + }); + // remove pushed down sort columns Review comment: for example This is your current implementation, it'll sort the whole table twice. ``` ❯ explain select column1 from foo order by column1, column2; +---------------+-----------------------------------------------------------------------------------------+ | plan_type | plan | +---------------+-----------------------------------------------------------------------------------------+ | logical_plan | Projection: #foo.column1 | | | Sort: #foo.column1 ASC NULLS LAST, #foo.column2 ASC NULLS LAST | | | TableScan: foo projection=Some([0, 1]) ``` If we only remove unselected columns, the plan will be the following. This will sort the whole table once and sort column1 once ``` ❯ explain select column1 from foo order by column1, column2; +---------------+-----------------------------------------------------------------------------------------+ | plan_type | plan | +---------------+-----------------------------------------------------------------------------------------+ | logical_plan | Sort: #foo.column1 ASC NULLS LAST, | | Projection: #foo.column1 | | | Sort: #foo.column2 ASC NULLS LAST | | | TableScan: foo projection=Some([0, 1]) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org