Github user jiangxb1987 commented on a diff in the pull request:
https://github.com/apache/spark/pull/15346#discussion_r81937555
--- Diff:
sql/core/src/test/scala/org/apache/spark/sql/execution/SparkSqlParserSuite.scala
---
@@ -67,9 +86,133 @@ class SparkSqlParserSuite extends PlanTest {
DescribeFunctionCommand(FunctionIdentifier("bar", database = None),
isExtended = true))
assertEqual("describe function foo.bar",
DescribeFunctionCommand(
- FunctionIdentifier("bar", database = Option("foo")), isExtended =
false))
+ FunctionIdentifier("bar", database = Some("foo")), isExtended =
false))
assertEqual("describe function extended f.bar",
- DescribeFunctionCommand(FunctionIdentifier("bar", database =
Option("f")), isExtended = true))
+ DescribeFunctionCommand(FunctionIdentifier("bar", database =
Some("f")), isExtended = true))
+ }
+
+ private def createTableUsing(
+ table: String,
+ database: Option[String] = None,
+ tableType: CatalogTableType = CatalogTableType.MANAGED,
+ storage: CatalogStorageFormat = CatalogStorageFormat.empty,
+ schema: StructType = new StructType,
+ provider: Option[String] = Some("parquet"),
+ partitionColumnNames: Seq[String] = Seq.empty,
+ bucketSpec: Option[BucketSpec] = None,
+ mode: SaveMode = SaveMode.ErrorIfExists,
+ query: Option[LogicalPlan] = None): CreateTable = {
+ CreateTable(
+ CatalogTable(
+ identifier = TableIdentifier(table, database),
+ tableType = tableType,
+ storage = storage,
+ schema = schema,
+ provider = provider,
+ partitionColumnNames = partitionColumnNames,
+ bucketSpec = bucketSpec
+ ), mode, query
+ )
+ }
+
+ private def createTempViewUsing(
+ table: String,
+ database: Option[String] = None,
+ schema: Option[StructType] = None,
+ replace: Boolean = true,
+ provider: String = "parquet",
+ options: Map[String, String] = Map.empty): LogicalPlan = {
+ CreateTempViewUsing(TableIdentifier(table, database), schema, replace,
provider, options)
+ }
+
+ private def createTable(
+ table: String,
+ database: Option[String] = None,
+ tableType: CatalogTableType = CatalogTableType.MANAGED,
+ storage: CatalogStorageFormat = CatalogStorageFormat.empty.copy(
+ inputFormat = HiveSerDe.sourceToSerDe("textfile").get.inputFormat,
+ outputFormat =
HiveSerDe.sourceToSerDe("textfile").get.outputFormat),
+ schema: StructType = new StructType,
+ provider: Option[String] = Some("hive"),
+ partitionColumnNames: Seq[String] = Seq.empty,
+ comment: Option[String] = None,
+ mode: SaveMode = SaveMode.ErrorIfExists,
+ query: Option[LogicalPlan] = None): CreateTable = {
+ CreateTable(
+ CatalogTable(
+ identifier = TableIdentifier(table, database),
+ tableType = tableType,
+ storage = storage,
+ schema = schema,
+ provider = provider,
+ partitionColumnNames = partitionColumnNames,
+ comment = comment
+ ), mode, query
+ )
}
+ test("create table - schema") {
+ assertEqual("CREATE TABLE my_tab(a INT COMMENT 'test', b STRING)",
+ createTable(
+ table = "my_tab",
+ schema = (new StructType)
+ .add("a", IntegerType, nullable = true, "test")
+ .add("b", StringType)
+ )
+ )
+ assertEqual("CREATE TABLE my_tab(a INT COMMENT 'test', b STRING) " +
+ "PARTITIONED BY (c INT, d STRING COMMENT 'test2')",
+ createTable(
+ table = "my_tab",
+ schema = (new StructType)
+ .add("a", IntegerType, nullable = true, "test")
+ .add("b", StringType)
+ .add("c", IntegerType)
+ .add("d", StringType, nullable = true, "test2"),
+ partitionColumnNames = Seq("c", "d")
+ )
+ )
+ assertEqual("CREATE TABLE my_tab(id BIGINT, nested STRUCT<col1:
STRING,col2: INT>)",
+ createTable(
+ table = "my_tab",
+ schema = (new StructType)
+ .add("id", LongType)
+ .add("nested", (new StructType)
+ .add("col1", StringType)
+ .add("col2", IntegerType)
+ )
+ )
+ )
+ // Partitioned by a StructType should be accepted by `SparkSqlParser`
but will fail an analyze
+ // rule in `AnalyzeCreateTable`.
+ assertEqual("CREATE TABLE my_tab(a INT COMMENT 'test', b STRING) " +
+ "PARTITIONED BY (nested STRUCT<col1: STRING,col2: INT>)",
+ createTable(
+ table = "my_tab",
+ schema = (new StructType)
+ .add("a", IntegerType, nullable = true, "test")
+ .add("b", StringType)
+ .add("nested", (new StructType)
+ .add("col1", StringType)
+ .add("col2", IntegerType)
+ ),
+ partitionColumnNames = Seq("nested")
+ )
+ )
+ intercept("CREATE TABLE my_tab(a: INT COMMENT 'test', b: STRING)",
--- End diff --
The colon issue of table schema is inconvenient to test in
`DataTypeParserSuite`, so we test the cases here.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]