[ 
https://issues.apache.org/jira/browse/SPARK-53758?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18023827#comment-18023827
 ] 

David Milicevic edited comment on SPARK-53758 at 9/30/25 2:30 PM:
------------------------------------------------------------------

The issue is originating from `SqlScriptingExecution#getNextStatement`, more 
specifically from this piece of code:
{code:java}
      // If the last frame is a handler, set leave statement to be the next one 
in the
      // innermost scope that should be exited.
      if (lastFrame.frameType == SqlScriptingFrameType.HANDLER && 
context.frames.nonEmpty) {
        // Remove the scope if handler is executed.
        if (context.firstHandlerScopeLabel.isDefined
          && lastFrame.scopeLabel.get == context.firstHandlerScopeLabel.get) {
          context.firstHandlerScopeLabel = None
        }
        // Inject leave statement into the execution plan of the last frame.
        injectLeaveStatement(context.frames.last.executionPlan, 
lastFrame.scopeLabel.get)
      }{code}
 

The handlers are properly found in `findHandler` and properly added to the call 
stack. In this case, we will have call stack looking like SQL_SCRIPT -> HANDLER 
-> HANDLER, which is fine. However, when the last handler is exited, we are 
injecting LEAVE statements in SQL_SCRIPT and the other HANDLER. Now, this would 
be fine, except with the current implementation, when the other HANDLER is 
exited, the LEAVE statement would get overridden with different label.


was (Author: JIRAUSER304692):
The issue is originating from `SqlScriptingExecution#getNextStatement`, more 
specifically from this piece of code:

 
{code:java}
      // If the last frame is a handler, set leave statement to be the next one 
in the
      // innermost scope that should be exited.
      if (lastFrame.frameType == SqlScriptingFrameType.HANDLER && 
context.frames.nonEmpty) {
        // Remove the scope if handler is executed.
        if (context.firstHandlerScopeLabel.isDefined
          && lastFrame.scopeLabel.get == context.firstHandlerScopeLabel.get) {
          context.firstHandlerScopeLabel = None
        }
        // Inject leave statement into the execution plan of the last frame.
        injectLeaveStatement(context.frames.last.executionPlan, 
lastFrame.scopeLabel.get)
      }{code}
 

> EXIT handler not exiting properly if triggered from another EXIT handler
> ------------------------------------------------------------------------
>
>                 Key: SPARK-53758
>                 URL: https://issues.apache.org/jira/browse/SPARK-53758
>             Project: Spark
>          Issue Type: Bug
>          Components: Spark Core
>    Affects Versions: 4.1.0, 4.0.1
>            Reporter: Teodor
>            Priority: Major
>
> An unexpected behavior happens when an EXIT handler inside of an outer scope 
> gets triggered by an exception inside an EXIT handler thats inside an inner 
> nested scope: instead of leaving the outer scope after finishing all the 
> exception handling, execution only leaves the inner scope.
> Example:
> {code:java}
> BEGIN
>   DECLARE VARIABLE flag INT = -1;
>   l1: BEGIN
>     DECLARE EXIT HANDLER FOR UNRESOLVED_COLUMN.WITHOUT_SUGGESTION
>     BEGIN
>       SELECT flag;
>       SET flag = 2;
>     END;
>     l2: BEGIN
>       DECLARE EXIT HANDLER FOR DIVIDE_BY_ZERO
>       BEGIN
>         SELECT flag;
>         SET flag = 1;
>         select X; -- select non existing variable
>         SELECT 2;
>       END;
>       SELECT 5;
>       SELECT 1/0; -- divide by zero
>       SELECT 6;
>     END l2;
>     SELECT 3, flag;
>   END l1;
> END {code}
>  
> Returns 3, 2 at the end, but it shouldn't reach that part of the code.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

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

Reply via email to