Search code examples
apache-sparkhivepartitionhive-partitionshiveddl

How to undo ALTER TABLE ... ADD PARTITION without deleting data


Let's suppose I have two hive tables, table_1 and table_2. I use:

ALTER TABLE table_2 ADD PARTITION (col=val) LOCATION [table_1_location]

Now, table_2 will have the data in table_1 at the partition where col = val.

What I want to do is reverse this process. I want table_2 not to have the partition at col=val, and I want table_1 to keep its original data.

How can I do this?


Solution

  • Make your table EXTERNAL first:

    ALTER TABLE table_2 SET TBLPROPERTIES('EXTERNAL'='TRUE');
    

    Then drop partition, the data will remain, only table_2 partition metadata will be deleted:

    ALTER TABLE table_2 DROP PARTITION (col=val)
    

    table_1 partition data will remain as is.