You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "miland-db (via GitHub)" <gi...@apache.org> on 2025/12/01 16:54:25 UTC

[PR] [SPARK-54558][SQL] Fix Internal Exception when Exception Handlers with no BEGIN/END are used [spark]

miland-db opened a new pull request, #53271:
URL: https://github.com/apache/spark/pull/53271

   ### What changes were proposed in this pull request?
   When Exception Handlers which don't have BEGIN-END body are triggered, internal exception `java.util.NoSuchElementException` was thrown instead of executing properly or propagating/raising the new error if it happens in handler.
   
   ```
   BEGIN
     DECLARE EXIT HANDLER FOR SQLEXCEPTION
       SELECT 1;
     
     SELECT 1/0;
   END 
   ```
   
   
   ### Why are the changes needed?
   Code was encountering a bug which throws internal error for what should be valid user code.
   
   ### Does this PR introduce _any_ user-facing change?
   No.
   
   
   ### How was this patch tested?
   New unit tests in `SqlScriptingExecutionSuite`.
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   No.
   


-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54558][SQL] Fix Internal Exception when Exception Handlers with no BEGIN/END are used [spark]

Posted by "miland-db (via GitHub)" <gi...@apache.org>.
miland-db commented on PR #53271:
URL: https://github.com/apache/spark/pull/53271#issuecomment-3597733258

   Adding @cloud-fan, @davidm-db, @dejankrak-db 


-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54558][SQL] Fix Internal Exception when Exception Handlers with no BEGIN/END are used [spark]

Posted by "dejankrak-db (via GitHub)" <gi...@apache.org>.
dejankrak-db commented on code in PR #53271:
URL: https://github.com/apache/spark/pull/53271#discussion_r2578068074


##########
sql/core/src/test/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionSuite.scala:
##########
@@ -1323,6 +1323,89 @@ class SqlScriptingExecutionSuite extends QueryTest with SharedSparkSession {
     verifySqlScriptResult(sqlScript, expected = expected)
   }
 
+  test("exit handler body without BEGIN-END propagates error properly") {
+    val sqlScript =
+      """
+        |BEGIN
+        |  DECLARE EXIT HANDLER FOR SQLEXCEPTION
+        |    INSERT INTO test_table_non_existing VALUES(1, 2, 3);
+        |
+        |  SELECT 1/0;
+        |END
+        |""".stripMargin
+    val exception = intercept[AnalysisException] {
+      verifySqlScriptResult(sqlScript, Seq.empty)
+    }
+    checkError(
+      exception = exception,
+      condition = "TABLE_OR_VIEW_NOT_FOUND",
+      sqlState = Some("42P01"),
+      parameters = Map("relationName" -> toSQLId("test_table_non_existing")),
+      context = ExpectedContext(
+        fragment = "test_table_non_existing",
+        start = 63,
+        stop = 85)
+    )
+  }
+
+  test("continue handler body without BEGIN-END propagates error properly") {

Review Comment:
   Are continue handlers fully supported in Spark OSS as well?



-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54558][SQL] Fix Internal Exception when Exception Handlers with no BEGIN/END are used [spark]

Posted by "miland-db (via GitHub)" <gi...@apache.org>.
miland-db commented on code in PR #53271:
URL: https://github.com/apache/spark/pull/53271#discussion_r2578077239


##########
sql/core/src/test/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionSuite.scala:
##########
@@ -1323,6 +1323,89 @@ class SqlScriptingExecutionSuite extends QueryTest with SharedSparkSession {
     verifySqlScriptResult(sqlScript, expected = expected)
   }
 
+  test("exit handler body without BEGIN-END propagates error properly") {
+    val sqlScript =
+      """
+        |BEGIN
+        |  DECLARE EXIT HANDLER FOR SQLEXCEPTION
+        |    INSERT INTO test_table_non_existing VALUES(1, 2, 3);
+        |
+        |  SELECT 1/0;
+        |END
+        |""".stripMargin
+    val exception = intercept[AnalysisException] {
+      verifySqlScriptResult(sqlScript, Seq.empty)
+    }
+    checkError(
+      exception = exception,
+      condition = "TABLE_OR_VIEW_NOT_FOUND",
+      sqlState = Some("42P01"),
+      parameters = Map("relationName" -> toSQLId("test_table_non_existing")),
+      context = ExpectedContext(
+        fragment = "test_table_non_existing",
+        start = 63,
+        stop = 85)
+    )
+  }
+
+  test("continue handler body without BEGIN-END propagates error properly") {

Review Comment:
   Yes.



-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54558][SQL] Fix Internal Exception when Exception Handlers with no BEGIN/END are used [spark]

