Skip to main content

Schema Change

You can modify the schema of a VeloDB Cloud table through ALTER TABLE. This article focuses on column-related schema changes. For index-related changes, refer to Table Index for the modification methods of different indexes.

Two types of schema change​

The engine supports two types of schema change operations: lightweight and heavyweight. The two differ significantly in execution complexity, speed, and resource consumption. Before choosing, you can refer to the table below for a quick decision:

FeatureLightweight Schema ChangeHeavyweight Schema Change
Execution speedUsually faster because historical data is not rewrittenDepends on data volume, workload, and available resources
Requires data rewriteNo, only metadata is modifiedYes, involves rewriting data files
System performance impactSmall impactMay affect system performance, especially during data conversion
Resource consumptionLowUses compute, I/O, and temporary storage while old and new data coexist
Typical operationsEligible value-column additions/deletions, column renames, and VARCHAR length increasesOther supported type changes, changing NOT NULL to NULL, and column reordering

Lightweight schema change​

Only modifies metadata, without rewriting historical data files. Eligible operations include:

  • Adding or dropping value columns.
  • Renaming columns.
  • Increasing VARCHAR length without changing column position, when other column restrictions permit it.

VeloDB Cloud enables the light_schema_change table property through managed property handling. This does not make every schema change lightweight.

Heavyweight schema change​

Involves rewriting or converting data files in the background. Supported operations outside the lightweight category are heavyweight, for example:

  • Modifying the data type of a column.
  • Changing a column from NOT NULL to NULL.
  • Modifying the sort order of columns.

Execution flow:

  1. The background starts a data conversion job, rewriting the original data into new data files in units of tablets.
  2. During conversion, "double writing" occurs: new data is written to both the new tablet and the old tablet.
  3. After conversion completes, the old tablet is deleted, and the new tablet takes over.

Usage examples​

The table below lists common operation scenarios and the corresponding entry points. Run the examples in an existing example_db database with the required create, alter, and query privileges. Use unused table names, and wait for any preceding schema-change job on a table to finish before submitting another change:

User scenarioOperation syntax
Rename a columnRENAME COLUMN
Add a key/value columnADD COLUMN
Add multiple columns at onceADD COLUMN (...)
Drop a columnDROP COLUMN
Modify column type / positionMODIFY COLUMN
Reorder columnsORDER BY

Rename a column​

ALTER TABLE [database.]table RENAME COLUMN old_column_name new_column_name;

For specific syntax, refer to ALTER TABLE RENAME.

Add a column​

Notes:

  • When adding a value column to an aggregate model, you must specify agg_type.
  • When adding a key column to a non-aggregate model (such as DUPLICATE KEY), you must specify the KEY keyword.

Example 1: Add a column to a non-aggregate table​

  1. Table creation statement:

    CREATE TABLE IF NOT EXISTS example_db.my_table_duplicate(
    col1 int,
    col2 int,
    col3 int,
    col4 int,
    col5 int
    ) DUPLICATE KEY(col1, col2, col3)
    DISTRIBUTED BY RANDOM BUCKETS 10;
  2. Add the key column key_col after col1:

    ALTER TABLE example_db.my_table_duplicate ADD COLUMN key_col INT KEY DEFAULT "0" AFTER col1;
  3. Add the value column value_col after col4:

    ALTER TABLE example_db.my_table_duplicate ADD COLUMN value_col INT DEFAULT "0" AFTER col4;

Example 2: Add a column to an aggregate table​

  1. Table creation statement:

    CREATE TABLE IF NOT EXISTS example_db.my_table_aggregate(
    col1 int,
    col2 int,
    col3 int,
    col4 int SUM,
    col5 varchar(32) REPLACE DEFAULT "abc"
    ) AGGREGATE KEY(col1, col2, col3)
    DISTRIBUTED BY HASH(col1) BUCKETS 10;
  2. Add the key column key_col after col1:

    ALTER TABLE example_db.my_table_aggregate ADD COLUMN key_col INT DEFAULT "0" AFTER col1;
  3. Add the value column value_col after col4 with the SUM aggregate type:

    ALTER TABLE example_db.my_table_aggregate ADD COLUMN value_col INT SUM DEFAULT "0" AFTER col4;

