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...
  • puneetvijwani's avatar
    2 years ago

    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')