5
5
import logging
6
6
import time
7
7
from datetime import datetime , timezone
8
- from typing import Any , Callable , Dict , List , Mapping , Optional , Sequence , Tuple
8
+ from typing import (
9
+ Any ,
10
+ Callable ,
11
+ Dict ,
12
+ List ,
13
+ Mapping ,
14
+ Optional ,
15
+ Sequence ,
16
+ Set ,
17
+ Tuple ,
18
+ Union ,
19
+ )
9
20
10
21
import clickhouse_driver
11
22
import numpy
@@ -156,7 +167,7 @@ def __init__(
156
167
self ._client = client
157
168
# The following attributes belong to the batched insert mechanism.
158
169
# Inserting in batches is much faster than inserting single rows.
159
- self ._str_cols = set ()
170
+ self ._str_cols : Set [ str ] = set ()
160
171
self ._insert_query : str = ""
161
172
self ._insert_queue : List [Dict [str , Any ]] = []
162
173
self ._last_insert = time .time ()
@@ -176,13 +187,16 @@ def append(
176
187
self ._insert_query = f"INSERT INTO { self .cid } (`_draw_idx`,`{ names } `) VALUES"
177
188
self ._str_cols = {k for k , v in params .items () if "str" in numpy .asarray (v ).dtype .name }
178
189
179
- # Convert str ndarrays to lists
190
+ params_ins : Dict [str , Union [numpy .ndarray , int , float , List [str ]]] = {
191
+ "_draw_idx" : self ._draw_idx ,
192
+ ** params ,
193
+ }
194
+ # Convert str-dtyped ndarrays to lists
180
195
for col in self ._str_cols :
181
- params [col ] = params [col ].tolist ()
196
+ params_ins [col ] = params [col ].tolist ()
182
197
183
198
# Queue up for insertion
184
- params ["_draw_idx" ] = self ._draw_idx
185
- self ._insert_queue .append (params )
199
+ self ._insert_queue .append (params_ins )
186
200
self ._draw_idx += 1
187
201
188
202
if (
@@ -242,13 +256,14 @@ def _get_rows(
242
256
243
257
# Without draws return empty arrays of the correct shape/dtype
244
258
if not draws :
245
- if is_rigid (nshape ):
246
- return numpy .empty (shape = [0 ] + nshape , dtype = dtype )
259
+ if is_rigid (nshape ) and nshape is not None :
260
+ return numpy .empty (shape = [0 , * nshape ] , dtype = dtype )
247
261
return numpy .array ([], dtype = object )
248
262
249
263
# The unpacking must also account for non-rigid shapes
250
264
# and str-dtyped empty arrays default to fixed length 1 strings.
251
265
# The [None] list is slower, but more flexible in this regard.
266
+ buffer : Union [numpy .ndarray , Sequence ]
252
267
if is_rigid (nshape ) and dtype != "str" :
253
268
assert nshape is not None
254
269
buffer = numpy .empty ((draws , * nshape ), dtype )
@@ -292,7 +307,7 @@ def __init__(
292
307
self ,
293
308
meta : RunMeta ,
294
309
* ,
295
- created_at : datetime = None ,
310
+ created_at : Optional [ datetime ] = None ,
296
311
client_fn : Callable [[], clickhouse_driver .Client ],
297
312
) -> None :
298
313
self ._client_fn = client_fn
@@ -331,8 +346,8 @@ class ClickHouseBackend(Backend):
331
346
332
347
def __init__ (
333
348
self ,
334
- client : clickhouse_driver .Client = None ,
335
- client_fn : Callable [[], clickhouse_driver .Client ] = None ,
349
+ client : Optional [ clickhouse_driver .Client ] = None ,
350
+ client_fn : Optional [ Callable [[], clickhouse_driver .Client ] ] = None ,
336
351
):
337
352
"""Create a ClickHouse backend around a database client.
338
353
0 commit comments