jayzhan211 commented on code in PR #27319:
URL: https://github.com/apache/datafusion/pull/27319#discussion_r4652554766


##########
datafusion/physical-expr/src/equivalence/class.rs:
##########
@@ -673,7 +685,25 @@ impl EquivalenceGroup {
                 return Some(Arc::clone(target));
             }
         }
-        None
+        // Members of a class are equal on every row, so a projectable member
+        // stands in for `expr`.
+        let group = equivalents?;
+        if let Some(known) = tried.get(expr) {
+            return known.clone();
+        }
+        // Until `expr` has a result, `None` stops cycles such as `x = f(y)`,
+        // `y = g(x)`. The stored result then serves later uses of `expr`, such
+        // as the second `x` in `x * x`.
+        tried.insert(Arc::clone(expr), None);
+        let projected = group.get_equivalence_class(expr).and_then(|cls| {
+            cls.iter()
+                .filter(|member| *member != expr)
+                .find_map(|member| {
+                    Self::project_expr_indirect(aug_mapping, member, 
equivalents, tried)
+                })
+        });
+        tried.insert(Arc::clone(expr), projected.clone());
+        projected

Review Comment:
   @cetra3 thanks for the update, and sorry: the `tried` shape from my patch 
makes `project()` cubic in class size, because each dropped member of a class 
searches the whole class again. EXPLAIN on `SELECT c0 FROM w WHERE c1 = c2 AND 
c2 = c3 AND ...` goes from 26 to 99 ms with 64 equated columns and from 101 to 
684 ms with 128 (debug build). Keying `tried` by class index brings it to 30 / 
114 ms, and the PR's tests, the full slt suite and clippy pass with it:
   
   ```diff
   @@ fn project_expr_indirect
        /// If `equivalents` is given and `expr` still cannot be projected, this
        /// projects another member of the class of `expr` in that group. 
`tried`
   -    /// holds the result of that step for each expression, for one top-level
   -    /// call.
   +    /// holds the result of that step for each class, keyed by its index in
   +    /// `equivalents`, for one top-level call.
        fn project_expr_indirect(
            aug_mapping: &AugmentedMapping,
            expr: &Arc<dyn PhysicalExpr>,
            equivalents: Option<&Self>,
   -        tried: &mut HashMap<Arc<dyn PhysicalExpr>, Option<Arc<dyn 
PhysicalExpr>>>,
   +        tried: &mut HashMap<usize, Option<Arc<dyn PhysicalExpr>>>,
        ) -> Option<Arc<dyn PhysicalExpr>> {
   @@
            let group = equivalents?;
   -        if let Some(known) = tried.get(expr) {
   +        let &idx = group.map.get(expr)?;
   +        if let Some(known) = tried.get(&idx) {
                return known.clone();
            }
   -        // Until `expr` has a result, `None` stops cycles such as `x = 
f(y)`,
   -        // `y = g(x)`. The stored result then serves later uses of `expr`, 
such
   -        // as the second `x` in `x * x`.
   -        tried.insert(Arc::clone(expr), None);
   -        let projected = group.get_equivalence_class(expr).and_then(|cls| {
   -            cls.iter()
   -                .filter(|member| *member != expr)
   -                .find_map(|member| {
   -                    Self::project_expr_indirect(aug_mapping, member, 
equivalents, tried)
   -                })
   -        });
   -        tried.insert(Arc::clone(expr), projected.clone());
   +        // Until the class has a result, `None` stops cycles such as `x = 
f(y)`,
   +        // `y = g(x)`. The stored result then serves every later member of 
the
   +        // class, such as the second `x` in `x * x`.
   +        tried.insert(idx, None);
   +        let projected = group.classes[idx]
   +            .iter()
   +            .filter(|member| *member != expr)
   +            .find_map(|member| {
   +                Self::project_expr_indirect(aug_mapping, member, 
equivalents, tried)
   +            });
   +        tried.insert(idx, projected.clone());
            projected
   ```
   
   <details>
   <summary>Timings, and why the map stays per top-level call</summary>
   
   EXPLAIN of `SELECT c0 FROM w WHERE c1 = c2 AND ... AND c{n-1} = c{n}`, debug 
`datafusion-cli`, best of 3:
   
   | n | main | this PR | class-keyed `tried` |
   |---|---|---|---|
   | 8 | 2 ms | 3 ms | 3 ms |
   | 16 | 5 ms | 6 ms | 5 ms |
   | 32 | 9 ms | 19 ms | 11 ms |
   | 64 | 26 ms | 99 ms | 30 ms |
   | 128 | 101 ms | 684 ms | 114 ms |
   
   Sharing one `tried` across a whole `project_expressions` call would be 
linear, but a `None` stored while another class was still in progress then 
sticks. With `a = b + 1 = d * 2`, `b = a - 1` and a projection that keeps only 
`d`, `project_expressions([a, b])` gives `b = None`, while this PR and the 
per-call version give `d * 2 - 1`.
   
   </details>



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