You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/03/09 04:33:10 UTC

[GitHub] [spark] LuciferYang commented on a change in pull request #35694: [SPARK-38360][SQL][SS][PYTHON] Introduce a `exists` function for `TreeNode` to eliminate duplicate code patterns

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
   ```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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org