Partitioning based on a simple time function expression
If you frequently query and manage data based on continuous time ranges, you only need to specify a date type (DATE or DATETIME) column as the partition column and specify year, month, day, or hour as the partition granularity in the time function expression. The database will automatically create partitions and set the partitions’ start and end dates or datetime based on the loaded data and partition expression.
However, in some special scenarios, such as partitioning historical data into partitions by month and recent data into partitions by day, you must use range partitioning to create partitions.
Note
PARTITION BY date_trunc(column) and PARTITION BY time_slice(column) are considered range partitioning, despite their format of expression partitioning. Therefore, you can use the ALTER TABLE ... ADD PARTITION statement for range partitions to add new partitions to tables using such partitioning strategies.
Syntax
Parameters
expression
Required: YESDescription: A simple time function expression that use the
date_trunc or time_slice functions. If you use the function time_slice, you do not need to pass the boundary parameter. It is because in this scenario, the default and valid value for this parameter is floor, and the value cannot be ceil.
time_unit
Required: YESDescription: The partition granularity, which can be
hour, day, week, month, or year. If partition granularity is hour, the partition column must be of the DATETIME data type and cannot be of the DATE data type.
partition_column
Required: YESDescription: The name of the partition column.
- The partition column can only be of the
DATEorDATETIMEdata type. The partition column allowsNULLvalues. - The partition column can be of the
DATEorDATETIMEdata type if thedate_truncfunction is used. The partition column must be of theDATETIMEdata type if thetime_slicefunction is used. - If the partition column is of the
DATEdata type, the supported range is[0000-01-01 ~ 9999-12-31]. If the partition column is of theDATETIMEdata type, the supported range is[0000-01-01 01:01:01 ~ 9999-12-31 23:59:59] - Currently, you can specify only one partition column; multiple partition columns are not supported.
partition_live_number
Required: NODescription: The number of the most recent partitions to be retained. Partitions that start on or before the current time are sorted in chronological order, and only the most recent
partition_live_number of them are retained; the others are deleted. For a DATE partition column, the cutoff is the current date. For a DATETIME partition column, the cutoff is the current timestamp, so a partition that starts later today (for example, an hourly partition) is not counted. Partitions that start after the cutoff are neither counted nor deleted. Expired partitions are dropped forcibly and cannot be restored with RECOVER. The database schedules tasks to manage the number of partitions, and the scheduling interval can be configured through the FE dynamic parameter dynamic_partition_check_interval_seconds, which defaults to 600 seconds (10 minutes). Suppose that the current date is April 4, 2023, partition_live_number is set to 2, and the partitions include p20230401, p20230402, p20230403, p20230404. The partitions p20230403 and p20230404 are retained, and other partitions are deleted. If dirty data is loaded, such as data from the future dates April 5 and April 6, partitions include p20230401, p20230402, p20230403, p20230404, and p20230405, and p20230406. Then partitions p20230403, p20230404, p20230405, and p20230406 are retained, and the other partitions are deleted.
partition_retention_condition
From v3.5.0 onwards, native tables support Common Partition Expression TTL.
partition_retention_condition: The expression that declares the partitions to be retained dynamically. Partitions that do not meet the condition in the expression will be dropped regularly. Example: 'partition_retention_condition' = 'dt >= CURRENT_DATE() - INTERVAL 3 MONTH'.
- The expression can only contain partition columns and constants. Non-partition columns are not supported.
- Common Partition Expression applies to List partitions and Range partitions differently:
- For tables with List partitions, the database supports deleting partitions filtered by the Common Partition Expression.
- For tables with Range partitions, the database can only filter and delete partitions using the partition pruning capability of FE. Partitions correspond to predicates that are not supported by partition pruning cannot be filtered and deleted.
Usage notes
- During data loading, the database automatically creates some partitions based on the loaded data, but if the load job fails for some reason, the partitions that are automatically created by the database cannot be automatically deleted.
- The database sets the default maximum number of automatically created partitions for one load to 4096, which can be configured by the FE parameter
auto_partition_max_creation_number_per_load. This parameter can prevent you from accidentally creating too many partitions. - The naming rule for partitions is consistent with the naming rule for dynamic partitioning.
Examples
Example 1: Suppose you frequently query data by day. You can use the partition expressiondate_trunc() and set the partition column as event_day and the partition granularity as day at table creation. Data is automatically partitioned based on dates during loading. Data of the same day is stored in one partition and partition pruning can be used to significantly improve query efficiency.
p20230226 and p20230227, with ranges [2023-02-26 00:00:00, 2023-02-27 00:00:00) and [2023-02-27 00:00:00, 2023-02-28 00:00:00) respectively. If subsequent loaded data falls within these ranges, they are automatically routed to the corresponding partitions.
partition_live_number property to specify the number of partitions to retain.
time_slice() and set the partition column as event_day and the partition granularity to one week at table creation. Data for one week is stored in one partition and partition pruning can be used to significantly improve query efficiency.
Partitioning based on the column expression (since v3.1)
If you frequently query and manage data of a specific type, you only need to specify the column representing the type as the partition column. The database will automatically create partitions based on the partition column values of the loaded data. However, in some special scenarios, such as when the table contains a columncity, and you frequently query and manage data based on countries and cities. You must use list partitioning to store data of multiple cities within the same country in one partition.
Syntax
Parameters
partition_columns
Required: YESDescription: The names of partition columns.
- The partition column values can be string (
BINARYnot supported),dateordatetime,integer, andbooleanvalues. The partition column allowsNULLvalues. - Each partition can only contain data with the same value in the partition column. To include data with different values in a partition column in a partition, see List partitioning.
PARTITION BY (dt,city) with PARTITION BY dt,city.
Usage notes
- During data loading, the database automatically creates some partitions based on the loaded data, but if the load job fails for some reason, the partitions that are automatically created by the database cannot be automatically deleted.
- The database sets the default maximum number of automatically created partitions for one load to 4096, which can be configured by the FE parameter
auto_partition_max_creation_number_per_load. This parameter can prevent you from accidentally creating too many partitions. - The naming rule for partitions: if multiple partition columns are specified, the values of different partition columns are connected with an underscore
_in the partition name, and the format isp<value in partition column 1>_<value in partition column 2>_.... For example, if two columnsdtandprovinceare specified as partition columns, both of which are string types, and a data row with values2022-04-01andbeijingis loaded, the corresponding partition automatically created is namedp20220401_beijing.
Examples
Example 1: Suppose you frequently query details of the data center billing based on time ranges and specific cities. At table creation, you can use a partition expression to specify the first partition columns asdt and city. This way, data belonging to the same date and city are routed into the same partition, and partition pruning can be used to significantly improve query efficiency.
p20220401_Houston based on the loaded data. During subsequent loading, data with the values 2022-04-01 and Houston in the partition columns dt and city are stored in this partition.
Tip
Each partition can only contain data with the specified one value for the partition column. To specify multiple values for a partition column in a partition, see List partitions.
Partitioning based on a complex time function expression (since v3.4)
From v3.4.0 onwards, expression partitioning supports any expressions that returnDATE or DATETIME types to accommodate even more complex partitioning scenarios. For the supported time functions, see Appendix - Supported time functions.
For example, you can define a Unix timestamp column, and use from_unixtime() directly against the column in the partition expression to define the partition key, instead of define a generated DATE or DATETIME column with the function. For more about the usage, see Examples.
From v3.4.4 onwards, partition pruning are supported for partitions based on most DATETIME-related functions.
Examples
Example 1: Suppose you assign a Unix timestamp to each data row and frequently query data by day. You can use the timestamp column with the functionfrom_unixtime() in the expression to define the timestamp as the partition column and the partition granularity to a day at table creation. Data of each day is stored in one partition and partition pruning can be used to improve query efficiency.
INT type timestamp to each data row and store data on a monthly basis. You can use the timestamp column with the functions cast() and str_to_date() in the expression to transform the timestamp into the DATE type, set it as the partition column, and the partition granularity to a month using date_trunc() at table creation. Data of each month is stored in one partition and partition pruning can be used to improve query efficiency.
Usage notes
Partition pruning is applicable to cases of partitioning based on a complex time function expression:- If the partition clause is
PARTITION BY from_unixtime(ts), queries with filters in the formatts>1727224687can be pruned to corresponding partitions. - If the partition clause is
PARTITION BY str2date(CAST(ts AS string),'%Y%m'), queries with filters in the formatts = "20240506"can be pruned. - The above cases are also applicable to Partitioning based on mixed expression.
Partitioning based on the mixed expression (since v3.4)
From v3.4.0 onwards, expression partitioning supports multiple partition columns with one of them being a time function expression.Examples
Example 1: Suppose you assign a Unix timestamp to each data row and frequently query data by day and a specific city. You can use the timestamp column (with thefrom_unixtime() function) and the city column as the partition columns at table creation. Data of each day in each city is stored in one partition and partition pruning can be used to improve query efficiency.
Manage partitions
Load data into partitions
During data loading, the database will automatically create partitions based on the loaded data and partition rule defined by the partition expression. When using expression partitioning, you can use INSERT OVERWRITE to dynamically overwrite data without specifying the partition name. The database will automatically route and overwrite the data in the corresponding partitions. If you want to overwrite data in a specific partition, you can explicitly provide a partition range inPARTITION(). Note that for expression partitions, you need to provide the starting date or datetime of that partition (the partition granularity configured at table creation) or the specific column values, rather than just the partition name. If the partition does not exist, it will be automatically created during data loading.
View partitions
When you want to view specific information about automatically created partitions, you need to use theSHOW PARTITIONS FROM <table_name> statement. The SHOW CREATE TABLE <table_name> statement only returns the syntax for expression partitioning that is configured at table creation.
Merge expression partitions
In data management, partitioning based on different time granularity is crucial to optimizing queries and storage. To improve storage efficiency and query performance, the database supports merging multiple expression partitions of a finer time granularity into one of a rougher time granularity, for example, merging partitions by day into one partition by month. By merging partitions that meet the specified condition (time range), the database allows you to partition data on different time granularity.Syntax
Parameters
PARTITION BY <time_expr>
Required: YESDescription: Specifies the new time granularity for the partitioning strategy, for example,
PARTITION BY date_trunc('month', dt).
WHERE <time_range_column> BETWEEN <start_time> AND <end_time>
Required: YESDescription: Specifies the time range of the partitions to be merged. Partitions within this range will be merged based on the rules defined in the
PARTITION BY clause.
Example
Merge the partitions in the tablesite_access1, and change the partition time granularity from day to month. The time range of the partitions to be merged is from 2024-01-01 to 2024-03-31.
- Day-level partitions
2024-01-01to2024-01-31are merged into a month-level partition of2024-01. - Day-level partitions
2024-02-01to2024-02-29are merged into a month-level partition of2024-02. - Day-level partitions
2024-03-01to2024-03-31are merged into a month-level partition of2024-03.
Usage notes
- Merging is supported only for expression partitions based on a time function.
- Merging partitions with multiple partition columns is not supported.
- Parallel execution of merging and Schema Change/DML operations is not supported.
Limits
- Since v3.1.0, the database’s shared-data mode supports the time function expression. And since v3.1.1, the database’s shared-data mode further supports the column expression.
- Currently, using CTAS to create tables configured expression partitioning is not supported.
- Currently, using Spark Load to load data to tables that use expression partitioning is not supported.
- When the
ALTER TABLE <table_name> DROP PARTITION <partition_name>statement is used to delete a partition created by using the column expression, data in the partition is directly removed and cannot be recovered. - From v3.4.0, v3.3.8, v3.2.13, and v3.1.16 onwards, the database supports backing up and restoring tables created with the expression partitioning strategy.
Appendix
Supported time functions
Expression partitioning supports the following functions: Time functions:timediffdatediffto_daysyears_add/subquarters_add/submonths_add/subweeks_add/subdate_add/subdays_add/subhours_add/subminutes_add/subseconds_add/submilliseconds_add/subdate_truncdate_format(YmdHiSf/YmdHisf)str2date(YmdHiSf/YmdHisf)str_to_date(YmdHiSf/YmdHisf)to_iso8601to_dateunix_timestampfrom_unixtime(YmdHiSf/YmdHisf)time_slice
- add
- subtract
- cast
- Combined usage of multiple time functions is supported.
- The system default time zone is used for all the time functions listed above.
- The value format of the time function,
YmdHiSf, must start with the roughest time granularity,%Y. Formats that start with a finer time granularity, for example,%m-%d, are not allowed.
PARTITION BY from_unixtime(cast(str as INT) + 3600, '%Y-%m-%d')