Posted by "miland-db (via GitHub)" <gi...@apache.org>.
miland-db commented on code in PR #53271:
URL: https://github.com/apache/spark/pull/53271#discussion_r2578093467


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -300,12 +300,15 @@ class AstBuilder extends DataTypeAstBuilder
         parsingCtx)
     } else {
       // If there is no compound body, then there must be a statement or set statement.
+      val labelText = parsingCtx.labelContext.enterLabeledScope(None, None)
       val statement = Option(ctx.statement().asInstanceOf[ParserRuleContext])
         .orElse(Option(ctx.setStatementInsideSqlScript().asInstanceOf[ParserRuleContext]))
         .map { s =>
           SingleStatement(parsedPlan = visit(s).asInstanceOf[LogicalPlan])
         }
-      CompoundBody(Seq(statement.get), None, isScope = false)
+      val compoundBody = CompoundBody(Seq(statement.get), Some(labelText), isScope = false)

Review Comment:
   Added.



-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54558][SQL] Fix Internal Exception when Exception Handlers with no BEGIN/END are used [spark]

Posted by "miland-db (via GitHub)" <gi...@apache.org>.
miland-db commented on PR #53271:
URL: https://github.com/apache/spark/pull/53271#issuecomment-3599250693

   Hi @cloud-fan, failure in `ProtobufCatalystDataConversionSuite` does not look related to changes in this PR


-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54558][SQL] Fix Internal Exception when Exception Handlers with no BEGIN/END are used [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #53271: [SPARK-54558][SQL] Fix Internal Exception when Exception Handlers with no BEGIN/END are used
URL: https://github.com/apache/spark/pull/53271


-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54558][SQL] Fix Internal Exception when Exception Handlers with no BEGIN/END are used [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #53271:
URL: https://github.com/apache/spark/pull/53271#issuecomment-3599593915

   thanks, merging to master/4.1!


-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54558][SQL] Fix Internal Exception when Exception Handlers with no BEGIN/END are used [spark]

Posted by "dejankrak-db (via GitHub)" <gi...@apache.org>.
dejankrak-db commented on code in PR #53271:
URL: https://github.com/apache/spark/pull/53271#discussion_r2578064565


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -300,12 +300,15 @@ class AstBuilder extends DataTypeAstBuilder
         parsingCtx)
     } else {
       // If there is no compound body, then there must be a statement or set statement.
+      val labelText = parsingCtx.labelContext.enterLabeledScope(None, None)
       val statement = Option(ctx.statement().asInstanceOf[ParserRuleContext])
         .orElse(Option(ctx.setStatementInsideSqlScript().asInstanceOf[ParserRuleContext]))
         .map { s =>
           SingleStatement(parsedPlan = visit(s).asInstanceOf[LogicalPlan])
         }
-      CompoundBody(Seq(statement.get), None, isScope = false)
+      val compoundBody = CompoundBody(Seq(statement.get), Some(labelText), isScope = false)

Review Comment:
   Consider adding comment explaining the issue that we are addressing here.



-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54558][SQL] Fix Internal Exception when Exception Handlers with no BEGIN/END are used [spark]

Posted by "dejankrak-db (via GitHub)" <gi...@apache.org>.
dejankrak-db commented on code in PR #53271:
URL: https://github.com/apache/spark/pull/53271#discussion_r2578061968


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -300,12 +300,15 @@ class AstBuilder extends DataTypeAstBuilder
         parsingCtx)
     } else {
       // If there is no compound body, then there must be a statement or set statement.
+      val labelText = parsingCtx.labelContext.enterLabeledScope(None, None)

Review Comment:
   Using named parameters here would improve readability



-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org