Skip to content
Merged
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 @@ -312,4 +312,10 @@ object HoodieSqlCommonUtils extends SparkAdapterSupport {
case field if resolver(field.name, name) => field
}
}

// Compare a [[StructField]] to another, return true if they have the same column
// name(by resolver) and dataType.
def columnEqual(field: StructField, other: StructField, resolver: Resolver): Boolean = {
resolver(field.name, other.name) && field.dataType == other.dataType
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ case class AlterHoodieTableChangeColumnCommand(
throw new AnalysisException(s"Can't find column `$columnName` given table data columns " +
s"${hoodieCatalogTable.dataSchema.fieldNames.mkString("[`", "`, `", "`]")}")
)
// Throw an AnalysisException if the column name/dataType is changed.
if (!columnEqual(originColumn, newColumn, resolver)) {
throw new AnalysisException(
"ALTER TABLE CHANGE COLUMN is not supported for changing column " +
s"'${originColumn.name}' with type '${originColumn.dataType}' to " +
s"'${newColumn.name}' with type '${newColumn.dataType}'")
}

// Get the new schema
val newTableSchema = StructType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,6 @@ alter table h2_p add columns(ext0 int);
+----------+
| ok |
+----------+
alter table h2_p change column ext0 ext0 bigint;
+----------+
| ok |
+----------+

# DROP TABLE
drop table h0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ class TestAlterTable extends TestHoodieSqlBase {
)

// change column's data type
spark.sql(s"alter table $newTableName change column id id bigint")
assertResult(StructType(Seq(StructField("id", LongType, nullable = true))))(
spark.sql(s"select id from $newTableName").schema)
checkExceptionContain(s"alter table $newTableName change column id id bigint") (
"ALTER TABLE CHANGE COLUMN is not supported for changing column 'id'" +
" with type 'IntegerType' to 'id' with type 'LongType'"
)

// Insert data to the new table.
spark.sql(s"insert into $newTableName values(2, 'a2', 12, 1000, 'e0')")
Expand Down