Skip to content

Commit 9cc981b

Browse files
authored
Minor: Simplify conjunction and disjunction, improve docs (#10446)
1 parent c7dbfeb commit 9cc981b

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

datafusion/expr/src/utils.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,20 +1107,49 @@ fn split_binary_impl<'a>(
11071107
/// assert_eq!(conjunction(split), Some(expr));
11081108
/// ```
11091109
pub fn conjunction(filters: impl IntoIterator<Item = Expr>) -> Option<Expr> {
1110-
filters.into_iter().reduce(|accum, expr| accum.and(expr))
1110+
filters.into_iter().reduce(Expr::and)
11111111
}
11121112

11131113
/// Combines an array of filter expressions into a single filter
11141114
/// expression consisting of the input filter expressions joined with
11151115
/// logical OR.
11161116
///
11171117
/// Returns None if the filters array is empty.
1118+
///
1119+
/// # Example
1120+
/// ```
1121+
/// # use datafusion_expr::{col, lit};
1122+
/// # use datafusion_expr::utils::disjunction;
1123+
/// // a=1 OR b=2
1124+
/// let expr = col("a").eq(lit(1)).or(col("b").eq(lit(2)));
1125+
///
1126+
/// // [a=1, b=2]
1127+
/// let split = vec![
1128+
/// col("a").eq(lit(1)),
1129+
/// col("b").eq(lit(2)),
1130+
/// ];
1131+
///
1132+
/// // use disjuncton to join them together with `OR`
1133+
/// assert_eq!(disjunction(split), Some(expr));
1134+
/// ```
11181135
pub fn disjunction(filters: impl IntoIterator<Item = Expr>) -> Option<Expr> {
1119-
filters.into_iter().reduce(|accum, expr| accum.or(expr))
1136+
filters.into_iter().reduce(Expr::or)
11201137
}
11211138

1122-
/// returns a new [LogicalPlan] that wraps `plan` in a [LogicalPlan::Filter] with
1123-
/// its predicate be all `predicates` ANDed.
1139+
/// Returns a new [LogicalPlan] that filters the output of `plan` with a
1140+
/// [LogicalPlan::Filter] with all `predicates` ANDed.
1141+
///
1142+
/// # Example
1143+
/// Before:
1144+
/// ```text
1145+
/// plan
1146+
/// ```
1147+
///
1148+
/// After:
1149+
/// ```text
1150+
/// Filter(predicate)
1151+
/// plan
1152+
/// ```
11241153
pub fn add_filter(plan: LogicalPlan, predicates: &[&Expr]) -> Result<LogicalPlan> {
11251154
// reduce filters to a single filter with an AND
11261155
let predicate = predicates

0 commit comments

Comments
 (0)