Search code examples
clickhouse

create table with schema inferenced from data


In clickhouse version 22.1 it is possible to inference schema. e.g.: DESC file('nonexist', 'Protobuf') SETTINGS format_schema='file.proto:LogEntry'

But is it possible to create table with columns obtained from DESCRIBE?


Solution

  • yes

    cat /var/lib/clickhouse/user_files/aa.csv
    a, b, 4
    
    create table t1 Engine=Log  as select * from file('aa.csv');
    
    DESCRIBE TABLE  t1
    ┌─name─┬─type──────────────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
    │ c1   │ Nullable(String)  │              │                    │         │                  │                │
    │ c2   │ Nullable(String)  │              │                    │         │                  │                │
    │ c3   │ Nullable(Float64) │              │                    │         │                  │                │
    └──────┴───────────────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
    
    
    create table t1 Engine=Log  as select * from file('aa.csv') where 0;
    DESCRIBE TABLE  t1
    ┌─name─┬─type──────────────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
    │ c1   │ Nullable(String)  │              │                    │         │                  │                │
    │ c2   │ Nullable(String)  │              │                    │         │                  │                │
    │ c3   │ Nullable(Float64) │              │                    │         │                  │                │
    └──────┴───────────────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