asynch 0.2.3__tar.gz → 0.2.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. {asynch-0.2.3 → asynch-0.2.4}/PKG-INFO +1 -1
  2. {asynch-0.2.3 → asynch-0.2.4}/asynch/connection.py +2 -4
  3. {asynch-0.2.3 → asynch-0.2.4}/asynch/pool.py +31 -8
  4. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/__init__.py +2 -1
  5. asynch-0.2.4/asynch/proto/columns/datecolumn.py +77 -0
  6. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/context.py +6 -2
  7. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/streams/buffered.py +41 -52
  8. {asynch-0.2.3 → asynch-0.2.4}/pyproject.toml +1 -1
  9. asynch-0.2.3/asynch/proto/columns/datecolumn.py +0 -48
  10. {asynch-0.2.3 → asynch-0.2.4}/CHANGELOG.md +0 -0
  11. {asynch-0.2.3 → asynch-0.2.4}/LICENSE +0 -0
  12. {asynch-0.2.3 → asynch-0.2.4}/README.md +0 -0
  13. {asynch-0.2.3 → asynch-0.2.4}/asynch/__init__.py +0 -0
  14. {asynch-0.2.3 → asynch-0.2.4}/asynch/cursors.py +0 -0
  15. {asynch-0.2.3 → asynch-0.2.4}/asynch/errors.py +0 -0
  16. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/__init__.py +0 -0
  17. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/block.py +0 -0
  18. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/arraycolumn.py +0 -0
  19. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/base.py +0 -0
  20. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/boolcolumn.py +0 -0
  21. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/datetimecolumn.py +0 -0
  22. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/decimalcolumn.py +0 -0
  23. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/enumcolumn.py +0 -0
  24. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/floatcolumn.py +0 -0
  25. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/intcolumn.py +0 -0
  26. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/intervalcolumn.py +0 -0
  27. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/ipcolumn.py +0 -0
  28. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/jsoncolumn.py +0 -0
  29. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/lowcardinalitycolumn.py +0 -0
  30. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/mapcolumn.py +0 -0
  31. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/nestedcolumn.py +0 -0
  32. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/nothingcolumn.py +0 -0
  33. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/nullablecolumn.py +0 -0
  34. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/nullcolumn.py +0 -0
  35. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/simpleaggregatefunctioncolumn.py +0 -0
  36. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/stringcolumn.py +0 -0
  37. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/tuplecolumn.py +0 -0
  38. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/util.py +0 -0
  39. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/columns/uuidcolumn.py +0 -0
  40. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/compression/__init__.py +0 -0
  41. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/compression/lz4.py +0 -0
  42. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/compression/lz4hc.py +0 -0
  43. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/compression/zstd.py +0 -0
  44. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/connection.py +0 -0
  45. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/constants.py +0 -0
  46. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/cs.py +0 -0
  47. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/opentelemetry.py +0 -0
  48. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/progress.py +0 -0
  49. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/protocol.py +0 -0
  50. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/result.py +0 -0
  51. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/settings/__init__.py +0 -0
  52. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/settings/available.py +0 -0
  53. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/settings/types.py +0 -0
  54. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/streams/__init__.py +0 -0
  55. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/streams/block.py +0 -0
  56. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/streams/compressed.py +0 -0
  57. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/utils/__init__.py +0 -0
  58. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/utils/compat.py +0 -0
  59. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/utils/escape.py +0 -0
  60. {asynch-0.2.3 → asynch-0.2.4}/asynch/proto/utils/helpers.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: asynch
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: A asyncio driver for ClickHouse with native tcp protocol
5
5
  Home-page: https://github.com/long2ice/asynch
6
6
  License: Apache-2.0
@@ -33,7 +33,6 @@ class Connection:
33
33
  self._is_closed = False
34
34
  self._echo = echo
35
35
  self._cursor_cls = cursor_cls
36
- self._connected = False
37
36
  if dsn:
