Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spark] Fix char/varchar to string column conversions #2731

Merged
merged 1 commit into from
Mar 14, 2024
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 @@ -41,7 +41,7 @@ import org.apache.spark.sql.catalyst.analysis.{Resolver, UnresolvedAttribute}
import org.apache.spark.sql.catalyst.catalog.CatalogUtils
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.logical.{IgnoreCachedData, QualifiedColType}
import org.apache.spark.sql.catalyst.util.CharVarcharUtils
import org.apache.spark.sql.catalyst.util.{CharVarcharUtils, SparkCharVarcharUtils}
import org.apache.spark.sql.connector.catalog.TableCatalog
import org.apache.spark.sql.connector.catalog.TableChange.{After, ColumnPosition, First}
import org.apache.spark.sql.connector.expressions.FieldReference
Expand Down Expand Up @@ -570,12 +570,25 @@ case class AlterTableChangeColumnDeltaCommand(
case Some(newDefaultValue) => result.withCurrentDefaultValue(newDefaultValue)
case None => result.clearCurrentDefaultValue()
}
val updatedColumnMetadata =
if (!SparkCharVarcharUtils.hasCharVarchar(newColumn.dataType)) {
// Remove the char/varchar property from the metadata that
// indicates that this column is a char/varchar column.
// We construct this throwaway object because
// CharVarcharUtils.cleanAttrMetadata takes an AttributeReference.
val throwAwayAttrRef = AttributeReference(
result.name, result.dataType, nullable = result.nullable, result.metadata)()
CharVarcharUtils.cleanAttrMetadata(throwAwayAttrRef).metadata
} else {
result.metadata
}
result
.copy(
name = newColumn.name,
dataType =
SchemaUtils.changeDataType(oldColumn.dataType, newColumn.dataType, resolver),
nullable = newColumn.nullable)
nullable = newColumn.nullable,
metadata = updatedColumnMetadata)
}

// Replace existing field with new field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,17 @@ trait DeltaAlterTableTests extends DeltaAlterTableTestBase {
checkColType(spark.table("t").schema(1), VarcharType(5))
}
}

test("CHANGE COLUMN: allow change from char(x) to string type") {
withTable("t") {
sql("CREATE TABLE t(i VARCHAR(4)) USING delta")
sql("ALTER TABLE t CHANGE COLUMN i TYPE STRING")
val col = spark.table("t").schema.head
assert(col.dataType == StringType)
assert(CharVarcharUtils.getRawType(col.metadata).isEmpty)
sql("INSERT INTO t VALUES ('123456789')")
}
}
}

trait DeltaAlterTableByNameTests extends DeltaAlterTableTests {
Expand Down
Loading