BUCKET
将受支持的值映射到固定数量的哈希桶之一。BUCKET() 会返回一个确定性的 UInt32 值,范围为 0 到 bucket_count - 1,并且专为 PARTITION BY 和 CLUSTER BY 表达式而设计。
语法
BUCKET(<bucket_count>, <value>)
参数
当任一参数为 NULL 时,该函数返回 NULL。
示例
-- Returns an integer from 0 through 15.
SELECT BUCKET(16, 'customer-123');
使用固定的 bucket 数量对高基数键进行分区:
CREATE TABLE customer_events (
customer_id BIGINT,
event_time TIMESTAMP,
payload VARIANT
)
PARTITION BY (BUCKET(32, customer_id));
对于分布式写入,具有相同分区计算值的行可以被路由到同一个 writer:
CREATE TABLE customer_events_distributed (
customer_id BIGINT,
event_time TIMESTAMP
)
PARTITION BY (BUCKET(32, customer_id))
WRITE_DISTRIBUTION_MODE = 'hash';
WRITE_DISTRIBUTION_MODE = 'hash' 需要 PARTITION BY 子句。