-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Dataframe with_column and with_column_renamed performance improvements #14653
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
Changes from 6 commits
4679c0a
ff8c91d
90876d4
aae30e4
953e722
85cc464
0eb6862
4b6ab87
0463e82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -183,6 +183,8 @@ pub struct DataFrame { | |
| // Box the (large) SessionState to reduce the size of DataFrame on the stack | ||
| session_state: Box<SessionState>, | ||
| plan: LogicalPlan, | ||
| // whether we can skip validation for projection ops | ||
|
||
| projection_requires_validation: bool, | ||
| } | ||
|
|
||
| impl DataFrame { | ||
|
|
@@ -195,6 +197,7 @@ impl DataFrame { | |
| Self { | ||
| session_state: Box::new(session_state), | ||
| plan, | ||
| projection_requires_validation: true, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -332,6 +335,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan: project_plan, | ||
| projection_requires_validation: false, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -437,6 +441,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -477,6 +482,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -547,6 +553,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: !is_grouping_set, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -559,6 +566,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -597,6 +605,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: self.projection_requires_validation, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -634,6 +643,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -672,6 +682,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -703,6 +714,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -744,6 +756,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -944,6 +957,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: self.projection_requires_validation, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -993,6 +1007,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: self.projection_requires_validation, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -1060,6 +1075,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -1119,6 +1135,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -1154,6 +1171,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -1425,6 +1443,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: self.projection_requires_validation, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -1477,6 +1496,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -1512,6 +1532,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -1557,6 +1578,7 @@ impl DataFrame { | |
| DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: self.projection_requires_validation, | ||
| } | ||
| .collect() | ||
| .await | ||
|
|
@@ -1626,6 +1648,7 @@ impl DataFrame { | |
| DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: self.projection_requires_validation, | ||
| } | ||
| .collect() | ||
| .await | ||
|
|
@@ -1695,12 +1718,13 @@ impl DataFrame { | |
| DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: self.projection_requires_validation, | ||
| } | ||
| .collect() | ||
| .await | ||
| } | ||
|
|
||
| /// Add an additional column to the DataFrame. | ||
| /// Add or replace a column in the DataFrame. | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
|
|
@@ -1728,33 +1752,36 @@ impl DataFrame { | |
|
|
||
| let mut col_exists = false; | ||
| let new_column = expr.alias(name); | ||
| let mut fields: Vec<Expr> = plan | ||
| let mut fields: Vec<(Expr, bool)> = plan | ||
| .schema() | ||
| .iter() | ||
| .filter_map(|(qualifier, field)| { | ||
| if field.name() == name { | ||
| col_exists = true; | ||
| Some(new_column.clone()) | ||
| Some((new_column.clone(), true)) | ||
| } else { | ||
| let e = col(Column::from((qualifier, field))); | ||
| window_fn_str | ||
| .as_ref() | ||
| .filter(|s| *s == &e.to_string()) | ||
| .is_none() | ||
| .then_some(e) | ||
| .then_some((e, self.projection_requires_validation)) | ||
| } | ||
| }) | ||
| .collect(); | ||
|
|
||
| if !col_exists { | ||
| fields.push(new_column); | ||
| fields.push((new_column, true)); | ||
| } | ||
|
|
||
| let project_plan = LogicalPlanBuilder::from(plan).project(fields)?.build()?; | ||
| let project_plan = LogicalPlanBuilder::from(plan) | ||
| .project_with_validation(fields)? | ||
| .build()?; | ||
|
|
||
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan: project_plan, | ||
| projection_requires_validation: false, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -1811,19 +1838,23 @@ impl DataFrame { | |
| .iter() | ||
| .map(|(qualifier, field)| { | ||
| if qualifier.eq(&qualifier_rename) && field.as_ref() == field_rename { | ||
| col(Column::from((qualifier, field))) | ||
| .alias_qualified(qualifier.cloned(), new_name) | ||
| ( | ||
| col(Column::from((qualifier, field))) | ||
| .alias_qualified(qualifier.cloned(), new_name), | ||
| false, | ||
| ) | ||
| } else { | ||
| col(Column::from((qualifier, field))) | ||
| (col(Column::from((qualifier, field))), false) | ||
| } | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
| let project_plan = LogicalPlanBuilder::from(self.plan) | ||
| .project(projection)? | ||
| .project_with_validation(projection)? | ||
| .build()?; | ||
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan: project_plan, | ||
| projection_requires_validation: false, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -1889,6 +1920,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: self.projection_requires_validation, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -1924,6 +1956,7 @@ impl DataFrame { | |
| Ok(DataFrame { | ||
| session_state: self.session_state, | ||
| plan, | ||
| projection_requires_validation: self.projection_requires_validation, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