Skip to content

Commit

Permalink
[enhance](auto-partition) Constrain dynamic & auto partition use same…
Browse files Browse the repository at this point in the history
… interval unit if both enable (#30426)
  • Loading branch information
zclllyybb authored Jan 29, 2024
1 parent 9f30ebc commit c1305e4
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 1 deletion.
58 changes: 58 additions & 0 deletions docs/en/docs/advanced/partition/auto-partition.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,64 @@ mysql> show partitions from `DAILY_TRADE_VALUE`;

A partition created by the AUTO PARTITION function has the exact same functional properties as a manually created partition.

## Coupled with dynamic partitioning

AUTO PARTITION is supported on the same table as [DYNAMIC PARTITION](./dynamic-partition). for example:

```sql
CREATE TABLE tbl3
(
k1 DATETIME NOT NULL,
col1 int
)
AUTO PARTITION BY RANGE date_trunc(`k1`, 'year') ()
DISTRIBUTED BY HASH(k1)
PROPERTIES
(
"replication_num" = "1",
"dynamic_partition.create_history_partition"="true",
"dynamic_partition.enable" = "true",
"dynamic_partition.time_unit" = "year",
"dynamic_partition.start" = "-2",
"dynamic_partition.end" = "2",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "8"
);
```

When the two functions are used in combination, neither of their original functions is affected and they still act on the entire table. The behavior includes but is not limited to:

1. regardless of the creation method, the expired historical partitions will be periodically cleaned up or transferred to cold storage according to the rules specified by the DYNAMIC PARTITION properties
2. partition ranges cannot overlap or conflict. If a new partition range that needs to be created by DYNAMIC PARTITION has already been covered by an automatically or manually created partition, the partition creation will fail without affecting the business process.

The principle is that AUTO PARTITION is only a complementary means introduced to the creation of partitions, and that a partition, whether created manually, by AUTO PARTITION, or by DYNAMIC PARTITION, will be governed by DYNAMIC PARTITION functions.

### Constraint

In order to simplify the behavioral pattern of combine two partition methods, when the AUTO PARTITION and DYNAMIC PARTITION are both used, the **partition intervals of the two must be consistent** or the table creating will fail:

```sql
mysql > CREATE TABLE tbl3
(
k1 DATETIME NOT NULL,
col1 int
)
AUTO PARTITION BY RANGE date_trunc(`k1`, 'year') ()
DISTRIBUTED BY HASH(k1)
PROPERTIES
(
"replication_num" = "1",
"dynamic_partition.create_history_partition"="true",
"dynamic_partition.enable" = "true",
"dynamic_partition.time_unit" = "HOUR",
"dynamic_partition.start" = "-2",
"dynamic_partition.end" = "2",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "8"
);
ERROR 1105 (HY000): errCode = 2, detailMessage = errCode = 2, detailMessage = If support auto partition and dynamic partition at same time, they must have the same interval unit.
```

## caveat

- If a partition is created during the insertion or import of data and the entire import process does not complete (fails or is cancelled), the created partition is not automatically deleted.
Expand Down
58 changes: 58 additions & 0 deletions docs/zh-CN/docs/advanced/partition/auto-partition.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,64 @@ mysql> show partitions from `DAILY_TRADE_VALUE`;

经过自动分区功能所创建的PARTITION,与手动创建的PARTITION具有完全一致的功能性质。

## 与动态分区联用

自动分区支持与[动态分区](./dynamic-partition)同时作用于同一张表上,例如:

```sql
CREATE TABLE tbl3
(
k1 DATETIME NOT NULL,
col1 int
)
AUTO PARTITION BY RANGE date_trunc(`k1`, 'year') ()
DISTRIBUTED BY HASH(k1)
PROPERTIES
(
"replication_num" = "1",
"dynamic_partition.create_history_partition"="true",
"dynamic_partition.enable" = "true",
"dynamic_partition.time_unit" = "year",
"dynamic_partition.start" = "-2",
"dynamic_partition.end" = "2",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "8"
);
```

当两种功能联用时,它们的原始功能均不受影响,依旧作用于整张表上,行为包括但不限于:

1. 无论创建方式如何,过期的历史分区都会按动态分区功能指定的规则定期清理或转入冷存储
2. 分区范围不能重叠、冲突。如果动态分区需要创建的新分区范围已经被自动或手动创建的分区覆盖,则该分区创建会失败,但不影响业务过程。

其原则在于,自动分区仅是对创建分区引入的一种补充手段,一个分区无论是手动、经自动分区创建,还是经动态分区创建的,均会受到动态分区的管理。

### 限制

为简化两种分区方式联用的行为模式,当前自动分区和动态分区联用时,两者的**分区间隔必须一致**,否则建表将会失败:

```sql
mysql > CREATE TABLE tbl3
(
k1 DATETIME NOT NULL,
col1 int
)
AUTO PARTITION BY RANGE date_trunc(`k1`, 'year') ()
DISTRIBUTED BY HASH(k1)
PROPERTIES
(
"replication_num" = "1",
"dynamic_partition.create_history_partition"="true",
"dynamic_partition.enable" = "true",
"dynamic_partition.time_unit" = "HOUR",
"dynamic_partition.start" = "-2",
"dynamic_partition.end" = "2",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "8"
);
ERROR 1105 (HY000): errCode = 2, detailMessage = errCode = 2, detailMessage = If support auto partition and dynamic partition at same time, they must have the same interval unit.
```

## 注意事项

- 在数据的插入或导入过程中如果创建了分区,而整个导入过程没有完成(失败或被取消),被创建的分区不会被自动删除。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ public PartitionType getType() {
return type;
}

public ArrayList<Expr> getPartitionExprs() {
return partitionExprs;
}

public boolean isAutoCreatePartitions() {
return isAutoCreatePartitions;
}

public String toSql() {
throw new NotImplementedException("toSql not implemented");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.doris.analysis.FunctionCallExpr;
import org.apache.doris.analysis.HashDistributionDesc;
import org.apache.doris.analysis.KeysDesc;
import org.apache.doris.analysis.LiteralExpr;
import org.apache.doris.analysis.PartitionDesc;
import org.apache.doris.analysis.PartitionKeyDesc;
import org.apache.doris.analysis.QueryStmt;
Expand All @@ -64,6 +65,7 @@
import org.apache.doris.catalog.DatabaseProperty;
import org.apache.doris.catalog.DistributionInfo;
import org.apache.doris.catalog.DistributionInfo.DistributionInfoType;
import org.apache.doris.catalog.DynamicPartitionProperty;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.EnvFactory;
import org.apache.doris.catalog.EsTable;
Expand Down Expand Up @@ -2606,7 +2608,27 @@ private void createOlapTable(Database db, CreateTableStmt stmt) throws UserExcep
"Only support dynamic partition properties on range partition table");
}
}

