Skip to content

Commit 3bbec06

Browse files
committed
support types with comma inside map
1 parent 2e6331e commit 3bbec06

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

proton_driver/columns/mapcolumn.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import re
12
from .base import Column
23
from .intcolumn import UInt64Column
34
from ..util.helpers import pairwise
45

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

68
class MapColumn(Column):
79
py_types = (dict, )
@@ -51,7 +53,9 @@ def write_items(self, items, buf):
5153

5254

5355
def create_map_column(spec, column_by_spec_getter):
54-
key, value = spec[4:-1].split(',')
56+
# Match commas outside of parentheses, so we don't match the comma in
57+
# Decimal types.
58+
key, value = comma_re.split(spec[4:-1])
5559
key_column = column_by_spec_getter(key.strip())
5660
value_column = column_by_spec_getter(value.strip())
5761

tests/columns/test_map.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from tests.testcase import BaseTestCase
2-
2+
from decimal import Decimal
33

44
class MapTestCase(BaseTestCase):
55
# required_server_version = (21, 1, 2)
@@ -22,7 +22,7 @@ def _sorted_dicts(self, text):
2222
return '\n'.join(items) + '\n'
2323

2424
def test_simple(self):
25-
with self.create_stream('a map(string, uint64)'):
25+
with self.create_stream('a map(string, int)'):
2626
data = [
2727
({},),
2828
({'key1': 1}, ),
@@ -100,3 +100,22 @@ def test_array(self):
100100
)
101101
inserted = self.client.execute(query)
102102
self.assertEqual(inserted, data)
103+
104+
def test_decimal(self):
105+
with self.create_stream('a map(string, Decimal(9, 2))'):
106+
data = [
107+
({'key1': Decimal('123.45')}, ),
108+
({'key2': Decimal('234.56')}, ),
109+
({'key3': Decimal('345.67')}, )
110+
]
111+
self.client.execute('INSERT INTO test (a) VALUES', data)
112+
query = 'SELECT * FROM test'
113+
inserted = self.emit_cli(query)
114+
self.assertEqual(
115+
inserted,
116+
"{'key1':123.45}\n"
117+
"{'key2':234.56}\n"
118+
"{'key3':345.67}\n"
119+
)
120+
inserted = self.client.execute(query)
121+
self.assertEqual(inserted, data)

0 commit comments

Comments
 (0)