Forum Discussion

mrojze's avatar
mrojze
Helper II
2 years ago
Solved

Add partition to existing Delta Table

I have a Delta table with data. All columns have values.

I want to add a partition.

Is that possible? or I can only create partitions at creation time?

 

This is what I have tried and the error I am getting:

 

%%sql
ALTER TABLE Table1 ADD PARTITION (part_1)
 
Error: Found an empty partition key 'part_1'
  • mrojze  you can try something like this 

    CREATE TABLE NewTable
    USING delta
    PARTITIONED BY (part_1)
    AS SELECT *, some_column AS part_1 FROM ExistingTable

    or more simple

    df = spark.read.table('ExistingTable')

    df = df.withColumn('part_1', df.some_column)

    df.write.partitionBy('part_1').format('delta').saveAsTable('NewTable')