How to insert old data to AggregatingMergeTree for AggregateFunction(count) column type in Clickhouse?

huangapple go评论55阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月6日 22:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定