miland-db commented on code in PR #47423:
URL: https://github.com/apache/spark/pull/47423#discussion_r1711354710


##########
sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionNode.scala:
##########
@@ -144,46 +265,84 @@ abstract class 
CompoundNestedStatementIteratorExec(collection: Seq[CompoundState
           case _ => throw SparkException.internalError(
             "Unknown statement type encountered during SQL script 
interpretation.")
         }
-        localIterator.hasNext || childHasNext
+        (localIterator.hasNext || childHasNext || returnHere.isDefined) && 
!stopIteration

Review Comment:
   `returnHere` is added here to support correct iteration when handling an 
error with `continue` handler. We need this check because when we encounter 
statement that raises an error, we move `curr` to the next statement to be 
ready to continue execution after handler body. In this case:
   ```
   BEGIN
     DECLARE flag INT = -1;
     SELECT 2;
     BEGIN
       SELECT 3;
       BEGIN
         DECLARE zero_division CONDITION FOR '22012';
         DECLARE CONTINUE HANDLER FOR zero_division
         BEGIN
           SELECT flag;
           SET VAR flag = 1;
         END;
         SELECT 4;
         SELECT 1/0;
         SELECT 5;
       END;
       SELECT 6;
     END;
     SELECT 7;
     SELECT flag;
   END
   ```
   when we return to the execution after handler body is executed `hasNext` 
will return false because `curr` is already pointing at `SELECT 5` statement 
and there are no more elements. By checking if there is something to return to 
using `returnHere`, we solve this case.
   
   If there were some statement after `SELECT 5` in the same block, everything 
would work normally.



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