crate 2.2.1b3__tar.gz → 2.4.0__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.
- {crate-2.2.1b3 → crate-2.4.0}/CHANGES.rst +29 -2
- {crate-2.2.1b3 → crate-2.4.0}/PKG-INFO +4 -4
- {crate-2.2.1b3 → crate-2.4.0}/docs/by-example/client.rst +4 -8
- {crate-2.2.1b3 → crate-2.4.0}/docs/by-example/cursor.rst +52 -3
- {crate-2.2.1b3 → crate-2.4.0}/docs/by-example/http.rst +1 -1
- {crate-2.2.1b3 → crate-2.4.0}/docs/connect.rst +20 -1
- {crate-2.2.1b3 → crate-2.4.0}/docs/query.rst +7 -6
- {crate-2.2.1b3 → crate-2.4.0}/pyproject.toml +9 -9
- crate-2.4.0/src/crate/client/__init__.py +49 -0
- crate-2.4.0/src/crate/client/_codec.py +79 -0
- crate-2.4.0/src/crate/client/_options.py +120 -0
- crate-2.4.0/src/crate/client/_pep440.py +1 -0
- crate-2.4.0/src/crate/client/_pool.py +120 -0
- crate-2.4.0/src/crate/client/_request.py +131 -0
- crate-2.4.0/src/crate/client/_response.py +164 -0
- crate-2.4.0/src/crate/client/blob.py +105 -0
- crate-2.4.0/src/crate/client/connection.py +241 -0
- crate-2.4.0/src/crate/client/converter.py +211 -0
- crate-2.4.0/src/crate/client/cursor.py +431 -0
- crate-2.4.0/src/crate/client/exceptions.py +101 -0
- crate-2.4.0/src/crate/client/http.py +506 -0
- crate-2.4.0/src/crate/testing/layer.py +431 -0
- crate-2.4.0/src/crate/testing/util.py +95 -0
- crate-2.4.0/tests/client/test_connection.py +319 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/client/test_cursor.py +338 -11
- {crate-2.2.1b3 → crate-2.4.0}/tests/client/test_http.py +20 -3
- crate-2.4.0/tests/client/test_options.py +149 -0
- crate-2.4.0/tests/client/test_pool.py +148 -0
- crate-2.4.0/tests/client/test_request.py +177 -0
- crate-2.4.0/tests/client/test_response.py +217 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/client/test_serialization.py +1 -1
- crate-2.4.0/tests/testing/__init__.py +0 -0
- crate-2.2.1b3/examples/README.rst +0 -9
- crate-2.2.1b3/tests/client/test_connection.py +0 -169
- {crate-2.2.1b3 → crate-2.4.0}/.gitignore +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/CONTRIBUTING.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/DEVELOP.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/LICENSE +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/NOTICE +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/README.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/.gitignore +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/Makefile +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/_extra/robots.txt +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/blobs.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/build.json +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/by-example/blob.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/by-example/connection.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/by-example/https.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/by-example/index.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/conf.py +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/data-types.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/docutils.conf +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/getting-started.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/index-all.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/index.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/other-options.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/docs/requirements.txt +0 -0
- {crate-2.2.1b3/tests → crate-2.4.0/src/crate/testing}/__init__.py +0 -0
- {crate-2.2.1b3/tests/client → crate-2.4.0/tests}/__init__.py +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/assets/import/test_a.json +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/assets/mappings/locations.sql +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/assets/pki/cacert_invalid.pem +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/assets/pki/cacert_valid.pem +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/assets/pki/client_invalid.pem +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/assets/pki/client_valid.pem +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/assets/pki/readme.rst +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/assets/pki/server_valid.pem +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/assets/settings/test_a.json +0 -0
- {crate-2.2.1b3/tests/testing → crate-2.4.0/tests/client}/__init__.py +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/client/settings.py +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/client/test_blob.py +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/client/test_exceptions.py +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/client/test_utils.py +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/conftest.py +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/test_docs.py +0 -0
- {crate-2.2.1b3 → crate-2.4.0}/tests/testing/test_layer.py +0 -0
|
@@ -2,12 +2,39 @@
|
|
|
2
2
|
Changes for crate
|
|
3
3
|
=================
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
2026/10/30 2.4.0
|
|
6
|
+
================
|
|
7
|
+
- Breaking change: Raised the minimum ``urllib3`` version to 2.8.0. This
|
|
8
|
+
fixes CVE-2026-97687, CVE-2026-97688, and CVE-2026-97689. Support for
|
|
9
|
+
urllib3 1.x has been removed.
|
|
10
|
+
- Raised the minimum ``orjson`` version to 3.11.6, that fixes
|
|
11
|
+
CVE-2025-67221: ``orjson.dumps`` crashing on deeply nested input.
|
|
12
|
+
|
|
13
|
+
2026/09/17 2.3.0
|
|
14
|
+
================
|
|
15
|
+
- Added ``DefaultTypeConverter`` support that decodes ``DataType.UUID``
|
|
16
|
+
columns to Python ``uuid.UUID`` objects instead of returning the raw string.
|
|
17
|
+
- Added CrateDB column type identifiers to ``DataType``: ``INTERVAL`` (17),
|
|
18
|
+
``ROW`` (18), ``FLOAT_VECTOR`` (28), ``UUID`` (29), and ``REGTYPE`` (30).
|
|
19
|
+
Fixed ``Converter.get()`` raising ``ValueError`` for column type identifiers
|
|
20
|
+
it does not know. Unknown identifiers now fall back to the default converter.
|
|
21
|
+
|
|
22
|
+
- Breaking change: ``connect()`` now raises
|
|
23
|
+
``crate.client.exceptions.ConnectionError`` immediately if no configured
|
|
24
|
+
server node responds.
|
|
25
|
+
|
|
26
|
+
- Breaking change: ``DefaultTypeConverter`` now decodes ``DataType.BIT``
|
|
27
|
+
columns, converting CrateDB's ``B'0110'`` wire format to a plain string of
|
|
28
|
+
``0``/``1`` digits. Code that stripped the wrapper itself needs to be
|
|
29
|
+
adjusted, or can restore the previous behaviour by mapping
|
|
30
|
+
``DataType.BIT`` to a converter of its own.
|
|
31
|
+
|
|
32
|
+
2026/06/17 2.2.1
|
|
6
33
|
================
|
|
7
34
|
|
|
8
35
|
- Fixed ``cursor.execute()`` with ``bulk_parameters`` and pyformat SQL: when
|
|
9
36
|
rows are dicts, both the SQL template and the rows are now converted to
|
|
10
|
-
positional format before sending to CrateDB. Positional-list rows
|
|
37
|
+
positional format before sending to CrateDB. Positional-list rows
|
|
11
38
|
continue to work as before.
|
|
12
39
|
|
|
13
40
|
2026/06/04 2.2.0
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: crate
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.4.0
|
|
4
4
|
Summary: CrateDB Python Client
|
|
5
5
|
Author-email: "Crate.io" <office@crate.io>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -24,8 +24,8 @@ Classifier: Topic :: Internet :: WWW/HTTP
|
|
|
24
24
|
Classifier: Topic :: Scientific/Engineering :: Interface Engine/Protocol Translator
|
|
25
25
|
Classifier: Topic :: System :: Networking
|
|
26
26
|
Requires-Python: >=3.10
|
|
27
|
-
Requires-Dist: orjson
|
|
28
|
-
Requires-Dist: urllib3
|
|
27
|
+
Requires-Dist: orjson>=3.11.6
|
|
28
|
+
Requires-Dist: urllib3>=2.8.0
|
|
29
29
|
Requires-Dist: verlib2
|
|
30
30
|
Description-Content-Type: text/x-rst
|
|
31
31
|
|
|
@@ -29,12 +29,8 @@ respond, the request is automatically routed to the next server:
|
|
|
29
29
|
>>> connection = client.connect([invalid_host, crate_host])
|
|
30
30
|
>>> connection.close()
|
|
31
31
|
|
|
32
|
-
If no ``servers`` are
|
|
33
|
-
|
|
34
|
-
>>> connection = client.connect()
|
|
35
|
-
>>> connection.client._active_servers
|
|
36
|
-
['http://127.0.0.1:4200']
|
|
37
|
-
>>> connection.close()
|
|
32
|
+
If no ``servers`` are supplied to the ``connect`` method, the default address
|
|
33
|
+
``http://127.0.0.1:4200`` is used.
|
|
38
34
|
|
|
39
35
|
If the option ``error_trace`` is set to ``True``, the client will print a whole
|
|
40
36
|
traceback if a server error occurs:
|
|
@@ -77,7 +73,7 @@ connect:
|
|
|
77
73
|
|
|
78
74
|
The username for trusted users can also be provided in the URL:
|
|
79
75
|
|
|
80
|
-
>>> connection = client.connect(['
|
|
76
|
+
>>> connection = client.connect([crate_host.replace('://', '://trusted_me@')])
|
|
81
77
|
>>> connection.client.username
|
|
82
78
|
'trusted_me'
|
|
83
79
|
>>> connection.client.password
|
|
@@ -97,7 +93,7 @@ also need to provide ``password`` as argument for the ``connect()`` call:
|
|
|
97
93
|
|
|
98
94
|
The authentication credentials can also be provided in the URL:
|
|
99
95
|
|
|
100
|
-
>>> connection = client.connect(['
|
|
96
|
+
>>> connection = client.connect([crate_host.replace('://', '://me:my_secret_pw@')])
|
|
101
97
|
>>> connection.client.username
|
|
102
98
|
'me'
|
|
103
99
|
>>> connection.client.password
|
|
@@ -270,8 +270,10 @@ executemany() with named parameters
|
|
|
270
270
|
====================================
|
|
271
271
|
|
|
272
272
|
``executemany()`` also accepts a :class:`py:list` of :class:`py:dict` when
|
|
273
|
-
the SQL
|
|
274
|
-
template and all rows to positional format before sending to CrateDB:
|
|
273
|
+
the SQL statement contains ``%(name)s`` placeholders. The client converts both the SQL
|
|
274
|
+
template and all rows to positional format before sending them to CrateDB:
|
|
275
|
+
|
|
276
|
+
.. Hidden: set up mocked response
|
|
275
277
|
|
|
276
278
|
>>> connection.client.set_next_response({
|
|
277
279
|
... "results": [
|
|
@@ -341,7 +343,8 @@ Python data type conversion
|
|
|
341
343
|
|
|
342
344
|
The cursor object can optionally convert database types to native Python data
|
|
343
345
|
types. Currently, this is implemented for the CrateDB data types ``IP``,
|
|
344
|
-
``TIMESTAMP``, and ``
|
|
346
|
+
``TIMESTAMP``, ``TIMETZ``, ``BIT``, and ``UUID`` on behalf of the
|
|
347
|
+
``DefaultTypeConverter``.
|
|
345
348
|
|
|
346
349
|
>>> cursor = connection.cursor(converter=DefaultTypeConverter())
|
|
347
350
|
|
|
@@ -377,6 +380,52 @@ and decoded to a ``datetime.time`` object with the appropriate timezone:
|
|
|
377
380
|
[datetime.time(12, 30, 45, tzinfo=datetime.timezone.utc)]
|
|
378
381
|
|
|
379
382
|
|
|
383
|
+
CrateDB's ``BIT`` type is returned over HTTP in its SQL literal form,
|
|
384
|
+
``B'0110'``. It is decoded to a plain string of ``0``/``1`` digits.
|
|
385
|
+
|
|
386
|
+
>>> cursor = connection.cursor(converter=DefaultTypeConverter())
|
|
387
|
+
|
|
388
|
+
.. hide: set up the mocked response::
|
|
389
|
+
|
|
390
|
+
>>> connection.client.set_next_response({
|
|
391
|
+
... "col_types": [25],
|
|
392
|
+
... "rows":[ [ "B'0110'" ] ],
|
|
393
|
+
... "cols":[ "flags" ],
|
|
394
|
+
... "rowcount":1,
|
|
395
|
+
... "duration":1
|
|
396
|
+
... })
|
|
397
|
+
|
|
398
|
+
Executing the query and fetching the decoded result:
|
|
399
|
+
|
|
400
|
+
>>> cursor.execute("select b'0110'")
|
|
401
|
+
|
|
402
|
+
>>> cursor.fetchone()
|
|
403
|
+
['0110']
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
CrateDB's ``UUID`` type is returned over HTTP as a string. It is decoded to a
|
|
407
|
+
Python ``uuid.UUID`` object.
|
|
408
|
+
|
|
409
|
+
>>> cursor = connection.cursor(converter=DefaultTypeConverter())
|
|
410
|
+
|
|
411
|
+
.. hide: set up the mocked response::
|
|
412
|
+
|
|
413
|
+
>>> connection.client.set_next_response({
|
|
414
|
+
... "col_types": [29],
|
|
415
|
+
... "rows":[ [ "a5b3c1e0-1b7f-4f3e-9a2d-6c4e8f0a1b2c" ] ],
|
|
416
|
+
... "cols":[ "id" ],
|
|
417
|
+
... "rowcount":1,
|
|
418
|
+
... "duration":1
|
|
419
|
+
... })
|
|
420
|
+
|
|
421
|
+
Executing the query and fetching the decoded result:
|
|
422
|
+
|
|
423
|
+
>>> cursor.execute("select 'a5b3c1e0-1b7f-4f3e-9a2d-6c4e8f0a1b2c'::uuid")
|
|
424
|
+
|
|
425
|
+
>>> cursor.fetchone()
|
|
426
|
+
[UUID('a5b3c1e0-1b7f-4f3e-9a2d-6c4e8f0a1b2c')]
|
|
427
|
+
|
|
428
|
+
|
|
380
429
|
Custom data type conversion
|
|
381
430
|
===========================
|
|
382
431
|
|
|
@@ -220,7 +220,7 @@ When connecting to non-CrateDB servers, the HttpClient will raise a ConnectionEr
|
|
|
220
220
|
>>> http_client.server_infos(http_client._get_server())
|
|
221
221
|
Traceback (most recent call last):
|
|
222
222
|
...
|
|
223
|
-
crate.client.exceptions.ProgrammingError: Invalid server response of content-type 'text/html':
|
|
223
|
+
crate.client.exceptions.ProgrammingError: Invalid server response of content-type 'text/html...':
|
|
224
224
|
...
|
|
225
225
|
>>> http_client.close()
|
|
226
226
|
|
|
@@ -73,13 +73,32 @@ You can pass in as many node URLs as you like.
|
|
|
73
73
|
|
|
74
74
|
.. TIP::
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
When ``connect()`` is called, the client contacts each node to check
|
|
77
|
+
availability and determine the lowest server version. If no node responds,
|
|
78
|
+
a ``ConnectionError`` is raised immediately.
|
|
79
|
+
|
|
80
|
+
For every subsequent query, the client will attempt each node in sequence
|
|
77
81
|
until a successful connection is made. Nodes are moved to the end of the
|
|
78
82
|
list each time they are tried.
|
|
79
83
|
|
|
80
84
|
Over multiple query executions, this behaviour functions as client-side
|
|
81
85
|
*round-robin* load balancing. (This is analogous to `round-robin DNS`_.)
|
|
82
86
|
|
|
87
|
+
.. NOTE::
|
|
88
|
+
|
|
89
|
+
Wrap ``connect()`` in a ``try/except`` block to handle an unreachable
|
|
90
|
+
cluster gracefully:
|
|
91
|
+
|
|
92
|
+
.. code-block:: python
|
|
93
|
+
|
|
94
|
+
from crate import client
|
|
95
|
+
from crate.client.exceptions import ConnectionError
|
|
96
|
+
|
|
97
|
+
try:
|
|
98
|
+
connection = client.connect(["node-1:4200", "node-2:4200"])
|
|
99
|
+
except ConnectionError as e:
|
|
100
|
+
print(f"Could not reach CrateDB cluster: {e}")
|
|
101
|
+
|
|
83
102
|
.. _connection-options:
|
|
84
103
|
|
|
85
104
|
Connection options
|
|
@@ -73,8 +73,7 @@ The same parameter name may appear multiple times in the query:
|
|
|
73
73
|
... {"q": "Quasar"})
|
|
74
74
|
|
|
75
75
|
The client converts the ``%(name)s`` placeholders to ``$N`` positional
|
|
76
|
-
markers before sending the query to CrateDB
|
|
77
|
-
required.
|
|
76
|
+
markers before sending the query to CrateDB.
|
|
78
77
|
|
|
79
78
|
Bulk inserts
|
|
80
79
|
------------
|
|
@@ -85,7 +84,7 @@ Bulk inserts
|
|
|
85
84
|
Positional parameters
|
|
86
85
|
.....................
|
|
87
86
|
|
|
88
|
-
Pass a :class:`py:list` of
|
|
87
|
+
Pass a :class:`py:list` of sequences using ``?`` placeholders:
|
|
89
88
|
|
|
90
89
|
>>> cursor.executemany(
|
|
91
90
|
... "INSERT INTO locations (name, date, kind, position) VALUES (?, ?, ?, ?)",
|
|
@@ -115,9 +114,11 @@ rows to positional format before sending to CrateDB:
|
|
|
115
114
|
Using ``bulk_parameters`` directly
|
|
116
115
|
...................................
|
|
117
116
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
117
|
+
``execute()`` accepts a ``bulk_parameters`` keyword argument directly:
|
|
118
|
+
|
|
119
|
+
.. NOTE::
|
|
120
|
+
Please prefer ``executemany()`` for bulk inserts, it is the standard DB-API 2.0
|
|
121
|
+
interface. The ``bulk_parameters`` argument is a lower-level alternative.
|
|
121
122
|
|
|
122
123
|
>>> cursor.execute(
|
|
123
124
|
... "INSERT INTO locations (name, kind, position) VALUES (?, ?, ?)",
|
|
@@ -5,10 +5,10 @@ build-backend = "hatchling.build"
|
|
|
5
5
|
[tool.hatch.build.targets.sdist]
|
|
6
6
|
include = [
|
|
7
7
|
"/docs",
|
|
8
|
-
"/src/crate
|
|
8
|
+
"/src/crate",
|
|
9
9
|
"/tests",
|
|
10
|
-
"
|
|
11
|
-
"
|
|
10
|
+
"/*.rst",
|
|
11
|
+
"/*.txt",
|
|
12
12
|
]
|
|
13
13
|
exclude = [
|
|
14
14
|
"/docs/.crate-docs",
|
|
@@ -49,19 +49,19 @@ classifiers = [
|
|
|
49
49
|
"Topic :: System :: Networking",
|
|
50
50
|
]
|
|
51
51
|
dependencies = [
|
|
52
|
-
"orjson",
|
|
53
|
-
"urllib3",
|
|
52
|
+
"orjson>=3.11.6",
|
|
53
|
+
"urllib3>=2.8.0",
|
|
54
54
|
"verlib2",
|
|
55
55
|
]
|
|
56
56
|
|
|
57
57
|
[dependency-groups]
|
|
58
58
|
dev = [
|
|
59
|
-
"certifi",
|
|
59
|
+
"certifi>=2026.10.23",
|
|
60
60
|
"coverage",
|
|
61
|
-
"mypy<2.
|
|
61
|
+
"mypy<2.4",
|
|
62
62
|
"pytest<10",
|
|
63
63
|
"pytz",
|
|
64
|
-
"ruff<0.
|
|
64
|
+
"ruff<0.17",
|
|
65
65
|
]
|
|
66
66
|
docs = [
|
|
67
67
|
"sphinx",
|
|
@@ -174,7 +174,7 @@ lint.per-file-ignores."tests/*" = [
|
|
|
174
174
|
"S310", # False positive; it's a https url
|
|
175
175
|
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes
|
|
176
176
|
]
|
|
177
|
-
lint.per-file-ignores."src/crate/client/{connection.py,http.py}" = [
|
|
177
|
+
lint.per-file-ignores."src/crate/client/{connection.py,http.py,_pool.py,_response.py}" = [
|
|
178
178
|
"A004", # Import `ConnectionError` is shadowing a Python builtin
|
|
179
179
|
"A005", # Import `ConnectionError` is shadowing a Python builtin
|
|
180
180
|
]
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# -*- coding: utf-8; -*-
|
|
2
|
+
#
|
|
3
|
+
# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
|
|
4
|
+
# license agreements. See the NOTICE file distributed with this work for
|
|
5
|
+
# additional information regarding copyright ownership. Crate licenses
|
|
6
|
+
# this file to you under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License. You may
|
|
8
|
+
# obtain a copy of the License at
|
|
9
|
+
#
|
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
#
|
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
14
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
15
|
+
# License for the specific language governing permissions and limitations
|
|
16
|
+
# under the License.
|
|
17
|
+
#
|
|
18
|
+
# However, if you have executed another commercial license agreement
|
|
19
|
+
# with Crate these terms will supersede the license and you may use the
|
|
20
|
+
# software solely pursuant to the terms of the relevant commercial agreement.
|
|
21
|
+
|
|
22
|
+
from .connection import Connection as connect
|
|
23
|
+
from .exceptions import Error
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"connect",
|
|
27
|
+
"Error",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
# ruff: noqa: E402
|
|
31
|
+
try:
|
|
32
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
33
|
+
except (ImportError, ModuleNotFoundError): # pragma: no cover
|
|
34
|
+
from importlib_metadata import ( # type: ignore[assignment,no-redef,unused-ignore]
|
|
35
|
+
PackageNotFoundError,
|
|
36
|
+
version,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
__appname__ = "crate"
|
|
40
|
+
|
|
41
|
+
try:
|
|
42
|
+
__version__ = version(__appname__)
|
|
43
|
+
except PackageNotFoundError: # pragma: no cover
|
|
44
|
+
__version__ = "unknown"
|
|
45
|
+
|
|
46
|
+
# codeql[py/unused-global-variable]
|
|
47
|
+
apilevel = "2.0"
|
|
48
|
+
threadsafety = 1
|
|
49
|
+
paramstyle = "pyformat"
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# -*- coding: utf-8; -*-
|
|
2
|
+
#
|
|
3
|
+
# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
|
|
4
|
+
# license agreements. See the NOTICE file distributed with this work for
|
|
5
|
+
# additional information regarding copyright ownership. Crate licenses
|
|
6
|
+
# this file to you under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License. You may
|
|
8
|
+
# obtain a copy of the License at
|
|
9
|
+
#
|
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
#
|
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
14
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
15
|
+
# License for the specific language governing permissions and limitations
|
|
16
|
+
# under the License.
|
|
17
|
+
#
|
|
18
|
+
# However, if you have executed another commercial license agreement
|
|
19
|
+
# with Crate these terms will supersede the license and you may use the
|
|
20
|
+
# software solely pursuant to the terms of the relevant commercial agreement.
|
|
21
|
+
|
|
22
|
+
import calendar
|
|
23
|
+
import datetime as dt
|
|
24
|
+
import typing as t
|
|
25
|
+
from decimal import Decimal
|
|
26
|
+
|
|
27
|
+
import orjson
|
|
28
|
+
|
|
29
|
+
epoch_aware = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
|
|
30
|
+
epoch_naive = dt.datetime(1970, 1, 1)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def json_encoder(obj: t.Any) -> t.Union[int, str]:
|
|
34
|
+
"""
|
|
35
|
+
Encoder function for orjson, with additional type support.
|
|
36
|
+
|
|
37
|
+
- Python's `Decimal` type will be serialized to `str`.
|
|
38
|
+
- Python's `dt.datetime` and `dt.date` types will be
|
|
39
|
+
serialized to `int` after converting to milliseconds
|
|
40
|
+
since epoch.
|
|
41
|
+
- Python's `dt.time` will be serialized to `str`, following
|
|
42
|
+
the ISO format.
|
|
43
|
+
|
|
44
|
+
https://github.com/ijl/orjson#default
|
|
45
|
+
https://cratedb.com/docs/crate/reference/en/latest/general/ddl/data-types.html#type-timestamp
|
|
46
|
+
"""
|
|
47
|
+
if isinstance(obj, Decimal):
|
|
48
|
+
return str(obj)
|
|
49
|
+
if isinstance(obj, dt.datetime):
|
|
50
|
+
if obj.tzinfo is not None:
|
|
51
|
+
delta = obj - epoch_aware
|
|
52
|
+
else:
|
|
53
|
+
delta = obj - epoch_naive
|
|
54
|
+
return int(
|
|
55
|
+
delta.microseconds / 1000.0
|
|
56
|
+
+ (delta.seconds + delta.days * 24 * 3600) * 1000.0
|
|
57
|
+
)
|
|
58
|
+
if isinstance(obj, dt.time):
|
|
59
|
+
return obj.isoformat()
|
|
60
|
+
if isinstance(obj, dt.date):
|
|
61
|
+
return calendar.timegm(obj.timetuple()) * 1000
|
|
62
|
+
raise TypeError
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def json_dumps(obj: t.Any) -> bytes:
|
|
66
|
+
"""
|
|
67
|
+
Serialize to JSON format, using `orjson`, with additional type support.
|
|
68
|
+
|
|
69
|
+
https://github.com/ijl/orjson
|
|
70
|
+
"""
|
|
71
|
+
return orjson.dumps(
|
|
72
|
+
obj,
|
|
73
|
+
default=json_encoder,
|
|
74
|
+
option=(
|
|
75
|
+
orjson.OPT_PASSTHROUGH_DATETIME
|
|
76
|
+
| orjson.OPT_NON_STR_KEYS
|
|
77
|
+
| orjson.OPT_SERIALIZE_NUMPY
|
|
78
|
+
),
|
|
79
|
+
)
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# -*- coding: utf-8; -*-
|
|
2
|
+
#
|
|
3
|
+
# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
|
|
4
|
+
# license agreements. See the NOTICE file distributed with this work for
|
|
5
|
+
# additional information regarding copyright ownership. Crate licenses
|
|
6
|
+
# this file to you under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License. You may
|
|
8
|
+
# obtain a copy of the License at
|
|
9
|
+
#
|
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
#
|
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
14
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
15
|
+
# License for the specific language governing permissions and limitations
|
|
16
|
+
# under the License.
|
|
17
|
+
#
|
|
18
|
+
# However, if you have executed another commercial license agreement
|
|
19
|
+
# with Crate these terms will supersede the license and you may use the
|
|
20
|
+
# software solely pursuant to the terms of the relevant commercial agreement.
|
|
21
|
+
|
|
22
|
+
import re
|
|
23
|
+
import socket
|
|
24
|
+
import typing as t
|
|
25
|
+
from urllib.parse import urlparse
|
|
26
|
+
|
|
27
|
+
SQL_PATH = "/_sql?types=true"
|
|
28
|
+
|
|
29
|
+
_HTTP_PAT = re.compile("https?://.+", re.I)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def server_url(server: str) -> str:
|
|
33
|
+
"""
|
|
34
|
+
Normalizes a given server string to an url
|
|
35
|
+
|
|
36
|
+
>>> print(server_url('a'))
|
|
37
|
+
http://a
|
|
38
|
+
>>> print(server_url('a/path'))
|
|
39
|
+
http://a/path
|
|
40
|
+
>>> print(server_url('a:9345'))
|
|
41
|
+
http://a:9345
|
|
42
|
+
>>> print(server_url('a:9345/path'))
|
|
43
|
+
http://a:9345/path
|
|
44
|
+
>>> print(server_url('https://a:9345'))
|
|
45
|
+
https://a:9345
|
|
46
|
+
>>> print(server_url('https://a:9345/path'))
|
|
47
|
+
https://a:9345/path
|
|
48
|
+
>>> print(server_url('https://a'))
|
|
49
|
+
https://a
|
|
50
|
+
>>> print(server_url('https://a/path'))
|
|
51
|
+
https://a/path
|
|
52
|
+
>>> print(server_url('demo.crate.io'))
|
|
53
|
+
http://demo.crate.io
|
|
54
|
+
"""
|
|
55
|
+
if not _HTTP_PAT.match(server):
|
|
56
|
+
server = "http://%s" % server
|
|
57
|
+
parsed = urlparse(server)
|
|
58
|
+
return parsed.geturl()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def to_server_list(servers: t.Union[str, t.Iterable[str]]) -> t.List[str]:
|
|
62
|
+
if isinstance(servers, str):
|
|
63
|
+
servers = servers.split()
|
|
64
|
+
return [server_url(s) for s in servers]
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def credentials_from_url(
|
|
68
|
+
url: str, username: t.Optional[str], password: t.Optional[str]
|
|
69
|
+
) -> t.Tuple[t.Optional[str], t.Optional[str]]:
|
|
70
|
+
"""
|
|
71
|
+
Return the user name and password from the userinfo of `url`, falling
|
|
72
|
+
back to the given ones for the parts the URL does not contain.
|
|
73
|
+
"""
|
|
74
|
+
parsed = urlparse(url)
|
|
75
|
+
if parsed.username is not None:
|
|
76
|
+
username = parsed.username
|
|
77
|
+
if parsed.password is not None:
|
|
78
|
+
password = parsed.password
|
|
79
|
+
return username, password
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def validate_compress(compress: t.Any) -> t.Union[int, bool]:
|
|
83
|
+
if not isinstance(compress, (bool, int)):
|
|
84
|
+
raise TypeError(
|
|
85
|
+
f"compress must be bool or int, got {type(compress).__name__!r}"
|
|
86
|
+
)
|
|
87
|
+
return compress
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def sql_path(error_trace: bool, path: str = SQL_PATH) -> str:
|
|
91
|
+
if error_trace:
|
|
92
|
+
path += "&error_trace=true"
|
|
93
|
+
return path
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def keepalive_socket_options(
|
|
97
|
+
keepalive: bool = True,
|
|
98
|
+
tcp_keepidle: t.Optional[int] = None,
|
|
99
|
+
tcp_keepintvl: t.Optional[int] = None,
|
|
100
|
+
tcp_keepcnt: t.Optional[int] = None,
|
|
101
|
+
) -> t.List[t.Tuple[int, int, int]]:
|
|
102
|
+
"""
|
|
103
|
+
Return the `setsockopt` arguments for TCP keepalive, or an empty list
|
|
104
|
+
when `keepalive` is off.
|
|
105
|
+
"""
|
|
106
|
+
if not keepalive:
|
|
107
|
+
return []
|
|
108
|
+
|
|
109
|
+
# always use TCP keepalive
|
|
110
|
+
opts = [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)]
|
|
111
|
+
|
|
112
|
+
# hasattr check because some options depend on system capabilities
|
|
113
|
+
# see https://docs.python.org/3/library/socket.html#socket.SOMAXCONN
|
|
114
|
+
if hasattr(socket, "TCP_KEEPIDLE") and tcp_keepidle is not None:
|
|
115
|
+
opts.append((socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, tcp_keepidle))
|
|
116
|
+
if hasattr(socket, "TCP_KEEPINTVL") and tcp_keepintvl is not None:
|
|
117
|
+
opts.append((socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, tcp_keepintvl))
|
|
118
|
+
if hasattr(socket, "TCP_KEEPCNT") and tcp_keepcnt is not None:
|
|
119
|
+
opts.append((socket.IPPROTO_TCP, socket.TCP_KEEPCNT, tcp_keepcnt))
|
|
120
|
+
return opts
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from verlib2 import Version # noqa: F401
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# -*- coding: utf-8; -*-
|
|
2
|
+
#
|
|
3
|
+
# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
|
|
4
|
+
# license agreements. See the NOTICE file distributed with this work for
|
|
5
|
+
# additional information regarding copyright ownership. Crate licenses
|
|
6
|
+
# this file to you under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License. You may
|
|
8
|
+
# obtain a copy of the License at
|
|
9
|
+
#
|
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
#
|
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
14
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
15
|
+
# License for the specific language governing permissions and limitations
|
|
16
|
+
# under the License.
|
|
17
|
+
#
|
|
18
|
+
# However, if you have executed another commercial license agreement
|
|
19
|
+
# with Crate these terms will supersede the license and you may use the
|
|
20
|
+
# software solely pursuant to the terms of the relevant commercial agreement.
|
|
21
|
+
|
|
22
|
+
import heapq
|
|
23
|
+
import logging
|
|
24
|
+
import threading
|
|
25
|
+
import time
|
|
26
|
+
import typing as t
|
|
27
|
+
|
|
28
|
+
from crate.client.exceptions import ConnectionError
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class _ServerPool:
|
|
32
|
+
"""
|
|
33
|
+
Round-robin server selection. A dropped server is taken out of rotation
|
|
34
|
+
for `retry_interval` seconds.
|
|
35
|
+
|
|
36
|
+
Subclasses set `_logger` to log server changes on their own logger.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
retry_interval = 30
|
|
40
|
+
"""Retry interval for failed servers in seconds."""
|
|
41
|
+
|
|
42
|
+
_logger = logging.getLogger(__name__)
|
|
43
|
+
|
|
44
|
+
def __init__(
|
|
45
|
+
self,
|
|
46
|
+
servers: t.List[str],
|
|
47
|
+
clock: t.Callable[[], float] = time.time,
|
|
48
|
+
) -> None:
|
|
49
|
+
self._active_servers = servers
|
|
50
|
+
self._inactive_servers: t.List[t.Tuple[float, str, str]] = []
|
|
51
|
+
self._lock = threading.RLock()
|
|
52
|
+
self._clock = clock
|
|
53
|
+
|
|
54
|
+
def _get_server(self) -> str:
|
|
55
|
+
"""
|
|
56
|
+
Get server to use for request.
|
|
57
|
+
Also process inactive server list, re-add them after given interval.
|
|
58
|
+
"""
|
|
59
|
+
with self._lock:
|
|
60
|
+
inactive_server_count = len(self._inactive_servers)
|
|
61
|
+
for _ in range(inactive_server_count):
|
|
62
|
+
try:
|
|
63
|
+
ts, server, message = heapq.heappop(self._inactive_servers)
|
|
64
|
+
except IndexError:
|
|
65
|
+
pass
|
|
66
|
+
else:
|
|
67
|
+
if (ts + self.retry_interval) > self._clock():
|
|
68
|
+
# Not yet, put it back
|
|
69
|
+
heapq.heappush(
|
|
70
|
+
self._inactive_servers, (ts, server, message)
|
|
71
|
+
)
|
|
72
|
+
else:
|
|
73
|
+
self._active_servers.append(server)
|
|
74
|
+
self._logger.warning(
|
|
75
|
+
"Restored server %s into active pool", server
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
# if none is old enough, use oldest
|
|
79
|
+
if not self._active_servers:
|
|
80
|
+
ts, server, message = heapq.heappop(self._inactive_servers)
|
|
81
|
+
self._active_servers.append(server)
|
|
82
|
+
self._logger.info("Restored server %s into active pool", server)
|
|
83
|
+
|
|
84
|
+
server = self._active_servers[0]
|
|
85
|
+
self._roundrobin()
|
|
86
|
+
|
|
87
|
+
return server
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def active_servers(self) -> t.List[str]:
|
|
91
|
+
"""get the active servers for this client"""
|
|
92
|
+
with self._lock:
|
|
93
|
+
return list(self._active_servers)
|
|
94
|
+
|
|
95
|
+
def _drop_server(self, server: str, message: str) -> None:
|
|
96
|
+
"""
|
|
97
|
+
Drop server from active list and adds it to the inactive ones.
|
|
98
|
+
"""
|
|
99
|
+
try:
|
|
100
|
+
self._active_servers.remove(server)
|
|
101
|
+
except ValueError:
|
|
102
|
+
pass
|
|
103
|
+
else:
|
|
104
|
+
heapq.heappush(
|
|
105
|
+
self._inactive_servers, (self._clock(), server, message)
|
|
106
|
+
)
|
|
107
|
+
self._logger.warning("Removed server %s from active pool", server)
|
|
108
|
+
|
|
109
|
+
# if this is the last server raise exception, otherwise try next
|
|
110
|
+
if not self._active_servers:
|
|
111
|
+
raise ConnectionError(
|
|
112
|
+
("No more Servers available, exception from last server: %s")
|
|
113
|
+
% message
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
def _roundrobin(self) -> None:
|
|
117
|
+
"""
|
|
118
|
+
Very simple round-robin implementation
|
|
119
|
+
"""
|
|
120
|
+
self._active_servers.append(self._active_servers.pop(0))
|