This is an automated email from the ASF dual-hosted git repository. maxgekk pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push: new 3c847c6c11f [SPARK-44837][SQL] Change ALTER TABLE ALTER PARTITION column error message 3c847c6c11f is described below commit 3c847c6c11f6def15fced7bae55d0ccdeac1eb72 Author: Michael Zhang <m.zh...@databricks.com> AuthorDate: Fri Oct 20 10:55:50 2023 +0500 [SPARK-44837][SQL] Change ALTER TABLE ALTER PARTITION column error message ### What changes were proposed in this pull request? Improve the error message for `ALTER TABLE ALTER COLUMN` command on partition columns. ### Why are the changes needed? Currently, if a user executes `ALTER TABLE ALTER COLUMN` on a partition column, the error message provided is cryptic and does not help users. The improved error message makes it clear that the command is not supported for partition columns. ### Does this PR introduce _any_ user-facing change? New output when the command is executed on a partition column. > `org.apache.spark.sql.AnalysisException: [CANNOT_ALTER_PARTITION_COLUMN] ALTER TABLE (ALTER|CHANGE) COLUMN is not supported for partition columns, but found the partition column `i` in the table non-delta table spark_catalog.default.t. > at org.apache.spark.sql.errors.QueryCompilationErrors$.alterTableChangeColumnNotSupportedForPartitionColumn(QueryCompilationErrors.scala:2586)` Old error message when the `ALTER TABLE ALTER COLUMN` is executed on a partition column > `org.apache.spark.sql.AnalysisException: Can't find column `i` given table data columns [`k`]. > at org.apache.spark.sql.errors.QueryCompilationErrors$.cannotFindColumnError(QueryCompilationErrors.scala:2843) > at org.apache.spark.sql.execution.command.AlterTableChangeColumnCommand.$anonfun$findColumnByName$2(ddl.scala:490) > at scala.Option.getOrElse(Option.scala:189) > at org.apache.spark.sql.execution.command.AlterTableChangeColumnCommand.findColumnByName(ddl.scala:490) > at org.apache.spark.sql.execution.command.AlterTableChangeColumnCommand.run(ddl.scala:447)` ### How was this patch tested? All tests pass. Closes #42524 from michaelzhan-db/alter-partition-column-error-message. Lead-authored-by: Michael Zhang <m.zh...@databricks.com> Co-authored-by: michaelzhan-db <m.zh...@databricks.com> Signed-off-by: Max Gekk <max.g...@gmail.com> --- common/utils/src/main/resources/error/error-classes.json | 6 ++++++ docs/sql-error-conditions.md | 8 ++++++++ .../apache/spark/sql/errors/QueryCompilationErrors.scala | 9 +++++++++ .../org/apache/spark/sql/execution/command/ddl.scala | 4 ++++ .../org/apache/spark/sql/execution/command/DDLSuite.scala | 15 +++++++++++++++ .../scala/org/apache/spark/sql/sources/InsertSuite.scala | 5 +++-- 6 files changed, 45 insertions(+), 2 deletions(-) diff --git a/common/utils/src/main/resources/error/error-classes.json b/common/utils/src/main/resources/error/error-classes.json index 5082165a4b3..5731022b6f4 100644 --- a/common/utils/src/main/resources/error/error-classes.json +++ b/common/utils/src/main/resources/error/error-classes.json @@ -104,6 +104,12 @@ ], "sqlState" : "42KDE" }, + "CANNOT_ALTER_PARTITION_COLUMN" : { + "message" : [ + "ALTER TABLE (ALTER|CHANGE) COLUMN is not supported for partition columns, but found the partition column <columnName> in the table <tableName>." + ], + "sqlState" : "428FR" + }, "CANNOT_CAST_DATATYPE" : { "message" : [ "Cannot cast <sourceType> to <targetType>." diff --git a/docs/sql-error-conditions.md b/docs/sql-error-conditions.md index f22e5273746..d2100b5505b 100644 --- a/docs/sql-error-conditions.md +++ b/docs/sql-error-conditions.md @@ -118,6 +118,12 @@ Unable to find batch `<batchMetadataFile>`. The method `<methodName>` can not be called on streaming Dataset/DataFrame. +### CANNOT_ALTER_PARTITION_COLUMN + +[SQLSTATE: 428FR](sql-error-conditions-sqlstates.html#class-42-syntax-error-or-access-rule-violation) + +ALTER TABLE (ALTER|CHANGE) COLUMN is not supported for partition columns, but found the partition column `<columnName>` in the table `<tableName>`. + ### CANNOT_CAST_DATATYPE [SQLSTATE: 42846](sql-error-conditions-sqlstates.html#class-42-syntax-error-or-access-rule-violation) @@ -2298,3 +2304,5 @@ The operation `<operation>` requires a `<requiredType>`. But `<objectName>` is a The `<functionName>` requires `<expectedNum>` parameters but the actual number is `<actualNum>`. For more details see [WRONG_NUM_ARGS](sql-error-conditions-wrong-num-args-error-class.html) + + diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala index 5dfdd7757ad..1009c499aa3 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala @@ -2586,6 +2586,15 @@ private[sql] object QueryCompilationErrors extends QueryErrorsBase with Compilat "newType"-> toSQLType(newColumn.dataType))) } + def cannotAlterPartitionColumn( + tableName: String, + columnName: String): Throwable = { + new AnalysisException( + errorClass = "CANNOT_ALTER_PARTITION_COLUMN", + messageParameters = + Map("tableName" -> toSQLId(tableName), "columnName" -> toSQLId(columnName)) + ) + } def cannotFindColumnError(name: String, fieldNames: Array[String]): Throwable = { new AnalysisException( errorClass = "_LEGACY_ERROR_TEMP_1246", diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala index 0ef491c3b0f..7d1acc754f8 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala @@ -377,6 +377,10 @@ case class AlterTableChangeColumnCommand( val resolver = sparkSession.sessionState.conf.resolver DDLUtils.verifyAlterTableType(catalog, table, isView = false) + // Check that the column is not a partition column + if (table.partitionSchema.fieldNames.exists(resolver(columnName, _))) { + throw QueryCompilationErrors.cannotAlterPartitionColumn(table.qualifiedName, columnName) + } // Find the origin column from dataSchema by column name. val originColumn = findColumnByName(table.dataSchema, columnName, resolver) // Throw an AnalysisException if the column name/dataType is changed. diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala index b037aaac15e..56c4cde521d 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala @@ -2405,6 +2405,21 @@ abstract class DDLSuite extends QueryTest with DDLSuiteBase { "operation" -> "generated columns") ) } + + test("SPARK-44837: Error when altering partition column in non-delta table") { + withTable("t") { + sql("CREATE TABLE t(i INT, j INT, k INT) USING parquet PARTITIONED BY (i, j)") + checkError( + exception = intercept[AnalysisException] { + sql("ALTER TABLE t ALTER COLUMN i COMMENT 'comment'") + }, + errorClass = "CANNOT_ALTER_PARTITION_COLUMN", + sqlState = "428FR", + parameters = Map("tableName" -> "`spark_catalog`.`default`.`t`", + "columnName" -> "`i`") + ) + } + } } object FakeLocalFsFileSystem { diff --git a/sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala index cf1f4d4d4f2..eac7be7db44 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala @@ -1753,8 +1753,9 @@ class InsertSuite extends DataSourceTest with SharedSparkSession { exception = intercept[AnalysisException] { sql("alter table t alter column i set default false") }, - errorClass = "_LEGACY_ERROR_TEMP_1246", - parameters = Map("name" -> "i", "fieldNames" -> "[`s`, `q`]")) + errorClass = "CANNOT_ALTER_PARTITION_COLUMN", + parameters = Map("tableName" -> "`spark_catalog`.`default`.`t`", "columnName" -> "`i`") + ) } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org For additional commands, e-mail: commits-h...@spark.apache.org