-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Minor: Simplify conjunction and disjunction, improve docs #10446
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1107,20 +1107,49 @@ fn split_binary_impl<'a>( | |
/// assert_eq!(conjunction(split), Some(expr)); | ||
/// ``` | ||
pub fn conjunction(filters: impl IntoIterator<Item = Expr>) -> Option<Expr> { | ||
filters.into_iter().reduce(|accum, expr| accum.and(expr)) | ||
filters.into_iter().reduce(Expr::and) | ||
} | ||
|
||
/// Combines an array of filter expressions into a single filter | ||
/// expression consisting of the input filter expressions joined with | ||
/// logical OR. | ||
/// | ||
/// Returns None if the filters array is empty. | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// # use datafusion_expr::{col, lit}; | ||
/// # use datafusion_expr::utils::disjunction; | ||
/// // a=1 OR b=2 | ||
/// let expr = col("a").eq(lit(1)).or(col("b").eq(lit(2))); | ||
/// | ||
/// // [a=1, b=2] | ||
/// let split = vec![ | ||
/// col("a").eq(lit(1)), | ||
/// col("b").eq(lit(2)), | ||
/// ]; | ||
/// | ||
/// // use disjuncton to join them together with `OR` | ||
/// assert_eq!(disjunction(split), Some(expr)); | ||
/// ``` | ||
pub fn disjunction(filters: impl IntoIterator<Item = Expr>) -> Option<Expr> { | ||
filters.into_iter().reduce(|accum, expr| accum.or(expr)) | ||
filters.into_iter().reduce(Expr::or) | ||
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. Wow, surprise that this works 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. Yeah, that was my first reaction when I saw this in Filter pushdown and it took me a while to figure out what was actually doing 🤯 |
||
} | ||
|
||
/// returns a new [LogicalPlan] that wraps `plan` in a [LogicalPlan::Filter] with | ||
/// its predicate be all `predicates` ANDed. | ||
/// Returns a new [LogicalPlan] that filters the output of `plan` with a | ||
/// [LogicalPlan::Filter] with all `predicates` ANDed. | ||
/// | ||
/// # Example | ||
/// Before: | ||
/// ```text | ||
/// plan | ||
/// ``` | ||
/// | ||
/// After: | ||
/// ```text | ||
/// Filter(predicate) | ||
/// plan | ||
/// ``` | ||
pub fn add_filter(plan: LogicalPlan, predicates: &[&Expr]) -> Result<LogicalPlan> { | ||
// reduce filters to a single filter with an AND | ||
let predicate = predicates | ||
|
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 is the very minor simplification and while I was in here I figured I would improve the docs too