【问题标题】:How to change the column's type from numeric to array in Clickhouse如何在 Clickhouse 中将列的类型从数字更改为数组
【发布时间】:2019-09-06 06:24:16
【问题描述】:

我的表中有一个数据类型为Int32 的列。是否可以将列转换为数组数据类型Array(Int32)。如果没有其他方法,请告诉我。

【问题讨论】:

    标签: clickhouse


    【解决方案1】:

    ALTER TABLE .. MODIFY COLUMN-query 无法将列的类型从 int 更改为 Array(int),因为这种类型转换是不允许的。

    所以你需要遵循以下步骤:

    1. 添加类型为 Array(int) 的新列
    ALTER TABLE test.test_004 ADD COLUMN `value_array` Array(int);
    
    /* Test table preparation:
    CREATE TABLE test.test_004
    (
        `id` int,
        `value` int
    )
    ENGINE = MergeTree();
    
    INSERT INTO test.test_004 VALUES (1, 10), (2, 20), (3, 30), (4, 40);
    */
    
    1. update数组列值
    ALTER TABLE test.test_004
    UPDATE value_array = [value] WHERE 1
    
    /* Result
    ┌─id─┬─value─┬─value_array─┐
    │  1 │    10 │ [10]        │
    │  2 │    20 │ [20]        │
    │  3 │    30 │ [30]        │
    │  4 │    40 │ [40]        │
    └────┴───────┴─────────────┘
    */
    
    1. 重要:取决于表中的行数,此操作可能需要很长时间才能执行。要检查更新(突变)的状态或查找失败的原因,请观察 system.mutations-table
    SELECT *
    FROM system.mutations
    WHERE table = 'test_004'
    
    1. 当变异完成可以删除原始列
    ALTER TABLE test.test_004
    DROP COLUMN value
    

    备注:如果表位于多个服务器上,则为每个查询分配额外的ON CLUSTER-clause。

    【讨论】:

      猜你喜欢
      • 2017-03-29
      • 2020-06-08
      • 2011-12-02
      • 1970-01-01
      • 2021-02-27
      • 2023-02-26
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      相关资源
      最近更新 更多