Yaoxuan Wu created SPARK-62359:
----------------------------------

             Summary: SQL scripting: a NOT FOUND or SQLEXCEPTION handler is 
ignored when another handler is declared after it (cursor loop never ends, 
error not caught)
                 Key: SPARK-62359
                 URL: https://issues.apache.org/jira/browse/SPARK-62359
             Project: Spark
          Issue Type: Bug
          Components: SQL
    Affects Versions: 4.1.1, 4.2.0, 4.0.1
            Reporter: Yaoxuan Wu


In SQL scripting, a handler for NOT FOUND or SQLEXCEPTION is silently ignored 
when another handler is declared after it in the same compound statement. A 
cursor loop whose NOT FOUND handler is declared first never sees end of data 
and loops forever. An EXIT handler for SQLEXCEPTION that is declared first does 
not catch the error.

{code:sql}
-- spark.sql.scripting.cursorEnabled = true, 
spark.sql.scripting.continueHandlerEnabled = true (4.2)
-- t(id) = 1, 2, 3
BEGIN
  DECLARE x INT; DECLARE n INT DEFAULT 0; DECLARE e INT DEFAULT 0; DECLARE it 
INT DEFAULT 0;
  DECLARE done BOOLEAN DEFAULT false;
  DECLARE c CURSOR FOR SELECT id FROM t;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = true;
  DECLARE CONTINUE HANDLER FOR DIVIDE_BY_ZERO SET e = e + 1;   -- any second 
handler
  OPEN c;
  l: LOOP
    SET it = it + 1;
    IF it > 10 THEN LEAVE l; END IF;   -- guard, only so that the example 
terminates
    FETCH c INTO x;
    IF done THEN LEAVE l; END IF;
    SET n = n + 1;
  END LOOP l;
  SELECT n, done;
END
-- expected: n = 3, done = true
-- actual:   n = 10, done = false   (without the guard the script does not end)
{code}

The same script returns the expected result if the NOT FOUND handler is the 
only handler, if it is declared last, or if it is written as {{SQLSTATE 
'02000'}}.

Without cursors (4.1.1 and later, default configuration):
{code:sql}
BEGIN
  DECLARE r STRING DEFAULT 'not caught';
  BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION SET r = 'caught';
    DECLARE EXIT HANDLER FOR SQLSTATE '22003' SET r = 'overflow';
    SELECT 1 / 0;
  END;
  SELECT r;
END
-- expected: 'caught'
-- actual:   [DIVIDE_BY_ZERO] Division by zero   (the SQLEXCEPTION handler does 
not run)
{code}

* Cursor form: 4.2.0 and master (5.0.0-SNAPSHOT, 4056f98). SQLEXCEPTION form: 
4.1.1, 4.2.0, master, and 4.0.0 with {{spark.sql.scripting.enabled=true}}.
* Declaring the NOT FOUND / SQLEXCEPTION handler last avoids it.

Cause (from the source, please verify): {{SqlScriptingInterpreter}} builds the 
handler table of a compound in one loop over the declared handlers, and for 
each handler it assigns
{code:scala}
notFoundHandler = if (handler.exceptionHandlerTriggers.notFound) { ... 
Some(handlerExec) } else None
sqlExceptionHandler = if (handler.exceptionHandlerTriggers.sqlException) { ... 
Some(handlerExec) } else None
{code}
so every handler that does not name NOT FOUND (SQLEXCEPTION) resets the slot to 
None, and only the last declared handler can hold it. The condition and 
SQLSTATE maps are filled correctly, which is why {{SQLSTATE '02000'}} works. A 
possible fix: keep the previous value in the else branch ({{else 
notFoundHandler}} / {{else sqlExceptionHandler}}). The slots are also used by 
the duplicate-handler checks added in SPARK-52489.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

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

Reply via email to