Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix client.insert_dataframe() for tuple columns in tables #425

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions clickhouse_driver/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def read(self, buf):


class BaseBlock(object):
def __init__(self, columns_with_types=None, data=None,
info=None, types_check=False):
def __init__(
self, columns_with_types=None, data=None, info=None, types_check=False
):
self.columns_with_types = columns_with_types or []
self.types_check = types_check
self.info = info or BlockInfo()
Expand Down Expand Up @@ -96,7 +97,7 @@ def _check_number_of_columns(self, data):

got = len(data)
if expected_row_len != got:
msg = 'Expected {} columns, got {}'.format(expected_row_len, got)
msg = "Expected {} columns, got {}".format(expected_row_len, got)
raise ValueError(msg)

def _check_all_columns_equal_length(self, data):
Expand All @@ -105,12 +106,12 @@ def _check_all_columns_equal_length(self, data):
for column in data:
got = len(column)
if got != expected:
msg = 'Expected {} rows, got {}'.format(expected, got)
msg = "Expected {} rows, got {}".format(expected, got)
raise ValueError(msg)


class RowOrientedBlock(BaseBlock):
dict_row_types = (dict, )
dict_row_types = (dict,)
tuple_row_types = (list, tuple)
supported_row_types = dict_row_types + tuple_row_types

Expand Down Expand Up @@ -171,8 +172,8 @@ def _pure_mutate_dicts_to_rows(
columns_with_cwt = []
for name, type_ in columns_with_types:
cwt = None
if type_.startswith('Nested'):
inner_spec = get_inner_spec('Nested', type_)
if type_.startswith("Nested"):
inner_spec = get_inner_spec("Nested", type_)
cwt = get_inner_columns_with_types(inner_spec)
columns_with_cwt.append((name, cwt))

Expand All @@ -185,9 +186,11 @@ def _pure_mutate_dicts_to_rows(
if cwt is None:
new_data.append(row[name])
else:
new_data.append(self._pure_mutate_dicts_to_rows(
row[name], cwt, check_row_type
))
new_data.append(
self._pure_mutate_dicts_to_rows(
row[name], cwt, check_row_type
)
)
data[i] = new_data
# return for recursion
return data
Expand All @@ -197,7 +200,7 @@ def _check_rows(self, data):

got = len(data[0])
if expected_row_len != got:
msg = 'Expected {} columns, got {}'.format(expected_row_len, got)
msg = "Expected {} columns, got {}".format(expected_row_len, got)
raise ValueError(msg)

if self.types_check:
Expand All @@ -208,20 +211,19 @@ def _check_rows(self, data):
def _check_row_type(self, row):
if not isinstance(row, self.supported_row_types):
raise TypeError(
'Unsupported row type: {}. dict, list or tuple is expected.'
.format(type(row))
f"Unsupported row type: {type(row)}. "
"dict, list or tuple is expected."
)

def _check_tuple_row_type(self, row):
if not isinstance(row, self.tuple_row_types):
raise TypeError(
'Unsupported row type: {}. list or tuple is expected.'
.format(type(row))
f"Unsupported row type: {type(row)}. "
"list or tuple is expected."
)

def _check_dict_row_type(self, row):
if not isinstance(row, self.dict_row_types):
raise TypeError(
'Unsupported row type: {}. dict is expected.'
.format(type(row))
"Unsupported row type: {}. dict is expected.".format(type(row))
)
123 changes: 56 additions & 67 deletions clickhouse_driver/bufferedreader.c

Large diffs are not rendered by default.

Loading