ClickHouse Alter Inalified View添加列

发布于 2025-02-01 14:33:28 字数 2510 浏览 3 评论 0原文

env:Clikchouse版本:22.3.3.44;数据库引擎:Atomic

我有一个原始表和MV,类似的模式:

CREATE TABLE IF NOT EXISTS test.Income_Raw on cluster '{cluster}' (
  Id Int64,
  DateNum Date,
  Cnt Int64,
  LoadTime DateTime
) ENGINE==MergeTree
PARTITION BY toYYYYMMDD(LoadTime)
ORDER BY (Id, DateNum);

CREATE MATERIALIZED VIEW test.Income_MV on cluster '{cluster}'
ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/{shard}/{database}/{table}', '{replica}')
PARTITION BY toYYYYMM(DateNum)
ORDER BY (Id, DateNum)
TTL DateNum+ INTERVAL 100 DAY
AS SELECT
    DateNum,
    Id,
    argMaxState(Cnt,  LoadTime) as Cnt ,
    maxState( LoadTime)  as latest_loadtime
FROM test.Income_Raw
GROUP BY Id, DataNum;

现在我想将名为“价格”的列添加到Raw Table和MV, 因此,我按照以下步骤运行SQL:

// first I alter raw table
1. alter table test.Income_Raw on cluster '{cluster}' add column Price Int32

// below sqls, I run to alter MV
2. detach test.Income_MV on cluster '{cluster}'

3. alter test.`.inner_id.{uuid}` on cluster '{cluster}' add column Price Int32

// step 4, basically I just use 'attach' replace 'create' and add 'Price' to select query
4. attach MATERIALIZED VIEW test.Income_MV on cluster '{cluster}'
ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/{shard}/{database}/{table}', '{replica}')
PARTITION BY toYYYYMM(DateNum)
ORDER BY (Id, DateNum)
TTL DateNum+ INTERVAL 100 DAY
AS SELECT
    DateNum,
    Id,
    Price,
    argMaxState(Cnt,  LoadTime) as Cnt ,
    maxState( LoadTime)  as latest_loadtime
FROM test.Income_Raw
GROUP BY Id, DataNum, Price;

但是,在步骤4中,我遇到的错误

Code: 80. DB::Exception: Incorrect ATTACH TABLE query for Atomic database engine. Use one of the following queries instead:
1. ATTACH TABLE Income_MV;
2. CREATE TABLE Income_MV <table definition>;
3. ATTACH TABLE Income_MV FROM '/path/to/data/' <table definition>;
4. ATTACH TABLE Income_MVUUID '<uuid>' <table definition>;. (INCORRECT_QUERY) (version 22.3.3.44 (official build))

这些SQL是我从下面的参考文献中遵循的。

https://kb.altinity.com/altinity.com/altinity.com/altinity-kb-kb-schema -design/soletialized-views/

a>

所以我的问题是,如何修改MV选择查询,我错了哪一步?

env: Clikchouse version:22.3.3.44; Database engine: atomic

I have a raw table and mv, schema like this:

CREATE TABLE IF NOT EXISTS test.Income_Raw on cluster '{cluster}' (
  Id Int64,
  DateNum Date,
  Cnt Int64,
  LoadTime DateTime
) ENGINE==MergeTree
PARTITION BY toYYYYMMDD(LoadTime)
ORDER BY (Id, DateNum);

CREATE MATERIALIZED VIEW test.Income_MV on cluster '{cluster}'
ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/{shard}/{database}/{table}', '{replica}')
PARTITION BY toYYYYMM(DateNum)
ORDER BY (Id, DateNum)
TTL DateNum+ INTERVAL 100 DAY
AS SELECT
    DateNum,
    Id,
    argMaxState(Cnt,  LoadTime) as Cnt ,
    maxState( LoadTime)  as latest_loadtime
FROM test.Income_Raw
GROUP BY Id, DataNum;

now I want to add a column named 'price' to raw table and mv,
so I run sql step by step like below:

// first I alter raw table
1. alter table test.Income_Raw on cluster '{cluster}' add column Price Int32

// below sqls, I run to alter MV
2. detach test.Income_MV on cluster '{cluster}'

3. alter test.`.inner_id.{uuid}` on cluster '{cluster}' add column Price Int32

// step 4, basically I just use 'attach' replace 'create' and add 'Price' to select query
4. attach MATERIALIZED VIEW test.Income_MV on cluster '{cluster}'
ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/{shard}/{database}/{table}', '{replica}')
PARTITION BY toYYYYMM(DateNum)
ORDER BY (Id, DateNum)
TTL DateNum+ INTERVAL 100 DAY
AS SELECT
    DateNum,
    Id,
    Price,
    argMaxState(Cnt,  LoadTime) as Cnt ,
    maxState( LoadTime)  as latest_loadtime
FROM test.Income_Raw
GROUP BY Id, DataNum, Price;

but at step 4, I met error like this

Code: 80. DB::Exception: Incorrect ATTACH TABLE query for Atomic database engine. Use one of the following queries instead:
1. ATTACH TABLE Income_MV;
2. CREATE TABLE Income_MV <table definition>;
3. ATTACH TABLE Income_MV FROM '/path/to/data/' <table definition>;
4. ATTACH TABLE Income_MVUUID '<uuid>' <table definition>;. (INCORRECT_QUERY) (version 22.3.3.44 (official build))

these sqls I runned is I followed from below references.

https://kb.altinity.com/altinity-kb-schema-design/materialized-views/

Clickhouse altering materialized view's select

so my question is, how to modify mv select query, which step I was wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时常饿 2025-02-08 14:33:28

我想知道只需要:

准备:使用明确的目标表代替内部表格

1 alter mv目标表

2下降MV

3重新创建MV,并使用新查询

I figure out that just need:

prepare: use explicit target table instead inner table for MV

1 alter MV target table

2 drop MV

3 re-create MV with new query

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文