gengliangwang commented on code in PR #50875:
URL: https://github.com/apache/spark/pull/50875#discussion_r2093755211


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/CreatePipelineDatasetAsSelectParserSuiteBase.scala:
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.execution.command
+
+import org.apache.spark.SparkUnsupportedOperationException
+import org.apache.spark.sql.catalyst.parser.ParseException
+import org.apache.spark.sql.catalyst.plans.logical.{ColumnDefinition, 
CreatePipelineDatasetAsSelect}
+import org.apache.spark.sql.connector.expressions.{FieldReference, 
IdentityTransform}
+import org.apache.spark.sql.execution.SparkSqlParser
+import org.apache.spark.sql.execution.command.v1.CommandSuiteBase
+import org.apache.spark.sql.types.{IntegerType, MetadataBuilder, StringType, 
StructField, StructType}
+
+trait CreatePipelineDatasetAsSelectParserSuiteBase extends CommandSuiteBase {
+  protected lazy val parser = new SparkSqlParser()
+  protected val datasetSqlSyntax: String
+
+  test(s"CREATE query cannot be directly executed") {
+    checkError(
+      exception = intercept[SparkUnsupportedOperationException] {
+        sql(s"CREATE $datasetSqlSyntax table1 AS SELECT 1")
+      },
+      condition = 
"UNSUPPORTED_FEATURE.CREATE_PIPELINE_DATASET_QUERY_EXECUTION",
+      sqlState = "0A000",
+      parameters = Map("pipelineDatasetType" -> datasetSqlSyntax)
+    )
+  }
+
+  test("Column definitions are correctly parsed") {
+    Seq(
+      (s"""
+          |CREATE $datasetSqlSyntax table1 (
+          |  a INT NOT NULL,
+          |  b STRUCT<field: STRING, field2: INT> COMMENT "column description 
comment"
+          |)
+          |AS SELECT * FROM input
+       """.stripMargin,
+          Seq(
+            ColumnDefinition(
+              name = "a",
+              dataType = IntegerType,
+              nullable = false,
+              comment = None,
+              defaultValue = None,
+              generationExpression = None,
+              identityColumnSpec = None,
+              metadata = new MetadataBuilder().build()
+            ),
+            ColumnDefinition(
+              name = "b",
+              dataType = StructType(
+                Seq(
+                  StructField("field", StringType, true),
+                  StructField("field2", IntegerType, true)
+                )
+              ),
+              nullable = true,
+              comment = Some("column description comment"),
+              defaultValue = None,
+              generationExpression = None,
+              identityColumnSpec = None,
+              metadata = new MetadataBuilder().build()
+            )
+          )
+      ),
+      (s"CREATE $datasetSqlSyntax table1 AS SELECT * FROM input", Seq.empty)
+    ).foreach { case (query, columnDefs) =>
+      val plan = parser.parsePlan(query)
+      val cmd = plan.asInstanceOf[CreatePipelineDatasetAsSelect]
+      assert(cmd.columns == columnDefs)
+    }
+  }
+
+  test("Partitioning is correctly parsed") {
+    Seq(
+      (s"CREATE $datasetSqlSyntax table1 PARTITIONED BY (a) AS SELECT * FROM 
input",
+        Seq(IdentityTransform(FieldReference(Seq("a"))))),
+      (s"CREATE $datasetSqlSyntax table1 AS SELECT * FROM input", Seq.empty)
+    ).foreach { case (query, partitioning) =>
+      val plan = parser.parsePlan(query)
+      val cmd = plan.asInstanceOf[CreatePipelineDatasetAsSelect]
+      assert(cmd.partitioning == partitioning)
+    }
+  }
+
+  test("Location is unsupported") {
+    val ex = intercept[ParseException] {
+      parser.parsePlan(
+        s"""CREATE $datasetSqlSyntax table1 LOCATION 'dbfs:/path/to/table'
+           |AS SELECT * FROM input""".stripMargin)
+    }
+    assert(ex.getMessage.contains("Specifying location is not supported for " +
+      s"CREATE $datasetSqlSyntax statements."))
+  }
+
+  test("Column constraints are unsupported") {
+    val ex = intercept[ParseException] {
+      parser.parsePlan(s"CREATE $datasetSqlSyntax table1 (id INT CHECK(id > 
0)) AS SELECT 1")
+    }
+    assert(ex.getMessage.contains("Pipeline datasets do not currently support 
column constraints"))
+  }
+
+  test("Comment is correctly parsed") {
+    Seq(
+      (s"CREATE $datasetSqlSyntax table1 COMMENT 'this is a comment' AS SELECT 
* FROM input",
+        Some("this is a comment")),
+      (s"CREATE $datasetSqlSyntax table1 AS SELECT * FROM input", None)
+    ).foreach { case (query, commentOpt) =>
+      val plan = parser.parsePlan(query)
+      val cmd = plan.asInstanceOf[CreatePipelineDatasetAsSelect]
+      assert(cmd.tableSpec.comment == commentOpt)
+    }
+  }
+
+  test("STORED AS syntax is unsupported") {
+    val ex = intercept[ParseException] {
+      parser.parsePlan(s"CREATE $datasetSqlSyntax table1 STORED AS TEXTFILE AS 
SELECT * FROM input")
+    }
+    assert(ex.getMessage.contains("The STORED AS syntax is not supported for 
CREATE " +

Review Comment:
   For the rest of the test cases: let's use `checkError` instead of 
`assert(ex.getMessage.contains...`



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

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


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

Reply via email to