Skip to content

Commit 2df3869

Browse files
committed
Preserve projection for inline scan
1 parent 2f7cc9b commit 2df3869

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

datafusion/core/tests/execution/logical_plan.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
//! create them and depend on them. Test executable semantics of logical plans.
2020
2121
use arrow::array::Int64Array;
22-
use arrow::datatypes::{DataType, Field};
22+
use arrow::datatypes::{DataType, Field, Schema};
23+
use datafusion::datasource::{provider_as_source, ViewTable};
2324
use datafusion::execution::session_state::SessionStateBuilder;
24-
use datafusion_common::{Column, DFSchema, Result, ScalarValue, Spans};
25+
use datafusion_common::{Column, DFSchema, DFSchemaRef, Result, ScalarValue, Spans};
2526
use datafusion_execution::TaskContext;
2627
use datafusion_expr::expr::{AggregateFunction, AggregateFunctionParams};
2728
use datafusion_expr::logical_plan::{LogicalPlan, Values};
28-
use datafusion_expr::{Aggregate, AggregateUDF, Expr};
29+
use datafusion_expr::{
30+
Aggregate, AggregateUDF, EmptyRelation, Expr, LogicalPlanBuilder, UNNAMED_TABLE,
31+
};
2932
use datafusion_functions_aggregate::count::Count;
3033
use datafusion_physical_plan::collect;
3134
use std::collections::HashMap;
@@ -96,3 +99,32 @@ where
9699
};
97100
element
98101
}
102+
103+
#[test]
104+
fn inline_scan_projection_test() -> Result<()> {
105+
let name = UNNAMED_TABLE;
106+
let column = "a";
107+
108+
let schema = Schema::new(vec![
109+
Field::new("a", DataType::Int32, false),
110+
Field::new("b", DataType::Int32, false),
111+
]);
112+
let projection = vec![schema.index_of(column)?];
113+
114+
let provider = ViewTable::new(
115+
LogicalPlan::EmptyRelation(EmptyRelation {
116+
produce_one_row: false,
117+
schema: DFSchemaRef::new(DFSchema::try_from(schema)?),
118+
}),
119+
None,
120+
);
121+
let source = provider_as_source(Arc::new(provider));
122+
123+
let plan = LogicalPlanBuilder::scan(name, source, Some(projection))?.build()?;
124+
assert_eq!(
125+
format!("{plan}"),
126+
format!("TableScan: {name} projection=[{column}]")
127+
);
128+
129+
Ok(())
130+
}

datafusion/expr/src/logical_plan/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ impl LogicalPlanBuilder {
498498
TableScan::try_new(table_name, table_source, projection, filters, fetch)?;
499499

500500
// Inline TableScan
501-
if table_scan.filters.is_empty() {
501+
if table_scan.projection.is_none() && table_scan.filters.is_empty() {
502502
if let Some(p) = table_scan.source.get_logical_plan() {
503503
let sub_plan = p.into_owned();
504504
// Ensures that the reference to the inlined table remains the

0 commit comments

Comments
 (0)