Skip to content

Commit 2cbbaeb

Browse files
committed
[SPARK-52272][SQL] HiveExternalCatalog alter table loses comment updates
1 parent 7fee291 commit 2cbbaeb

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveExternalCatalog.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,11 +684,23 @@ private[spark] class HiveExternalCatalog(conf: SparkConf, hadoopConf: Configurat
684684
} else {
685685
(oldTableDef.schema, oldTableDef.partitionColumnNames)
686686
}
687+
688+
// alter column comments
689+
val newCommentMap = tableDefinition.schema.map(f => (f.name, f.getComment())).toMap
690+
val newSchemaWithComments = newSchema.copy(
691+
fields = newSchema.fields.map { f =>
692+
newCommentMap.getOrElse(f.name, None) match {
693+
case Some(comment) => f.withComment(comment)
694+
case _ => f
695+
}
696+
}
697+
)
698+
687699
// // Add old table's owner if we need to restore
688700
val owner = Option(tableDefinition.owner).filter(_.nonEmpty).getOrElse(oldTableDef.owner)
689701
val newDef = tableDefinition.copy(
690702
storage = newStorage,
691-
schema = newSchema,
703+
schema = newSchemaWithComments,
692704
partitionColumnNames = partitionColumnNames,
693705
bucketSpec = oldTableDef.bucketSpec,
694706
properties = newTableProps,

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3392,4 +3392,21 @@ class HiveDDLSuite
33923392
)
33933393
}
33943394
}
3395+
3396+
test("SPARK-52272: alter table loses column comments") {
3397+
val tabName = "tab1"
3398+
withTable(tabName) {
3399+
val catalog = spark.sessionState.catalog
3400+
3401+
sql(s"CREATE TABLE $tabName (col int COMMENT 'comment1') USING PARQUET")
3402+
val identifier = TableIdentifier(tabName, Some("default"))
3403+
3404+
val table = catalog.getTableMetadata(identifier)
3405+
val newTable = table.copy(schema =
3406+
new StructType().add("col", IntegerType, nullable = true, "comment2"))
3407+
catalog.alterTable(newTable)
3408+
val fetchedTable = catalog.getTableMetadata(identifier)
3409+
assert(fetchedTable.schema == newTable.schema)
3410+
}
3411+
}
33953412
}

0 commit comments

Comments
 (0)