davidm-db commented on code in PR #49427: URL: https://github.com/apache/spark/pull/49427#discussion_r1925131311
########## sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionContext.scala: ########## @@ -50,12 +51,24 @@ class SqlScriptingExecutionContext { throw SparkException.internalError(s"Cannot find handler: no frames.") } - frames.reverseIterator.foreach { frame => - val handler = frame.findHandler(condition, sqlState) + // If the last frame is a handler, try to find a handler in it first. + if (frames.last.frameType == SqlScriptingFrameType.HANDLER) { + val handler = frames.last.findHandler(condition, sqlState, firstHandlerScope) if (handler.isDefined) { return handler } } + + frames.reverseIterator.foreach { frame => + // Skip the current frame if it is a handler. + if (frame.frameType != SqlScriptingFrameType.HANDLER) { + val handler = frame.findHandler(condition, sqlState, firstHandlerScope) + if (handler.isDefined) { + firstHandlerScope = handler.get.scopeLabel + return handler + } + } + } Review Comment: agreed offline on how to refactor this, please don't forget to add a short TODO comment to explain what should be done when Stored Procedures are introduced. ########## sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionContext.scala: ########## @@ -50,12 +51,24 @@ class SqlScriptingExecutionContext { throw SparkException.internalError(s"Cannot find handler: no frames.") } - frames.reverseIterator.foreach { frame => - val handler = frame.findHandler(condition, sqlState) + // If the last frame is a handler, try to find a handler in it first. + if (frames.last.frameType == SqlScriptingFrameType.HANDLER) { + val handler = frames.last.findHandler(condition, sqlState, firstHandlerScope) if (handler.isDefined) { return handler } } + + frames.reverseIterator.foreach { frame => + // Skip the current frame if it is a handler. + if (frame.frameType != SqlScriptingFrameType.HANDLER) { + val handler = frame.findHandler(condition, sqlState, firstHandlerScope) + if (handler.isDefined) { + firstHandlerScope = handler.get.scopeLabel + return handler + } + } + } Review Comment: agreed offline on how to refactor this, please don't forget to add a short TODO comment to explain what should be done when Stored Procedures are introduced -- 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