cloud-fan commented on code in PR #49427:
URL: https://github.com/apache/spark/pull/49427#discussion_r1928128151


##########
sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingInterpreter.scala:
##########
@@ -63,13 +69,123 @@ case class SqlScriptingInterpreter(session: SparkSession) {
       case _ => None
     }
 
+  /**
+   * Transform [[CompoundBody]] into [[CompoundBodyExec]].
+ *
+   * @param compoundBody
+   *   CompoundBody to be transformed into CompoundBodyExec.
+   * @param args
+   *   A map of parameter names to SQL literal expressions.
+   * @param context
+   *   SqlScriptingExecutionContext keeps the execution state of current 
script.
+   * @return
+   *   Executable version of the CompoundBody .
+   */
+  private def transformBodyIntoExec(
+      compoundBody: CompoundBody,
+      args: Map[String, Expression],
+      context: SqlScriptingExecutionContext): CompoundBodyExec = {
+    // Add drop variables to the end of the body.
+    val variables = compoundBody.collection.flatMap {
+      case st: SingleStatement => getDeclareVarNameFromPlan(st.parsedPlan)
+      case _ => None
+    }
+    val dropVariables = variables
+      .map(varName => DropVariable(varName, ifExists = true))
+      .map(new SingleStatementExec(_, Origin(), args, isInternal = true, 
context))
+      .reverse
+
+    // Map of conditions to their respective handlers.
+    val conditionHandlerMap: HashMap[String, ErrorHandlerExec] = HashMap.empty
+    // Map of SqlStates to their respective handlers.
+    val sqlStateHandlerMap: HashMap[String, ErrorHandlerExec] = HashMap.empty
+    // NOT FOUND handler.
+    var notFoundHandler: Option[ErrorHandlerExec] = None
+    // Get SQLEXCEPTION handler.
+    var sqlExceptionHandler: Option[ErrorHandlerExec] = None
+
+    compoundBody.handlers.foreach(handler => {
+      val handlerBodyExec =
+        transformBodyIntoExec(
+          handler.body,
+          args,
+          context)
+
+      // Execution node of handler.
+      val handlerScopeLabel = if (handler.handlerType == HandlerType.EXIT) {
+        Some(compoundBody.label.get)
+      } else {
+        None
+      }
+
+      val handlerExec = new ErrorHandlerExec(
+        handlerBodyExec,
+        handler.handlerType,
+        handlerScopeLabel)
+
+      // For each condition handler is defined for, add corresponding key 
value pair
+      // to the conditionHandlerMap.
+      handler.handlerTriggers.conditions.foreach(condition => {
+        // Condition can either be the key in conditions map or SqlState.
+        if (conditionHandlerMap.contains(condition)) {
+          throw 
SqlScriptingErrors.duplicateHandlerForSameCondition(CurrentOrigin.get, 
condition)
+        } else {
+          conditionHandlerMap.addOne(condition, handlerExec)
+        }
+      })
+
+      // For each sqlState handler is defined for, add corresponding key value 
pair
+      // to the sqlStateHandlerMap.
+      handler.handlerTriggers.sqlStates.foreach(sqlState => {
+        // Condition can either be the key in conditions map or SqlState.

Review Comment:
   do we still need this comment?



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