我如何在 clickhouse 中使用 groupBitmapAnd 和 AggregatingMergeTree 引擎?
How can i use groupBitmapAnd with AggregatingMergeTree engine in clickhouse?
我想在 clickhouse 中维护一个 table,它使用位图“AND”逻辑合并具有相同 tag_id.
行的位图
由于位图在 clickhouse 和 groupBitmap 中被定义为 AggregateFunction(groupBitmap, UInt*) 并且将位图作为其参数,因此我创建了一个 table 如下:
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', groupBitmapAndState(bitmapBuild(cast([1,3,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, AggregateFunction(groupBitmap, UInt32))
)
ENGINE = AggregatingMergeTree()
ORDER BY tag_id
它丢失了列 z
的原始定义
此外,插入会异常结束:
DB::Exception: Aggregate function groupBitmapAndState(bitmapBuild(CAST([1, 2, 3, 4], 'Array(UInt32)'))) is found in wrong place in query: While processing groupBitmapAndState(bitmapBuild(CAST([1, 2, 3, 4], '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,7,8,9,10,11,12,13,14,15] as Array(UInt32))));
INSERT INTO bitmap_column_expr_test2 VALUES ('tag3', bitmapBuild(cast([2,4,6,8,10,12] as Array(UInt32))));
SELECT groupBitmapAnd(z) FROM bitmap_column_expr_test2 WHERE like(tag_id, 'tag%');
┌─groupBitmapAnd(z)─┐
│ 3 │
└───────────────────┘
我想在 clickhouse 中维护一个 table,它使用位图“AND”逻辑合并具有相同 tag_id.
行的位图由于位图在 clickhouse 和 groupBitmap 中被定义为 AggregateFunction(groupBitmap, UInt*) 并且将位图作为其参数,因此我创建了一个 table 如下:
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', groupBitmapAndState(bitmapBuild(cast([1,3,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, AggregateFunction(groupBitmap, UInt32))
)
ENGINE = AggregatingMergeTree()
ORDER BY tag_id
它丢失了列 z
此外,插入会异常结束:
DB::Exception: Aggregate function groupBitmapAndState(bitmapBuild(CAST([1, 2, 3, 4], 'Array(UInt32)'))) is found in wrong place in query: While processing groupBitmapAndState(bitmapBuild(CAST([1, 2, 3, 4], '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,7,8,9,10,11,12,13,14,15] as Array(UInt32))));
INSERT INTO bitmap_column_expr_test2 VALUES ('tag3', bitmapBuild(cast([2,4,6,8,10,12] as Array(UInt32))));
SELECT groupBitmapAnd(z) FROM bitmap_column_expr_test2 WHERE like(tag_id, 'tag%');
┌─groupBitmapAnd(z)─┐
│ 3 │
└───────────────────┘