Add multiple columns​

Notes:

  • When adding value columns to an aggregate model, you must specify agg_type.
  • In the aggregate-table example, a column without an aggregation method is a key column; value columns require an aggregation method.

Add multiple columns to an aggregate table:

  1. Table creation statement:

    CREATE TABLE IF NOT EXISTS example_db.my_table_multi(
    col1 int,
    col2 int,
    col3 int,
    col4 int SUM,
    col5 varchar(32) REPLACE DEFAULT "abc"
    ) AGGREGATE KEY(col1, col2, col3)
    DISTRIBUTED BY HASH(col1) BUCKETS 10;
  2. Add multiple columns at once:

    ALTER TABLE example_db.my_table_multi
    ADD COLUMN (c1 INT DEFAULT "1", c2 FLOAT SUM DEFAULT "0");

Drop a column​

Notes:

  • Partition and distribution columns cannot be dropped.
  • Key columns of a UNIQUE table cannot be dropped.

Drop a column from example_db.my_table_drop:

  1. Table creation statement:

    CREATE TABLE IF NOT EXISTS example_db.my_table_drop(
    col1 int,
    col2 int,
    col3 int,
    col4 int SUM,
    col5 varchar(32) REPLACE DEFAULT "abc"
    ) AGGREGATE KEY(col1, col2, col3)
    DISTRIBUTED BY HASH(col1) BUCKETS 10;
  2. Drop the col4 column from example_db.my_table_drop:

    ALTER TABLE example_db.my_table_drop DROP COLUMN col4;

Modify column type and position​

Notes:

  • When modifying a value column in an aggregate model, you must specify agg_type.
  • When modifying a key column in a non-aggregate model, you must specify the KEY keyword.
  • When modifying column type, the aggregation method and default value must stay the same as the original column.
  • Changing a column from NOT NULL to NULL is supported and runs as a heavyweight Schema Change. Changing from NULL to NOT NULL is not supported.
  • Partition columns and bucketing columns cannot be modified in any way.
  • Pay attention to precision loss when modifying columns. For supported type conversions, see Supported Type Conversions below.

Example:

  1. Table creation statement:

    CREATE TABLE IF NOT EXISTS example_db.my_table_modify(
    col0 int,
    col1 int DEFAULT "1",
    col2 int NOT NULL,
    col3 varchar(32),
    col4 int SUM,
    col5 varchar(32) REPLACE DEFAULT "abc"
    ) AGGREGATE KEY(col0, col1, col2, col3)
    DISTRIBUTED BY HASH(col0) BUCKETS 10;
  2. Change the type of the key column col1 to BIGINT and move it after col2 (whether modifying a key column or a value column, you must declare the complete column information):

    ALTER TABLE example_db.my_table_modify
    MODIFY COLUMN col1 BIGINT KEY DEFAULT "1" AFTER col2;
  3. Modify the maximum length of the col5 column in the base table. The original col5 is VARCHAR(32) REPLACE DEFAULT "abc" (only the VARCHAR length changes; aggregation method and default value must remain unchanged):

    ALTER TABLE example_db.my_table_modify
    MODIFY COLUMN col5 VARCHAR(64) REPLACE DEFAULT "abc";
  4. Modify the length of a field in a key column:

    ALTER TABLE example_db.my_table_modify
    MODIFY COLUMN col3 varchar(50) KEY NULL COMMENT 'to 50';
  5. Change a column from NOT NULL to NULL (heavyweight Schema Change):

    ALTER TABLE example_db.my_table_modify
    MODIFY COLUMN col2 INT NULL;

Supported type conversions​

Check the source values and destination range or length before converting. Schema-change conversions have restrictions beyond ordinary expression casts.

