在 ByteHouse 企业版中建表时,支持将列类型设置为 Bool 或 Boolean,并在内部存储为 UInt8,值为 true (1),false (0)。表创建后,Boolean 类型将显示为 Bool 类型。
select true as col, toTypeName(col); ┌─col──┬─toTypeName(true)─┐ │ 1 │ UInt8 │ └──────┴──────────────────┘ select true == 1 as col, toTypeName(col); ┌─col─┬─toTypeName(equals(true, 1))─┐ │ 1 │ UInt8 │ └─────┴─────────────────────────────┘
使用示例如下:
创建表,并插入数据。
CREATE TABLE IF NOT EXISTS db_1.test_bool_local ( `a` Int64, `b` String, `c` Bool, `d` Boolean ) ENGINE = HaUniqueMergeTree( '/clickhouse/{Account_ID}/{Cluster_Name}/db_1.test_bool_local/{shard}', '{replica}' ) ORDER BY a UNIQUE KEY a SETTINGS storage_policy = 's3_cold', index_granularity = 8192; CREATE TABLE IF NOT EXISTS db_1.test_bool ( `a` Int64, `b` String, `c` Bool, `d` Boolean ) ENGINE = Distributed( '{Cluster_Name}', 'db_1', 'test_bool_local', intHash64(a) ); INSERT INTO db_1.test_bool VALUES (1,'a', true, true), (2,'a', false, false), (3,'a', 1, 1), (4,'a', 0, 0); DESC db_1.test_bool; SELECT * from db_1.test_bool;
查看表的列定义。
DESC db_1.test_bool;
输出结果如下:
从结果中可以看到,建表时 d 列定义为 Boolean 类型,但在输出时显示为 Bool 类型。
查询表中的数据及对应类型。
SELECT * from db_1.test_bool;
输出结果如下: