Kimahriman commented on code in PR #34558:
URL: https://github.com/apache/spark/pull/34558#discussion_r1157743684


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeGenerator.scala:
##########
@@ -172,6 +172,40 @@ class CodegenContext extends Logging {
    */
   var currentVars: Seq[ExprCode] = null
 
+  /**
+   * Holding a map of current lambda variables.
+   */
+  var currentLambdaVars: mutable.Map[String, ExprCode] = mutable.HashMap.empty
+
+  def withLambdaVars(namedLambdas: Seq[NamedLambdaVariable],
+      f: Seq[ExprCode] => ExprCode): ExprCode = {
+    val lambdaVars = namedLambdas.map { namedLambda =>
+      val name = namedLambda.variableName
+      if (currentLambdaVars.get(name).nonEmpty) {
+        throw QueryExecutionErrors.lambdaVariableAlreadyDefinedError(name)
+      }
+      val isNull = if (namedLambda.nullable) {
+        JavaCode.isNullGlobal(addMutableState(JAVA_BOOLEAN, "lambdaIsNull"))
+      } else {
+        FalseLiteral
+      }
+      val value = addMutableState(javaType(namedLambda.dataType), 
"lambdaValue")
+      val lambdaVar = ExprCode(isNull, JavaCode.global(value, 
namedLambda.dataType))
+      currentLambdaVars.put(name, lambdaVar)
+      lambdaVar
+    }
+
+    val result = f(lambdaVars)
+    namedLambdas.foreach(v => currentLambdaVars.remove(v.variableName))

Review Comment:
   Yeah I never know all the exact ways that using `_` does and doesn't work 
when you use a function call, struggle with the cleanest way to write things 
like this. I like the second one I think



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeGenerator.scala:
##########
@@ -172,6 +172,40 @@ class CodegenContext extends Logging {
    */
   var currentVars: Seq[ExprCode] = null
 
+  /**
+   * Holding a map of current lambda variables.
+   */
+  var currentLambdaVars: mutable.Map[String, ExprCode] = mutable.HashMap.empty
+
+  def withLambdaVars(namedLambdas: Seq[NamedLambdaVariable],
+      f: Seq[ExprCode] => ExprCode): ExprCode = {
+    val lambdaVars = namedLambdas.map { namedLambda =>
+      val name = namedLambda.variableName
+      if (currentLambdaVars.get(name).nonEmpty) {
+        throw QueryExecutionErrors.lambdaVariableAlreadyDefinedError(name)
+      }
+      val isNull = if (namedLambda.nullable) {
+        JavaCode.isNullGlobal(addMutableState(JAVA_BOOLEAN, "lambdaIsNull"))
+      } else {
+        FalseLiteral
+      }
+      val value = addMutableState(javaType(namedLambda.dataType), 
"lambdaValue")
+      val lambdaVar = ExprCode(isNull, JavaCode.global(value, 
namedLambda.dataType))
+      currentLambdaVars.put(name, lambdaVar)
+      lambdaVar
+    }
+
+    val result = f(lambdaVars)
+    namedLambdas.foreach(v => currentLambdaVars.remove(v.variableName))
+    result
+  }
+
+  def getLambdaVar(name: String): ExprCode = {
+    currentLambdaVars.getOrElse(name, {

Review Comment:
   I saw this format in other parts of the code, but I also have seen it 
without, so can remove



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