Skip to content

Commit 4e2fc97

Browse files
authored
feat: Add named Tuple type (xzkostyan#318)
* feat: Add named Tuple type * fix: flake8
1 parent 53ff590 commit 4e2fc97

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

clickhouse_sqlalchemy/drivers/compilers/typecompiler.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,26 @@ def visit_ipv6(self, type_, **kw):
121121
return 'IPv6'
122122

123123
def visit_tuple(self, type_, **kw):
124-
cols = (
125-
self.process(type_api.to_instance(nested_type), **kw)
124+
cols = []
125+
is_named_type = all([
126+
isinstance(nested_type, tuple) and len(nested_type) == 2
126127
for nested_type in type_.nested_types
127-
)
128+
])
129+
if is_named_type:
130+
for nested_type in type_.nested_types:
131+
name = nested_type[0]
132+
name_type = nested_type[1]
133+
inner_type = self.process(
134+
type_api.to_instance(name_type),
135+
**kw
136+
)
137+
cols.append(
138+
f'{name} {inner_type}')
139+
else:
140+
cols = (
141+
self.process(type_api.to_instance(nested_type), **kw)
142+
for nested_type in type_.nested_types
143+
)
128144
return 'Tuple(%s)' % ', '.join(cols)
129145

130146
def visit_map(self, type_, **kw):

tests/test_ddl.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,26 @@ def test_create_table_tuple(self):
296296
'ENGINE = Memory'
297297
)
298298

299+
def test_create_table_named_tuple(self):
300+
table = Table(
301+
't1', self.metadata(),
302+
Column(
303+
'x',
304+
types.Tuple(
305+
('name', types.String),
306+
('value', types.Float32)
307+
)
308+
),
309+
engines.Memory()
310+
)
311+
312+
self.assertEqual(
313+
self.compile(CreateTable(table)),
314+
'CREATE TABLE t1 ('
315+
'x Tuple(name String, value Float32)) '
316+
'ENGINE = Memory'
317+
)
318+
299319
@require_server_version(21, 1, 3)
300320
def test_create_table_map(self):
301321
table = Table(

0 commit comments

Comments
 (0)