SourceDestinationConsideration
TINYINTSMALLINT, INT, BIGINT, LARGEINT, FLOAT, DOUBLEFloating-point conversion can lose integer precision
SMALLINTINT, BIGINT, LARGEINT, FLOAT, DOUBLECheck destination precision
INTBIGINT, LARGEINT, FLOAT, DOUBLEFloating-point conversion can lose integer precision
BIGINTLARGEINT, FLOAT, DOUBLEFloating-point conversion can lose integer precision
LARGEINTFLOAT, DOUBLEFloating-point conversion can lose integer precision
Numeric types, including DECIMALVARCHARThe destination must be long enough for the source representation
VARCHARVARCHARIncrease the maximum length; shrinking is not supported
VARCHAR or CHARSupported numeric typesExisting text must be convertible to the target type
VARCHAR or CHARDATESupported formats include %Y-%m-%d, %y-%m-%d, %Y%m%d, %y%m%d, %Y/%m/%d, and %y/%m/%d
DATETIMEDATEDiscards the time portion, for example, 2019-12-09 21:47:05 becomes 2019-12-09
DATEDATETIMEAdds a zero time component
FLOATDOUBLEIncreases the floating-point range and precision
INTDATEInvalid date values cause conversion failure
TINYINT, SMALLINT, INT, BIGINT, LARGEINT, FLOAT, DOUBLESTRINGDo not assume arbitrary reverse conversions from STRING are supported

This table summarizes common conversions, rather than every type pair. See ALTER TABLE COLUMN and test the exact conversion on representative data before changing a production table.

Reorder columns​

Notes:

  • All columns in the table must be listed.
  • Value columns must be placed after key columns.

Example:

  1. Table creation statement:

    CREATE TABLE IF NOT EXISTS example_db.my_table_reorder(
    k1 int DEFAULT "1",
    k2 int,
    k3 varchar(32),
    k4 date,
    v1 int SUM,
    v2 int MAX
    ) AGGREGATE KEY(k1, k2, k3, k4)
    DISTRIBUTED BY HASH(k1) BUCKETS 10;
  2. Reorder the columns in example_db.my_table_reorder:

    ALTER TABLE example_db.my_table_reorder
    ORDER BY (k3, k1, k2, k4, v2, v1);

Job management​

View jobs​

Use the SHOW ALTER TABLE COLUMN command to view the progress of Schema Change jobs. It shows jobs that are currently running or have completed. When a job involves materialized views, this command displays multiple rows, one for each materialized view. The following output is illustrative:

mysql > SHOW ALTER TABLE COLUMN\G;
*************************** 1. row ***************************
JobId: 20021
TableName: tbl1
CreateTime: 2019-08-05 23:03:13
FinishTime: 2019-08-05 23:03:42
IndexName: tbl1
IndexId: 20022
OriginIndexId: 20017
SchemaVersion: 2:792557838
TransactionId: 10023
State: FINISHED
Msg:
Progress: NULL
Timeout: 86400
1 row in set (0.00 sec)

Cancel jobs​

When the job state is not FINISHED or CANCELLED, you can cancel a Schema Change job with the following command:

CANCEL ALTER TABLE COLUMN FROM tbl_name;

Limitations​

CategoryLimitation
ConcurrencyA single table can have only one Schema Change job running at a time
Immutable columnsPartition columns and bucketing columns cannot be modified
Key column deletionWhen an aggregate table contains value columns aggregated with REPLACE, key columns cannot be deleted; key columns of a Unique table also cannot be deleted
Aggregate default valueWhen adding a SUM or REPLACE type value column, because the historical data has lost its detail information, the default value cannot actually reflect the post-aggregation value and is meaningless for historical data
Type modificationWhen modifying column type, the aggregation method and default value must match the original column information
Nullable modificationChanging NOT NULL to NULL is supported (heavyweight Schema Change). Changing NULL to NOT NULL is not supported
Unsupported modificationsModifying the aggregation type or default value is not supported

VeloDB Cloud manages frontend and backend service configuration. The self-managed worker-thread tuning and backend restart procedures do not apply to warehouse users. Inspect schema-change jobs with SHOW ALTER TABLE COLUMN, including State, Progress, and Msg, and contact VeloDB Cloud Support when service-level diagnosis or configuration changes are needed.

After a change completes, use SHOW CREATE TABLE and a query on the changed columns to verify the result. Heavyweight changes consume additional compute, I/O, and temporary storage; plan them around the workload rather than assuming a fixed duration or storage multiplier.

Enter to send · Shift+Enter for a new line