Altering Tables

Changing/Adding Table Properties

The following SQL sets write-buffer-size table property to 256 MB.

  1. ALTER TABLE my_table SET TBLPROPERTIES (
  2. 'write-buffer-size' = '256 MB'
  3. );

Removing Table Properties

The following SQL removes write-buffer-size table property.

  1. ALTER TABLE my_table UNSET TBLPROPERTIES ('write-buffer-size');

Changing/Adding Table Comment

The following SQL changes comment of table my_table to table comment.

  1. ALTER TABLE my_table SET TBLPROPERTIES (
  2. 'comment' = 'table comment'
  3. );

Removing Table Comment

The following SQL removes table comment.

  1. ALTER TABLE my_table UNSET TBLPROPERTIES ('comment');

Rename Table Name

The following SQL rename the table name to new name.

The simplest sql to call is:

  1. ALTER TABLE my_table RENAME TO my_table_new;

Note that: we can rename paimon table in spark this way:

  1. ALTER TABLE [catalog.[database.]]test1 RENAME to [database.]test2;

But we can’t put catalog name before the renamed-to table, it will throw an error if we write sql like this:

  1. ALTER TABLE catalog.database.test1 RENAME to catalog.database.test2;

If you use object storage, such as S3 or OSS, please use this syntax carefully, because the renaming of object storage is not atomic, and only partial files may be moved in case of failure.

Adding New Columns

The following SQL adds two columns c1 and c2 to table my_table.

  1. ALTER TABLE my_table ADD COLUMNS (
  2. c1 INT,
  3. c2 STRING
  4. );

Renaming Column Name

The following SQL renames column c0 in table my_table to c1.

  1. ALTER TABLE my_table RENAME COLUMN c0 TO c1;

Dropping Columns

The following SQL drops two columns c1 and c2 from table my_table.

  1. ALTER TABLE my_table DROP COLUMNS (c1, c2);

Dropping Partitions

The following SQL drops the partitions of the paimon table. For spark sql, you need to specify all the partition columns.

  1. ALTER TABLE my_table DROP PARTITION (`id` = 1, `name` = 'paimon');

Changing Column Comment

The following SQL changes comment of column buy_count to buy count.

  1. ALTER TABLE my_table ALTER COLUMN buy_count COMMENT 'buy count';

Adding Column Position

  1. ALTER TABLE my_table ADD COLUMN c INT FIRST;
  2. ALTER TABLE my_table ADD COLUMN c INT AFTER b;

Changing Column Position

  1. ALTER TABLE my_table ALTER COLUMN col_a FIRST;
  2. ALTER TABLE my_table ALTER COLUMN col_a AFTER col_b;

Changing Column Type

  1. ALTER TABLE my_table ALTER COLUMN col_a TYPE DOUBLE;