Skip to content

Commit

Permalink
Arbitrary dataframe columns order on insert #245
Browse files Browse the repository at this point in the history
  • Loading branch information
xzkostyan committed Sep 20, 2021
1 parent 265dea9 commit c8747b2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
23 changes: 18 additions & 5 deletions clickhouse_driver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,25 @@ def insert_dataframe(
except ImportError:
raise RuntimeError('Extras for NumPy must be installed')

columns = [dataframe[col].values for col in dataframe]
start_time = time()
self.make_query_settings(settings)
self.connection.force_connect()
self.last_query = QueryInfo()

return self.execute(
query, columns, columnar=True, external_tables=external_tables,
query_id=query_id, settings=settings
)
with self.disconnect_on_error(query):
self.connection.send_query(query, query_id=query_id)
self.connection.send_external_tables(external_tables)

sample_block = self.receive_sample_block()
rv = None
if sample_block:
columns = [x[0] for x in sample_block.columns_with_types]
data = [dataframe[column].values for column in columns]
rv = self.send_data(sample_block, data, columnar=True)
self.receive_end_of_query()

self.last_query.store_elapsed(time() - start_time)
return rv

def process_ordinary_query_with_progress(
self, query, params=None, with_column_types=False,
Expand Down
13 changes: 13 additions & 0 deletions tests/numpy/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ def test_insert_chunking(self):
)
self.assertEqual(rv, 3)

def test_insert_not_ordered_columns(self):
n = 10
df = pd.DataFrame({
'b': range(n),
'a': [str(x) for x in range(n)]
})[['b', 'a']]

with self.create_table('a String, b Float64'):
rv = self.client.insert_dataframe(
'INSERT INTO test (a, b) VALUES', df
)
self.assertEqual(rv, n)


class NoNumPyTestCase(BaseTestCase):
def setUp(self):
Expand Down

0 comments on commit c8747b2

Please sign in to comment.