diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala index c5888d72c2b23..645ed9e6bb0c1 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala @@ -178,16 +178,19 @@ private[sql] object CatalogV2Util { schema: StructType, changes: Seq[TableChange]): Array[Transform] = { - val newPartitioning = partitioning.filterNot(_.isInstanceOf[ClusterByTransform]).toBuffer - changes.foreach { - case clusterBy: ClusterBy => - newPartitioning += ClusterBySpec.extractClusterByTransform( + var newPartitioning = partitioning + // If there is a clusterBy change (only the first one), we overwrite the existing + // clustering columns. + val clusterByOpt = changes.collectFirst { case c: ClusterBy => c } + clusterByOpt.foreach { clusterBy => + newPartitioning = partitioning.map { + case _: ClusterByTransform => ClusterBySpec.extractClusterByTransform( schema, ClusterBySpec(clusterBy.clusteringColumns.toIndexedSeq), conf.resolver) - - case _ => - // ignore other changes + case other => other + } } - newPartitioning.toArray + + newPartitioning } /** diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/DescribeTableSuiteBase.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/DescribeTableSuiteBase.scala index 2588aa4313fa7..02e8a5e689998 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/DescribeTableSuiteBase.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/DescribeTableSuiteBase.scala @@ -181,8 +181,9 @@ trait DescribeTableSuiteBase extends QueryTest with DDLCommandTestUtils { test("describe a clustered table") { withNamespaceAndTable("ns", "tbl") { tbl => - sql(s"CREATE TABLE $tbl (col1 STRING COMMENT 'this is comment', col2 struct) " + + sql(s"CREATE TABLE $tbl (col1 STRING, col2 struct) " + s"$defaultUsing CLUSTER BY (col1, col2.x)") + sql(s"ALTER TABLE $tbl ALTER COLUMN col1 COMMENT 'this is comment';") val descriptionDf = sql(s"DESC $tbl") assert(descriptionDf.schema.map(field => (field.name, field.dataType)) === Seq( ("col_name", StringType),