databricksdelta-lake

How to rename a column in Databricks


How do you rename a column in Databricks?

The following does not work:

ALTER TABLE mySchema.myTable change COLUMN old_name new_name int

It returns the error:

ALTER TABLE CHANGE COLUMN is not supported for changing column 'old_name' with type 'IntegerType >(nullable = true)' to 'new_name' with type 'IntegerType (nullable = true)';

If it makes a difference, this table is using Delta Lake, and it is NOT partitioned or z-ordered by this "old_name" column.


Solution

  • Recently has been published some modifications which allow to rename columns on DELTA TABLES in Databricks.

    It is needed to set this properties on table:

    ALTER TABLE <table_name> SET TBLPROPERTIES (
      'delta.minReaderVersion' = '2',
      'delta.minWriterVersion' = '5',
      'delta.columnMapping.mode' = 'name'
    )
    

    Afterwards , you can rename the column as always.

    ALTER TABLE <table_name> RENAME COLUMN old_col_name TO new_col_name 
    

    Check this: https://docs.databricks.com/delta/delta-column-mapping.html

    Other usefull links:

    https://docs.databricks.com/delta/delta-batch.html#rename-columns-1

    https://docs.databricks.com/delta/delta-batch.html#change-column-type-or-name