Skip to content

Commit

Permalink
Support LowCardinality type modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
hhell committed Jul 10, 2019
1 parent e4db826 commit b8ae869
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
12 changes: 11 additions & 1 deletion clickhouse_sqlalchemy/drivers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
'Enum8': types.Enum8,
'Enum16': types.Enum16,
'_array': types.Array,
'_nullable': types.Nullable
'_nullable': types.Nullable,
'_lowcardinality': types.LowCardinality,
}


Expand Down Expand Up @@ -427,6 +428,10 @@ def visit_nullable(self, type_, **kw):
nested_type = type_api.to_instance(type_.nested_type)
return 'Nullable(%s)' % self.process(nested_type, **kw)

def visit_lowcardinality(self, type_, **kw):
nested_type = type_api.to_instance(type_.nested_type)
return "LowCardinality(%s)" % self.process(nested_type, **kw)

def visit_int8(self, type_, **kw):
return 'Int8'

Expand Down Expand Up @@ -582,6 +587,11 @@ def _get_column_type(self, name, spec):
coltype = self.ischema_names['_nullable']
return coltype(self._get_column_type(name, inner))

elif spec.startswith('LowCardinality'):
inner = spec[15:-1]
coltype = self.ischema_names['_lowcardinality']
return coltype(self._get_column_type(name, inner))

elif spec.startswith('Enum'):
pos = spec.find('(')
type = spec[:pos]
Expand Down
8 changes: 8 additions & 0 deletions clickhouse_sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ class UUID(String):
__visit_name__ = 'uuid'


class LowCardinality(types.TypeEngine):
__visit_name__ = 'lowcardinality'

def __init__(self, nested_type):
self.nested_type = nested_type
super(LowCardinality, self).__init__()


class Int8(Int):
__visit_name__ = 'int8'

Expand Down
6 changes: 6 additions & 0 deletions tests/test_reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def test_nullable(self):
self.assertIsInstance(coltype, types.Nullable)
self.assertEqual(coltype.nested_type, types.Int32)

def test_lowcardinality(self):
coltype = self._type_round_trip(types.LowCardinality(types.String))[0]

self.assertIsInstance(coltype, types.LowCardinality)
self.assertEqual(coltype.nested_type, types.String)

def test_enum8(self):
enum_options = {'three': 3, "quoted' ": 9, 'comma, ': 14}
coltype = self._type_round_trip(
Expand Down

0 comments on commit b8ae869

Please sign in to comment.