Altering Tables

Changing/Adding Table Properties

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

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

Removing Table Properties

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

  1. ALTER TABLE my_table RESET ('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 (
  2. 'comment' = 'table comment'
  3. );

Removing Table Comment

The following SQL removes table comment.

  1. ALTER TABLE my_table RESET ('comment');

Rename Table Name

The following SQL rename the table name to new name.

  1. ALTER TABLE my_table RENAME TO my_table_new;

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 (c1 INT, c2 STRING);

Renaming Column Name

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

  1. ALTER TABLE my_table RENAME c0 TO c1;

Dropping Columns

The following SQL drops two columns c1 and c2 from table my_table. In hive catalog, you need to ensure disable hive.metastore.disallow.incompatible.col.type.changes in your hive server, otherwise this operation may fail, throws an exception like The following columns have types incompatible with the existing columns in their respective positions.

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

Dropping Partitions

The following SQL drops the partitions of the paimon table.

For flink sql, you can specify the partial columns of partition columns, and you can also specify multiple partition values at the same time.

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

Changing Column Nullability

The following SQL changes nullability of column coupon_info.

  1. CREATE TABLE my_table (id INT PRIMARY KEY NOT ENFORCED, coupon_info FLOAT NOT NULL);
  2. -- Change column `coupon_info` from NOT NULL to nullable
  3. ALTER TABLE my_table MODIFY coupon_info FLOAT;
  4. -- Change column `coupon_info` from nullable to NOT NULL
  5. -- If there are NULL values already, set table option as below to drop those records silently before altering table.
  6. SET 'table.exec.sink.not-null-enforcer' = 'DROP';
  7. ALTER TABLE my_table MODIFY coupon_info FLOAT NOT NULL;

Changing nullable column to NOT NULL is only supported by Flink currently.

Changing Column Comment

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

  1. ALTER TABLE my_table MODIFY buy_count BIGINT COMMENT 'buy count';

Adding Column Position

To add a new column with specified position, use FIRST or AFTER col_name.

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

Changing Column Position

To modify an existent column to a new position, use FIRST or AFTER col_name.

  1. ALTER TABLE my_table MODIFY col_a DOUBLE FIRST;
  2. ALTER TABLE my_table MODIFY col_a DOUBLE AFTER col_b;

Changing Column Type

The following SQL changes type of column col_a to DOUBLE.

  1. ALTER TABLE my_table MODIFY col_a DOUBLE;

Adding watermark

The following SQL adds a computed column ts from existing column log_ts, and a watermark with strategy ts - INTERVAL '1' HOUR on column ts which is marked as event time attribute of table my_table.

  1. ALTER TABLE my_table ADD (
  2. ts AS TO_TIMESTAMP(log_ts) AFTER log_ts,
  3. WATERMARK FOR ts AS ts - INTERVAL '1' HOUR
  4. );

Dropping watermark

The following SQL drops the watermark of table my_table.

  1. ALTER TABLE my_table DROP WATERMARK;

Changing watermark

The following SQL modifies the watermark strategy to ts - INTERVAL '2' HOUR.

  1. ALTER TABLE my_table MODIFY WATERMARK FOR ts AS ts - INTERVAL '2' HOUR