-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
fix: preserve expression names when replacing placeholders #12126
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -279,6 +279,57 @@ where | |
expr.alias_if_changed(original_name) | ||
} | ||
|
||
/// Handles ensuring the name of rewritten expressions is not changed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI @findepi here is an example of using Expr::Alias as described in #1468 (comment) |
||
/// | ||
/// For example, if an expression `1 + 2` is rewritten to `3`, the name of the | ||
/// expression should be preserved: `3 as "1 + 2"` | ||
/// | ||
/// See <https://github.com/apache/datafusion/issues/3555> for details | ||
pub struct NamePreserver { | ||
use_alias: bool, | ||
} | ||
|
||
/// If the name of an expression is remembered, it will be preserved when | ||
/// rewriting the expression | ||
pub struct SavedName(Option<String>); | ||
|
||
impl NamePreserver { | ||
/// Create a new NamePreserver for rewriting the `expr` that is part of the specified plan | ||
pub fn new(plan: &LogicalPlan) -> Self { | ||
Self { | ||
use_alias: !matches!(plan, LogicalPlan::Filter(_) | LogicalPlan::Join(_)), | ||
} | ||
} | ||
|
||
/// Create a new NamePreserver for rewriting the `expr`s in `Projection` | ||
/// | ||
/// This will use aliases | ||
pub fn new_for_projection() -> Self { | ||
Self { use_alias: true } | ||
} | ||
|
||
pub fn save(&self, expr: &Expr) -> Result<SavedName> { | ||
let original_name = if self.use_alias { | ||
Some(expr.name_for_alias()?) | ||
} else { | ||
None | ||
}; | ||
|
||
Ok(SavedName(original_name)) | ||
} | ||
} | ||
|
||
impl SavedName { | ||
/// Ensures the name of the rewritten expression is preserved | ||
pub fn restore(self, expr: Expr) -> Result<Expr> { | ||
let Self(original_name) = self; | ||
match original_name { | ||
Some(name) => expr.alias_if_changed(name), | ||
None => Ok(expr), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use std::ops::Add; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,10 @@ use datafusion_expr::{logical_plan::LogicalPlan, Expr, Operator}; | |
|
||
use log::{debug, trace}; | ||
|
||
/// Re-export of `NamesPreserver` for backwards compatibility, | ||
/// as it was initially placed here and then moved elsewhere. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
pub use datafusion_expr::expr_rewriter::NamePreserver; | ||
|
||
/// Convenience rule for writing optimizers: recursively invoke | ||
/// optimize on plan's children and then return a node of the same | ||
/// type. Useful for optimizer rules which want to leave the type | ||
|
@@ -294,54 +298,3 @@ pub fn only_or_err<T>(slice: &[T]) -> Result<&T> { | |
pub fn merge_schema(inputs: Vec<&LogicalPlan>) -> DFSchema { | ||
expr_utils::merge_schema(inputs) | ||
} | ||
|
||
/// Handles ensuring the name of rewritten expressions is not changed. | ||
/// | ||
/// For example, if an expression `1 + 2` is rewritten to `3`, the name of the | ||
/// expression should be preserved: `3 as "1 + 2"` | ||
/// | ||
/// See <https://github.com/apache/datafusion/issues/3555> for details | ||
pub struct NamePreserver { | ||
use_alias: bool, | ||
} | ||
|
||
/// If the name of an expression is remembered, it will be preserved when | ||
/// rewriting the expression | ||
pub struct SavedName(Option<String>); | ||
|
||
impl NamePreserver { | ||
/// Create a new NamePreserver for rewriting the `expr` that is part of the specified plan | ||
pub fn new(plan: &LogicalPlan) -> Self { | ||
Self { | ||
use_alias: !matches!(plan, LogicalPlan::Filter(_) | LogicalPlan::Join(_)), | ||
} | ||
} | ||
|
||
/// Create a new NamePreserver for rewriting the `expr`s in `Projection` | ||
/// | ||
/// This will use aliases | ||
pub fn new_for_projection() -> Self { | ||
Self { use_alias: true } | ||
} | ||
|
||
pub fn save(&self, expr: &Expr) -> Result<SavedName> { | ||
let original_name = if self.use_alias { | ||
Some(expr.name_for_alias()?) | ||
} else { | ||
None | ||
}; | ||
|
||
Ok(SavedName(original_name)) | ||
} | ||
} | ||
|
||
impl SavedName { | ||
/// Ensures the name of the rewritten expression is preserved | ||
pub fn restore(self, expr: Expr) -> Result<Expr> { | ||
let Self(original_name) = self; | ||
match original_name { | ||
Some(name) => expr.alias_if_changed(name), | ||
None => Ok(expr), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This test on the main branch gave the following error