38
37
  self._connection = ProtoConnection(
39
38
  **self._parse_dsn(dsn), stack_track=stack_track, **kwargs
@@ -54,7 +53,7 @@ class Connection:
54
53
 
55
54
  @property
56
55
  def connected(self):
57
- return self._connected
56
+ return self._connection.connected
58
57
 
59
58
  @property
60
59
  def host(self):
@@ -93,10 +92,9 @@ class Connection:
93
92
  raise errors.NotSupportedError
94
93
 
95
94
  async def connect(self):
96
- if self._connected:
95
+ if self.connected:
97
96
  return
98
97
  await self._connection.connect()
99
- self._connected = True
100
98
 
101
99
  def cursor(self, cursor: Type[Cursor] = None) -> Cursor:
102
100
  cursor_cls = cursor or self._cursor_cls
@@ -1,11 +1,14 @@
1
1
  import asyncio
2
2
  import collections
3
+ import logging
3
4
  from asyncio import Condition
4
5
  from collections.abc import Coroutine
5
6
  from typing import Deque, Set
6
7
 
7
8
  from asynch.connection import Connection, connect
8
9
 
10
+ logger = logging.getLogger(__name__)
11
+
9
12
 
10
13
  class _ContextManager(Coroutine):
11
14
  __slots__ = ("_coro", "_obj")
@@ -144,8 +147,9 @@ class Pool(asyncio.AbstractServer):
144
147
  if self._closing:
145
148
  await connection.close()
146
149
  else:
150
+ connection._connection.reset_state()
147
151
  self._free.append(connection)
148
- fut = self._loop.create_task(self._wakeup())
152
+ fut = self._loop.create_task(self._wakeup())
149
153
  return fut
150
154
 
151
155
  async def _wakeup(self):
@@ -155,6 +159,15 @@ class Pool(asyncio.AbstractServer):
155
159
  def _wait(self):
156
160
  return len(self._terminated) > 0
157
161
 
162
+ async def _check_conn(self, conn) -> bool:
163
+ try:
164
+ async with conn.cursor() as cursor:
165
+ await cursor.execute("SELECT 1")
166
+ return True
167
+ except Exception as e:
168
+ logger.warning(e)
169
+ return False
170
+
158
171
  def acquire(self):
159
172
  return _PoolAcquireContextManager(self._acquire(), self)
160
173
 
@@ -163,19 +176,29 @@ class Pool(asyncio.AbstractServer):
163
176
  raise RuntimeError("Cannot acquire connection after closing pool")
164
177
  async with self._cond:
165
178
  while True:
166
- await self.initialize()
179
+ await self.initialize() # Restore minsize
180
+
181
+ if not self._free and self.size < self.maxsize:
182
+ await self.init_one_connection()
183
+
167
184
  if self._free:
168
185
  conn = self._free.popleft()
169
- self._used.add(conn)
170
- return conn
186
+ if await self._check_conn(conn):
187
+ self._used.add(conn)
188
+ return conn
189
+ else:
190
+ continue
171
191
  else:
172
192
  await self._cond.wait()
173
193
 
174
194
  async def initialize(self):
175
- while self.size < self.minsize:
176
- conn = await connect(**self._connection_kwargs)
177
- self._free.append(conn)
178
- self._cond.notify()
195
+ while self.freesize < self.minsize and self.size <= self.maxsize:
196
+ await self.init_one_connection()
197
+
198
+ async def init_one_connection(self):
199
+ conn = await connect(**self._connection_kwargs)
200
+ self._free.append(conn)
201
+ self._cond.notify()
179
202
 
180
203
  async def clear(self):
181
204
  """Close all free connections in pool."""
@@ -7,7 +7,7 @@ from ...errors import (
7
7
  from ..streams.buffered import BufferedReader, BufferedWriter
8
8
  from .arraycolumn import create_array_column
9
9
  from .boolcolumn import BoolColumn
10
- from .datecolumn import DateColumn
10
+ from .datecolumn import Date32Column, DateColumn
11
11
  from .datetimecolumn import create_datetime_column
12
12
  from .decimalcolumn import create_decimal_column
13
13
  from .enumcolumn import create_enum_column
@@ -52,6 +52,7 @@ column_by_type = {
52
52
  c.ch_type: c
53
53
  for c in [
54
54
  DateColumn,
55
+ Date32Column,
55
56
  Float32,
56
57
  Float64,
57
58
  BoolColumn,
@@ -0,0 +1,77 @@
1
+ from datetime import date, timedelta
2
+
3
+ import ciso8601
4
+
5
+ from .base import FormatColumn
6
+
7
+ epoch_start = date(1970, 1, 1)
8
+ epoch_end = date(2149, 6, 6)
9
+
10
+ epoch_start_date32 = date(1900, 1, 1)
11
+ epoch_end_date32 = date(2299, 12, 31)
12
+
13
+
14
+ class LazyLUT(dict):
15
+ def __init__(self, *args, _factory, **kwargs):
16
+ super().__init__(*args, **kwargs)
17
+ self._default_factory = _factory
18
+
19
+ def __missing__(self, key):
20
+ return self.setdefault(key, self._default_factory(key))
21
+
22
+
23
+ lazy_date_lut = LazyLUT(_factory=lambda x: epoch_start + timedelta(x))
24
+ lazy_date_lut_reverse = LazyLUT(_factory=lambda x: (x - epoch_start).days)
25
+
26
+
27
+ class DateColumn(FormatColumn):
28
+ ch_type = "Date"
29
+ py_types = (date,)
30
+ format = "H"
31
+
32
+ min_value = epoch_start
33
+ max_value = epoch_end
34
+
35
+ date_lut = lazy_date_lut
36
+ date_lut_reverse = lazy_date_lut_reverse
37
+
38
+ def before_write_items(self, items, nulls_map=None):
39
+ null_value = self.null_value
40
+
41
+ date_lut_reverse = self.date_lut_reverse
42
+ min_value = self.min_value
43
+ max_value = self.max_value
44
+
45
+ for i, item in enumerate(items):
46
+ if nulls_map and nulls_map[i]:
47
+ items[i] = null_value
48
+ continue
49
+ if isinstance(item, str):
50
+ item = ciso8601.parse_datetime(item).date()
51
+ if item is not date:
52
+ item = date(item.year, item.month, item.day)
53
+ if min_value <= item <= max_value:
54
+ items[i] = date_lut_reverse[item]
55
+ else:
56
+ items[i] = 0
57
+
58
+ def after_read_items(self, items, nulls_map=None):
59
+ date_lut = self.date_lut
60
+
61
+ if nulls_map is None:
62
+ return tuple(date_lut[item] for item in items)
63
+ else:
64
+ return tuple(
65
+ (None if is_null else date_lut[items[i]]) for i, is_null in enumerate(nulls_map)
66
+ )
67
+
68
+
69
+ class Date32Column(DateColumn):
70
+ ch_type = "Date32"
71
+ format = "i"
72
+
73
+ min_value = epoch_start_date32
74
+ max_value = epoch_end_date32
75
+
76
+ date_lut = lazy_date_lut
77
+ date_lut_reverse = lazy_date_lut_reverse
@@ -46,8 +46,12 @@ class ExecuteContext:
46
46
  self._connection.make_query_settings(settings)
47
47
 
48
48
  async def __aenter__(self):
49
- await self._connection.force_connect()
50
- self._connection.last_query = QueryInfo(self._connection.reader)
49
+ try:
50
+ await self._connection.force_connect()
51
+ self._connection.last_query = QueryInfo(self._connection.reader)
52
+ except (Exception, KeyboardInterrupt):
53
+ await self._connection.disconnect()
54
+ raise
51
55
 
52
56
  async def __aexit__(self, exc_type, exc_val, exc_tb):
53
57
  if exc_type:
@@ -111,6 +111,24 @@ class BufferedReader:
111
111
  self.current_buffer_size = 0
112
112
  self.position = 0
113
113
 
114
+ async def _refill_buffer(self):
115
+ if self.position == self.current_buffer_size:
116
+ self._reset_buffer()
117
+ await self._read_into_buffer()
118
+
119
+ def _is_buffer_empty(self):
120
+ return not (self.buffer or self.position)
121
+
122
+ async def _is_buffer_readable(self) -> bool:
123
+ await self._refill_buffer()
124
+ if self._is_buffer_empty():
125
+ return False
126
+ return True
127
+
128
+ def _reset_buffer(self):
129
+ self.position = 0
130
+ self.buffer = bytearray()
131
+
114
132
  async def _read_into_buffer(self):
115
133
  packet = await self.reader.read(self.buffer_max_size)
116
134
  self.buffer.extend(packet)
@@ -122,97 +140,68 @@ class BufferedReader:
122
140
  return packet
123
141
 
124
142
  async def read_varint(self):
125
- if self.position == self.current_buffer_size:
126
- self._reset_buffer()
127
- await self._read_into_buffer()
128
143
  packets = bytearray()
129
144
  while True:
145
+ if not (await self._is_buffer_readable()):
146
+ break
130
147
  packet = self._read_one()
131
148
  packets.append(packet)
132
149
  if packet < 0x80:
133
150
  break
134
151
  return leb128.u.decode(packets)
135
152
 
136
- def _reset_buffer(self):
137
- self.position = 0
138
- self.buffer = bytearray()
139
-
140
- async def read_str(self, as_bytes: bool = False):
141
- length = await self.read_varint()
142
- packet = await self.read_bytes(length)
143
- if as_bytes:
144
- return packet
145
- return packet.decode()
146
-
147
- async def read_fixed_str(self, length: int, as_bytes: bool = False):
148
- packet = await self.read_bytes(length)
149
- if as_bytes:
150
- return packet
151
- return packet.decode()
152
-
153
153
  async def read_bytes(self, length: int):
154
154
  packets = bytearray()
155
155
  while length > 0:
156
- if self.position == self.current_buffer_size:
157
- self._reset_buffer()
158
- await self._read_into_buffer()
159
-
156
+ if not (await self._is_buffer_readable()):
157
+ break
160
158
  read_position = self.position + length
161
159
  packet = self.buffer[self.position : read_position] # noqa: E203
162
160
  length -= len(packet)
163
161
  self.position += len(packet)
164
162
  packets.extend(packet)
165
-
166
163
  return packets
167
164
 
165
+ async def read_fixed_str(self, length: int, as_bytes: bool = False):
166
+ packet = await self.read_bytes(length)
167
+ if as_bytes:
168
+ return packet
169
+ return packet.decode()
170
+
171
+ async def read_str(self, as_bytes: bool = False):
172
+ length = await self.read_varint()
173
+ return await self.read_fixed_str(length=length, as_bytes=as_bytes)
174
+
168
175
  async def read_int(self, fmt: str):
169
176
  s = struct.Struct("<" + fmt)
170
177
  packet = await self.read_bytes(s.size)
171
178
  return s.unpack(packet)[0]
172
179
 
173
- async def read_int8(
174
- self,
175
- ):
180
+ async def read_int8(self):
176
181
  return await self.read_int("b")
177
182
 
178
- async def read_int16(
179
- self,
180
- ):
183
+ async def read_int16(self):
181
184
  return await self.read_int("h")
182
185
 
183
- async def read_int32(
184
- self,
185
- ):
186
+ async def read_int32(self):
186
187
  return await self.read_int("i")
187
188
 
188
- async def read_int64(
189
- self,
190
- ):
189
+ async def read_int64(self):
191
190
  return await self.read_int("q")
192
191
 
193
- async def read_uint8(
194
- self,
195
- ):
192
+ async def read_uint8(self):
196
193
  return await self.read_int("B")
197
194
 
198
- async def read_uint16(
199
- self,
200
- ):
195
+ async def read_uint16(self):
201
196
  return await self.read_int("H")
202
197
 
203
- async def read_uint32(
204
- self,
205
- ):
198
+ async def read_uint32(self):
206
199
  return await self.read_int("I")
207
200
 
208
- async def read_uint64(
209
- self,
210
- ):
201
+ async def read_uint64(self):
211
202
  return await self.read_int("Q")
212
203
 
213
- async def read_uint128(
214
- self,
215
- ):
204
+ async def read_uint128(self):
216
205
  hi = await self.read_int("Q")
217
206
  lo = await self.read_int("Q")
218
207
  return (hi << 64) + lo
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "asynch"
3
- version = "0.2.3"
3
+ version = "0.2.4"
4
4
  description = "A asyncio driver for ClickHouse with native tcp protocol"
5
5
  authors = ["long2ice <long2ice@gmail.com>"]
6
6
  license = "Apache-2.0"
@@ -1,48 +0,0 @@
1
- from datetime import date, timedelta
2
-
3
- import ciso8601
4
-
5
- from .base import FormatColumn
6
-
7
- epoch_start = date(1970, 1, 1)
8
-
9
-
10
- class DateColumn(FormatColumn):
11
- ch_type = "Date"
12
- py_types = (date,)
13
- format = "H"
14
-
15
- epoch_start = epoch_start
16
- epoch_end = date(2105, 12, 31)
17
-
18
- date_lut = {x: epoch_start + timedelta(x) for x in range(65535)}
19
- date_lut_reverse = {value: key for key, value in date_lut.items()}
20
-
21
- def before_write_items(self, items, nulls_map=None):
22
- null_value = self.null_value
23
-
24
- date_lut_reverse = self.date_lut_reverse
25
- epoch_start = self.epoch_start
26
- epoch_end = self.epoch_end
27
-
28
- for i, item in enumerate(items):
29
- if nulls_map and nulls_map[i]:
30
- items[i] = null_value
31
- continue
32
-
33
- if isinstance(item, str):
34
- item = ciso8601.parse_datetime(item).date()
35
- if item > epoch_end or item < epoch_start:
36
- items[i] = 0
37
- else:
38
- items[i] = date_lut_reverse[item]
39
-
40
- def after_read_items(self, items, nulls_map=None):
41
- date_lut = self.date_lut
42
-
43
- if nulls_map is None:
44
- return tuple(date_lut[item] for item in items)
45
- else:
46
- return tuple(
47
- (None if is_null else date_lut[items[i]]) for i, is_null in enumerate(nulls_map)
48
- )
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes