davidm-db commented on code in PR #46665: URL: https://github.com/apache/spark/pull/46665#discussion_r1607348639
########## sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/BatchParserSuite.scala: ########## @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.catalyst.parser + +import org.apache.spark.SparkFunSuite +import org.apache.spark.sql.catalyst.plans.SQLHelper + +class BatchParserSuite extends SparkFunSuite with SQLHelper { + import CatalystSqlParser._ + + test("single select") { + val batch = "SELECT 1;" + val tree = parseBatch(batch) + assert(tree.collection.length == 1) + assert(tree.collection.head.isInstanceOf[SparkStatementWithPlan]) + val sparkStatement = tree.collection.head.asInstanceOf[SparkStatementWithPlan] + assert(sparkStatement.getText(batch) == "SELECT 1;") + } + + test("single select without ;") { + val batch = "SELECT 1" + val tree = parseBatch(batch) + assert(tree.collection.length == 1) + assert(tree.collection.head.isInstanceOf[SparkStatementWithPlan]) + val sparkStatement = tree.collection.head.asInstanceOf[SparkStatementWithPlan] + assert(sparkStatement.getText(batch) == "SELECT 1") + } + + test("multi select without ; - should fail") { + val batch = "SELECT 1 SELECT 1" + val e = intercept[ParseException] { + parseBatch(batch) + } + assert(e.getErrorClass === "PARSE_SYNTAX_ERROR") + assert(e.getMessage.contains("Syntax error")) + assert(e.getMessage.contains("SELECT 1 SELECT 1")) + } + + test("multi select") { + val batch = "BEGIN SELECT 1;SELECT 2; END" + val tree = parseBatch(batch) + assert(tree.collection.length == 2) + assert(tree.collection.forall(_.isInstanceOf[SparkStatementWithPlan])) + + batch.split(";") + .map(cleanupStatementString) + .zip(tree.collection) + .foreach { case (expected, statement) => + val sparkStatement = statement.asInstanceOf[SparkStatementWithPlan] + val statementText = sparkStatement.getText(batch) + assert(statementText == expected) + } + } + + test("multi statement") { + val batch = + """ + |BEGIN + | SELECT 1; + | SELECT 2; + | INSERT INTO A VALUES (a, b, 3); + | SELECT a, b, c FROM T; + | SELECT * FROM T; + |END""".stripMargin + val tree = parseBatch(batch) + assert(tree.collection.length == 5) + assert(tree.collection.forall(_.isInstanceOf[SparkStatementWithPlan])) + batch.split(";") + .map(cleanupStatementString) + .zip(tree.collection) + .foreach { case (expected, statement) => + val sparkStatement = statement.asInstanceOf[SparkStatementWithPlan] + val statementText = sparkStatement.getText(batch) + assert(statementText == expected) + } + } + + test("multi statement without ; at the end") { + val batch = + """ + |BEGIN + |SELECT 1; + |SELECT 2; + |INSERT INTO A VALUES (a, b, 3); + |SELECT a, b, c FROM T; + |SELECT * FROM T + |END""".stripMargin + val tree = parseBatch(batch) + assert(tree.collection.length == 5) + assert(tree.collection.forall(_.isInstanceOf[SparkStatementWithPlan])) + batch.split(";") + .map(cleanupStatementString) + .zip(tree.collection) + .foreach { case (expected, statement) => + val sparkStatement = statement.asInstanceOf[SparkStatementWithPlan] + val statementText = sparkStatement.getText(batch) + assert(statementText == expected) + } + } + + test("nested begin end") { + val batch = + """ + |BEGIN + | BEGIN + | SELECT 1; + | END; + | BEGIN + | BEGIN + | SELECT 2; + | SELECT 3; + | END; + | END; + |END""".stripMargin + val tree = parseBatch(batch) + assert(tree.collection.length == 2) + assert(tree.collection.head.isInstanceOf[BatchBody]) + val body1 = tree.collection.head.asInstanceOf[BatchBody] + assert(body1.collection.length == 1) + assert(body1.collection.head.asInstanceOf[SparkStatementWithPlan].getText(batch) == "SELECT 1") + + val body2 = tree.collection(1).asInstanceOf[BatchBody] + assert(body2.collection.length == 1) + assert(body2.collection.head.isInstanceOf[BatchBody]) + val nestedBody = body2.collection.head.asInstanceOf[BatchBody] + assert( + nestedBody.collection.head.asInstanceOf[SparkStatementWithPlan].getText(batch) == "SELECT 2") + assert( + nestedBody.collection(1).asInstanceOf[SparkStatementWithPlan].getText(batch) == "SELECT 3") + } + + // Helper methods Review Comment: Is there any way in Scala to add section comments? Or any other way to achieve anything similar? -- 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]
