Help Center/ MapReduce Service/ Component Operation Guide (LTS)/ Using Hudi/ Configuring Default Values for Hudi Data Columns
Updated on 2024-10-09 GMT+08:00

Configuring Default Values for Hudi Data Columns

This feature allows you to set default values for columns when you add columns to a table. When you query historical data, the default value is returned for the new column.

This topic is available for MRS 3.3.0 or later only.

Constraints

  • If data has been rewritten before default values are set for a new column, the default values of the column cannot be returned when historical data is queried. In this case, NULL values are returned. Some or all data will be rewritten when data is imported to the database, updated, compacted, or clustered.
  • The default values of a column must match the column type. If they do not match, the type will be forcibly converted. This loses the precision of the default values or changes the default values to NULL.
  • The default values of historical data are the default values set for the column for the first time. Changing the default values of a column for multiple times does not affect the query result of historical data.
  • After the default value is set, it cannot be rolled back.
  • Currently, Spark SQL does not support the function of viewing default column values. You can run the show create table command on Hive beeline to view default column values.

Scope

Currently, only the int, bigint, float, double, decimal, string, date, timestamp, boolean, and binary data types are supported.

Table 1 Engines supported

Component

DDL Operation

Support for Write Operation

Support for Read Operation

SparkSQL

Y

Y

Y

Spark DataSource

N

N

Y

Flink

N

N

Y

HetuEngine

N

N

Y

Hive

N

N

Y

Example

For details about the SQL syntax, see Hudi SQL Syntax Reference.

The following is an example:

  • Create a table and specify default values for columns.
    create table if not exists h3(
    id bigint,
    name string,
    price double default 12.34
    ) using hudi
    options (
    primaryKey = 'id',
    type = 'mor',
    preCombineField = 'name'
    );
  • Add columns and specify default values for the columns.
    alter table h3 add columns(col1 string default 'col1_value');
    alter table h3 add columns(col2 string default 'col2_value', col3 int default 1);
  • Change default values of columns.
    alter table h3 alter column price set default 14.56;
  • Inset data and use column default values.
    insert into h3(id, name) values(1, 'aaa');
    insert into h3(id, name, price) select 2, 'bbb', 12.5;