Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit f875384

Browse files
authored
Merge pull request #87 from databendlabs/feat/support-batch-insert-dict
feat: support batch insert with dict list
2 parents d29c4a3 + 566e1a9 commit f875384

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

databend_py/client.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def _iter_receive_result(self, query, query_id=None, with_column_types=False):
8181
yield r
8282

8383
def execute(
84-
self, query, params=None, with_column_types=False, query_id=None, settings=None
84+
self, query, params=None, with_column_types=False, query_id=None, settings=None
8585
):
8686
"""
8787
Executes query.
@@ -138,17 +138,20 @@ def _process_insert_query(self, query, params):
138138
if params is not None and len(params) > 0:
139139
if isinstance(params[0], tuple):
140140
tuple_ls = params
141+
elif isinstance(params[0], dict):
142+
# if params type is list[dictionary], then it's a insert query
143+
tuple_ls = [tuple(p.values()) for p in params]
141144
else:
142145
tuple_ls = [
143-
tuple(params[i : i + batch_size])
146+
tuple(params[i: i + batch_size])
144147
for i in range(0, len(params), batch_size)
145148
]
146149
insert_rows = len(tuple_ls)
147150
self._uploader.upload_to_table_by_copy(table_name, tuple_ls)
148151
return insert_rows
149152

150153
def _process_ordinary_query(
151-
self, query, params=None, with_column_types=False, query_id=None
154+
self, query, params=None, with_column_types=False, query_id=None
152155
):
153156
if params is not None:
154157
query = self._substitute_params(query, params, self.connection.context)
@@ -159,7 +162,7 @@ def _process_ordinary_query(
159162
)
160163

161164
def execute_iter(
162-
self, query, params=None, with_column_types=False, query_id=None, settings=None
165+
self, query, params=None, with_column_types=False, query_id=None, settings=None
163166
):
164167
if params is not None:
165168
query = self._substitute_params(query, params, self.connection.context)
@@ -168,7 +171,7 @@ def execute_iter(
168171
)
169172

170173
def _iter_process_ordinary_query(
171-
self, query, with_column_types=False, query_id=None
174+
self, query, with_column_types=False, query_id=None
172175
):
173176
return self._iter_receive_result(
174177
query, query_id=query_id, with_column_types=with_column_types

tests/test_client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ def test_batch_insert_with_tuple(self):
131131
_, ss = c.execute("select * from test")
132132
self.assertEqual(ss, [(3, "aa"), (4, "bb")])
133133

134+
def test_batch_insert_with_dict_list(self):
135+
c = Client.from_url(self.databend_url)
136+
c.execute("DROP TABLE IF EXISTS test")
137+
c.execute("CREATE TABLE if not exists test (x Int32,y VARCHAR)")
138+
c.execute("DESC test")
139+
_, r1 = c.execute("INSERT INTO test (x,y) VALUES", [{"x": 5, "y": "cc"}, {"x": 6, "y": "dd"}])
140+
self.assertEqual(r1, 2)
141+
_, ss = c.execute("select * from test")
142+
self.assertEqual(ss, [(5, "cc"), (6, "dd")])
143+
134144
def test_iter_query(self):
135145
client = Client.from_url(self.databend_url)
136146
result = client.execute_iter("select 1", with_column_types=False)

0 commit comments

Comments
 (0)