alamb commented on code in PR #3868:
URL: https://github.com/apache/arrow-datafusion/pull/3868#discussion_r999908569
##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -640,6 +640,155 @@ impl PhysicalExpr for BinaryExpr {
self.evaluate_with_resolved_args(left, &left_data_type, right,
&right_data_type)
.map(|a| ColumnarValue::Array(a))
}
+
+ fn expr_stats(&self) -> Arc<dyn PhysicalExprStats> {
+ Arc::new(BinaryExprStats {
+ op: self.op,
+ left: Arc::clone(self.left()),
+ right: Arc::clone(self.right()),
+ })
+ }
+}
+
+struct BinaryExprStats {
+ op: Operator,
+ left: Arc<dyn PhysicalExpr>,
+ right: Arc<dyn PhysicalExpr>,
+}
+
+impl PhysicalExprStats for BinaryExprStats {
+ fn boundaries(&self, columns: &[ColumnStatistics]) ->
Option<ExprBoundaries> {
+ match &self.op {
+ Operator::Eq
+ | Operator::Gt
+ | Operator::Lt
+ | Operator::LtEq
+ | Operator::GtEq => {
+ let l_bounds = self.left.expr_stats().boundaries(columns)?;
+ let r_bounds = self.right.expr_stats().boundaries(columns)?;
+ match (l_bounds.reduce(), r_bounds.reduce()) {
+ (_, Some(r)) => compare_left_boundaries(&self.op,
&l_bounds, r),
+ (Some(scalar_value), _) => {
+ compare_left_boundaries(&self.op.swap()?, &r_bounds,
scalar_value)
+ }
+ _ => None,
+ }
+ }
+ _ => None,
+ }
+ }
+}
+
+// Compute the general selectivity of a comparison predicate (>, >=, <, <=)
between
Review Comment:
```suggestion
// Compute the bounds of a comparison predicate (>, >=, <, <=) between
```
I really like this framework @isidentical -- 👍 very nice
The algorithm is quite similar in spirit to what we have in the expr pruning
module (which given min/max values for columns from statistics will try and
figure out if a predicate is always false / none), though the implementation is
different.
https://github.com/apache/arrow-datafusion/blob/d2d8447/datafusion/core/src/physical_optimizer/pruning.rs#L441-L503
Longer term it would be great to figure out how to unify them (my preference
would be on top of this statistics framework rather than the somewhat mind
bending rewrite that occurs in pruning predicate)
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]