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


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/SqlScriptingParserSuite.scala:
##########
@@ -2317,6 +2332,223 @@ class SqlScriptingParserSuite extends SparkFunSuite 
with SQLHelper {
       head.asInstanceOf[SingleStatement].getText == "SELECT 3")
   }
 
+  test("declare condition: custom sqlstate") {
+    val sqlScriptText =
+      """
+        |BEGIN
+        |  DECLARE test CONDITION FOR SQLSTATE '12000';
+        |  SELECT 1;
+        |END""".stripMargin
+    val tree = parsePlan(sqlScriptText).asInstanceOf[CompoundBody]
+    assert(tree.conditions.size == 1)
+    assert(tree.conditions("test").equals("12000"))
+  }
+
+  ignore("declare condition: default sqlstate") {
+    val sqlScriptText =
+      """
+        |BEGIN
+        |  DECLARE test CONDITION;
+        |END""".stripMargin
+    val tree = parsePlan(sqlScriptText).asInstanceOf[CompoundBody]
+    assert(tree.conditions.size == 1)
+    assert(tree.conditions("test").equals("45000")) // Default SQLSTATE
+  }
+
+  test("declare condition in wrong place") {
+    val sqlScriptText =
+      """
+        |BEGIN
+        |  SELECT 1;
+        |  DECLARE test_condition CONDITION FOR SQLSTATE '12345';
+        |END""".stripMargin
+    val exception = intercept[SqlScriptingException] {
+      parsePlan(sqlScriptText)
+    }
+    checkError(
+      exception = exception,
+      condition = "INVALID_ERROR_CONDITION_DECLARATION.ONLY_AT_BEGINNING",
+      parameters = Map("conditionName" -> "`test_condition`"))
+    assert(exception.origin.line.contains(2))
+  }
+
+  test("declare qualified condition") {
+    val sqlScriptText =
+      """
+        |BEGIN
+        |  DECLARE TEST.CONDITION CONDITION FOR SQLSTATE '12345';
+        |END""".stripMargin
+    val exception = intercept[SqlScriptingException] {
+      parsePlan(sqlScriptText)
+    }
+    checkError(
+      exception = exception,
+      condition = 
"INVALID_ERROR_CONDITION_DECLARATION.QUALIFIED_CONDITION_NAME",
+      parameters = Map("conditionName" -> "TEST.CONDITION"))
+    assert(exception.origin.line.contains(3))
+  }
+
+  test("declare handler in wrong place") {
+    val sqlScriptText =
+      """
+        |BEGIN
+        |  SELECT 1;
+        |  DECLARE EXIT HANDLER FOR test_condition BEGIN SELECT 1; END;
+        |END""".stripMargin
+    val exception = intercept[SqlScriptingException] {
+      parsePlan(sqlScriptText)
+    }
+    checkError(
+      exception = exception,
+      condition = "INVALID_HANDLER_DECLARATION",
+      parameters = Map.empty)
+    assert(exception.origin.line.contains(2))
+  }
+
+
+  test("declare handler with compound body") {
+    val sqlScriptText =
+      """
+        |BEGIN
+        |  DECLARE EXIT HANDLER FOR test_condition BEGIN SELECT 1; END;
+        |END""".stripMargin
+    val tree = parsePlan(sqlScriptText).asInstanceOf[CompoundBody]
+    assert(tree.handlers.length == 1)
+    assert(tree.handlers.head.isInstanceOf[ErrorHandler])
+    assert(tree.handlers.head.handlerTriggers.conditions.size == 1)
+    
assert(tree.handlers.head.handlerTriggers.conditions.contains("test_condition"))
+    assert(tree.handlers.head.body.collection.size == 1)
+    
assert(tree.handlers.head.body.collection.head.isInstanceOf[SingleStatement])
+    
assert(tree.handlers.head.body.collection.head.asInstanceOf[SingleStatement]
+      .parsedPlan.isInstanceOf[Project])
+  }
+
+  // This test works because END is not keyword here but a part of the 
statement.
+  // It represents the name of the column in returned dataframe.
+  test("declare handler single statement with END") {
+    val sqlScriptText =
+      """
+        |BEGIN
+        |  DECLARE EXIT HANDLER FOR test_condition SELECT 1 END;
+        |END""".stripMargin
+    val tree = parsePlan(sqlScriptText).asInstanceOf[CompoundBody]
+    assert(tree.handlers.length == 1)
+    assert(tree.handlers.head.isInstanceOf[ErrorHandler])
+    assert(tree.handlers.head.handlerTriggers.conditions.size == 1)
+    
assert(tree.handlers.head.handlerTriggers.conditions.contains("test_condition"))
+    assert(tree.handlers.head.body.collection.size == 1)
+    
assert(tree.handlers.head.body.collection.head.isInstanceOf[SingleStatement])
+    
assert(tree.handlers.head.body.collection.head.asInstanceOf[SingleStatement]
+      .parsedPlan.isInstanceOf[Project])
+  }
+
+  test("declare handler single statement") {
+    val sqlScriptText =
+      """
+        |BEGIN
+        |  DECLARE EXIT HANDLER FOR test_condition SELECT 1;

Review Comment:
   No, there can be multiple handlers for different exceptions, hence the whole 
`most appropriate handler` choosing logic.



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