Skip to content
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

Early exit on column normalisation to improve DataFrame performance #14636

Merged
merged 3 commits into from
Feb 17, 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
3 changes: 2 additions & 1 deletion datafusion/core/src/dataframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,8 @@ impl DataFrame {
.iter()
.map(|(qualifier, field)| {
if qualifier.eq(&qualifier_rename) && field.as_ref() == field_rename {
col(Column::from((qualifier, field))).alias(new_name)
col(Column::from((qualifier, field)))
.alias_qualified(qualifier.cloned(), new_name)
} else {
col(Column::from((qualifier, field)))
}
Expand Down
26 changes: 21 additions & 5 deletions datafusion/core/tests/dataframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use datafusion::{assert_batches_eq, assert_batches_sorted_eq};
use datafusion_catalog::TableProvider;
use datafusion_common::{
assert_contains, Constraint, Constraints, DataFusionError, ParamValues, ScalarValue,
UnnestOptions,
TableReference, UnnestOptions,
};
use datafusion_common_runtime::SpawnedTask;
use datafusion_execution::config::SessionConfig;
Expand Down Expand Up @@ -1617,9 +1617,25 @@ async fn with_column_renamed() -> Result<()> {
// accepts table qualifier
.with_column_renamed("aggregate_test_100.c2", "two")?
// no-op for missing column
.with_column_renamed("c4", "boom")?
.collect()
.await?;
.with_column_renamed("c4", "boom")?;

let references: Vec<_> = df_sum_renamed
.schema()
.iter()
.map(|(a, _)| a.cloned())
.collect();

assert_eq!(
references,
vec![
Some(TableReference::bare("aggregate_test_100")), // table name is preserved
Some(TableReference::bare("aggregate_test_100")),
Some(TableReference::bare("aggregate_test_100")),
None // total column
]
);

let batches = &df_sum_renamed.collect().await?;

assert_batches_sorted_eq!(
[
Expand All @@ -1629,7 +1645,7 @@ async fn with_column_renamed() -> Result<()> {
"| a | 3 | -72 | -69 |",
"+-----+-----+-----+-------+",
],
&df_sum_renamed
batches
);

Ok(())
Expand Down
8 changes: 7 additions & 1 deletion datafusion/expr/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,10 +834,16 @@ impl LogicalPlanBuilder {
plan: &LogicalPlan,
column: impl Into<Column>,
) -> Result<Column> {
let column = column.into();
if column.relation.is_some() {
// column is already normalized
return Ok(column);
}

Comment on lines +837 to +842
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a no brainer performance boost. Thanks!

let schema = plan.schema();
let fallback_schemas = plan.fallback_normalize_schemas();
let using_columns = plan.using_columns()?;
column.into().normalize_with_schemas_and_ambiguity_check(
column.normalize_with_schemas_and_ambiguity_check(
&[&[schema], &fallback_schemas],
&using_columns,
)
Expand Down
Loading