-
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
Improve Canonicalize API #8983
Merged
Merged
Improve Canonicalize API #8983
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e303dd7
Improve Canonicalize API
alamb eec2b83
Update datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
alamb e7095cd
Simplify the API
alamb 8ed5300
Merge remote-tracking branch 'apache/main' into alamb/canonicalize_api
alamb 1bb7d09
fix comment
alamb 7941576
add more flavor in comments
alamb d048fb7
Merge remote-tracking branch 'apache/main' into alamb/canonicalize_api
alamb d684dbf
Merge remote-tracking branch 'apache/main' into alamb/canonicalize_api
alamb 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,9 @@ pub struct ExprSimplifier<S> { | |
/// Guarantees about the values of columns. This is provided by the user | ||
/// in [ExprSimplifier::with_guarantees()]. | ||
guarantees: Vec<(Expr, NullableInterval)>, | ||
/// Should expressions be canonicalized before simplification? Defaults to | ||
/// true | ||
canonicalize: bool, | ||
} | ||
|
||
pub const THRESHOLD_INLINE_INLIST: usize = 3; | ||
|
@@ -70,6 +73,7 @@ impl<S: SimplifyInfo> ExprSimplifier<S> { | |
Self { | ||
info, | ||
guarantees: vec![], | ||
canonicalize: true, | ||
} | ||
} | ||
|
||
|
@@ -137,6 +141,12 @@ impl<S: SimplifyInfo> ExprSimplifier<S> { | |
let mut inlist_simplifier = InListSimplifier::new(); | ||
let mut guarantee_rewriter = GuaranteeRewriter::new(&self.guarantees); | ||
|
||
let expr = if self.canonicalize { | ||
expr.rewrite(&mut Canonicalizer::new())? | ||
} else { | ||
expr | ||
}; | ||
|
||
// TODO iterate until no changes are made during rewrite | ||
// (evaluating constants can enable new simplifications and | ||
// simplifications can enable new constant evaluation) | ||
|
@@ -151,10 +161,6 @@ impl<S: SimplifyInfo> ExprSimplifier<S> { | |
.rewrite(&mut simplifier) | ||
} | ||
|
||
pub fn canonicalize(&self, expr: Expr) -> Result<Expr> { | ||
let mut canonicalizer = Canonicalizer::new(); | ||
expr.rewrite(&mut canonicalizer) | ||
} | ||
/// Apply type coercion to an [`Expr`] so that it can be | ||
/// evaluated as a [`PhysicalExpr`](datafusion_physical_expr::PhysicalExpr). | ||
/// | ||
|
@@ -229,14 +235,68 @@ impl<S: SimplifyInfo> ExprSimplifier<S> { | |
self.guarantees = guarantees; | ||
self | ||
} | ||
|
||
/// Should [`Canonicalizer`] be applied before simplification? | ||
/// | ||
/// If true (the default), the expression will be rewritten to canonical | ||
/// form before simplification. This is useful to ensure that the simplifier | ||
/// can apply all possible simplifications. | ||
/// | ||
/// Some expressions, such as those in some Joins, can not be canonicalized | ||
/// without changing their meaning. In these cases, canonicalization should | ||
/// be disabled. | ||
/// | ||
/// ```rust | ||
/// use arrow::datatypes::{DataType, Field, Schema}; | ||
/// use datafusion_expr::{col, lit, Expr}; | ||
/// use datafusion_expr::interval_arithmetic::{Interval, NullableInterval}; | ||
/// use datafusion_common::{Result, ScalarValue, ToDFSchema}; | ||
/// use datafusion_physical_expr::execution_props::ExecutionProps; | ||
/// use datafusion_optimizer::simplify_expressions::{ | ||
/// ExprSimplifier, SimplifyContext}; | ||
/// | ||
/// let schema = Schema::new(vec![ | ||
/// Field::new("a", DataType::Int64, false), | ||
/// Field::new("b", DataType::Int64, false), | ||
/// Field::new("c", DataType::Int64, false), | ||
/// ]) | ||
/// .to_dfschema_ref().unwrap(); | ||
/// | ||
/// // Create the simplifier | ||
/// let props = ExecutionProps::new(); | ||
/// let context = SimplifyContext::new(&props) | ||
/// .with_schema(schema); | ||
/// let simplifier = ExprSimplifier::new(context); | ||
/// | ||
/// // Expression: a = c AND 1 = b | ||
/// let expr = col("a").eq(col("c")).and(lit(1).eq(col("b"))); | ||
/// | ||
/// // With canonicalization, the expression is rewritten to canonical form | ||
/// // (though it is no simpler in this case): | ||
/// let canonical = simplifier.simplify(expr.clone()).unwrap(); | ||
/// // Expression has been rewritten to: (c = a AND b = 1) | ||
/// assert_eq!(canonical, col("c").eq(col("a")).and(col("b").eq(lit(1)))); | ||
/// | ||
/// // If canonicalization is disabled, the expression is not changed | ||
/// let non_canonicalized = simplifier | ||
/// .with_canonicalize(false) | ||
/// .simplify(expr.clone()) | ||
/// .unwrap(); | ||
/// | ||
/// assert_eq!(non_canonicalized, expr); | ||
/// ``` | ||
pub fn with_canonicalize(mut self, canonicalize: bool) -> Self { | ||
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. Here is the new api that is part of the ExprSimplifer that mirrors the guarantees |
||
self.canonicalize = canonicalize; | ||
self | ||
} | ||
} | ||
|
||
/// Canonicalize any BinaryExprs that are not in canonical form | ||
/// | ||
/// `<literal> <op> <col>` is rewritten to `<col> <op> <literal>` | ||
/// | ||
/// `<col1> <op> <col2>` is rewritten so that the name of `col1` sorts higher | ||
/// than `col2` (`b > a` would be canonicalized to `a < b`) | ||
/// than `col2` (`a > b` would be canonicalized to `b < a`) | ||
struct Canonicalizer {} | ||
|
||
impl Canonicalizer { | ||
|
@@ -2889,8 +2949,7 @@ mod tests { | |
let simplifier = ExprSimplifier::new( | ||
SimplifyContext::new(&execution_props).with_schema(schema), | ||
); | ||
let cano = simplifier.canonicalize(expr)?; | ||
simplifier.simplify(cano) | ||
simplifier.simplify(expr) | ||
} | ||
|
||
fn simplify(expr: Expr) -> 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
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.
Oh, seeing this just made me realize the doc for Canonicalize struct actually states the opposite order:
https://github.com/apache/arrow-datafusion/blob/94a6192f6be30b7f6d009bc936a866bf5dcb280c/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs#L239
Probably can fix as part of this PR?
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.
Aha, that's my previous fault.
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.
fixed in 1bb7d09