mbutrovich commented on code in PR #27742:
URL: https://github.com/apache/datafusion/pull/27742#discussion_r4700123715


##########
datafusion/optimizer/src/simplify_expressions/simplify_predicates.rs:
##########
@@ -238,6 +253,24 @@ fn simplify_conjunction(predicates: Vec<Expr>) -> 
Result<Vec<Expr>> {
     Ok(result)
 }
 
+/// A predicate kept as is, or the place of a column's simplified predicates.
+enum Slot {
+    Other(Expr),
+    Column(Column),
+}
+
+fn push_column_predicate(
+    column_predicates: &mut BTreeMap<Column, Vec<Expr>>,
+    slots: &mut Vec<Slot>,
+    col: Column,
+    predicate: Expr,
+) {
+    if !column_predicates.contains_key(&col) {
+        slots.push(Slot::Column(col.clone()));
+    }
+    column_predicates.entry(col).or_default().push(predicate);
+}

Review Comment:
   Could we do this without `Slot`? If the first pass keeps the predicates in a 
`Vec<Vec<Expr>>` in output order, with a `BTreeMap<Column, usize>` from each 
column to its group, every predicate is already in its final place. The second 
pass then only simplifies the column groups in place and flattens. The `expect` 
on line 240 goes away too, because there's no second map to keep in sync with 
the slots:
   
   ```rust
       let mut groups: Vec<Vec<Expr>> = Vec::with_capacity(predicates.len());
       let mut column_groups: BTreeMap<Column, usize> = BTreeMap::new();
   
       // the loop is unchanged, except `slots.push(Slot::Other(pred))` becomes
       // `groups.push(vec![pred])` and the helper takes `&mut column_groups, 
&mut groups`
   
       for i in column_groups.into_values() {
           let preds = std::mem::take(&mut groups[i]);
           let simplified = match simplify_column_predicates(preds.clone()) {
               Ok(simplified) => simplified,
               Err(_) => preds,
           };
           if simplified.iter().any(is_false) {
               return Ok(always_false());
           }
           groups[i] = simplified;
       }
   
       Ok(groups.into_iter().flatten().collect())
   }
   
   fn push_column_predicate(
       column_groups: &mut BTreeMap<Column, usize>,
       groups: &mut Vec<Vec<Expr>>,
       col: Column,
       predicate: Expr,
   ) {
       let i = *column_groups.entry(col).or_insert_with(|| {
           groups.push(vec![]);
           groups.len() - 1
       });
       groups[i].push(predicate);
   }
   ```
   
   The cost is one single-element `Vec` per conjunct that isn't a column 
comparison, once per filter at plan time.



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