You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/11/28 13:18:19 UTC
[GitHub] [arrow-datafusion] alamb opened a new pull request #1375: (WIP) Consolidate ConstantFolding and SimplifyExpression
alamb opened a new pull request #1375:
URL: https://github.com/apache/arrow-datafusion/pull/1375
# Which issue does this PR close?
Builds on https://github.com/apache/arrow-datafusion/pull/1374
Re https://github.com/apache/arrow-datafusion/issues/1160 (more sophisticated expression simplification)
# Rationale for this change
`ConstantFolding` and `SimplifyExpression` both simplify expressions, in slightly different ways, which is confusing and redundant. It also means bug fixes such as #1319 didn't get applied to `SimplifyExpression`.
# What changes are included in this PR?
1. Combine `ConstantFolding` into `SimplifyExpression` optimizer pass
2. Move tests
Note:
I plan one final PR to combine `simplify` and `Simplifier` logic but to keep reviews easier I am going to do that in another PR
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on a change in pull request #1375: Consolidate `ConstantFolding` and `SimplifyExpression`
Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #1375:
URL: https://github.com/apache/arrow-datafusion/pull/1375#discussion_r759223686
##########
File path: datafusion/src/execution/context.rs
##########
@@ -896,12 +895,13 @@ impl Default for ExecutionConfig {
target_partitions: num_cpus::get(),
batch_size: 8192,
optimizers: vec![
- Arc::new(ConstantFolding::new()),
+ // Simplify expressions first to maximize the chance
+ // of applying other optimizations
+ Arc::new(SimplifyExpressions::new()),
Review comment:
This change has the additional benefit of allowing even more filters to be pushed down (e.g. `column * 1 < 5` which is simplified by the code in `simpify` to `column < 5` can now also be pushed down
##########
File path: datafusion/src/execution/context.rs
##########
@@ -896,12 +895,13 @@ impl Default for ExecutionConfig {
target_partitions: num_cpus::get(),
batch_size: 8192,
optimizers: vec![
- Arc::new(ConstantFolding::new()),
+ // Simplify expressions first to maximize the chance
+ // of applying other optimizations
+ Arc::new(SimplifyExpressions::new()),
Review comment:
This change has the additional benefit of allowing even more filters to be pushed down (e.g. `column * 1 < 5` which is simplified by the code in `simplify` to `column < 5` can now also be pushed down
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on a change in pull request #1375: Consolidate `ConstantFolding` and `SimplifyExpression`
Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #1375:
URL: https://github.com/apache/arrow-datafusion/pull/1375#discussion_r759201211
##########
File path: datafusion/src/optimizer/simplify_expressions.rs
##########
@@ -284,9 +274,62 @@ impl OptimizerRule for SimplifyExpressions {
fn optimize(
&self,
plan: &LogicalPlan,
- _execution_props: &ExecutionProps,
+ execution_props: &ExecutionProps,
) -> Result<LogicalPlan> {
- optimize(plan)
+ // We need to pass down the all schemas within the plan tree to `optimize_expr` in order to
+ // to evaluate expression types. For example, a projection plan's schema will only include
+ // projected columns. With just the projected schema, it's not possible to infer types for
+ // expressions that references non-projected columns within the same project plan or its
+ // children plans.
+ let mut simplifier =
Review comment:
this logic is moved from ConstantFolding
##########
File path: datafusion/src/optimizer/simplify_expressions.rs
##########
@@ -284,9 +274,62 @@ impl OptimizerRule for SimplifyExpressions {
fn optimize(
&self,
plan: &LogicalPlan,
- _execution_props: &ExecutionProps,
+ execution_props: &ExecutionProps,
) -> Result<LogicalPlan> {
- optimize(plan)
+ // We need to pass down the all schemas within the plan tree to `optimize_expr` in order to
+ // to evaluate expression types. For example, a projection plan's schema will only include
+ // projected columns. With just the projected schema, it's not possible to infer types for
+ // expressions that references non-projected columns within the same project plan or its
+ // children plans.
+ let mut simplifier =
+ super::simplify_expressions::Simplifier::new(plan.all_schemas());
+
+ let mut const_evaluator =
+ super::simplify_expressions::ConstEvaluator::new(execution_props);
+
+ let new_inputs = plan
+ .inputs()
+ .iter()
+ .map(|input| self.optimize(input, execution_props))
+ .collect::<Result<Vec<_>>>()?;
+
+ let expr = plan
+ .expressions()
+ .into_iter()
+ .map(|e| {
+ // We need to keep original expression name, if any.
+ // Constant folding should not change expression name.
+ let name = &e.name(plan.schema());
+
+ // TODO combine simplify into Simplifier
+ let e = simplify(&e);
+
+ // TODO iterate until no changes are made
+ // during rewrite (evaluating constants can
+ // enable new simplifications and
+ // simplifications can enable new constant
+ // evaluation)
+ let new_e = e
+ // fold constants and then simplify
+ .rewrite(&mut const_evaluator)?
+ .rewrite(&mut simplifier)?;
+
+ let new_name = &new_e.name(plan.schema());
+
+ // TODO simplify this logic
+ if let (Ok(expr_name), Ok(new_expr_name)) = (name, new_name) {
Review comment:
Note that now the fix for alias names (introduced by @viirya in https://github.com/apache/arrow-datafusion/pull/1319) is applied to the result of `simplify` as well -- previously it was not
##########
File path: datafusion/src/optimizer/simplify_expressions.rs
##########
@@ -284,9 +274,62 @@ impl OptimizerRule for SimplifyExpressions {
fn optimize(
&self,
plan: &LogicalPlan,
- _execution_props: &ExecutionProps,
+ execution_props: &ExecutionProps,
) -> Result<LogicalPlan> {
- optimize(plan)
+ // We need to pass down the all schemas within the plan tree to `optimize_expr` in order to
+ // to evaluate expression types. For example, a projection plan's schema will only include
+ // projected columns. With just the projected schema, it's not possible to infer types for
+ // expressions that references non-projected columns within the same project plan or its
+ // children plans.
+ let mut simplifier =
+ super::simplify_expressions::Simplifier::new(plan.all_schemas());
+
+ let mut const_evaluator =
+ super::simplify_expressions::ConstEvaluator::new(execution_props);
+
+ let new_inputs = plan
+ .inputs()
+ .iter()
+ .map(|input| self.optimize(input, execution_props))
+ .collect::<Result<Vec<_>>>()?;
+
+ let expr = plan
+ .expressions()
+ .into_iter()
+ .map(|e| {
+ // We need to keep original expression name, if any.
+ // Constant folding should not change expression name.
+ let name = &e.name(plan.schema());
+
+ // TODO combine simplify into Simplifier
+ let e = simplify(&e);
Review comment:
Here is the call into the existing `simplify` function that is in the `SimplifyExpressions` pass (originally written by @jgoday)
##########
File path: datafusion/src/optimizer/simplify_expressions.rs
##########
@@ -1562,4 +1556,388 @@ mod tests {
.unwrap(),
)
}
+
Review comment:
these tests are just moved from `constant_folding.rs`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on pull request #1375: Consolidate `ConstantFolding` and `SimplifyExpression`
Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #1375:
URL: https://github.com/apache/arrow-datafusion/pull/1375#issuecomment-982578141
The new tpch query test added in #1368 by @viirya is already coming in handy to prevent regressions -- it found a [problem](https://github.com/apache/arrow-datafusion/runs/4366849992?check_suite_focus=true) with an earlier version of this pr where simplification wasn't done until after the other optimizations. ❤️
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on a change in pull request #1375: Consolidate `ConstantFolding` and `SimplifyExpression`
Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #1375:
URL: https://github.com/apache/arrow-datafusion/pull/1375#discussion_r759205639
##########
File path: datafusion/src/optimizer/simplify_expressions.rs
##########
@@ -284,9 +274,62 @@ impl OptimizerRule for SimplifyExpressions {
fn optimize(
&self,
plan: &LogicalPlan,
- _execution_props: &ExecutionProps,
+ execution_props: &ExecutionProps,
) -> Result<LogicalPlan> {
- optimize(plan)
+ // We need to pass down the all schemas within the plan tree to `optimize_expr` in order to
+ // to evaluate expression types. For example, a projection plan's schema will only include
+ // projected columns. With just the projected schema, it's not possible to infer types for
+ // expressions that references non-projected columns within the same project plan or its
+ // children plans.
+ let mut simplifier =
+ super::simplify_expressions::Simplifier::new(plan.all_schemas());
+
+ let mut const_evaluator =
+ super::simplify_expressions::ConstEvaluator::new(execution_props);
+
+ let new_inputs = plan
+ .inputs()
+ .iter()
+ .map(|input| self.optimize(input, execution_props))
+ .collect::<Result<Vec<_>>>()?;
+
+ let expr = plan
+ .expressions()
+ .into_iter()
+ .map(|e| {
+ // We need to keep original expression name, if any.
+ // Constant folding should not change expression name.
+ let name = &e.name(plan.schema());
+
+ // TODO combine simplify into Simplifier
+ let e = simplify(&e);
+
+ // TODO iterate until no changes are made
+ // during rewrite (evaluating constants can
+ // enable new simplifications and
+ // simplifications can enable new constant
+ // evaluation)
+ let new_e = e
+ // fold constants and then simplify
+ .rewrite(&mut const_evaluator)?
+ .rewrite(&mut simplifier)?;
+
+ let new_name = &new_e.name(plan.schema());
+
+ // TODO simplify this logic
+ if let (Ok(expr_name), Ok(new_expr_name)) = (name, new_name) {
Review comment:
Note that now the fix for alias names (introduced by @viirya in https://github.com/apache/arrow-datafusion/pull/1319) is applied to the result of `simplify` as well -- previously it was not 🎉
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb merged pull request #1375: Consolidate `ConstantFolding` and `SimplifyExpression`
Posted by GitBox <gi...@apache.org>.
alamb merged pull request #1375:
URL: https://github.com/apache/arrow-datafusion/pull/1375
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org