LuciferYang commented on a change in pull request #35694:
URL: https://github.com/apache/spark/pull/35694#discussion_r822278258



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala
##########
@@ -246,6 +246,16 @@ abstract class TreeNode[BaseType <: TreeNode[BaseType]] 
extends Product with Tre
     children.foldLeft(Option.empty[BaseType]) { (l, r) => l.orElse(r.find(f)) }
   }
 
+  /**
+   * Test whether there is [[TreeNode]] satisfies the conditions specified in 
`f`.
+   * The condition is recursively applied to this node and all of its children 
(pre-order).
+   */
+  def exists(f: BaseType => Boolean): Boolean = if (f(this)) {

Review comment:
       Like the following?
   
   ```scala
   def exists(pf: PartialFunction[BaseType, Boolean]): Boolean = {
       if (pf.applyOrElse(this, { _: Any => false})) {
         true
       } else {
         children.exists(c => pf.applyOrElse(c, { _: Any => false}))
       }
     }
   ```
   
   This implementation is more like `PartialFunction#cond` as follows:
   
   ```scala
    /** Creates a Boolean test based on a value and a partial function.
      *  It behaves like a 'match' statement with an implied 'case _ => false'
      *  following the supplied cases.
      *
      *  @param  x   the value to test
      *  @param  pf  the partial function
      *  @return true, iff `x` is in the domain of `pf` and `pf(x) == true`.
      */
     def cond[T](x: T)(pf: PartialFunction[T, Boolean]): Boolean = 
pf.applyOrElse(x, constFalse)
   ```
   
   This requires the assumption that `case _ => false`
   
   




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