Skip to main content

ALTER TABLE REPLACE

Description​

Atomic substitution of two tables. This operation applies only to OLAP tables.

ALTER TABLE [db.]tbl1 REPLACE WITH TABLE tbl2
[PROPERTIES('swap' = 'true')];

Replace table tbl1 with table tbl2.

If the swap parameter is true, the data in the table named tbl1 will be the data in the original table named tbl2 after the replacement. The data in the table named tbl2 is the data in the original tbl1 table. That is, two tables of data have been swapped.

If the swap parameter is false, the data in the tbl1 table will be the data in the tbl2 table after the replacement. The table named tbl2 is deleted.

Theory​

The replace table function actually turns the following set of operations into an atomic operation.

If you want to replace table A with table B and swap is true, do the following:

  1. Rename table B as table A.
  2. Rename table A as table B.

If swap is false, do as follows:

  1. Delete table A.
  2. Rename table B as table A.

Notice​

  1. The default swap parameter is true. That is, a table replacement operation is equivalent to an exchange of data between two tables.
  2. If the swap parameter is set to false, the replaced table (table A) will be deleted and cannot be restored.
  3. The replacement operation can only occur between two OLAP tables and does not check whether the table structure of the two tables is consistent.
  4. The original permission Settings are not changed. Because the permission check is based on the table name.

Examples​

  1. Atomic swap tbl1 with tbl2 without dropping any tables(Note: if you delete it, you actually delete tbl1 and rename tbl2 to tbl1.)
ALTER TABLE tbl1 REPLACE WITH TABLE tbl2;

or

ALTER TABLE tbl1 REPLACE WITH TABLE tbl2 PROPERTIES('swap' = 'true');
  1. Atomic swap tbl1 with tbl2 and deleting the tbl2 table(Keep tbl1 and the data of the original tbl2)
ALTER TABLE tbl1 REPLACE WITH TABLE tbl2 PROPERTIES('swap' = 'false');

Keywords​

ALTER, TABLE, REPLACE, ALTER TABLE

Best Practice​

  1. Atomic overlay write operations

In some cases, the user wants to be able to rewrite the data of a certain table, but if the data is deleted first and then imported, the data cannot be viewed for a period of time in between. At this time, the user can first use the CREATE TABLE LIKE statement to create a new table with the same structure, import the new data into the new table, and use the replacement operation to atomically replace the old table to achieve the goal. Atomic overwrite write operations at the partition level, see temp partition documentation.