// check the interval same between dynamic & auto range partition
DynamicPartitionProperty dynamicProperty = olapTable.getTableProperty()
.getDynamicPartitionProperty();
if (dynamicProperty.isExist() && dynamicProperty.getEnable()
&& partitionDesc.isAutoCreatePartitions()) {
String dynamicUnit = dynamicProperty.getTimeUnit();
ArrayList<Expr> autoExprs = partitionDesc.getPartitionExprs();
for (Expr autoExpr : autoExprs) {
Expr func = (FunctionCallExpr) autoExpr;
for (Expr child : func.getChildren()) {
if (child instanceof LiteralExpr) {
String autoUnit = ((LiteralExpr) child).getStringValue();
if (!dynamicUnit.equalsIgnoreCase(autoUnit)) {
throw new AnalysisException(
"If support auto partition and dynamic partition at same time, "
+ "they must have the same interval unit.");
}
}
}
}
}
} catch (AnalysisException e) {
throw new DdlException(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,54 @@ suite("test_auto_partition_behavior") {
"""
exception "The auto partition column must be NOT NULL"
}

// PROHIBIT different timeunit of interval when use both auto & dynamic partition
test{
sql "set experimental_enable_nereids_planner=true;"
sql """
CREATE TABLE tbl3
(
k1 DATETIME NOT NULL,
col1 int
)
auto PARTITION BY RANGE date_trunc(`k1`, 'year') ()
DISTRIBUTED BY HASH(k1)
PROPERTIES
(
"replication_num" = "1",
"dynamic_partition.create_history_partition"="true",
"dynamic_partition.enable" = "true",
"dynamic_partition.time_unit" = "HOUR",
"dynamic_partition.start" = "-2",
"dynamic_partition.end" = "2",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "8"
);
"""
exception "If support auto partition and dynamic partition at same time, they must have the same interval unit."
}
test{
sql "set experimental_enable_nereids_planner=false;"
sql """
CREATE TABLE tbl3
(
k1 DATETIME NOT NULL,
col1 int
)
auto PARTITION BY RANGE date_trunc(`k1`, 'year') ()
DISTRIBUTED BY HASH(k1)
PROPERTIES
(
"replication_num" = "1",
"dynamic_partition.create_history_partition"="true",
"dynamic_partition.enable" = "true",
"dynamic_partition.time_unit" = "HOUR",
"dynamic_partition.start" = "-2",
"dynamic_partition.end" = "2",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "8"
);
"""
exception "If support auto partition and dynamic partition at same time, they must have the same interval unit."
}
}

0 comments on commit c1305e4

Please sign in to comment.