Basic operations
Create generated columns
Syntax
Create generated columns at table creation
Create a table namedtest_tbl1 with five columns of which columns newcol1 and newcol2 are generated columns whose values are computed by using the specified expressions and referencing the values of the regular columns data_array and data_json respectively.
- Generated columns must be defined after regular columns.
- Aggregate functions cannot be used in the expressions for generated columns.
- The expressions for generated column cannot reference other generated columns or auto-increment columns, but the expressions can reference multiple regular columns.
- The data type of a generated column must match the data type of the result generated by the expression for the generated column.
- Generated columns cannot be created on Aggregate tables.
Add generated columns after table creation
Tip In most cases, the expressions frequently used during querying are determined after table creation, so generated columns are often added after table creation. For performance purposes, the underlying logic for the database to add generated columns after table creation is optimized. As such, when adding a generated column, the database does not need to rewrite all the data. Instead, the database only needs to write the data of the newly added generated column and associate that data with the existing physical data files, which greatly improves the efficiency of adding generated columns after table creation.-
Create a table named
test_tbl2with three regular columnsid,data_array, anddata_json. Insert a data row into the table. -
Execute ALTER TABLE … ADD COLUMN … to add generated columns
newcol1andnewcol2, which are created by evaluating the expressions based on the values of regular columnsdata_arrayanddata_json.NOTICE:- Adding generated columns to Aggregate tables is not supported.
- Regular columns need to be defined before generated columns. When you use the ALTER TABLE … ADD COLUMN … statement to add a regular column without specifying the position of the new regular column, the system automatically places it before the generated columns. Moreover, you cannot use AFTER to explicitly place the regular column after a generated column.
-
Query the table data.
The result shows that the generated columns
newcol1andnewcol2are added to the table, and StarRocks automatically computes their values based on the expression.
Load data into generated columns
During data loading, the database automatically calculates the values for generated columns based on the expressions. You cannot specify the values of generated columns. The following example uses the INSERT INTO statement to load data: The database computes a generated column from the values stored in the columns that its expression references. If the loaded data has a different type from a referenced column, the database converts the data to that column’s type first and then evaluates the expression on the converted value. For example, if the expression references aDATE column and the loaded value is the string '20260910', the expression is evaluated on the DATE value 2026-09-10, not on the original string.
-
Use INSERT INTO to insert a record into the
test_tbl1table. Note that you cannot specify the values for the generated columns within theVALUES ()clause. -
Query the table data.
The results show that StarRocks automatically computes the values for the generated columns
newcol1andnewcol2based on the expressions. NOTICE: The following error is returned if you specify values for the generated columns during data loading:
Modify generated columns
Tip When modifying a generated column, the database needs to rewrite all the data, which is time-consuming and resource-intensive. If it is unavoidable to use ALTER TABLE to modify generated columns, it is recommended to evaluate the cost and time involved in advance. You can modify the data type and expression of a generated column.-
Create a table
test_tbl3with five columns of which columnsnewcol1andnewcol2are generated columns whose values are computed by using the specified expressions and referencing the values of the regular columnsdata_arrayanddata_jsonrespectively. Insert a data row into the table. -
Modified the generated columns
newcol1andnewcol2:-
Change the data type of the generated column
newcol1toARRAY<INT>and change its expression todata_array. -
Modify the expression of the generated column
newcol2to extract the values of fieldbfrom the regular columndata_json.
-
Change the data type of the generated column
-
View the modified schema and the data in the table.
-
View the modified schema.
-
Query the table data after modification. The result shows that StarRocks recalculates the values of the generated columns
newcol1andnewcol2based on the modified expressions.
-
View the modified schema.
Drop a generated column
Drop columnnewcol1 from the table test_tbl3
NOTICE: If a generated colum references a regular column in the expression, you cannot directly drop or modify that regular column. Instead, you need to first drop the generated column and then drop or modify the regular column.
Query rewrites
If the expression in a query matches the expression of a generated column, the optimizer automatically rewrites the query to directly read the values of the generated column.-
Suppose that you create a table
test_tbl4with the following schema: -
If you query the data in the table
test_tbl4by using theSELECT array_avg(data_array), json_string(json_query(data_json, "$.a")) FROM test_tbl4;statement, the query involves only the regular columnsdata_arrayanddata_json. However, the expressions in the query match the expressions of the generated columnsnewcol1andnewcol2. In this case, the execution plan shows that the CBO automatically rewrites the query to read the values of the generated columnsnewcol1andnewcol2.
Partial updates and generated columns
To perform partial updates on a Primary Key table, you must specify all the regular columns referenced by the generated columns in thecolumns parameter. The following example uses Stream Load to perform partial updates.
-
Create a table
test_tbl5with five columns of which columnsnewcol1andnewcol2are generated columns whose values are computed by using the specified expressions and referencing the values of the regular columnsdata_arrayanddata_jsonrespectively. Insert a data row into the table. -
Prepare a CSV file
my_data1.csvto update some columns in thetest_tbl5table. -
Use Stream Load with the
my_data1.csvfile to update some columns of thetest_tbl5table. You need to setpartial_update:trueand specify all the regular columns referenced by the generated columns in thecolumnsparameter. -
Query the table data.
-
Prepare a CSV file
my_data2.csv. -
When partial column updates are performed by using Stream Load with the
my_data2.csvfile, if the values for thedata_jsoncolumn are not provided inmy_data2.csvand thecolumnsparameter in the Stream Load job does not include thedata_jsoncolumn, even if thedata_jsoncolumn allows null values, an error is returned by Stream Load because the columndata_jsonis referenced by the generated columnnewcol2.