Scenarios
The Aggregate table is well suited to data statistics and analytics scenarios. A few examples are as follows:- Help website or app providers analyze the amount of traffic and time that their users spend on a specific website or app and the total number of visits to the website or app.
- Help advertising agencies analyze the total clicks, total views, and consumption statistics of an advertisement that they provide for their customers.
- Help e-commerce companies analyze their annual trading data to identify the geographic bestsellers within individual quarters or months.
- Most queries are aggregate queries, such as SUM, MAX, and MIN.
- Raw detailed data does not need to be retrieved.
- Historical data is not frequently updated. Only new data is appended.
Principle
From the phrase of data ingestion to data query, data in the Aggregate tables is aggregated multiple times as follows:- In the data ingestion phase, each batch of data forms a version when data is loaded into the Aggregate table in batches. In one version, data with the same aggregate key will be aggregated.
- In the background compaction phase, when the files of multiple data versions that are generated at data ingestion are periodically compacted into a large file, the database aggregates the data that has the same aggregate key in the large file.
- In the data query phase, the database aggregates the data that has the same aggregate key among all data versions before it returns the query result.
The database aggregates the four raw records into the following two records at data ingestion.
Create a table
Suppose that you want to analyze the numbers of visits by users from different cities to different web pages. In this example, create a table namedexample_db.aggregate_tbl, define site_id, date, and city_code as the aggregate key, define pv as a value column, and specify the SUM function for the pv column.
The statement for creating the table is as follows:
NOTICE
- When you create a table, you must specify the bucketing column by using the
DISTRIBUTED BY HASHclause. For detailed information, see bucketing.- Since v2.5.7, the database can automatically set the number of buckets (BUCKETS) when you create a table or add a partition. You no longer need to manually set the number of buckets. For detailed information, see set the number of buckets.
Generic aggregate function states
Beta feature Advice on use of Beta features The database supports generic aggregate function states from v3.4.0. In data analysis and summaries, Aggregate tables reduce the data processed during queries, improving query performance. For large datasets, Aggregate tables are highly effective as they summarize data by dimensions before querying. They also serve as a vital method for incremental aggregate function computation in the database. However, in earlier versions, support is limited to built-in functions such asSUM, MAX, MIN, REPLACE, HLL_UNION, PERCENTILE_UNION, and BITMAP_UNION, while in theory, all built-in aggregate functions can be used in aggregate tables. To address this limitation, generic aggregate states is introduced to support the storage of all built-in function states.
Store generic aggregate states
You can define generic aggregate states in Aggregate tables by specifying the function name and input parameter types to uniquely identify an aggregate function. The column type will automatically be inferred as the intermediate state type of the aggregate function. Definition:- col_name: Name of the column.
- agg_func_name: Name of the aggregate function whose intermediate states needs to be stored.
- parameter_type: Input parameter type of the aggregate function. The function can be identified uniquely with the parameter type.
- Only the database built-in functions with at least one parameter are supported. Java and Python UDAFs are not supported.
- For stability and extensibility, the aggregate state column type is always Nullable (except for the count function) and cannot be modified.
- Parameter values are unnecessary for defining multi-parameter functions because types can be inferred and parameter values are not involved in computation.
- Complex parameters like ORDER BY and DISTINCT are not supported.
- The support for specific built-in functions, such as
GROUP_CONCAT,WINDOW_FUNNEL, andAPPROX_TOP_K, is still under development. They will be supported in future releases. For detailed information, see FunctionSet.java#UNSUPPORTED_AGG_STATE_FUNCTIONS.
Combinator functions
Generic aggregate states use combinator functions to encapsulate intermediate state computation and flow._state combinator function
The _state function converts input parameters into intermediate state types.
Definition:
- agg_func_name: Name of the aggregate function that needs to transfer the input parameters into intermediate state types.
- input_col1/col2: Input columns of the aggregate function.
- agg_intermediate_type: Return type of the
_statefunction, that is, the intermediate state type of the aggregate function.
_state is a scalar function. You do not need to define the aggregate column for the computation of input parameter state.
Example:
_union combinator function
The _union function merges multiple intermediate state columns into a single state.
Definition:
- agg_func_name: Name of the aggregate function.
- input_col: Input columns of the aggregate function. The input column type is the intermediate state type of the aggregate function. You can obtain it using
_statefunctions. - agg_intermediate_type: Return type of the
_unionfunction, that is, the intermediate state type of the aggregate function.
_union is an aggregate function. It returns the intermediate state type instead of the type of functionās final result.
Example:
_merge combinator function
The _merge combinator function encapsulates aggregate functions as a generic aggregate function to calculate the final aggregation result of multiple intermediate states.
Definition:
- agg_func_name: Name of the aggregate function.
- input_col: Input columns of the aggregate function. The input column type is the intermediate state type of the aggregate function. You can obtain it using
_statefunctions. - agg_intermediate_type: Return type of the
_mergefunction, that is, the final aggregation result of the aggregate function.
Use generic aggregate states in materialized views
Generic aggregate states can be used in synchronous and asynchronous materialized views to accelerate query performance with rollup of aggregate states.Generic aggregate states in synchronous materialized views
Example:Generic aggregate states in asynchronous materialized views
Example:Usage notes
-
Aggregate key:
- In the CREATE TABLE statement, the aggregate key must be defined before other columns.
-
The aggregate key can be explicitly defined using
AGGREGATE KEY. TheAGGREGATE KEYmust include all columns except the value columns, otherwise the table fails to be created. If the aggregate key is not explicitly defined usingAGGREGATE KEY, all columns except the value columns are considered as the aggregate key by default. - The aggregate key has uniqueness constraint.
- Value column: Define a column as the value column by specifying an aggregate function after the column name. This column generally holds data that needs to be aggregated.
- Aggregate function: The aggregate function used for the value column. For supported aggregate functions for the Aggregate tables, see CREATE TABLE.
-
Sort key
-
Since v3.3.0, the sort key is decoupled from the aggregate key in the Aggregate table. The Aggregate table supports specifying the sort key using
ORDER BYand specifying the aggregate key usingAGGREGATE KEY. The columns in the sort key and the aggregate key need to be the same, but the order of the columns does not need to be the same. - When queries are run, sort key columns are filtered before the aggregation of multiple data versions, whereas value columns are filtered after the aggregation of multiple data versions. Therefore, we recommend that you identify the columns that are frequently used as filter conditions and define these columns as the sort key. This way, data filtering can start before the aggregation of multiple data versions to improve query performance.
-
Since v3.3.0, the sort key is decoupled from the aggregate key in the Aggregate table. The Aggregate table supports specifying the sort key using
- When you create a table, you can only create Bitmap indexes or Bloom Filter indexes on the key columns of the table.
What to do next
After a table is created, you can use various data ingestion methods to load data into the database. For information about the data ingestion methods that are supported by the database, see Loading options.Note: When you load data into a table that uses the Aggregate table, you can only update all columns of the table. For example, when you update the precedingexample_db.aggregate_tbltable, you must update all its columns, which aresite_id,date,city_code, andpv.