Search code examples
hivehiveql

How to update only one partition field when the hive table has multiple partition fields?


I have a Hive table called table, it has two string partition field, yearsandmonth. Now I only want to update the years partition but not update month partition.

I tried the sql below but failed.

ALTER TABLE table PARTITION (years='2021') RENAME TO PARTITION (years='2020');

Apache calcite logged that

org.apache.calcite.runtime.CalciteContextException: Sql 1: From line 1, column 43 to line 1, column 84: Number of target table partition columns 2 does not equal number of source partition columns 1


Solution

  • Since partition is a folder structure, you need to mention all partition names.

    ALTER TABLE table PARTITION (years='2021',month='1') RENAME TO PARTITION (years='2020',month='1');
    ALTER TABLE table PARTITION (years='2021',month='2') RENAME TO PARTITION (years='2020',month='2');
    ALTER TABLE table PARTITION (years='2021',month='3') RENAME TO PARTITION (years='2020',month='3');
    ...
    

    Or else you can create a new table with same structure but partitioned by new partition. And then insert from old table to new table and then drop the new table.