davidm-db commented on code in PR #49427:
URL: https://github.com/apache/spark/pull/49427#discussion_r1916427834


##########
sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionContext.scala:
##########
@@ -81,12 +107,79 @@ class SqlScriptingExecutionFrame(
       scopes.remove(scopes.length - 1)
     }
   }
+
+  def findHandler(condition: String, sqlState: String): 
Option[ErrorHandlerExec] = {
+    if (scopes.isEmpty) {
+      throw SparkException.internalError(s"Cannot find handler: no scopes.")
+    }
+
+    scopes.reverseIterator.foreach { scope =>
+      val handler = scope.findHandler(condition, sqlState)
+      if (handler.isDefined) {
+        return handler
+      }
+    }
+    None
+  }
 }
 
 /**
  * SQL scripting execution scope - keeps track of the current execution scope.
  *
  * @param label
  *   Label of the scope.
+ * @param conditionHandlerMap
+ *   Map holding condition/sqlState to handler mapping.
+ * @return
+ *   Handler for the given condition.
  */
-class SqlScriptingExecutionScope(val label: String)
+class SqlScriptingExecutionScope(
+    val label: String,
+    val conditionHandlerMap: HashMap[String, ErrorHandlerExec]) {
+
+  /**
+   * Finds the most appropriate error handler for exception based on its 
condition and SQL state.
+   *
+   * The method follows these rules to determine the most appropriate handler:
+   * 1. Specific named condition handlers (e.g., DIVIDE_BY_ZERO) are checked 
first.
+   * 2. If no specific condition handler is found, SQLSTATE handlers are 
checked.
+   * 3. For SQLSTATEs starting with '02', a generic NOT FOUND handler is used 
if available.
+   * 4. For other SQLSTATEs (except those starting with 'XX' or '02'), a 
generic SQLEXCEPTION
+   *    handler is used if available.
+   *
+   * Note: Handlers defined in the innermost compound statement where the 
exception was raised
+   * are considered.
+   *
+   * @param condition Error condition of the exception to find handler for.
+   * @param sqlState SQLSTATE of the exception to find handler for.
+   *
+   * @return Handler for the given condition if exists.
+   */
+  def findHandler(condition: String, sqlState: String): 
Option[ErrorHandlerExec] = {
+    // Check if there is a specific handler for the given condition.
+    conditionHandlerMap.get(condition)

Review Comment:
   Tagging @srielau to double check this, it's probably the most important 
thing from the standard perspective.
   There are usage examples in `SqlScriptingExecutionSuite.scala`.



-- 
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-unsubscr...@spark.apache.org

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


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

Reply via email to