This is an automated email from the ASF dual-hosted git repository.
jonah pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new e60318553d Reuse `NamePreserver` in `SimplifyExpressions` (#12238)
e60318553d is described below
commit e60318553d6ac36b2d07466acebef861fde2936e
Author: Jonah Gao <[email protected]>
AuthorDate: Fri Aug 30 14:08:29 2024 +0800
Reuse `NamePreserver` in `SimplifyExpressions` (#12238)
---
datafusion/expr/src/expr_rewriter/mod.rs | 2 ++
.../src/simplify_expressions/simplify_exprs.rs | 18 +++++++-----------
2 files changed, 9 insertions(+), 11 deletions(-)
diff --git a/datafusion/expr/src/expr_rewriter/mod.rs
b/datafusion/expr/src/expr_rewriter/mod.rs
index 61b0f6d9bb..35c5738364 100644
--- a/datafusion/expr/src/expr_rewriter/mod.rs
+++ b/datafusion/expr/src/expr_rewriter/mod.rs
@@ -311,6 +311,8 @@ impl NamePreserver {
/// Create a new NamePreserver for rewriting the `expr` that is part of
the specified plan
pub fn new(plan: &LogicalPlan) -> Self {
Self {
+ // The schema of Filter and Join nodes comes from their inputs
rather than their output expressions,
+ // so there is no need to use aliases to preserve expression names.
use_alias: !matches!(plan, LogicalPlan::Filter(_) |
LogicalPlan::Join(_)),
}
}
diff --git a/datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs
b/datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs
index e13ce382a3..1c39313ea1 100644
--- a/datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs
+++ b/datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs
@@ -27,6 +27,7 @@ use datafusion_expr::simplify::SimplifyContext;
use datafusion_expr::utils::merge_schema;
use crate::optimizer::ApplyOrder;
+use crate::utils::NamePreserver;
use crate::{OptimizerConfig, OptimizerRule};
use super::ExprSimplifier;
@@ -119,18 +120,13 @@ impl SimplifyExpressions {
simplifier
};
- // the output schema of a filter or join is the input schema. Thus they
- // can't handle aliased expressions
- let use_alias = !matches!(plan, LogicalPlan::Filter(_) |
LogicalPlan::Join(_));
+ // Preserve expression names to avoid changing the schema of the plan.
+ let name_preserver = NamePreserver::new(&plan);
plan.map_expressions(|e| {
- let new_e = if use_alias {
- // TODO: unify with `rewrite_preserving_name`
- let original_name = e.name_for_alias()?;
- simplifier.simplify(e)?.alias_if_changed(original_name)
- } else {
- simplifier.simplify(e)
- }?;
-
+ let original_name = name_preserver.save(&e)?;
+ let new_e = simplifier
+ .simplify(e)
+ .and_then(|expr| original_name.restore(expr))?;
// TODO it would be nice to have a way to know if the expression
was simplified
// or not. For now conservatively return Transformed::yes
Ok(Transformed::yes(new_e))
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]