nvander1 commented on a change in pull request #24761: [SPARK-27905] [SQL] Add
higher order function 'forall'
URL: https://github.com/apache/spark/pull/24761#discussion_r293119295
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/higherOrderFunctions.scala
##########
@@ -379,6 +379,31 @@ case class ArrayFilter(
override def prettyName: String = "filter"
}
+trait ArrayExistsForAllBase
+extends ArrayBasedSimpleHigherOrderFunction with CodegenFallback {
+ def check(cond: Boolean): Boolean
+
+ override def dataType: DataType = BooleanType
+ override def functionType: AbstractDataType = BooleanType
+
+ @transient lazy val LambdaFunction(_, Seq(elementVar: NamedLambdaVariable),
_) = function
+
+ override def nullSafeEval(inputRow: InternalRow, argumentValue: Any): Any = {
+ val arr = argumentValue.asInstanceOf[ArrayData]
+ val f = functionForEval
+ var continue = true
+ var i = 0
+ while (i < arr.numElements && continue) {
+ elementVar.value.set(arr.get(i, elementVar.dataType))
+ if (check(f.eval(inputRow).asInstanceOf[Boolean])) {
+ continue = !continue
+ }
+ i += 1
+ }
+ !check(continue)
+ }
Review comment:
The previous exists implementation:
```scala
override def nullSafeEval(inputRow: InternalRow, argumentValue: Any): Any
= {
val arr = argumentValue.asInstanceOf[ArrayData]
val f = functionForEval
var exists = false
var i = 0
while (i < arr.numElements && !exists) {
elementVar.value.set(arr.get(i, elementVar.dataType))
if (f.eval(inputRow).asInstanceOf[Boolean]) {
exists = true
}
i += 1
}
exists
}
```
Compare it to the first forall implementation I proposed:
```scala
override def nullSafeEval(inputRow: InternalRow, argumentValue: Any): Any
= {
val arr = argumentValue.asInstanceOf[ArrayData]
val f = functionForEval
var forall = true
var i = 0
while (i < arr.numElements && forall) {
elementVar.value.set(arr.get(i, elementVar.dataType))
if (!f.eval(inputRow).asInstanceOf[Boolean]) {
forall = false
}
i += 1
}
forall
}
The two implementaitons were really similar and were abstracted into the
current one:
```scala
override def nullSafeEval(inputRow: InternalRow, argumentValue: Any): Any
= {
val arr = argumentValue.asInstanceOf[ArrayData]
val f = functionForEval
var continue = true
var i = 0
while (i < arr.numElements && continue) {
elementVar.value.set(arr.get(i, elementVar.dataType))
if (check(f.eval(inputRow).asInstanceOf[Boolean])) {
continue = !continue
}
i += 1
}
!check(continue)
}
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]