Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ case class CatalogTable(
* schema of this table's partition columns
*/
def partitionSchema: StructType = {
val partitionFields = schema.takeRight(partitionColumnNames.length)
val partitionFields = partitionColumnNames.map { partCol =>
schema.find(_.name == partCol).get
Copy link
Contributor

@LuciferYang LuciferYang Dec 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this safe? Is there any Exception of None.get here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this consistent with the result of

partitionColumnNames.flatMap { partCol =>
  schema.find(_.name == partCol)
}

?

}
assert(partitionFields.map(_.name) == partitionColumnNames)

StructType(partitionFields)
Expand All @@ -268,7 +270,9 @@ case class CatalogTable(
* schema of this table's data columns
*/
def dataSchema: StructType = {
val dataFields = schema.dropRight(partitionColumnNames.length)
val dataFields = schema.filterNot { i =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i is easy to associate with index. Should we change this variable name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. I will change it. Thank you.

partitionColumnNames.contains(i.name)
}
StructType(dataFields)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ case class CreateDataSourceTableCommand(table: CatalogTable, ignoreIfExists: Boo

case _ =>
table.copy(
schema = dataSource.schema,
schema = table.schema.merge(dataSource.schema),
partitionColumnNames = partitionColumnNames,
// If metastore partition management for file source tables is enabled, we start off with
// partition provider hive, but no partitions in the metastore. The user has to call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,7 @@ case class PreprocessTableCreation(sparkSession: SparkSession) extends Rule[Logi
} else {
DDLUtils.checkTableColumns(tableDesc)
val normalizedTable = normalizeCatalogTable(tableDesc.schema, tableDesc)

val partitionSchema = normalizedTable.partitionColumnNames.map { partCol =>
normalizedTable.schema.find(_.name == partCol).get
}

val reorderedSchema =
StructType(normalizedTable.schema.filterNot(partitionSchema.contains) ++ partitionSchema)

c.copy(tableDesc = normalizedTable.copy(schema = reorderedSchema))
c.copy(tableDesc = normalizedTable)
}

case create: V2CreateTablePlan if create.childrenResolved =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,9 @@ desc formatted char_part
struct<col_name:string,data_type:string,comment:string>
-- !query output
c1 char(5)
c2 char(2)
v1 varchar(6)
v2 varchar(2)
c2 char(2)
# Partition Information
# col_name data_type comment
v2 varchar(2)
Expand Down Expand Up @@ -540,9 +540,9 @@ desc formatted char_part
struct<col_name:string,data_type:string,comment:string>
-- !query output
c1 char(5)
c2 char(2)
v1 varchar(6)
v2 varchar(2)
c2 char(2)
# Partition Information
# col_name data_type comment
v2 varchar(2)
Expand Down Expand Up @@ -575,9 +575,9 @@ desc formatted char_part
struct<col_name:string,data_type:string,comment:string>
-- !query output
c1 char(5)
c2 char(2)
v1 varchar(6)
v2 varchar(2)
c2 char(2)
# Partition Information
# col_name data_type comment
v2 varchar(2)
Expand Down Expand Up @@ -609,9 +609,9 @@ desc formatted char_part
struct<col_name:string,data_type:string,comment:string>
-- !query output
c1 char(5)
c2 char(2)
v1 varchar(6)
v2 varchar(2)
c2 char(2)
# Partition Information
# col_name data_type comment
v2 varchar(2)
Expand Down Expand Up @@ -643,9 +643,9 @@ desc formatted char_part
struct<col_name:string,data_type:string,comment:string>
-- !query output
c1 char(5)
c2 char(2)
v1 varchar(6)
v2 varchar(2)
c2 char(2)
# Partition Information
# col_name data_type comment
v2 varchar(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ SHOW CREATE TABLE tbl
struct<createtab_stmt:string>
-- !query output
CREATE TABLE `default`.`tbl` (
`a` INT,
`b` STRING,
`c` INT,
`a` INT)
`c` INT)
USING parquet
PARTITIONED BY (a)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,27 @@ abstract class ShowCreateTableSuite extends QueryTest with SQLTestUtils {
}
}

test("SPARK-37517: Keep consistent columns order with user specified") {
val t = "SPARK_37517"
withTable(t) {
sql(
s"""
|CREATE TABLE $t (
| a bigint NOT NULL,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused that the table will be created success although column a is nullable. It seems to me that partition columns should not be nullable. @cloud-fan

| b bigint NOT NULL,
| c ARRAY<INT>,
| d STRUCT<x: INT, y: ARRAY<BOOLEAN>>
|)
|USING PARQUET
|PARTITIONED BY (a)
""".stripMargin)
val expected = s"CREATE TABLE `default`.`$t` (" +
s" `a` BIGINT NOT NULL, `b` BIGINT, `c` ARRAY<INT>," +
s" `d` STRUCT<`x`: INT, `y`: ARRAY<BOOLEAN>>) USING PARQUET PARTITIONED BY (a)"
assert(getShowDDL(s"SHOW CREATE TABLE $t") == expected)
}
}

protected def getShowDDL(showCreateTableSql: String): String = {
sql(showCreateTableSql).head().getString(0).split("\n").map(_.trim).mkString(" ")
}
Expand Down