Skip to content

Commit

Permalink
allow char/varchar to string column conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvarya-db committed Mar 14, 2024
1 parent 3369968 commit 17e78f5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
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

0 comments on commit 17e78f5

Please sign in to comment.