Github user viirya commented on the issue:

    https://github.com/apache/spark/pull/21405
  
    @mgaido91 Yes, as @rednaxelafx said in previous RP, #19813 will be the 
first use case. I'd like to use this API to transform statement-based 
expressions in code blocks.
    
    For example, `code` contains Java code of an expression. We find it is too 
long and we need to split it out as function. Because statement-based 
expressions can't be directly passed into. We need to transform them as 
variables first. I think the usage would looks like:
    
    ```scala
    val collectedStatements = HashMap[ExprValue, ExprValue]
    val transformed = code.transformExprValues {
      case s @ SimpleExprValue(_, javaType) =>
        if (collectedStatements.contains(s)) {
          collectedStatements(s)
        } else {
          val aliasedVariable = JavaCode.variable(ctx.freshName("var"), 
javaType)
          collectedStatements += s -> aliasedVariable
          aliasedVariable
        }
    }
    val createVariables = collectedStatements.foldLeft(EmptyBlock) { (block, 
(statement, variable)) =>
      block + code"${statement.javaType.getName} $variable = $statement;"
    }
    ev.copy(code = createVariables + transformed)
    ```


---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to