微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 clickhouse 中使用 groupBitmapAnd 和 AggregatingMergeTree 引擎?

如何解决如何在 clickhouse 中使用 groupBitmapAnd 和 AggregatingMergeTree 引擎?

我想在 clickhouse 中维护一个表,它使用位图“AND”逻辑来合并具有相同 tag_id 的行的位图。

由于位图在 clickhouse 和 groupBitmap 中被定义为 AggregateFunction(groupBitmap,UInt*) 并且将位图作为其参数,我创建了一个如下表:

CREATE TABLE test.bitmap_column_expr_test2
(
    `tag_id` String,`z` AggregateFunction(groupBitmapAnd,AggregateFunction(groupBitmap,UInt32))
)
ENGINE = AggregatingMergeTree()
ORDER BY tag_id
SETTINGS index_granularity = 8192

就我而言,我想插入如下数据:

INSERT INTO test.bitmap_column_expr_test2 VALUES ('tag1',groupBitmapAndState(bitmapBuild(cast([1,2,3,4] as Array(UInt32)))));
INSERT INTO test.bitmap_column_expr_test2 VALUES ('tag1',groupBitmapAndState(bitmapBuild(cast([1] as Array(UInt32)))));
INSERT INTO test.bitmap_column_expr_test2 VALUES ('tag1',4] as Array(UInt32)))));

我希望通过以下方式获得位图和结果:

SELECT bitmapToArray(groupBitmapAndMergeState(z)) FROM test.bitmap_column_expr_test2;

但是,我的 ddl 被 clickhouse 重写为:

CREATE TABLE test.bitmap_column_expr_test2
(
    `tag_id` String,`z` AggregateFunction(groupBitmap,UInt32))
)
ENGINE = AggregatingMergeTree()
ORDER BY tag_id

丢失了列 z

的原始定义

此外,插入将以异常结束:

DB::Exception: Aggregate function groupBitmapAndState(bitmapBuild(CAST([1,4],'Array(UInt32)'))) is found in wrong place in query: While processing groupBitmapAndState(bitmapBuild(CAST([1,'Array(UInt32)'))) (version 20.11.4.13 (official build))

我不确定在 AggregatingMergeTree 中通过“AND”逻辑合并的行中获取位图是否正确。

解决方法

groupBitmapAnd 接受表示为 AggregateFunction(groupBitmap,UInt_) 类型的位图作为参数。

ClickHouse 不支持嵌套聚合类型(在本例中为 AggregateFunction(groupBitmapAnd,AggregateFunction(groupBitmap,UInt_)))。

所以它需要遵循官方文档中描述的模式 - https://clickhouse.tech/docs/en/sql-reference/aggregate-functions/reference/groupbitmapand/#groupbitmapand

CREATE TABLE bitmap_column_expr_test2
(
    tag_id String,z AggregateFunction(groupBitmap,UInt32)
)
ENGINE = MergeTree
ORDER BY tag_id;

INSERT INTO bitmap_column_expr_test2 VALUES ('tag1',bitmapBuild(cast([1,2,3,4,5,6,7,8,9,10] as Array(UInt32))));
INSERT INTO bitmap_column_expr_test2 VALUES ('tag2',bitmapBuild(cast([6,10,11,12,13,14,15] as Array(UInt32))));
INSERT INTO bitmap_column_expr_test2 VALUES ('tag3',bitmapBuild(cast([2,12] as Array(UInt32))));

SELECT groupBitmapAnd(z) FROM bitmap_column_expr_test2 WHERE like(tag_id,'tag%');
┌─groupBitmapAnd(z)─┐
│               3   │
└───────────────────┘

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。