Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support LowCardinality type modifier #59

Merged
merged 1 commit into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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