-
Notifications
You must be signed in to change notification settings - Fork 2.1k
perf: Reorder predicates in conjuncts via simple heuristic #22343
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
Open
neilconway
wants to merge
6
commits into
apache:main
Choose a base branch
from
neilconway:neilc/perf-predicate-reorder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
52c5ad3
opt: reorder Filter conjuncts so cheap predicates evaluate first
neilconway c7523fd
Trim unnecessary unit test
neilconway 7284fe1
Tweak text
neilconway 40995f1
prettier
neilconway 74b99e3
.
neilconway 4a4528b
Update expected plan for TPC-H Q16
neilconway 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 hidden or 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 hidden or 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
190 changes: 190 additions & 0 deletions
190
datafusion/optimizer/src/simplify_expressions/reorder_predicates.rs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Reorder conjunctive (`AND`) predicates so that cheap predicates run before | ||
| //! expensive ones. | ||
| //! | ||
| //! DataFusion's `AND` evaluator short-circuits the right-hand side when the | ||
| //! left-hand side keeps few rows, so leading with a cheap predicate shrinks | ||
| //! the batch that expensive ones see. | ||
| //! | ||
| //! The cost of evaluating a predicate is assessed with a simple, conservative | ||
| //! heuristic: we define an allow-list of cheap operations, and consider an | ||
| //! expression to be cheap if it consists ONLY of cheap operations; everything | ||
| //! else is considered expensive. | ||
| //! | ||
| //! The sort is stable, so order within each class is preserved. | ||
|
|
||
| use datafusion_common::tree_node::TreeNode; | ||
| use datafusion_expr::{BinaryExpr, Expr, Operator}; | ||
|
|
||
| /// Stable partition of `predicates`: cheap first, then expensive. | ||
| /// | ||
| /// Returns `(predicates, changed)`. When `changed` is `false` the input was | ||
| /// already cheap-first and the caller can skip rebuilding the conjunction. | ||
| pub(crate) fn reorder_predicates(predicates: Vec<Expr>) -> (Vec<Expr>, bool) { | ||
| if predicates.len() <= 1 { | ||
| return (predicates, false); | ||
| } | ||
|
|
||
| // Volatile predicates may have observable side-effects and reordering | ||
| // conjuncts can change how many times they evaluate. Preserve user order | ||
| // if any predicate contains a volatile expression. | ||
| if predicates.iter().any(Expr::is_volatile) { | ||
| return (predicates, false); | ||
| } | ||
|
|
||
| let classes: Vec<bool> = predicates.iter().map(is_cheap_predicate).collect(); | ||
|
|
||
| // A reorder is needed iff an expensive predicate precedes a cheap one | ||
| let needs_reorder = classes.windows(2).any(|w| !w[0] && w[1]); | ||
| if !needs_reorder { | ||
| return (predicates, false); | ||
| } | ||
|
|
||
| let mut cheap = Vec::with_capacity(predicates.len()); | ||
| let mut expensive = Vec::new(); | ||
| for (p, is_cheap) in predicates.into_iter().zip(classes) { | ||
| if is_cheap { | ||
| cheap.push(p); | ||
| } else { | ||
| expensive.push(p); | ||
| } | ||
| } | ||
| cheap.extend(expensive); | ||
| (cheap, true) | ||
| } | ||
|
|
||
| /// Returns true if every node in `expr`'s tree is cheap. | ||
| fn is_cheap_predicate(expr: &Expr) -> bool { | ||
| !expr | ||
| .exists(|node| Ok(!is_cheap_node(node))) | ||
| .expect("is_cheap_node is infallible") | ||
| } | ||
|
|
||
| /// Returns true if `expr` is itself cheap. | ||
| /// | ||
| /// We use a simple, conservative heuristic to determine if an expression is | ||
| /// cheap to evaluate: we enumerate known-cheap operations (e.g., equality | ||
| /// comparisons, negations, casts), and consider anything outside this list to | ||
| /// be expensive. New/unrecognized expressions therefore default to being | ||
| /// expensive. | ||
| fn is_cheap_node(expr: &Expr) -> bool { | ||
| match expr { | ||
| // Direct reads and literals. | ||
| Expr::Column(_) | ||
| | Expr::Literal(_, _) | ||
| | Expr::ScalarVariable(_, _) | ||
| | Expr::Placeholder(_) | ||
| | Expr::OuterReferenceColumn(_, _) | ||
| | Expr::LambdaVariable(_) | ||
| // Wrappers; children are walked separately by `is_cheap_predicate`. | ||
| | Expr::Alias(_) | ||
| // Single-row unary predicates and arithmetic negation. | ||
| | Expr::Not(_) | ||
| | Expr::Negative(_) | ||
| | Expr::IsNull(_) | ||
| | Expr::IsNotNull(_) | ||
| | Expr::IsTrue(_) | ||
| | Expr::IsFalse(_) | ||
| | Expr::IsUnknown(_) | ||
| | Expr::IsNotTrue(_) | ||
| | Expr::IsNotFalse(_) | ||
| | Expr::IsNotUnknown(_) | ||
| // Composite cheap forms; child expressions are walked separately. | ||
| | Expr::Between(_) | ||
| | Expr::Case(_) | ||
| | Expr::Cast(_) | ||
| | Expr::TryCast(_) | ||
| | Expr::InList(_) => true, | ||
| // BinaryExpr is cheap unless the operator is LIKE or regexp matching. | ||
| Expr::BinaryExpr(BinaryExpr { op, .. }) => !matches!( | ||
| op, | ||
| Operator::LikeMatch | ||
| | Operator::ILikeMatch | ||
| | Operator::NotLikeMatch | ||
| | Operator::NotILikeMatch | ||
| | Operator::RegexMatch | ||
| | Operator::RegexIMatch | ||
| | Operator::RegexNotMatch | ||
| | Operator::RegexNotIMatch | ||
| ), | ||
| _ => false, | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use datafusion_expr::{col, lit}; | ||
|
|
||
| #[test] | ||
| fn like_predicate_moves_after_equality() { | ||
| let cheap = col("a").eq(lit(1)); | ||
| let expensive = col("b").like(lit("%foo%")); | ||
| let (out, changed) = reorder_predicates(vec![expensive.clone(), cheap.clone()]); | ||
| assert_eq!(out, vec![cheap, expensive]); | ||
| assert!(changed); | ||
| } | ||
|
|
||
| #[test] | ||
| fn order_among_cheap_predicates_is_preserved() { | ||
| let p1 = col("a").eq(lit(1)); | ||
| let p2 = col("b").eq(lit(2)); | ||
| let p3 = col("c").eq(lit(3)); | ||
| let input = vec![p1.clone(), p2.clone(), p3.clone()]; | ||
| let (out, changed) = reorder_predicates(input.clone()); | ||
| assert_eq!(out, input); | ||
| assert!(!changed); | ||
| } | ||
|
|
||
| #[test] | ||
| fn order_among_expensive_predicates_is_preserved() { | ||
| let p1 = col("a").like(lit("%a%")); | ||
| let p2 = Expr::BinaryExpr(BinaryExpr::new( | ||
| Box::new(col("b")), | ||
| Operator::RegexMatch, | ||
| Box::new(lit("foo")), | ||
| )); | ||
| let p3 = col("c").like(lit("%c%")); | ||
| let input = vec![p1.clone(), p2.clone(), p3.clone()]; | ||
| let (out, changed) = reorder_predicates(input.clone()); | ||
| assert_eq!(out, input); | ||
| assert!(!changed); | ||
| } | ||
|
|
||
| #[test] | ||
| fn already_cheap_first_reports_no_change() { | ||
| let cheap = col("a").eq(lit(1)); | ||
| let expensive = col("b").like(lit("%a%")); | ||
| let input = vec![cheap.clone(), expensive.clone()]; | ||
| let (out, changed) = reorder_predicates(input.clone()); | ||
| assert_eq!(out, input); | ||
| assert!(!changed); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nested_expensive_under_not_is_expensive() { | ||
| // The top node is `Not`, which is on the cheap allow-list. The walk | ||
| // must descend into the `Like` to flag this predicate as expensive. | ||
| let cheap = col("a").eq(lit(1)); | ||
| let nested = Expr::Not(Box::new(col("b").like(lit("%foo%")))); | ||
| let (out, changed) = reorder_predicates(vec![nested.clone(), cheap.clone()]); | ||
| assert_eq!(out, vec![cheap, nested]); | ||
| assert!(changed); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Unrelated to the core change but should save a few cycles.