miland-db commented on code in PR #49427: URL: https://github.com/apache/spark/pull/49427#discussion_r1916430493
########## 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: One thing missing from here is where to start the search from to respect this rule: - A condition handler cannot apply to any statement defined in its own body or the body of any condition handler declared in the same compound statement. -- 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