diff --git a/docs/reference/sql/functions/overview.md b/docs/reference/sql/functions/overview.md index 6b2c2a232..e3ac65c74 100644 --- a/docs/reference/sql/functions/overview.md +++ b/docs/reference/sql/functions/overview.md @@ -18,6 +18,7 @@ Use this page as a quick index to GreptimeDB function references. - [Vector Functions](./vector.md) - [Approximate Functions](./approximate.md) - [Anomaly Detection Functions](./anomaly.md) + - [Partitioning Functions](./partition.md) - [AI Functions (experimental)](./ai/overview.md) - [ai_match — natural-language matching probability](./ai/functions.md#ai_match) - [ai_choose — classification](./ai/functions.md#ai_choose) diff --git a/docs/reference/sql/functions/partition.md b/docs/reference/sql/functions/partition.md new file mode 100644 index 000000000..6fdd4ad9f --- /dev/null +++ b/docs/reference/sql/functions/partition.md @@ -0,0 +1,154 @@ +--- +keywords: [partitioning functions, iceberg_hash, hash partitioning, Murmur3, partition rules, hash breakpoints, SQL functions] +description: Describes the iceberg_hash family of partitioning functions used to build hash partition rules, including iceberg_hash, iceberg_hash_bounds, iceberg_hash_breakpoints, and iceberg_hash_ranges_sql. +--- + +# Partitioning Functions + +GreptimeDB can partition a table by the hash of a column with `iceberg_hash` partition rules, as described in [Hash partitioning](/user-guide/deployments-administration/manage-data/table-sharding.md#hash-partitioning). The functions on this page are the SQL forms of that hash. They let you measure what a value hashes to, split the hash space into buckets, and render a complete rule list that can be pasted into `PARTITION ON COLUMNS`: + +| Function | Purpose | Result | +| --- | --- | --- | +| [`iceberg_hash(value)`](#iceberg_hash) | Hash of one string or integer value | `INT32` hash, `NULL` for `NULL` | +| [`iceberg_hash_bounds(n)`](#iceberg_hash_bounds) | Breakpoints of an equal-width `n`-bucket split | Comma-separated `INT32` literals | +| [`iceberg_hash_breakpoints(v1, v2, ...)`](#iceberg_hash_breakpoints) | Hashes of the given constant values | Sorted, deduplicated `INT32` literals | +| [`iceberg_hash_ranges_sql(column, n)`](#iceberg_hash_ranges_sql) | Complete partition rule list for `n` buckets | One rule per line, ready to paste | + +All four functions are immutable and only produce values or text: none of them creates or changes a partition. + +:::note Availability +The functions on this page and `iceberg_hash` partition rules are available in Nightly builds. They are not included in GreptimeDB v1.3.0 or v1.3.1. +::: + +## The Iceberg hash + +`iceberg_hash` is the 32-bit Murmur3 hash (seed 0) defined by the [Apache Iceberg specification, appendix B](https://iceberg.apache.org/spec/#appendix-b-32-bit-hash-requirements). Strings are hashed over their UTF-8 bytes, integers are hashed as 8-byte little-endian longs, and the result is an `INT32`. The same hash is implemented by Trino and by ClickHouse's `icebergHash`, so a row's bucket can be recomputed outside GreptimeDB. + +The SQL function and the partition rule planner share one implementation, so `SELECT iceberg_hash(...)` always agrees with how rows are routed to partitions. + +### `iceberg_hash` + +`iceberg_hash(value)` returns the hash of one value as `INT32`. The value can be a `STRING` or a signed or unsigned integer (`INT8` through `INT64`, `UINT8` through `UINT64`), the same domain a partition rule accepts. `NULL` hashes to `NULL`. + +```sql +SELECT iceberg_hash('iceberg'), iceberg_hash(34), iceberg_hash(NULL); +``` + +```sql ++-------------------------------+-------------------------+--------------------+ +| iceberg_hash(Utf8("iceberg")) | iceberg_hash(Int64(34)) | iceberg_hash(NULL) | ++-------------------------------+-------------------------+--------------------+ +| 1210000089 | 2017239379 | | ++-------------------------------+-------------------------+--------------------+ +``` + +Use it to check which bucket a value falls into, or to inspect the hash distribution of an existing column before choosing a bucket layout. + +### `iceberg_hash_bounds` + +`iceberg_hash_bounds(n)` returns the `n - 1` breakpoints of an equal-width split of the `INT32` hash space, as a comma-separated string. Breakpoint `i` is `INT32_MIN + floor(i * 2^32 / n)`, so each bucket covers an equally wide slice of the hash space. With many distinct keys, the hash usually spreads keys evenly across the buckets, but rows with the same key always share a bucket; hot or frequently repeated keys can still create hot partitions. + +The argument must be a constant integer between 2 and 1024; any integer width works. + +```sql +SELECT iceberg_hash_bounds(2), iceberg_hash_bounds(4); +``` + +```sql ++-------------------------------+-------------------------------+ +| iceberg_hash_bounds(Int64(2)) | iceberg_hash_bounds(Int64(4)) | ++-------------------------------+-------------------------------+ +| 0 | -1073741824, 0, 1073741824 | ++-------------------------------+-------------------------------+ +``` + +`iceberg_hash_bounds(2)` returns `0` because a single breakpoint splits the hash space into two halves: values whose hash is below `0` fall in the first bucket, the rest in the second. + +### `iceberg_hash_breakpoints` + +`iceberg_hash_breakpoints(v1, v2, ...)` hashes the given constant values and returns them sorted and deduplicated, as a comma-separated string. Use it to build buckets around known key values—for example, hotspot hosts—instead of around equal widths. + +The values must all be constants and must all come from one hash encoding: all strings, or all integers (integer widths may mix). A string and an integer hash through different encodings, so mixing them is rejected. `NULL` is not a valid breakpoint—a `NULL` hash never matches a comparison bound—so it is rejected as well, and at least one value is required. + +```sql +SELECT iceberg_hash_breakpoints('iceberg', 'a', 'iceberg'); +``` + +```sql ++---------------------------------------------------------------------+ +| iceberg_hash_breakpoints(Utf8("iceberg"),Utf8("a"),Utf8("iceberg")) | ++---------------------------------------------------------------------+ +| 1009084850, 1210000089 | ++---------------------------------------------------------------------+ +``` + +Duplicate hash values are removed, so the output contains one breakpoint per distinct hash value. Different input values share a breakpoint if their hashes collide. + +### `iceberg_hash_ranges_sql` + +`iceberg_hash_ranges_sql(column, n)` renders the complete rule list for an equal-width `n`-bucket split of `column`, one rule per line, ready to be pasted into `PARTITION ON COLUMNS`. The column name is rendered as a quoted SQL identifier with embedded quotes escaped, so names that are not plain lowercase identifiers—`host-name`, `HOST`, `a b`—round-trip through the paste. + +The first argument is the raw column name as a string; the second is a constant bucket count subject to the same `[2, 1024]` range as `iceberg_hash_bounds`. + +```sql +SELECT iceberg_hash_ranges_sql('host-name', 4); +``` + +```sql ++-----------------------------------------------------------------------------+ +| iceberg_hash_ranges_sql(Utf8("host-name"),Int64(4)) | ++-----------------------------------------------------------------------------+ +| iceberg_hash("host-name") < -1073741824, | +| iceberg_hash("host-name") >= -1073741824 AND iceberg_hash("host-name") < 0, | +| iceberg_hash("host-name") >= 0 AND iceberg_hash("host-name") < 1073741824, | +| iceberg_hash("host-name") >= 1073741824 | ++-----------------------------------------------------------------------------+ +``` + +## Build a hash-partitioned table + +To create a table partitioned by the hash of a column, generate the rule list, then paste it into the table definition: + +```sql +SELECT iceberg_hash_ranges_sql('host', 4); +``` + +```sql +iceberg_hash("host") < -1073741824, +iceberg_hash("host") >= -1073741824 AND iceberg_hash("host") < 0, +iceberg_hash("host") >= 0 AND iceberg_hash("host") < 1073741824, +iceberg_hash("host") >= 1073741824 +``` + +```sql +CREATE TABLE requests ( + host STRING, + ts TIMESTAMP DEFAULT current_timestamp() TIME INDEX, + PRIMARY KEY (host) +) +PARTITION ON COLUMNS (host) ( + iceberg_hash(host) < -1073741824, + iceberg_hash(host) >= -1073741824 AND iceberg_hash(host) < 0, + iceberg_hash(host) >= 0 AND iceberg_hash(host) < 1073741824, + iceberg_hash(host) >= 1073741824 +); +``` + +To build buckets around specific values instead of equal widths, hash the values with `iceberg_hash_breakpoints(...)`, then use each returned hash as an upper bound and add a final rule with no upper bound. For example, the two breakpoints shown above produce three rules: + +```sql +iceberg_hash(host) < 1009084850, +iceberg_hash(host) >= 1009084850 AND iceberg_hash(host) < 1210000089, +iceberg_hash(host) >= 1210000089 +``` + +Use the breakpoints in the returned order when writing the rules. + +:::tip NOTE +`NULL` hashes to `NULL` and never matches a comparison bound, so rows with `NULL` in the hashed column always land in the first partition (partition `p0`). +::: + +:::warning Limitations +- Only `STRING` and integer columns can be hashed; other types fail at execution, as in a partition rule. +- Filters on a hashed partition column do not prune regions. A query such as `WHERE host = 'x'` scans every hash range; only conditions on other columns prune. +::: diff --git a/docs/user-guide/deployments-administration/manage-data/table-sharding.md b/docs/user-guide/deployments-administration/manage-data/table-sharding.md index a6c9438ca..aa520684b 100644 --- a/docs/user-guide/deployments-administration/manage-data/table-sharding.md +++ b/docs/user-guide/deployments-administration/manage-data/table-sharding.md @@ -59,7 +59,7 @@ The syntax mainly consists of two parts: 1. `PARTITION ON COLUMNS` followed by a comma-separated list of column names. This specifies which columns will be used for partitioning. Currently, any column defined in the table can be used as a partition column. Note that the ranges of all partitions must **not** overlap. -2. `RULE LIST` is a list of multiple partition rules. Each rule is a partition condition, and GreptimeDB generates partition names such as `p0`, `p1`, and so on for these rules. The expressions here can use `=`, `!=`, `>`, `>=`, `<`, `<=`, `AND`, `OR`, column names, and literals. +2. `RULE LIST` is a list of multiple partition rules. Each rule is a partition condition, and GreptimeDB generates partition names such as `p0`, `p1`, and so on for these rules. The expressions here can use `=`, `!=`, `>`, `>=`, `<`, `<=`, `AND`, `OR`, column names, and literals. The column side of a comparison can also wrap the column in `iceberg_hash(...)`; see [Hash partitioning](#hash-partitioning) below. :::tip Choosing partition columns We recommend partitioning on Tag columns (the primary key columns). This matters most for tables that are **not** append-only (deduplication is enabled): the partition columns should be a subset of the primary key columns. That way, rows with the same primary key always go to the same partition, so deduplication and merging keep working correctly. @@ -69,6 +69,52 @@ We recommend partitioning on Tag columns (the primary key columns). This matters The parentheses in `PARTITION ON COLUMNS (...)` only accept column names (e.g., `device_id`, `area`), not expressions like `device_id + 1`. GreptimeDB does not support MySQL's `PARTITION BY RANGE` syntax. ::: +## Hash partitioning + +:::note Availability +Hash partitioning with `iceberg_hash` rules is available in Nightly builds. It is not included in GreptimeDB v1.3.0 or v1.3.1. +::: + +Range rules on raw values need hand-written ranges, and how evenly rows spread across them depends on the value distribution. A rule can instead compare the hash of a partition column against `INT32` literals: wrap the column in `iceberg_hash(...)` and split the hash space into equal-width buckets. This usually distributes distinct keys evenly, but rows with the same key always share a bucket, so hot or frequently repeated keys can still create hot partitions. + +```sql +CREATE TABLE requests ( + host STRING, + ts TIMESTAMP DEFAULT current_timestamp(), + v DOUBLE, + TIME INDEX (ts), + PRIMARY KEY (host) +) +PARTITION ON COLUMNS (host) ( + iceberg_hash(host) < -1073741824, + iceberg_hash(host) >= -1073741824 AND iceberg_hash(host) < 0, + iceberg_hash(host) >= 0 AND iceberg_hash(host) < 1073741824, + iceberg_hash(host) >= 1073741824 +); +``` + +`iceberg_hash` is the 32-bit Murmur3 hash defined by the Iceberg specification and can be applied to `STRING` and integer columns. `NULL` hashes to `NULL`, so rows with `NULL` in the hashed column always land in the first partition. The column list in `PARTITION ON COLUMNS` still names the raw column; the hash only appears inside the rules. + +You do not need to compute the hash breakpoints by hand. The [partitioning functions](/reference/sql/functions/partition.md) generate them for you: + +```sql +SELECT iceberg_hash_ranges_sql('host', 4); +``` + +The output is the complete rule list, ready to paste into the table definition: + +```sql +iceberg_hash("host") < -1073741824, +iceberg_hash("host") >= -1073741824 AND iceberg_hash("host") < 0, +iceberg_hash("host") >= 0 AND iceberg_hash("host") < 1073741824, +iceberg_hash("host") >= 1073741824 +``` + +:::warning Limitations +- Filters on a hashed partition column do not prune regions: a query such as `WHERE host = 'x'` scans every hash range. Conditions on other columns still prune. +- Tables created with `iceberg_hash` rules require every node in the cluster to run a version that supports `iceberg_hash`; older nodes cannot read such partition metadata. +::: + ## Create a partitioned table You can use the MySQL CLI to [connect to GreptimeDB](/user-guide/protocols/mysql.md) and create a distributed table. diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/functions/overview.md b/i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/functions/overview.md index 7a10cd9b3..9328ee671 100644 --- a/i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/functions/overview.md +++ b/i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/functions/overview.md @@ -18,6 +18,7 @@ description: 提供了 GreptimeDB 中函数的概述,包括函数的分类、 - [向量函数](./vector.md) - [近似函数](./approximate.md) - [异常检测函数](./anomaly.md) + - [分区函数](./partition.md) - [AI 函数(实验功能)](./ai/overview.md) - [ai_match — 自然语言匹配概率](./ai/functions.md#ai_match) - [ai_choose — 分类](./ai/functions.md#ai_choose) diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/functions/partition.md b/i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/functions/partition.md new file mode 100644 index 000000000..5ec480972 --- /dev/null +++ b/i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/functions/partition.md @@ -0,0 +1,154 @@ +--- +keywords: [分区函数, iceberg_hash, 哈希分区, Murmur3, 分区规则, 哈希断点, SQL 函数] +description: 介绍用于构建哈希分区规则的 iceberg_hash 系列分区函数,包括 iceberg_hash、iceberg_hash_bounds、iceberg_hash_breakpoints 和 iceberg_hash_ranges_sql。 +--- + +# 分区函数 + +GreptimeDB 可以按列的哈希值对表进行分区,即 `iceberg_hash` 分区规则,详见[哈希分区](/user-guide/deployments-administration/manage-data/table-sharding.md#哈希分区)。本页介绍的函数就是该哈希的 SQL 形式,用于查看某个值哈希后的结果、把哈希空间切分成若干桶,以及生成可直接粘贴进 `PARTITION ON COLUMNS` 的完整规则列表: + +| 函数 | 用途 | 结果 | +| --- | --- | --- | +| [`iceberg_hash(value)`](#iceberg_hash) | 计算单个字符串或整数值的哈希 | `INT32` 哈希值,`NULL` 返回 `NULL` | +| [`iceberg_hash_bounds(n)`](#iceberg_hash_bounds) | 生成等宽 `n` 桶切分的断点 | 逗号分隔的 `INT32` 字面量 | +| [`iceberg_hash_breakpoints(v1, v2, ...)`](#iceberg_hash_breakpoints) | 计算给定常量值的哈希 | 排序并去重后的 `INT32` 字面量 | +| [`iceberg_hash_ranges_sql(column, n)`](#iceberg_hash_ranges_sql) | 生成 `n` 桶的完整分区规则列表 | 每行一条规则,可直接粘贴 | + +这四个函数都是不可变的,只产生值或文本:它们都不会创建或修改分区。 + +:::note 可用版本 +本页函数及 `iceberg_hash` 分区规则目前仅在 Nightly 版本中提供,不包含在 GreptimeDB v1.3.0 和 v1.3.1 中。 +::: + +## Iceberg 哈希 + +`iceberg_hash` 是 [Apache Iceberg 规范附录 B](https://iceberg.apache.org/spec/#appendix-b-32-bit-hash-requirements) 定义的 32 位 Murmur3 哈希(种子为 0)。字符串按其 UTF-8 字节哈希,整数按 8 字节小端 long 哈希,结果为 `INT32`。Trino 和 ClickHouse 的 `icebergHash` 实现的是同一种哈希,因此一行的分桶结果可以在 GreptimeDB 之外重新计算。 + +SQL 函数与分区规则规划器共用同一实现,因此 `SELECT iceberg_hash(...)` 的结果始终与行的实际路由方式一致。 + +### `iceberg_hash` + +`iceberg_hash(value)` 返回单个值的哈希,类型为 `INT32`。值可以是 `STRING` 或有符号/无符号整数(`INT8` 到 `INT64`、`UINT8` 到 `UINT64`),与分区规则接受的取值域相同。`NULL` 哈希后仍为 `NULL`。 + +```sql +SELECT iceberg_hash('iceberg'), iceberg_hash(34), iceberg_hash(NULL); +``` + +```sql ++-------------------------------+-------------------------+--------------------+ +| iceberg_hash(Utf8("iceberg")) | iceberg_hash(Int64(34)) | iceberg_hash(NULL) | ++-------------------------------+-------------------------+--------------------+ +| 1210000089 | 2017239379 | | ++-------------------------------+-------------------------+--------------------+ +``` + +可以用它检查某个值落在哪个桶里,也可以在选择桶布局之前查看现有列的哈希分布。 + +### `iceberg_hash_bounds` + +`iceberg_hash_bounds(n)` 返回将 `INT32` 哈希空间等宽切成 `n` 份所需的 `n - 1` 个断点,以逗号分隔的字符串表示。第 `i` 个断点为 `INT32_MIN + floor(i * 2^32 / n)`,因此每个桶覆盖的哈希空间宽度相同。当不同键值较多时,哈希通常能让键值均匀分布到各个桶中;但相同键值的行始终进入同一个桶,热点键值或大量重复值仍可能造成热点分区。 + +参数必须是 2 到 1024 之间的常量整数,任意整数宽度均可。 + +```sql +SELECT iceberg_hash_bounds(2), iceberg_hash_bounds(4); +``` + +```sql ++-------------------------------+-------------------------------+ +| iceberg_hash_bounds(Int64(2)) | iceberg_hash_bounds(Int64(4)) | ++-------------------------------+-------------------------------+ +| 0 | -1073741824, 0, 1073741824 | ++-------------------------------+-------------------------------+ +``` + +`iceberg_hash_bounds(2)` 返回 `0`,因为单个断点把哈希空间分成两半:哈希值小于 `0` 的值进入第一个桶,其余进入第二个桶。 + +### `iceberg_hash_breakpoints` + +`iceberg_hash_breakpoints(v1, v2, ...)` 对给定的常量值计算哈希,排序并去重后以逗号分隔的字符串返回。用它可以根据已知的关键值(例如热点主机)来划分桶,而不是按等宽切分。 + +所有值必须是常量,且必须来自同一种哈希编码:全部是字符串,或全部是整数(整数宽度可以混用)。字符串和整数的哈希编码不同,混用会被拒绝。`NULL` 不是合法的断点——`NULL` 哈希永远不会匹配比较边界——同样会被拒绝;此外至少需要提供一个值。 + +```sql +SELECT iceberg_hash_breakpoints('iceberg', 'a', 'iceberg'); +``` + +```sql ++---------------------------------------------------------------------+ +| iceberg_hash_breakpoints(Utf8("iceberg"),Utf8("a"),Utf8("iceberg")) | ++---------------------------------------------------------------------+ +| 1009084850, 1210000089 | ++---------------------------------------------------------------------+ +``` + +重复的哈希值会被去除,因此输出中每个不同的哈希值只对应一个断点;如果不同输入值的哈希发生碰撞,它们会共用一个断点。 + +### `iceberg_hash_ranges_sql` + +`iceberg_hash_ranges_sql(column, n)` 为 `column` 生成等宽 `n` 桶切分的完整规则列表,每行一条规则,可直接粘贴进 `PARTITION ON COLUMNS`。列名会按带引号的 SQL 标识符输出并转义内部引号,因此 `host-name`、`HOST`、`a b` 等不是简单小写标识符的名称也能原样往返。 + +第一个参数是字符串形式的原始列名;第二个参数是常量桶数,取值范围与 `iceberg_hash_bounds` 相同,为 `[2, 1024]`。 + +```sql +SELECT iceberg_hash_ranges_sql('host-name', 4); +``` + +```sql ++-----------------------------------------------------------------------------+ +| iceberg_hash_ranges_sql(Utf8("host-name"),Int64(4)) | ++-----------------------------------------------------------------------------+ +| iceberg_hash("host-name") < -1073741824, | +| iceberg_hash("host-name") >= -1073741824 AND iceberg_hash("host-name") < 0, | +| iceberg_hash("host-name") >= 0 AND iceberg_hash("host-name") < 1073741824, | +| iceberg_hash("host-name") >= 1073741824 | ++-----------------------------------------------------------------------------+ +``` + +## 构建哈希分区表 + +要创建按列哈希分区的表,先生成规则列表,再把它粘贴到表定义中: + +```sql +SELECT iceberg_hash_ranges_sql('host', 4); +``` + +```sql +iceberg_hash("host") < -1073741824, +iceberg_hash("host") >= -1073741824 AND iceberg_hash("host") < 0, +iceberg_hash("host") >= 0 AND iceberg_hash("host") < 1073741824, +iceberg_hash("host") >= 1073741824 +``` + +```sql +CREATE TABLE requests ( + host STRING, + ts TIMESTAMP DEFAULT current_timestamp() TIME INDEX, + PRIMARY KEY (host) +) +PARTITION ON COLUMNS (host) ( + iceberg_hash(host) < -1073741824, + iceberg_hash(host) >= -1073741824 AND iceberg_hash(host) < 0, + iceberg_hash(host) >= 0 AND iceberg_hash(host) < 1073741824, + iceberg_hash(host) >= 1073741824 +); +``` + +如果希望围绕特定值而不是等宽来划分桶,可以用 `iceberg_hash_breakpoints(...)` 计算这些值的哈希,再把每个返回的哈希值依次作为上界,并补充最后一条不设上界的规则。例如,上面输出的两个断点可以生成以下三条规则: + +```sql +iceberg_hash(host) < 1009084850, +iceberg_hash(host) >= 1009084850 AND iceberg_hash(host) < 1210000089, +iceberg_hash(host) >= 1210000089 +``` + +返回结果已按哈希值排序,编写规则时请保持该顺序。 + +:::tip 注意 +`NULL` 哈希后仍为 `NULL`,永远不会匹配比较边界,因此被哈希列为 `NULL` 的行总是落入第一个分区(分区 `p0`)。 +::: + +:::warning 限制 +- 只有 `STRING` 和整数列可以哈希;其他类型会在执行时报错,与分区规则中的行为一致。 +- 对被哈希分区列的过滤条件不会裁剪 region。形如 `WHERE host = 'x'` 的查询会扫描所有哈希区间;只有其他列上的条件才会裁剪。 +::: diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/user-guide/deployments-administration/manage-data/table-sharding.md b/i18n/zh/docusaurus-plugin-content-docs/current/user-guide/deployments-administration/manage-data/table-sharding.md index cb92bbf7f..f35cc6829 100644 --- a/i18n/zh/docusaurus-plugin-content-docs/current/user-guide/deployments-administration/manage-data/table-sharding.md +++ b/i18n/zh/docusaurus-plugin-content-docs/current/user-guide/deployments-administration/manage-data/table-sharding.md @@ -61,7 +61,7 @@ PARTITION ON COLUMNS () ( 1. `PARTITION ON COLUMNS` 后跟一个用逗号分隔的列名列表,指定了将用于分区的列。目前,表中定义的任意列都可以作为分区列。请注意,所有分区的范围必须**不能**重叠。 -2. `RULE LIST` 是多个分区规则的列表。每个规则是一个分区条件,GreptimeDB 会为这些规则生成 `p0`、`p1` 等分区名称。这里的表达式可以使用 `=`, `!=`, `>`, `>=`, `<`, `<=`, `AND`, `OR`, 列名和字面量。 +2. `RULE LIST` 是多个分区规则的列表。每个规则是一个分区条件,GreptimeDB 会为这些规则生成 `p0`、`p1` 等分区名称。这里的表达式可以使用 `=`, `!=`, `>`, `>=`, `<`, `<=`, `AND`, `OR`, 列名和字面量。比较的列一侧也可以用 `iceberg_hash(...)` 包裹列名,详见下文的[哈希分区](#哈希分区)。 :::tip 选择分区列 建议使用 Tag 列(主键列)进行分区。对于**不是** append-only 的表(启用了去重)尤其重要:分区列应该是主键列的子集。这样具有相同主键的行会始终进入同一个分区,从而保证去重和合并可以正确工作。 @@ -71,6 +71,52 @@ PARTITION ON COLUMNS () ( `PARTITION ON COLUMNS` 括号里只能写列名(如 `device_id`、`area`),不支持表达式。GreptimeDB 不支持 MySQL 的 `PARTITION BY RANGE` 语法。 ::: +## 哈希分区 + +:::note 可用版本 +使用 `iceberg_hash` 规则的哈希分区目前仅在 Nightly 版本中提供,不包含在 GreptimeDB v1.3.0 和 v1.3.1 中。 +::: + +对原始值写范围规则需要手工划定区间,而且各分区的均衡程度取决于值的分布。规则也可以改为比较分区列的哈希值与 `INT32` 字面量:用 `iceberg_hash(...)` 包裹列名,把哈希空间切成等宽的桶。这通常能让不同的键值均匀分布,但相同键值的行始终进入同一个桶,因此热点键值或大量重复值仍可能造成热点分区。 + +```sql +CREATE TABLE requests ( + host STRING, + ts TIMESTAMP DEFAULT current_timestamp(), + v DOUBLE, + TIME INDEX (ts), + PRIMARY KEY (host) +) +PARTITION ON COLUMNS (host) ( + iceberg_hash(host) < -1073741824, + iceberg_hash(host) >= -1073741824 AND iceberg_hash(host) < 0, + iceberg_hash(host) >= 0 AND iceberg_hash(host) < 1073741824, + iceberg_hash(host) >= 1073741824 +); +``` + +`iceberg_hash` 是 Iceberg 规范定义的 32 位 Murmur3 哈希,可应用于 `STRING` 和整数列。`NULL` 哈希后仍为 `NULL`,因此被哈希列为 `NULL` 的行总是落入第一个分区。`PARTITION ON COLUMNS` 的列名列表仍然填写原始列名,哈希只出现在规则内部。 + +哈希断点无需手工计算,[分区函数](/reference/sql/functions/partition.md)可以自动生成: + +```sql +SELECT iceberg_hash_ranges_sql('host', 4); +``` + +输出就是完整的规则列表,可直接粘贴到表定义中: + +```sql +iceberg_hash("host") < -1073741824, +iceberg_hash("host") >= -1073741824 AND iceberg_hash("host") < 0, +iceberg_hash("host") >= 0 AND iceberg_hash("host") < 1073741824, +iceberg_hash("host") >= 1073741824 +``` + +:::warning 限制 +- 对被哈希分区列的过滤条件不会裁剪 region:形如 `WHERE host = 'x'` 的查询会扫描所有哈希区间;其他列上的条件仍会裁剪。 +- 使用 `iceberg_hash` 规则创建的表要求集群中所有节点都运行支持 `iceberg_hash` 的版本;旧版本节点无法读取此类分区元数据。 +::: + ## 创建分区表 你可以使用 MySQL CLI [连接到 GreptimeDB](/user-guide/protocols/mysql.md) 并创建一个分布式表。 diff --git a/sidebars.ts b/sidebars.ts index 00342881b..4846849f2 100644 --- a/sidebars.ts +++ b/sidebars.ts @@ -673,6 +673,7 @@ const sidebars: SidebarsConfig = { 'reference/sql/functions/vector', 'reference/sql/functions/approximate', 'reference/sql/functions/anomaly', + 'reference/sql/functions/partition', ], }, {