Apache DataFusion SQL Query Engine
ARROW-9184: [Rust][Datafusion] table scan without projection should return all columns
Qingping Hou7e13985f4e71
diff --git a/rust/datafusion/src/optimizer/projection_push_down.rs b/rust/datafusion/src/optimizer/projection_push_down.rsindex 7fa40664ae..cb2c3fdd79 100644--- a/rust/datafusion/src/optimizer/projection_push_down.rs+++ b/rust/datafusion/src/optimizer/projection_push_down.rs@@ -34,7 +34,7 @@ impl OptimizerRule for ProjectionPushDown {fn optimize(&mut self, plan: &LogicalPlan) -> Result<LogicalPlan> {let mut accum: HashSet<usize> = HashSet::new();let mut mapping: HashMap<usize, usize> = HashMap::new();- self.optimize_plan(plan, &mut accum, &mut mapping)+ self.optimize_plan(plan, &mut accum, &mut mapping, false)}}@@ -49,23 +49,31 @@ impl ProjectionPushDown {plan: &LogicalPlan,accum: &mut HashSet<usize>,mapping: &mut HashMap<usize, usize>,+ has_projection: bool,) -> Result<LogicalPlan> {match plan {LogicalPlan::Projection { expr, input, .. } => {// collect all columns referenced by projection expressionsutils::exprlist_to_column_indices(&expr, accum)?;- LogicalPlanBuilder::from(&self.optimize_plan(&input, accum, mapping)?)- .project(self.rewrite_expr_list(expr, mapping)?)?- .build()+ LogicalPlanBuilder::from(+ &self.optimize_plan(&input, accum, mapping, true)?,+ )+ .project(self.rewrite_expr_list(expr, mapping)?)?+ .build()}LogicalPlan::Selection { expr, input } => {// collect all columns referenced by filter expressionutils::expr_to_column_indices(expr, accum)?;- LogicalPlanBuilder::from(&self.optimize_plan(&input, accum, mapping)?)- .filter(self.rewrite_expr(expr, mapping)?)?- .build()+ LogicalPlanBuilder::from(&self.optimize_plan(+ &input,+ accum,+ mapping,+ has_projection,+ )?)+ .filter(self.rewrite_expr(expr, mapping)?)?+ .build()}LogicalPlan::Aggregate {input,@@ -77,20 +85,30 @@ impl ProjectionPushDown {utils::exprlist_to_column_indices(&group_expr, accum)?;utils::exprlist_to_column_indices(&aggr_expr, accum)?;- LogicalPlanBuilder::from(&self.optimize_plan(&input, accum, mapping)?)- .aggregate(- self.rewrite_expr_list(group_expr, mapping)?,- self.rewrite_expr_list(aggr_expr, mapping)?,- )?- .build()+ LogicalPlanBuilder::from(&self.optimize_plan(+ &input,+ accum,+ mapping,+ has_projection,+ )?)+ .aggregate(+ self.rewrite_expr_list(group_expr, mapping)?,+ self.rewrite_expr_list(aggr_expr, mapping)?,+ )?+ .build()}LogicalPlan::Sort { expr, input, .. } => {// collect all columns referenced by sort expressionsutils::exprlist_to_column_indices(&expr, accum)?;- LogicalPlanBuilder::from(&self.optimize_plan(&input, accum, mapping)?)- .sort(self.rewrite_expr_list(expr, mapping)?)?- .build()+ LogicalPlanBuilder::from(&self.optimize_plan(+ &input,+ accum,+ mapping,+ has_projection,+ )?)+ .sort(self.rewrite_expr_list(expr, mapping)?)?+ .build()}LogicalPlan::EmptyRelation { schema } => Ok(LogicalPlan::EmptyRelation {schema: schema.clone(),@@ -102,8 +120,13 @@ impl ProjectionPushDown {projection,..} => {- let (projection, projected_schema) =- get_projected_schema(&table_schema, projection, accum, mapping)?;+ let (projection, projected_schema) = get_projected_schema(+ &table_schema,+ projection,+ accum,+ mapping,+ has_projection,+ )?;// return the table scan with projectionOk(LogicalPlan::TableScan {@@ -120,9 +143,13 @@ impl ProjectionPushDown {projection,..} => {- let (projection, projected_schema) =- get_projected_schema(&schema, projection, accum, mapping)?;-+ let (projection, projected_schema) = get_projected_schema(+ &schema,+ projection,+ accum,+ mapping,+ has_projection,+ )?;Ok(LogicalPlan::InMemoryScan {data: data.clone(),schema: schema.clone(),@@ -138,8 +165,13 @@ impl ProjectionPushDown {projection,..} => {- let (projection, projected_schema) =- get_projected_schema(&schema, projection, accum, mapping)?;+ let (projection, projected_schema) = get_projected_schema(+ &schema,+ projection,+ accum,+ mapping,+ has_projection,+ )?;Ok(LogicalPlan::CsvScan {path: path.to_owned(),@@ -156,8 +188,13 @@ impl ProjectionPushDown {projection,..} => {- let (projection, projected_schema) =- get_projected_schema(&schema, projection, accum, mapping)?;+ let (projection, projected_schema) = get_projected_schema(+ &schema,+ projection,+ accum,+ mapping,+ has_projection,+ )?;Ok(LogicalPlan::ParquetScan {path: path.to_owned(),@@ -169,9 +206,14 @@ impl ProjectionPushDown {LogicalPlan::Limit { expr, input, .. } => {// Note that limit expressions are scalar values so there is no need to// rewrite them but we do need to optimize the input to the limit plan- LogicalPlanBuilder::from(&self.optimize_plan(&input, accum, mapping)?)- .limit(expr.clone())?- .build()+ LogicalPlanBuilder::from(&self.optimize_plan(+ &input,+ accum,+ mapping,+ has_projection,+ )?)+ .limit(expr.clone())?+ .build()}LogicalPlan::CreateExternalTable {schema,@@ -274,6 +316,7 @@ fn get_projected_schema(projection: &Option<Vec<usize>>,accum: &HashSet<usize>,mapping: &mut HashMap<usize, usize>,+ has_projection: bool,) -> Result<(Vec<usize>, Schema)> {if projection.is_some() {return Err(ExecutionError::General(@@ -283,13 +326,22 @@ fn get_projected_schema(// once we reach the table scan, we can use the accumulated set of column// indexes as the projection in the table scan- let mut projection: Vec<usize> = Vec::with_capacity(accum.len());- accum.iter().for_each(|i| projection.push(*i));+ let mut projection = accum.iter().map(|i| *i).collect::<Vec<usize>>();- // Ensure that we are reading at least one column from the table in case the query- // does not reference any columns directly such as "SELECT COUNT(1) FROM table"if projection.is_empty() {- projection.push(0);+ if has_projection {+ // Ensure that we are reading at least one column from the table in case the query+ // does not reference any columns directly such as "SELECT COUNT(1) FROM table"+ projection.push(0);+ } else {+ // for table scan without projection, we default to return all columns+ projection = table_schema+ .fields()+ .iter()+ .enumerate()+ .map(|(i, _)| i)+ .collect::<Vec<usize>>();+ }}// sort the projection otherwise we get non-deterministic behavior@@ -439,6 +491,31 @@ mod tests {Ok(())}+ #[test]+ fn table_scan_without_projection() -> Result<()> {+ let table_scan = test_table_scan()?;+ let plan = LogicalPlanBuilder::from(&table_scan).build()?;+ // should expand projection to all columns without projection+ let expected = "TableScan: test projection=Some([0, 1, 2])";+ assert_optimized_plan_eq(&plan, expected);+ Ok(())+ }++ #[test]+ fn table_scan_with_literal_projection() -> Result<()> {+ let table_scan = test_table_scan()?;+ let plan = LogicalPlanBuilder::from(&table_scan)+ .project(vec![+ Expr::Literal(ScalarValue::Int64(1)),+ Expr::Literal(ScalarValue::Int64(2)),+ ])?+ .build()?;+ let expected = "Projection: Int64(1), Int64(2)\+ \n TableScan: test projection=Some([0])";+ assert_optimized_plan_eq(&plan, expected);+ Ok(())+ }+fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) {let optimized_plan = optimize(plan).expect("failed to optimize plan");let formatted_plan = format!("{:?}", optimized_plan);