Skip to content

Commit f434017

Browse files
aksenofaaksenov
andauthored
Support Nested Map Type (#314) (#315)
* Support Nested Map Type (#314) * Add tests for Map and nested map (#314) * fix flake8 (#314) --------- Co-authored-by: aaksenov <aaksenov@dpkapp.ru>
1 parent f261350 commit f434017

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

clickhouse_sqlalchemy/drivers/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def _get_column_type(self, name, spec):
273273
coltype = self.ischema_names['_map']
274274
inner_types = [
275275
self._get_column_type(name, t.strip())
276-
for t in inner.split(',')
276+
for t in inner.split(',', 1)
277277
]
278278
return coltype(*inner_types)
279279

tests/sql/test_selectable.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from sqlalchemy import Column, and_
1+
from sqlalchemy import Column, and_, func
22
from sqlalchemy.exc import CompileError
33
from sqlalchemy.sql import expression
44

@@ -585,3 +585,49 @@ def test_distinct_on(self):
585585
self.compile(query),
586586
'SELECT DISTINCT ON (t1.x) t1.x FROM t1'
587587
)
588+
589+
def test_map_type(self):
590+
table = self._make_table(
591+
't1',
592+
Column('x', types.Int32, primary_key=True),
593+
Column('y', types.Map(types.String, types.String))
594+
)
595+
596+
query = select(
597+
func.mapKeys(table.c.y), func.mapValues(table.c.y)
598+
).where(
599+
func.has(table.c.y, 'foo')
600+
)
601+
self.assertEqual(
602+
self.compile(query, literal_binds=True),
603+
'SELECT mapKeys(t1.y) AS "mapKeys_1", '
604+
'mapValues(t1.y) AS "mapValues_1" '
605+
'FROM t1 '
606+
'WHERE has(t1.y, \'foo\')'
607+
)
608+
609+
def test_nested_map_type(self):
610+
table = self._make_table(
611+
't1',
612+
Column('x', types.Int32, primary_key=True),
613+
Column(
614+
'y',
615+
types.Map(
616+
types.String,
617+
types.Map(types.String, types.String)
618+
)
619+
)
620+
)
621+
622+
query = select(
623+
func.mapKeys(table.c.y), func.mapValues(table.c.y)
624+
).where(
625+
func.has(table.c.y, 'foo')
626+
)
627+
self.assertEqual(
628+
self.compile(query, literal_binds=True),
629+
'SELECT mapKeys(t1.y) AS "mapKeys_1", '
630+
'mapValues(t1.y) AS "mapValues_1" '
631+
'FROM t1 '
632+
'WHERE has(t1.y, \'foo\')'
633+
)

0 commit comments

Comments
 (0)