英文:
How to insert old data to AggregatingMergeTree for AggregateFunction(count) column type in Clickhouse?
问题
I have old table:
CREATE TABLE old_stats
(
id String,
results_count UInt64
)
ENGINE = AggregatingMergeTree
PRIMARY KEY (id);
New aggregating table:
CREATE TABLE new_stats
(
id String,
results_count AggregateFunction(count)
)
ENGINE = AggregatingMergeTree
PRIMARY KEY (id);
How directly insert into new_stats from old_stats
INSERT INTO new_stats
SELECT
id,
result_count
FROM old_stats;
Not working solutions:
countState(result_count) as result_count makes exception Conversion from AggregateFunction(count, UInt8) to AggregateFunction(count) is not supported
arrayReduce('countState', range(1, results_count)) makes exception Conversion from AggregateFunction(count, UInt8) to AggregateFunction(count) is not supported
英文:
I have old table:
CREATE TABLE old_stats
(
id String,
results_count UInt64
)
ENGINE = AggregatingMergeTree
PRIMARY KEY (id);
New aggregating table:
CREATE TABLE new_stats
(
id String,
results_count AggregateFunction(count)
)
ENGINE = AggregatingMergeTree
PRIMARY KEY (id);
How directly insert into new_stats from old_stats
INSERT INTO new_stats
SELECT
id,
result_count
FROM old_stats;
Not working solutions:
countState(result_count) as result_count makes exception Conversion from AggregateFunction(count, UInt8) to AggregateFunction(count) is not supported
arrayReduce('countState', range(1, results_count)) makes exception Conversion from AggregateFunction(count, UInt8) to AggregateFunction(count) is not supported
答案1
得分: 1
If count is just a running total of integers, use SimpleAggregateFunction:
CREATE TABLE new_stats
(
`id` String,
`results_count` SimpleAggregateFunction(sum, UInt64)
)
ENGINE = AggregatingMergeTree
PRIMARY KEY id
Notice the result from the following inserts:
insert into old_stats values (1, 200);
insert into new_stats values (1,10);
SELECT * FROM new_stats FINAL
Query id: 718c2240-97ca-431c-b8cf-9690961100ad
┌─id─┬─results_count─┐
│ 1 │ 210 │
└────┴───────────────┘
And...your query above will work fine (where you insert the old table into the new one)
英文:
If count is just a running total of integers, use SimpleAggregateFunction:
CREATE TABLE new_stats
(
`id` String,
`results_count` SimpleAggregateFunction(sum, UInt64)
)
ENGINE = AggregatingMergeTree
PRIMARY KEY id
Notice the result from the following inserts:
insert into old_stats values (1, 200);
insert into new_stats values (1,10);
SELECT * FROM new_stats FINAL
Query id: 718c2240-97ca-431c-b8cf-9690961100ad
┌─id─┬─results_count─┐
│ 1 │ 210 │
└────┴───────────────┘
And...your query above will work fine (where you insert the old table into the new one)
答案2
得分: 0
我刚在处理另一个项目时意识到答案。你MV中的列是AggregateFunction(count)类型的,因此你需要使用countState函数将值插入其中。
尝试以下查询(我刚刚进行了简单测试,它有效):
INSERT INTO new_stats SELECT
id,
countState(results_count) AS results_count
FROM old_stats
GROUP BY id
英文:
I just realized the answer as I was working on a different project. Your column in the MV is of type AggregateFunction(count), so you need to use the countState function to insert values into it.
Try the following query (it worked in a simple test I just ran):
INSERT INTO new_stats SELECT
id,
countState(results_count) AS results_count
FROM old_stats
GROUP BY id
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论