How to change the column datatype of a delta table

Shukla, Mayank - Vendor 1 Reputation point
2022-04-14T17:03:31.3+00:00

How to change the column datatype of a delta table

Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
2,031 questions
{count} votes

1 answer

Sort by: Most helpful
  1. KranthiPakala-MSFT 46,432 Reputation points Microsoft Employee
    2022-05-03T19:49:33.29+00:00

    Hello @Shukla, Mayank - Vendor ,

    As per the discussion with internal team, unfortunately there is no ALTER TABLE ALTER COLUMN at this moment. The possible workaround suggested is to change a datatype by reading the data in datafram using PySpark as shown below:

    df = df.withColumn("id", col("colname").cast("newdatatype")) # changes a column datatype.   
    

    Then to write it back to the delta table:

    df.write.format("delta").mode("overwrite").option("overwriteSchema", "true").save(folder_path) # overwrites the delta table and is another version:  
    

    If you have any feedback regarding this, I would recommend you to please log a feature request in IDEAS forum here -

    1. https://feedback.azure.com/d365community/forum/2efba7dc-ef24-ec11-b6e6-000d3a4f0da0
    2. https://docs.databricks.com/resources/ideas.html

    All the feedback shared in this forum are actively monitored and reviewed by product team who are responsible for building it and will take action accordingly. Please do share the feedback link once it is posted so that others can up-vote and comment on it to help increase the priority of the feature request.

    Hope this info helps.

    1 person found this answer helpful.