Skip to content

Do not swap with projection when file is partitioned #14956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions datafusion/core/tests/physical_optimizer/projection_pushdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,3 +1382,54 @@ fn test_union_after_projection() -> Result<()> {

Ok(())
}

#[test]
fn test_partition_col_projection_pushdown() -> Result<()> {
let file_schema = Arc::new(Schema::new(vec![
Field::new("int_col", DataType::Int32, true),
Field::new("string_col", DataType::Utf8, true),
]));

let partitioned_schema = Arc::new(Schema::new(vec![
Field::new("int_col", DataType::Int32, true),
Field::new("string_col", DataType::Utf8, true),
Field::new("partition_col", DataType::Utf8, true),
]));

let source = FileScanConfig::new(
ObjectStoreUrl::parse("test:///").unwrap(),
file_schema.clone(),
Arc::new(CsvSource::default()),
)
.with_file(PartitionedFile::new("x".to_string(), 100))
.with_table_partition_cols(vec![Field::new("partition_col", DataType::Utf8, true)])
.with_projection(Some(vec![0, 1, 2]))
.build();

let projection = Arc::new(ProjectionExec::try_new(
vec![
(
col("string_col", partitioned_schema.as_ref())?,
"string_col".to_string(),
),
(
col("partition_col", partitioned_schema.as_ref())?,
"partition_col".to_string(),
),
(
col("int_col", partitioned_schema.as_ref())?,
"int_col".to_string(),
),
],
source,
)?);

let after_optimize =
ProjectionPushdown::new().optimize(projection, &ConfigOptions::new())?;

let expected = ["ProjectionExec: expr=[string_col@1 as string_col, partition_col@2 as partition_col, int_col@0 as int_col]",
" DataSourceExec: file_groups={1 group: [[x]]}, projection=[int_col, string_col, partition_col], file_type=csv, has_header=false"];
assert_eq!(get_plan_string(&after_optimize), expected);

Ok(())
}
43 changes: 27 additions & 16 deletions datafusion/datasource/src/file_scan_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,22 +266,33 @@ impl DataSource for FileScanConfig {
) -> Result<Option<Arc<dyn ExecutionPlan>>> {
// If there is any non-column or alias-carrier expression, Projection should not be removed.
// This process can be moved into CsvExec, but it would be an overlap of their responsibility.
Ok(all_alias_free_columns(projection.expr()).then(|| {
let file_scan = self.clone();
let source = Arc::clone(&file_scan.file_source);
let new_projections = new_projections_for_columns(
projection,
&file_scan
.projection
.clone()
.unwrap_or((0..self.file_schema.fields().len()).collect()),
);
file_scan
// Assign projected statistics to source
.with_projection(Some(new_projections))
.with_source(source)
.build() as _
}))

let partitioned_columns_in_proj = projection.expr().iter().any(|(expr, _)| {
expr.as_any()
.downcast_ref::<Column>()
.map(|expr| expr.index() >= self.file_schema.fields().len())
.unwrap_or(false)
});

Ok(
(all_alias_free_columns(projection.expr()) && !partitioned_columns_in_proj)
.then(|| {
let file_scan = self.clone();
let source = Arc::clone(&file_scan.file_source);
let new_projections = new_projections_for_columns(
projection,
&file_scan
.projection
.clone()
.unwrap_or((0..self.file_schema.fields().len()).collect()),
);
file_scan
// Assign projected statistics to source
.with_projection(Some(new_projections))
.with_source(source)
.build() as _
}),
)
}
}

Expand Down