davidm-db commented on code in PR #47462:
URL: https://github.com/apache/spark/pull/47462#discussion_r1706742219


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/SqlScriptingParserSuite.scala:
##########
@@ -527,6 +542,57 @@ class SqlScriptingParserSuite extends SparkFunSuite with 
SQLHelper {
       .getText == "SELECT 42")
   }
 
+
+  test("while") {
+    val sqlScriptText =
+      """BEGIN
+        |lbl: WHILE 1 = 1 DO
+        |  SELECT 1;
+        |END WHILE lbl;
+        |END
+      """.stripMargin
+    val tree = parseScript(sqlScriptText)
+    assert(tree.collection.length == 1)
+    assert(tree.collection.head.isInstanceOf[WhileStatement])
+
+    val whileStmt = tree.collection.head.asInstanceOf[WhileStatement]
+    assert(whileStmt.condition.isInstanceOf[SingleStatement])
+    assert(whileStmt.condition.getText == "1 = 1")
+
+    assert(whileStmt.body.isInstanceOf[CompoundBody])
+    assert(whileStmt.body.collection.length == 1)
+    assert(whileStmt.body.collection.head.isInstanceOf[SingleStatement])
+    
assert(whileStmt.body.collection.head.asInstanceOf[SingleStatement].getText == 
"SELECT 1")
+
+    assert(whileStmt.label.contains("lbl"))
+  }
+
+  test("while with complex condition") {
+    val sqlScriptText =
+    """
+      |BEGIN
+      |CREATE TABLE t (a INT, b STRING, c DOUBLE) USING parquet;
+      |WHILE (SELECT COUNT(*) < 2 FROM t) DO
+      |  SELECT 42;
+      |END WHILE;
+      |END
+      |""".stripMargin
+
+    val tree = parseScript(sqlScriptText)
+    assert(tree.collection.length == 2)
+    assert(tree.collection(1).isInstanceOf[WhileStatement])
+
+    val whileStmt = tree.collection(1).asInstanceOf[WhileStatement]
+    assert(whileStmt.condition.isInstanceOf[SingleStatement])
+    assert(whileStmt.condition.getText == "(SELECT COUNT(*) < 2 FROM t)")
+
+    assert(whileStmt.body.isInstanceOf[CompoundBody])
+    assert(whileStmt.body.collection.length == 1)
+    assert(whileStmt.body.collection.head.isInstanceOf[SingleStatement])
+    
assert(whileStmt.body.collection.head.asInstanceOf[SingleStatement].getText == 
"SELECT 42")
+
+  }
+

Review Comment:
   let's add a test or two more, something a bit more complex - nested loop 
maybe, if/else within the loop...



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