From 8f0287f4462d697b295456c643ecba5092c19332 Mon Sep 17 00:00:00 2001 From: Saul Pwanson Date: Wed, 17 Aug 2022 18:09:27 -0700 Subject: [PATCH] feat(clickhouse): add enum, ipaddr, json, lowcardinality to type parser --- ibis/backends/clickhouse/datatypes.py | 31 ++++++++++++++++++++ ibis/backends/clickhouse/tests/test_types.py | 9 ++++++ 2 files changed, 40 insertions(+) diff --git a/ibis/backends/clickhouse/datatypes.py b/ibis/backends/clickhouse/datatypes.py index 7f20d98adce4..fd3eeb9bab54 100644 --- a/ibis/backends/clickhouse/datatypes.py +++ b/ibis/backends/clickhouse/datatypes.py @@ -151,6 +151,32 @@ def struct(): nullable=False, ) + @p.generate + def enum_value(): + yield dt.SPACES + key = yield dt.RAW_STRING + yield dt.spaceless_string('=') + value = yield p.digit.at_least(1).concat() + return (key, int(value)) + + @p.generate + def lowcardinality(): + yield dt.spaceless_string('LowCardinality') + yield dt.LPAREN + r = yield ty + yield dt.RPAREN + return r + + @p.generate + def enum(): + yield dt.spaceless_string('enum') + enumsz = yield p.digit.at_least(1).concat() + enumsz = int(enumsz) + yield dt.LPAREN + yield enum_value.sep_by(dt.COMMA).map(dict) # ignore values + yield dt.RPAREN + return dt.String(nullable=False) + ty = ( nullable | nested @@ -160,6 +186,11 @@ def struct(): | array | map | struct + | enum + | lowcardinality + | dt.spaceless_string("IPv4", "IPv6").result(dt.inet(nullable=False)) + | dt.spaceless_string("Object('json')").result(dt.json(nullable=False)) + | dt.spaceless_string("JSON").result(dt.json(nullable=False)) ) return ty.parse(text) diff --git a/ibis/backends/clickhouse/tests/test_types.py b/ibis/backends/clickhouse/tests/test_types.py index 7b1785f551b3..7a0ede88f0e5 100644 --- a/ibis/backends/clickhouse/tests/test_types.py +++ b/ibis/backends/clickhouse/tests/test_types.py @@ -30,6 +30,15 @@ def test_columns_types_with_additional_argument(con): @pytest.mark.parametrize( ('ch_type', 'ibis_type'), [ + ( + "Enum8('' = 0, 'CDMA' = 1, 'GSM' = 2, 'LTE' = 3, 'NR' = 4)", + dt.String(nullable=False), + ), + ('IPv4', dt.inet(nullable=False)), + ('IPv6', dt.inet(nullable=False)), + ('JSON', dt.json(nullable=False)), + ("Object('json')", dt.json(nullable=False)), + ('LowCardinality(String)', dt.String(nullable=False)), ('Array(Int8)', dt.Array(dt.Int8(nullable=False), nullable=False)), ('Array(Int16)', dt.Array(dt.Int16(nullable=False), nullable=False)), ('Array(Int32)', dt.Array(dt.Int32(nullable=False), nullable=False)),