Skip to content

Commit

Permalink
Fix empty external tables sending #240
Browse files Browse the repository at this point in the history
  • Loading branch information
xzkostyan committed Aug 19, 2021
1 parent 10103f8 commit 78e389e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clickhouse_driver/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ def normalize(self, data):

@property
def num_columns(self):
if self.columns_with_types is not None:
return len(self.columns_with_types)

return len(self.data[0]) if self.num_rows else 0

@property
Expand Down
5 changes: 5 additions & 0 deletions clickhouse_driver/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,11 @@ def send_cancel(self):

def send_external_tables(self, tables, types_check=False):
for table in tables or []:
if not table['structure']:
raise ValueError(
'Empty table "{}" structure'.format(table['name'])
)

block = RowOrientedBlock(table['structure'], table['data'],
types_check=types_check)
self.send_data(block, table_name=table['name'])
Expand Down
20 changes: 20 additions & 0 deletions tests/test_external_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,23 @@ def test_select(self):
}]
rv = self.client.execute('SELECT * FROM test', external_tables=tables)
self.assertEqual(rv, [(100, [2, 4, 6, 8]), (500, [1, 3, 5, 7])])

def test_send_empty_table(self):
tables = [{
'name': 'test',
'structure': [('x', 'Int32')],
'data': []
}]
rv = self.client.execute('SELECT * FROM test', external_tables=tables)
self.assertEqual(rv, [])

def test_send_empty_table_structure(self):
tables = [{
'name': 'test',
'structure': [],
'data': []
}]
with self.assertRaises(ValueError) as e:
self.client.execute('SELECT * FROM test', external_tables=tables)

self.assertIn('Empty table "test" structure', str(e.exception))

0 comments on commit 78e389e

Please sign in to comment.