-
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Nested with flatten_nested=0 (#285)
Support Nested with flatten_nested=0
- Loading branch information
Showing
5 changed files
with
290 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
|
||
from .arraycolumn import create_array_column | ||
|
||
|
||
def create_nested_column(spec, column_by_spec_getter): | ||
return create_array_column( | ||
'Array(Tuple({}))'.format(','.join(get_nested_columns(spec))), | ||
column_by_spec_getter=column_by_spec_getter | ||
) | ||
|
||
|
||
def get_nested_columns(spec): | ||
brackets = 0 | ||
column_begin = 0 | ||
|
||
inner_spec = get_inner_spec(spec) | ||
nested_columns = [] | ||
for i, x in enumerate(inner_spec + ','): | ||
if x == ',': | ||
if brackets == 0: | ||
nested_columns.append(inner_spec[column_begin:i]) | ||
column_begin = i + 1 | ||
elif x == '(': | ||
brackets += 1 | ||
elif x == ')': | ||
brackets -= 1 | ||
elif x == ' ': | ||
if brackets == 0: | ||
column_begin = i + 1 | ||
return nested_columns | ||
|
||
|
||
def get_columns_with_types(spec): | ||
brackets = 0 | ||
prev_comma = 0 | ||
prev_space = 0 | ||
|
||
inner_spec = get_inner_spec(spec) | ||
columns_with_types = [] | ||
|
||
for i, x in enumerate(inner_spec + ','): | ||
if x == ',': | ||
if brackets == 0: | ||
columns_with_types.append(( | ||
inner_spec[prev_comma:prev_space].strip(), | ||
inner_spec[prev_space:i] | ||
)) | ||
prev_comma = i + 1 | ||
elif x == '(': | ||
brackets += 1 | ||
elif x == ')': | ||
brackets -= 1 | ||
elif x == ' ': | ||
if brackets == 0: | ||
prev_space = i + 1 | ||
return columns_with_types | ||
|
||
|
||
def get_inner_spec(spec): | ||
brackets = 1 | ||
offset = len('Nested(') | ||
i = offset | ||
for i, ch in enumerate(spec[offset:], offset): | ||
if brackets == 0: | ||
break | ||
|
||
if ch == '(': | ||
brackets += 1 | ||
|
||
elif ch == ')': | ||
brackets -= 1 | ||
|
||
return spec[offset:i] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
from tests.testcase import BaseTestCase | ||
from tests.util import require_server_version | ||
from clickhouse_driver.columns import nestedcolumn | ||
|
||
|
||
class NestedTestCase(BaseTestCase): | ||
def setUp(self): | ||
ret = super(NestedTestCase, self).setUp() | ||
return ret | ||
|
||
def entuple(self, lst): | ||
return tuple( | ||
self.entuple(x) if isinstance(x, list) else x for x in lst | ||
) | ||
|
||
@require_server_version(21, 3, 13) | ||
def test_simple(self): | ||
columns = 'n Nested(i Int32, s String)' | ||
|
||
# INSERT INTO test_nested VALUES ([(0, 'a'), (1, 'b')]); | ||
data = [([(0, 'a'), (1, 'b')],)] | ||
|
||
with self.create_table(columns, flatten_nested=0): | ||
self.client.execute( | ||
'INSERT INTO test (n) VALUES', data | ||
) | ||
|
||
query = 'SELECT * FROM test' | ||
inserted = self.emit_cli(query) | ||
self.assertEqual(inserted, "[(0,'a'),(1,'b')]\n") | ||
|
||
inserted = self.client.execute(query) | ||
self.assertEqual(inserted, data) | ||
|
||
projected_i = self.client.execute('SELECT n.i FROM test') | ||
self.assertEqual( | ||
projected_i, | ||
[([0, 1],)] | ||
) | ||
|
||
projected_s = self.client.execute('SELECT n.s FROM test') | ||
self.assertEqual( | ||
projected_s, | ||
[(['a', 'b'],)] | ||
) | ||
|
||
@require_server_version(21, 3, 13) | ||
def test_multiple_rows(self): | ||
columns = 'n Nested(i Int32, s String)' | ||
|
||
data = [([(0, 'a'), (1, 'b')],), ([(3, 'd'), (4, 'e')],)] | ||
|
||
with self.create_table(columns, flatten_nested=0): | ||
self.client.execute( | ||
'INSERT INTO test (n) VALUES', data | ||
) | ||
|
||
query = 'SELECT * FROM test' | ||
inserted = self.emit_cli(query) | ||
self.assertEqual( | ||
inserted, | ||
"[(0,'a'),(1,'b')]\n[(3,'d'),(4,'e')]\n" | ||
) | ||
|
||
inserted = self.client.execute(query) | ||
self.assertEqual(inserted, data) | ||
|
||
@require_server_version(21, 3, 13) | ||
def test_dict(self): | ||
columns = 'n Nested(i Int32, s String)' | ||
|
||
data = [ | ||
{'n': [{'i': 0, 's': 'a'}, {'i': 1, 's': 'b'}]}, | ||
{'n': [{'i': 3, 's': 'd'}, {'i': 4, 's': 'e'}]}, | ||
] | ||
|
||
with self.create_table(columns, flatten_nested=0): | ||
self.client.execute( | ||
'INSERT INTO test (n) VALUES', data | ||
) | ||
|
||
query = 'SELECT * FROM test' | ||
inserted = self.emit_cli(query) | ||
self.assertEqual( | ||
inserted, | ||
"[(0,'a'),(1,'b')]\n[(3,'d'),(4,'e')]\n" | ||
) | ||
|
||
inserted = self.client.execute(query) | ||
self.assertEqual( | ||
inserted, | ||
[([(0, 'a'), (1, 'b')],), ([(3, 'd'), (4, 'e')],)] | ||
) | ||
|
||
def test_get_nested_columns(self): | ||
self.assertEqual( | ||
nestedcolumn.get_nested_columns( | ||
'Nested(a Tuple(Array(Int8)),\n b Nullable(String))', | ||
), | ||
['Tuple(Array(Int8))', 'Nullable(String)'] | ||
) | ||
|
||
def test_get_columns_with_types(self): | ||
self.assertEqual( | ||
nestedcolumn.get_columns_with_types( | ||
'Nested(a Tuple(Array(Int8)),\n b Nullable(String))', | ||
), | ||
[('a', 'Tuple(Array(Int8))'), ('b', 'Nullable(String)')] | ||
) | ||
|
||
def test_get_inner_spec(self): | ||
inner = 'a Tuple(Array(Int8), Array(Int64)), b Nullable(String)' | ||
self.assertEqual( | ||
nestedcolumn.get_inner_spec('Nested({}) dummy '.format(inner)), | ||
inner | ||
) |