Skip to content

Commit

Permalink
Support decimals inside Map types.
Browse files Browse the repository at this point in the history
  • Loading branch information
zaius authored and xzkostyan committed Jun 18, 2022
1 parent 4a16f74 commit b51d036
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion clickhouse_driver/columns/mapcolumn.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import re
from .base import Column
from .intcolumn import UInt64Column
from ..util.helpers import pairwise


comma_re = re.compile(r',(?![^()]*\))')


class MapColumn(Column):
py_types = (dict, )

Expand Down Expand Up @@ -53,7 +57,9 @@ def write_items(self, items, buf):


def create_map_column(spec, column_by_spec_getter, column_options):
key, value = spec[4:-1].split(',')
# Match commas outside of parentheses so we don't match the comma in
# Decimal types.
key, value = comma_re.split(spec[4:-1])
key_column = column_by_spec_getter(key.strip())
value_column = column_by_spec_getter(value.strip())

Expand Down
21 changes: 21 additions & 0 deletions tests/columns/test_map.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from tests.testcase import BaseTestCase
from decimal import Decimal


class MapTestCase(BaseTestCase):
Expand Down Expand Up @@ -99,3 +100,23 @@ def test_array(self):
)
inserted = self.client.execute(query)
self.assertEqual(inserted, data)

def test_decimal(self):
columns = 'a Map(String, Decimal(9, 2))'
with self.create_table(columns):
data = [
({'key1': Decimal('123.45')}, ),
({'key2': Decimal('234.56')}, ),
({'key3': Decimal('345.67')}, )
]
self.client.execute('INSERT INTO test (a) VALUES', data)
query = 'SELECT * FROM test'
inserted = self.emit_cli(query)
self.assertEqual(
inserted,
"{'key1':123.45}\n"
"{'key2':234.56}\n"
"{'key3':345.67}\n"
)
inserted = self.client.execute(query)
self.assertEqual(inserted, data)

0 comments on commit b51d036

Please sign in to comment.