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:
| Feature | Lightweight Schema Change | Heavyweight Schema Change |
|---|---|---|
| Execution speed | Usually faster because historical data is not rewritten | Depends on data volume, workload, and available resources |
| Requires data rewrite | No, only metadata is modified | Yes, involves rewriting data files |
| System performance impact | Small impact | May affect system performance, especially during data conversion |
| Resource consumption | Low | Uses compute, I/O, and temporary storage while old and new data coexist |
| Typical operations | Eligible value-column additions/deletions, column renames, and VARCHAR length increases | Other 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
VARCHARlength 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 NULLtoNULL. - Modifying the sort order of columns.
Execution flow:
- The background starts a data conversion job, rewriting the original data into new data files in units of tablets.
- During conversion, "double writing" occurs: new data is written to both the new tablet and the old tablet.
- 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 scenario | Operation syntax |
|---|---|
| Rename a column | RENAME COLUMN |
| Add a key/value column | ADD COLUMN |
| Add multiple columns at once | ADD COLUMN (...) |
| Drop a column | DROP COLUMN |
| Modify column type / position | MODIFY COLUMN |
| Reorder columns | ORDER 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 theKEYkeyword.
Example 1: Add a column to a non-aggregate table
-
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; -
Add the key column
key_colaftercol1:ALTER TABLE example_db.my_table_duplicate ADD COLUMN key_col INT KEY DEFAULT "0" AFTER col1; -
Add the value column
value_colaftercol4: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
-
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; -
Add the key column
key_colaftercol1:ALTER TABLE example_db.my_table_aggregate ADD COLUMN key_col INT DEFAULT "0" AFTER col1; -
Add the value column
value_colaftercol4with theSUMaggregate 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:
-
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; -
Add multiple columns at once:
ALTER TABLE example_db.my_table_multiADD 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:
-
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; -
Drop the
col4column fromexample_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
KEYkeyword. - When modifying column type, the aggregation method and default value must stay the same as the original column.
- Changing a column from
NOT NULLtoNULLis supported and runs as a heavyweight Schema Change. Changing fromNULLtoNOT NULLis 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:
-
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; -
Change the type of the key column
col1toBIGINTand move it aftercol2(whether modifying a key column or a value column, you must declare the complete column information):ALTER TABLE example_db.my_table_modifyMODIFY COLUMN col1 BIGINT KEY DEFAULT "1" AFTER col2; -
Modify the maximum length of the
col5column in the base table. The originalcol5isVARCHAR(32) REPLACE DEFAULT "abc"(only the VARCHAR length changes; aggregation method and default value must remain unchanged):ALTER TABLE example_db.my_table_modifyMODIFY COLUMN col5 VARCHAR(64) REPLACE DEFAULT "abc"; -
Modify the length of a field in a key column:
ALTER TABLE example_db.my_table_modifyMODIFY COLUMN col3 varchar(50) KEY NULL COMMENT 'to 50'; -
Change a column from
NOT NULLtoNULL(heavyweight Schema Change):ALTER TABLE example_db.my_table_modifyMODIFY 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.
| Source | Destination | Consideration |
|---|---|---|
TINYINT | SMALLINT, INT, BIGINT, LARGEINT, FLOAT, DOUBLE | Floating-point conversion can lose integer precision |
SMALLINT | INT, BIGINT, LARGEINT, FLOAT, DOUBLE | Check destination precision |
INT | BIGINT, LARGEINT, FLOAT, DOUBLE | Floating-point conversion can lose integer precision |
BIGINT | LARGEINT, FLOAT, DOUBLE | Floating-point conversion can lose integer precision |
LARGEINT | FLOAT, DOUBLE | Floating-point conversion can lose integer precision |
Numeric types, including DECIMAL | VARCHAR | The destination must be long enough for the source representation |
VARCHAR | VARCHAR | Increase the maximum length; shrinking is not supported |
VARCHAR or CHAR | Supported numeric types | Existing text must be convertible to the target type |
VARCHAR or CHAR | DATE | Supported formats include %Y-%m-%d, %y-%m-%d, %Y%m%d, %y%m%d, %Y/%m/%d, and %y/%m/%d |
DATETIME | DATE | Discards the time portion, for example, 2019-12-09 21:47:05 becomes 2019-12-09 |
DATE | DATETIME | Adds a zero time component |
FLOAT | DOUBLE | Increases the floating-point range and precision |
INT | DATE | Invalid date values cause conversion failure |
TINYINT, SMALLINT, INT, BIGINT, LARGEINT, FLOAT, DOUBLE | STRING | Do 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:
-
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; -
Reorder the columns in
example_db.my_table_reorder:ALTER TABLE example_db.my_table_reorderORDER 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
| Category | Limitation |
|---|---|
| Concurrency | A single table can have only one Schema Change job running at a time |
| Immutable columns | Partition columns and bucketing columns cannot be modified |
| Key column deletion | When 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 value | When 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 modification | When modifying column type, the aggregation method and default value must match the original column information |
| Nullable modification | Changing NOT NULL to NULL is supported (heavyweight Schema Change). Changing NULL to NOT NULL is not supported |
| Unsupported modifications | Modifying the aggregation type or default value is not supported |
Related configuration
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.