srielau commented on code in PR #53530:
URL: https://github.com/apache/spark/pull/53530#discussion_r2699711370
##########
sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionNode.scala:
##########
@@ -314,6 +314,12 @@ class CompoundBodyExec(
/** Used to stop the iteration in cases when LEAVE statement is encountered.
*/
private var stopIteration = false
+ /**
+ * Flag to prevent advancing curr after an exception was thrown and handled
by a CONTINUE handler.
+ * When true, the next call to next() should return curr without advancing.
+ */
+ private[scripting] var returnCurrentWithoutAdvancing = false
Review Comment:
The Key Insight
FETCH INTO raising CURSOR_NO_MORE_ROWS is indeed the FIRST statement in
Spark to raise a SQLSTATE '02xxx' (NOT FOUND / NO DATA) condition.
What This Means
Before cursors: Spark had never encountered a completion condition (SQLSTATE
'02xxx')
Existing exception handling: Assumes ALL exceptions abort execution without
a handler
SQL Standard: SQLSTATE '02xxx' are completion conditions, not exceptions -
they should allow continuation even WITHOUT a handler
The problem: Spark treats completion conditions as fatal exceptions
The Evidence
✅ Only ONE SQLSTATE '02xxx' in all of Spark: CURSOR_NO_MORE_ROWS (02000)
✅ All 23 non-cursor CONTINUE handler tests pass WITHOUT the patch
✅ 11 cursor tests fail WITHOUT the patch
✅ The issue reproduces even WITHOUT declaring any handlers
The Wrong Fix (What We Removed)
The returnCurrentWithoutAdvancing patch was trying to fix iterator
advancement after exception handling, but this was addressing the symptom
(wrong iterator state) not the cause (completion conditions treated as
exceptions).
The Right Fix (What's Needed)
Simply check if the SQLSTATE starts with '02' and allow continuation:
case None if e.getSqlState != null && e.getSqlState.startsWith("02") => //
Completion condition without handler - continue execution (SQL Standard)
()case None => // True exception without handler - abort throw e
This is the proper SQL Standard-compliant solution!
--
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]