Skip to content

Commit ec6abec

Browse files
Fix None Projections in Projection Pushdown (#9005)
* Fix none projections * Update select.slt
1 parent 7005e2e commit ec6abec

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

datafusion/core/src/physical_optimizer/projection_pushdown.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,12 @@ fn try_swapping_with_csv(
163163
// This process can be moved into CsvExec, but it would be an overlap of their responsibility.
164164
all_alias_free_columns(projection.expr()).then(|| {
165165
let mut file_scan = csv.base_config().clone();
166-
let new_projections =
167-
new_projections_for_columns(projection, &file_scan.projection);
166+
let new_projections = new_projections_for_columns(
167+
projection,
168+
&file_scan
169+
.projection
170+
.unwrap_or((0..csv.schema().fields().len()).collect()),
171+
);
168172
file_scan.projection = Some(new_projections);
169173

170174
Arc::new(CsvExec::new(
@@ -188,8 +192,11 @@ fn try_swapping_with_memory(
188192
// This process can be moved into MemoryExec, but it would be an overlap of their responsibility.
189193
all_alias_free_columns(projection.expr())
190194
.then(|| {
191-
let new_projections =
192-
new_projections_for_columns(projection, memory.projection());
195+
let all_projections = (0..memory.schema().fields().len()).collect();
196+
let new_projections = new_projections_for_columns(
197+
projection,
198+
memory.projection().as_ref().unwrap_or(&all_projections),
199+
);
193200

194201
MemoryExec::try_new(
195202
memory.partitions(),
@@ -216,8 +223,11 @@ fn try_swapping_with_streaming_table(
216223
.projection()
217224
.as_ref()
218225
.map(|i| i.as_ref().to_vec());
219-
let new_projections =
220-
new_projections_for_columns(projection, &streaming_table_projections);
226+
let new_projections = new_projections_for_columns(
227+
projection,
228+
&streaming_table_projections
229+
.unwrap_or((0..streaming_table.schema().fields().len()).collect()),
230+
);
221231

222232
let mut lex_orderings = vec![];
223233
for lex_ordering in streaming_table.projected_output_ordering().into_iter() {
@@ -833,15 +843,15 @@ fn all_alias_free_columns(exprs: &[(Arc<dyn PhysicalExpr>, String)]) -> bool {
833843
/// ensure that all expressions are `Column` expressions without aliases.
834844
fn new_projections_for_columns(
835845
projection: &ProjectionExec,
836-
source: &Option<Vec<usize>>,
846+
source: &[usize],
837847
) -> Vec<usize> {
838848
projection
839849
.expr()
840850
.iter()
841851
.filter_map(|(expr, _)| {
842852
expr.as_any()
843853
.downcast_ref::<Column>()
844-
.and_then(|expr| source.as_ref().map(|proj| proj[expr.index()]))
854+
.map(|expr| source[expr.index()])
845855
})
846856
.collect()
847857
}

datafusion/sqllogictest/test_files/select.slt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,3 +1569,17 @@ query I
15691569
select count(1) from v;
15701570
----
15711571
1
1572+
1573+
# run below query without logical optimizations
1574+
statement ok
1575+
set datafusion.optimizer.max_passes=0;
1576+
1577+
statement ok
1578+
CREATE TABLE t(a int, b int);
1579+
1580+
query I
1581+
select a from t;
1582+
----
1583+
1584+
statement ok
1585+
set datafusion.optimizer.max_passes=3;

0 commit comments

Comments
 (0)