From 26e00ab3f87ef0ae30fa3a16ca9a756688294aa4 Mon Sep 17 00:00:00 2001 From: jackwener Date: Thu, 23 Feb 2023 14:15:22 +0800 Subject: [PATCH 1/2] fix inline_table_scan --- datafusion/optimizer/src/inline_table_scan.rs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/datafusion/optimizer/src/inline_table_scan.rs b/datafusion/optimizer/src/inline_table_scan.rs index 39e7d43845f6..94b1b779e0c4 100644 --- a/datafusion/optimizer/src/inline_table_scan.rs +++ b/datafusion/optimizer/src/inline_table_scan.rs @@ -49,11 +49,14 @@ impl OptimizerRule for InlineTableScan { source, table_name, filters, + projection, .. }) if filters.is_empty() => { if let Some(sub_plan) = source.get_logical_plan() { + let projection_exprs = + generate_projection_expr(projection, sub_plan)?; let plan = LogicalPlanBuilder::from(sub_plan.clone()) - .project(vec![Expr::Wildcard])? + .project(projection_exprs)? .alias(table_name)?; Ok(Some(plan.build()?)) } else { @@ -73,6 +76,23 @@ impl OptimizerRule for InlineTableScan { } } +fn generate_projection_expr( + projection: &Option>, + sub_plan: &LogicalPlan, +) -> Result> { + let mut exprs = vec![]; + if let Some(projection) = projection { + for i in projection { + exprs.push(Expr::Column( + sub_plan.schema().fields()[*i].qualified_column(), + )); + } + } else { + exprs.push(Expr::Wildcard); + } + Ok(exprs) +} + #[cfg(test)] mod tests { use std::{sync::Arc, vec}; From 74f75d019f35b18ca9c55a591cee33c854d067fc Mon Sep 17 00:00:00 2001 From: jackwener Date: Thu, 23 Feb 2023 19:24:32 +0800 Subject: [PATCH 2/2] add test --- datafusion/optimizer/src/inline_table_scan.rs | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/datafusion/optimizer/src/inline_table_scan.rs b/datafusion/optimizer/src/inline_table_scan.rs index 94b1b779e0c4..6b58399192ee 100644 --- a/datafusion/optimizer/src/inline_table_scan.rs +++ b/datafusion/optimizer/src/inline_table_scan.rs @@ -111,7 +111,10 @@ mod tests { } fn schema(&self) -> arrow::datatypes::SchemaRef { - Arc::new(Schema::new(vec![Field::new("a", DataType::Int64, false)])) + Arc::new(Schema::new(vec![ + Field::new("a", DataType::Int64, false), + Field::new("b", DataType::Int64, false), + ])) } fn supports_filter_pushdown( @@ -170,9 +173,25 @@ mod tests { let plan = scan.filter(col("x.a").eq(lit(1)))?.build()?; let expected = "Filter: x.a = Int32(1)\ \n SubqueryAlias: x\ - \n Projection: y.a\ + \n Projection: y.a, y.b\ \n TableScan: y"; assert_optimized_plan_eq(Arc::new(InlineTableScan::new()), &plan, expected) } + + #[test] + fn inline_table_scan_with_projection() -> datafusion_common::Result<()> { + let scan = LogicalPlanBuilder::scan( + "x".to_string(), + Arc::new(CustomSource::new()), + Some(vec![0]), + )?; + + let plan = scan.build()?; + let expected = "SubqueryAlias: x\ + \n Projection: y.a\ + \n TableScan: y"; + + assert_optimized_plan_eq(Arc::new(InlineTableScan::new()), &plan, expected) + } }