MaxCompute supports automatic partition (AUTO PARTITION) tables. The partition columns of automatic partition tables can be automatically generated based on time computing functions or data write time. This topic describes how to use automatic partition tables generated based on time computing functions.
Introduction
MaxCompute supports two types of partitioned tables: regular partitioned tables and auto-partitioned (AUTO PARTITION) tables. You can choose a table type based on how you want to generate partition columns for different scenarios. For more information, see Partitioned table overview.
Compared with other big data products that support partitioning based on date/time type columns or partitioning after truncating these columns, MaxCompute offers more flexible partitioning capabilities. MaxCompute supports using specific time computing functions on time/date type data columns (DATE, DATETIME, TIMESTAMP, TIMESTAMP_NTZ) to automatically generate partition column values based on the calculation results of data columns, thereby implementing table partitioning.
Additionally, MaxCompute supports automatically obtaining the time when data is written, combined with user-specified time computing functions, to generate partition column values based on the calculation results for table partitioning. For more information, see Auto partition table based on data writing time.
Limits
Automatic partition tables based on time computing functions currently support only one partition column and do not support multi-level partitioning.
The partition column of an automatic partition table based on time computing functions is automatically generated according to the partition generation expression specified when the table is created. The partition field type is STRING by default and cannot be modified.
Auto-partitioning based on time functions or data write time is supported only when you write data using MaxCompute SQL. This feature is not supported for other data write methods, such as using a Flink connector.
Usage notes
Create an automatic partition table based on time computing functions
Syntax
Create an automatic partition table based on time computing functions.
CREATE [OR REPLACE] TABLE [IF NOT EXISTS] <table_name> (<col_name> <data_type>, ... )
[COMMENT <table_comment>]
AUTO PARTITIONED BY (<auto_partition_expression> [AS <auto_partition_column_name>]);Parameters
Parameter | Required | Description |
table_name | Yes | The name of the table to be created. |
col_name | Yes | The column name. |
data_type | Yes | The data type of the column. |
table_comment | No | The comment of the table. |
auto_partition_expression | Yes | The expression that defines how to calculate the partition column. Currently, only the TRUNC_TIME function is supported for generating partition column calculations. Note When creating an automatic partition table based on data write time, the system automatically obtains the data write time and truncates it using the TRUNC_TIME function to generate partition column values. |
auto_partition_column_name | No | The name of the generated partition column. If the partition column name is not specified, the system uses Note After partitioning using the partition expression, a STRING type partition column is generated based on the calculation results. The column type and column values do not support direct operations. |
Examples
Example 1: Truncate time/date type column data by day to calculate and generate automatic partitions.
Create an automatic partition table
sale_detailthat includes a TIMESTAMP type time data columnsale_time. In the DDL statement, truncate this column data by day, and generate AUTO PARTITION table partitions based on the calculation results.CREATE TABLE IF NOT EXISTS sale_detail( shop_name STRING, total_price DOUBLE, sale_time TIMESTAMP ) AUTO PARTITIONED BY (TRUNC_TIME(sale_time, 'day'));The
sale_detailauto-partitioned table created by the statement contains four columns:shop_name,total_price,sale_time, and_pt_col_0_. The_pt_col_0_column is the automatically generated partition column, and its data type is STRING. You can run theDESC sale_detail;statement to view the table information. The following is an example of the result:+------------------------------------------------------------------------------------+ | Owner: ALIYUN$*** | | Project: *** | | TableComment: | +------------------------------------------------------------------------------------+ | CreateTime: 2025-06-26 11:21:55 | | LastDDLTime: 2025-06-26 11:21:55 | | LastModifiedTime: 2025-06-26 11:21:55 | +------------------------------------------------------------------------------------+ | InternalTable: YES | Size: 0 | +------------------------------------------------------------------------------------+ | Native Columns: | +------------------------------------------------------------------------------------+ | Field | Type | Label | Comment | +------------------------------------------------------------------------------------+ | shop_name | string | | | | total_price | double | | | | sale_time | timestamp | | | +------------------------------------------------------------------------------------+ | Partition Columns: | +------------------------------------------------------------------------------------+ | _pt_col_0_ | string | | +------------------------------------------------------------------------------------+NoteTo view the partition generation calculation logic of the AUTO PARTITION column, you can use SHOW CREATE TABLE.
Example 2: Explicitly specify the name of the generated partition column.
Create an automatic partition table
sale_detail2, adding an explicitly specified partition column namesale_datebased on Example 1.CREATE TABLE IF NOT EXISTS sale_detail2( shop_name STRING, total_price DOUBLE, sale_time TIMESTAMP ) AUTO PARTITIONED BY (TRUNC_TIME(sale_time, 'day') AS sale_date);Example 3: If the input data column of the TRUNC_TIME function is of DATE data type and partitioned by day, the following simplified syntax is supported.
CREATE TABLE IF NOT EXISTS sale_detail3( shop_name STRING, total_price DOUBLE, sale_date DATE ) AUTO PARTITIONED BY (TRUNC_TIME(sale_date , 'day')); -- The above SQL statement can be simplified to CREATE TABLE IF NOT EXISTS sale_detail3( shop_name STRING, total_price DOUBLE, sale_date DATE ) AUTO PARTITIONED BY (sale_date);ImportantThis simplified syntax only supports DATE data type, not DATETIME/TIMESTAMP/TIMESTAMP_NTZ data types.
Write data to an automatic partition table based on time computing functions
When users use SQL statements to write data to an AUTO PARTITION table, the system automatically generates calculation expressions based on the partition definition specified when the table was created, and then determines the corresponding partition values based on the data in the table. Users cannot explicitly specify partition values.
Example: Write data to the partition table sale_detail2 created based on time computing functions.
-- Set the project local time zone to East Eight Zone.
set odps.sql.timezone=Asia/Shanghai;
-- Same as the creation method of the partition table sale_detail2 above.
CREATE TABLE IF NOT EXISTS sale_detail2(
shop_name STRING,
total_price DOUBLE,
sale_time TIMESTAMP )
AUTO PARTITIONED BY (TRUNC_TIME(sale_time, 'day') AS sale_date);
INSERT INTO sale_detail2 VALUES
('chongqin_shop',101101,timestamp '2025-02-04 01:15:30'),
('shenzhen_shop',202202,timestamp '2024-03-29 15:30:30'),
('hangzhou_shop',303303,timestamp '2025-02-04 08:30:30'),
('shanghai_shop',404404,timestamp '2025-02-28 01:50:30');
In this example, the correspondence between the values of the sale_time time data column and the partition values generated after TRUNC_TIME calculation is as follows:
sale_time column-Asia/Shanghai time zone | Partition column sale_date value (generated after truncating the time column to day, representing UTC time) |
2025-02-04 01:15:30 | 2025-02-03 |
2024-03-29 15:30:30 | 2024-03-29 |
2025-02-04 08:30:30 | 2025-02-04 |
2025-02-28 01:50:30 | 2025-02-27 |
Note that for time zone-related data types such as DATETIME/TIMESTAMP, TRUNC_TIME converts local time to UTC time before truncation.
In this example, the project local time zone is set to East Eight Zone using
set odps.sql.timezone=Asia/Shanghai;. The time zone affects the partition values generated by TRUNC_TIME truncation.
Query an automatic partition table based on time computing functions
Example 1: When executing
select *query, the generated partition column is not displayed by default.-- Enable full table scan set odps.sql.allow.fullscan=true; SELECT * FROM sale_detail2; -- Result +------------+-------------+------------+ | shop_name | total_price | sale_time | +------------+-------------+------------+ | hangzhou_shop | 303303.0 | 2025-02-04 08:30:30 | | shanghai_shop | 404404.0 | 2025-02-28 01:50:30 | | shenzhen_shop | 202202.0 | 2024-03-29 15:30:30 | | chongqin_shop | 101101.0 | 2025-02-04 01:15:30 | +------------+-------------+------------+Example 2: To display the generated partition column in the output, run
SELECT *,sale_date from sale_detail2;. Thesale_datecolumn is the name of the partition column that was specified when the table was created.SELECT *,sale_date from sale_detail2; -- Result +------------+-------------+------------+------------+ | shop_name | total_price | sale_time | sale_date | +------------+-------------+------------+------------+ | shanghai_shop | 404404.0 | 2025-02-28 01:50:30 | 2025-02-27 | | hangzhou_shop | 303303.0 | 2025-02-04 08:30:30 | 2025-02-04 | | shenzhen_shop | 202202.0 | 2024-03-29 15:30:30 | 2024-03-29 | | chongqin_shop | 101101.0 | 2025-02-04 01:15:30 | 2025-02-03 | +------------+-------------+------------+------------+
Partition pruning for automatic partition tables based on time computing functions
When querying automatic partition tables, partition pruning is supported only in the following scenarios.
You can check the SQL execution plan in Logview or use the EXPLAIN command to determine whether partition pruning is working. For more information, see Evaluate the effectiveness of partition pruning.
Scenario 1: Using the generated partition column as a filter condition, partition pruning is supported during query execution.
For the table sale_detail2, you can filter data through the generated partition column sale_date when querying. Using the filter condition WHERE sale_date > '2025-02-03';, only partitions that meet the filter condition sale_date > '2025-02-03' are scanned.
SELECT * FROM sale_detail2 WHERE sale_date > '2025-02-03';
-- Result
+------------+-------------+------------+
| shop_name | total_price | sale_time |
+------------+-------------+------------+
| shanghai_shop | 404404.0 | 2025-02-28 01:50:30 |
| hangzhou_shop | 303303.0 | 2025-02-04 08:30:30 |
+------------+-------------+------------+Scenario 2: Using the time/date type data column that generates the partition column (without applying functions to this column) as a filter condition, partition pruning is supported during query execution.
For the table sale_detail2, you can filter data through the time data column sale_time in the table. Using the filter condition WHERE sale_time > '2025-02-04 08:00:00';, the system determines which partition data meets the scan condition for sale_time and only scans the corresponding partitions that meet the condition.
SELECT * FROM sale_detail2 WHERE sale_time > '2025-02-04 08:00:00';
-- Result
+------------+-------------+------------+
| shop_name | total_price | sale_time |
+------------+-------------+------------+
| shanghai_shop | 404404.0 | 2025-02-28 01:50:30 |
| hangzhou_shop | 303303.0 | 2025-02-04 08:30:30 |
+------------+-------------+------------+Scenario 3: Applying function calculations to the time/date type data column used to generate the partition column when creating the table, and using the calculation results as filter conditions. Currently, partition pruning is supported in some function calculation scenarios.
Functions that support partition pruning | Conditions for partition pruning to take effect |
Partition pruning is supported when the time truncation granularity is the same as in the table creation statement. | |
Partition pruning is supported when the time truncation granularity is the same as in the table creation statement. | |
CAST AS DATE | Partition pruning is supported when the truncation granularity for partition generation in the table creation is day. |
Partition pruning is supported when the truncation granularity for partition generation in the table creation is day. |
Example 1: When you filter the
sale_timetime column using the TRUNC_TIME function or the DATETRUNC function without a time zone parameter, and the time truncation granularity is the same as that in the table creation statement, partition pruning takes effect.SELECT * FROM sale_detail2 WHERE TRUNC_TIME(sale_time, 'day') > '2025-02-04'; SELECT * FROM sale_detail2 WHERE DATETRUNC(sale_time, 'dd') > '2025-02-04 00:00:00'; -- Result +------------+-------------+------------+ | shop_name | total_price | sale_time | +------------+-------------+------------+ | shanghai_shop | 404404.0 | 2025-02-28 01:50:30 | +------------+-------------+------------+Example 2: Partition pruning is effective if you filter the
sale_timecolumn using CAST AS DATE or the TO_DATE function without a time zone parameter. For this to work, the truncation granularity specified when the table was created must be `day`.SELECT * FROM sale_detail2 WHERE CAST(sale_time AS date) = '2025-02-04'; SELECT * FROM sale_detail2 WHERE TO_DATE(sale_time) = '2025-02-04'; -- Result +------------+-------------+------------+ | shop_name | total_price | sale_time | +------------+-------------+------------+ | hangzhou_shop | 303303.0 | 2025-02-04 08:30:30 | +------------+-------------+------------+