gabotechs commented on code in PR #27832:
URL: https://github.com/apache/datafusion/pull/27832#discussion_r4736556449


##########
datafusion/physical-plan/src/filter.rs:
##########
@@ -1233,57 +1233,55 @@ fn holds_each_value_once(column: &ColumnStatistics, 
num_rows: &Precision<usize>)
     distinct.saturating_add(*nulls) >= *rows
 }
 
-/// Heuristic selectivity for predicates that fail interval analysis.
+/// Heuristic selectivity for conjuncts without an interval estimate.
 ///
-/// Splits the predicate into AND conjuncts. For each equality (`col = expr`)
-/// where at least one side is a [`Column`] with a known NDV, the selectivity
-/// contribution is `1 / NDV`. When both sides are columns with known NDV,
-/// `1 / max(left_NDV, right_NDV)` is used. All remaining conjuncts that
-/// cannot be estimated are covered by a single application of
-/// `default_selectivity`, preserving the pre-existing estimate for predicates
-/// that contain no recognizable equality.
+/// For string comparisons and `IN` lists on the same column, use the smallest
+/// estimate so their shared NULL rejection is counted once.
+/// For other equalities with a known column NDV, use `1 / NDV`, taking the
+/// larger NDV when both sides have one. Apply the default selectivity once
+/// for all conjuncts without an estimate.
 fn compute_fallback_selectivity(
-    predicate: &Arc<dyn PhysicalExpr>,
-    column_statistics: &[ColumnStatistics],
+    conjuncts: &[&Arc<dyn PhysicalExpr>],
+    schema: &Schema,
+    input_stats: &Statistics,
     default_selectivity: u8,
 ) -> f64 {
-    let conjuncts = split_conjunction(predicate);
+    let default_selectivity = default_selectivity as f64 / 100.0;
+    let mut column_selectivities = BTreeMap::<usize, f64>::new();

Review Comment:
   I guess `BTreeMap` is here so the product is taken in a stable order? If so, 
worth a few words, otherwise it reads like a `HashMap` would do.



##########
datafusion/physical-plan/src/filter.rs:
##########
@@ -1233,57 +1233,55 @@ fn holds_each_value_once(column: &ColumnStatistics, 
num_rows: &Precision<usize>)
     distinct.saturating_add(*nulls) >= *rows
 }
 
-/// Heuristic selectivity for predicates that fail interval analysis.
+/// Heuristic selectivity for conjuncts without an interval estimate.
 ///
-/// Splits the predicate into AND conjuncts. For each equality (`col = expr`)
-/// where at least one side is a [`Column`] with a known NDV, the selectivity
-/// contribution is `1 / NDV`. When both sides are columns with known NDV,
-/// `1 / max(left_NDV, right_NDV)` is used. All remaining conjuncts that
-/// cannot be estimated are covered by a single application of
-/// `default_selectivity`, preserving the pre-existing estimate for predicates
-/// that contain no recognizable equality.
+/// For string comparisons and `IN` lists on the same column, use the smallest
+/// estimate so their shared NULL rejection is counted once.
+/// For other equalities with a known column NDV, use `1 / NDV`, taking the
+/// larger NDV when both sides have one. Apply the default selectivity once
+/// for all conjuncts without an estimate.
 fn compute_fallback_selectivity(
-    predicate: &Arc<dyn PhysicalExpr>,
-    column_statistics: &[ColumnStatistics],
+    conjuncts: &[&Arc<dyn PhysicalExpr>],
+    schema: &Schema,
+    input_stats: &Statistics,
     default_selectivity: u8,
 ) -> f64 {
-    let conjuncts = split_conjunction(predicate);
+    let default_selectivity = default_selectivity as f64 / 100.0;
+    let mut column_selectivities = BTreeMap::<usize, f64>::new();
     let mut selectivity = 1.0;
     let mut has_unhandled = false;
 
     for expr in conjuncts {
-        let mut handled = false;
+        if let Some((column, estimate)) =
+            string_selectivity(expr, schema, input_stats, 
default_selectivity).or_else(
+                || in_list_selectivity(expr.downcast_ref::<InListExpr>()?, 
input_stats),
+            )
+        {
+            column_selectivities
+                .entry(column)
+                .and_modify(|current| *current = current.min(estimate))

Review Comment:
   Taking the minimum avoids counting the NULLs twice, but it also drops the 
extra restriction. In `test_filter_statistics_same_column_residuals`, `kind <> 
'A' AND kind NOT IN ('B', 'C')` estimates 400 rows, while excluding 3 of 10 
distinct values over 500 non-null rows gives 350.
   
   Would it work to multiply the non-null fractions per column and scale to 
non-null rows once? Fine as a follow-up too, but then I'd call the estimate 
conservative in the doc comment above.



##########
datafusion/physical-plan/src/filter.rs:
##########
@@ -390,14 +390,32 @@ impl FilterExec {
         } else {
             let null_rejecting_columns = 
collect_null_rejecting_columns(predicate);
 
-            let analysis_ctx = if check_statistics_support(predicate, schema)
-                && !has_decimal_equality(predicate, schema)
+            let supports_intervals = |expr: &Arc<dyn PhysicalExpr>| {
+                check_statistics_support(expr, schema)
+                    && !has_decimal_equality(expr, schema)
+            };
+            let (interval_predicate, residual) = if 
supports_intervals(predicate) {

Review Comment:
   When the same column shows up in both halves, e.g. `a > 5 AND a IN (1, 2, 
3)`, the interval estimate and the residual estimate are multiplied as if 
independent, so this contradiction gets a small non-zero estimate. That's 
acceptable since an AND still never raises the estimate, but a short note here 
would help the next reader see it's deliberate.



##########
datafusion/physical-plan/src/filter.rs:
##########
@@ -407,39 +425,20 @@ impl FilterExec {
             // aggregate output without min/max, as in `HAVING sum(l_quantity)
             // > 300` in TPC-H Q18. The heuristic estimate below is used then,
             // while the analyzed bounds still narrow the column statistics.
-            let selectivity = analysis_ctx
+            let (interval_selectivity, unestimated_conjuncts) = analysis_ctx

Review Comment:
   nit: the `map_or_else` over a tuple took me a moment. A `match` on 
`analysis_ctx.selectivity` would make it more obvious that a missing interval 
selectivity sends every conjunct to the fallback, not just the residual.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to