Skip to content

Commit

Permalink
Implement Decimal reflection (#74)
Browse files Browse the repository at this point in the history
* Decimal parse support
  • Loading branch information
armymaksim authored and xzkostyan committed Jan 26, 2020
1 parent f128cfc commit 2f79283
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clickhouse_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .sql import Table, select


VERSION = (0, 1, 2)
VERSION = (0, 1, 3)
__version__ = '.'.join(str(x) for x in VERSION)


Expand Down
10 changes: 10 additions & 0 deletions clickhouse_sqlalchemy/drivers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'DateTime': DATETIME,
'Float64': FLOAT,
'Float32': FLOAT,
'Decimal': types.Decimal,
'String': types.String,
'UUID': types.UUID,
'IPv4': types.IPv4,
Expand Down Expand Up @@ -663,6 +664,9 @@ def _get_column_type(self, name, spec):
type_enum = enum.Enum('%s_enum' % name, options)
return lambda: coltype(type_enum)

elif spec.lower().startswith('decimal'):
coltype = self.ischema_names['Decimal']
return coltype(*self._parse_decimal_params(spec))
else:
try:
return self.ischema_names[spec]
Expand All @@ -671,6 +675,12 @@ def _get_column_type(self, name, spec):
(spec, name))
return sqltypes.NullType

@staticmethod
def _parse_decimal_params(spec):
ints = spec.split('(')[-1].split(')')[0] # get all data in brackets
precision, scale = ints.split(',')
return int(precision.strip()), int(scale.strip())

@staticmethod
def _parse_options(option_string):
options = dict()
Expand Down
7 changes: 7 additions & 0 deletions tests/test_reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,10 @@ def test_enum16(self):
self.assertEqual(
{o.name: o.value for o in coltype.enum_class}, enum_options
)

def test_decimal(self):
coltype = self._type_round_trip(types.Decimal(38, 38))[0]

self.assertIsInstance(coltype, types.Decimal)
self.assertEqual(coltype.precision, 38)
self.assertEqual(coltype.scale, 38)

0 comments on commit 2f79283

Please sign in to comment.