duckhaven-sql-connector 0.1.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.
- duckhaven_sql_connector-0.1.0/.gitignore +20 -0
- duckhaven_sql_connector-0.1.0/CHANGELOG.md +32 -0
- duckhaven_sql_connector-0.1.0/LICENSE +201 -0
- duckhaven_sql_connector-0.1.0/PKG-INFO +131 -0
- duckhaven_sql_connector-0.1.0/README.md +101 -0
- duckhaven_sql_connector-0.1.0/pyproject.toml +55 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/__init__.py +97 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/_arrow.py +31 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/_metadata.py +81 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/_params.py +142 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/_telemetry.py +71 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/_version.py +24 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/client.py +175 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/config.py +79 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/connection.py +174 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/cursor.py +268 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/dbapi.py +151 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/errors.py +85 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/py.typed +0 -0
- duckhaven_sql_connector-0.1.0/src/duckhaven_sql_connector/result.py +89 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
.eggs/
|
|
6
|
+
build/
|
|
7
|
+
dist/
|
|
8
|
+
# uv / venv
|
|
9
|
+
.venv/
|
|
10
|
+
# hatch-vcs generated version files
|
|
11
|
+
**/_version.py
|
|
12
|
+
# tooling caches
|
|
13
|
+
.pytest_cache/
|
|
14
|
+
.ruff_cache/
|
|
15
|
+
.coverage
|
|
16
|
+
coverage.xml
|
|
17
|
+
htmlcov/
|
|
18
|
+
|
|
19
|
+
# dbt writes run logs into the working tree during tests
|
|
20
|
+
logs/
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `duckhaven-sql-connector` are documented here. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versions come from git tags
|
|
5
|
+
(`sql-connector-vX.Y.Z`).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.1.0] - 2026-07-17
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- Initial DB-API 2.0 (PEP 249) client for DuckHaven's SQL session API: `connect`,
|
|
14
|
+
`Connection` (one SQL session), `Cursor` (submit/poll/fetch), the exception hierarchy,
|
|
15
|
+
module globals (`paramstyle = "qmark"`), and type objects.
|
|
16
|
+
- Pooled `httpx` transport with PAT bearer auth, idempotent-only retry/backoff, and
|
|
17
|
+
HTTP-status/error-body → DB-API exception mapping.
|
|
18
|
+
- Safe client-side `qmark` parameter binding and cursor-paginated JSON results, behind a
|
|
19
|
+
result-transport seam ready for a future server-side Arrow/EXTERNAL_LINKS disposition.
|
|
20
|
+
- Optional extras: `arrow` (`Cursor.fetch_arrow_table`) and `otel` (client spans + W3C
|
|
21
|
+
`traceparent` propagation), plus dependency-free instrumentation `Hooks`.
|
|
22
|
+
- Cursor metadata methods (`catalogs`/`schemas`/`tables`/`columns`) over
|
|
23
|
+
`information_schema`, for dbt/BI relation introspection.
|
|
24
|
+
- Connection-scoped `Connection.cancel()` that cancels the session's in-flight statement
|
|
25
|
+
(for drivers like dbt that abort a run from another thread); the statement's id is now
|
|
26
|
+
recorded before polling, so a cancel arriving mid-run reaches the running statement.
|
|
27
|
+
- Retry hardening: honors a server `Retry-After` header and bounds retries by a total-time
|
|
28
|
+
budget (`RetryPolicy.max_elapsed`, raising `MaxRetryDurationError`).
|
|
29
|
+
- A pinned OpenAPI contract subset with an anti-drift test, env-gated live integration
|
|
30
|
+
tests, and a quickstart example.
|
|
31
|
+
- Workspace and package scaffolding: uv workspace, Apache-2.0 license, Ruff/pre-commit,
|
|
32
|
+
CI matrix (Python 3.10–3.14), and the tag-prefixed release workflow.
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 DuckHaven
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: duckhaven-sql-connector
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: DB-API 2.0 client for DuckHaven's SQL session/statement API
|
|
5
|
+
Project-URL: Homepage, https://github.com/tamasmrtn/duckhaven-clients
|
|
6
|
+
Project-URL: Source, https://github.com/tamasmrtn/duckhaven-clients
|
|
7
|
+
Author: DuckHaven
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: connector,dbapi,duckdb,duckhaven,sql
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Database
|
|
20
|
+
Classifier: Topic :: Database :: Front-Ends
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Requires-Dist: httpx>=0.28
|
|
24
|
+
Provides-Extra: arrow
|
|
25
|
+
Requires-Dist: pyarrow>=15; extra == 'arrow'
|
|
26
|
+
Provides-Extra: otel
|
|
27
|
+
Requires-Dist: opentelemetry-api>=1.36; extra == 'otel'
|
|
28
|
+
Requires-Dist: opentelemetry-instrumentation-httpx>=0.48b0; extra == 'otel'
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# duckhaven-sql-connector
|
|
32
|
+
|
|
33
|
+
A [PEP 249 (DB-API 2.0)](https://peps.python.org/pep-0249/) Python client for
|
|
34
|
+
[DuckHaven](https://github.com/tamasmrtn/duckhaven-clients)'s SQL **session** API.
|
|
35
|
+
|
|
36
|
+
It is a **pure HTTP client of DuckHaven's public REST API**: it authenticates with a
|
|
37
|
+
DuckHaven Personal Access Token (`dh_pat_…`), opens a SQL session bound to one compute
|
|
38
|
+
agent, runs statements against that session's persistent DuckDB connection, and fetches
|
|
39
|
+
results. It never talks to a compute node directly and depends on no DuckHaven server
|
|
40
|
+
internals.
|
|
41
|
+
|
|
42
|
+
This connector is the shared transport that `dbt-duckhaven`, the dlt `duckhaven`
|
|
43
|
+
destination, a future CLI, and Airflow operators build on.
|
|
44
|
+
|
|
45
|
+
## Install
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
pip install duckhaven-sql-connector
|
|
49
|
+
# optional extras:
|
|
50
|
+
pip install "duckhaven-sql-connector[arrow]" # client-side Arrow tables
|
|
51
|
+
pip install "duckhaven-sql-connector[otel]" # OpenTelemetry trace propagation
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Usage
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from duckhaven_sql_connector import connect
|
|
58
|
+
|
|
59
|
+
with connect(
|
|
60
|
+
host="https://duckhaven.internal",
|
|
61
|
+
workspace="analytics",
|
|
62
|
+
token="dh_pat_…",
|
|
63
|
+
catalog="sales", # optional default catalog
|
|
64
|
+
# agent="…-uuid-…", # optional explicit compute (an agent UUID); omit to auto-pick
|
|
65
|
+
) as conn:
|
|
66
|
+
with conn.cursor() as cur:
|
|
67
|
+
cur.execute("SELECT ? AS n", [1]) # qmark params, rendered safely client-side
|
|
68
|
+
print(cur.description, cur.fetchall())
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
A runnable version is in [`examples/quickstart.py`](examples/quickstart.py).
|
|
72
|
+
|
|
73
|
+
> **Note:** The DuckHaven SQL session surface is disabled unless the operator sets
|
|
74
|
+
> `SQL_SESSIONS_ENABLED=true` on the server. Against a server with it off, opening a
|
|
75
|
+
> session raises an `OperationalError`.
|
|
76
|
+
|
|
77
|
+
## Errors
|
|
78
|
+
|
|
79
|
+
Failures raise the standard [PEP 249 exceptions](https://peps.python.org/pep-0249/#exceptions),
|
|
80
|
+
carrying the server's `code`/`status_code`/`detail`:
|
|
81
|
+
|
|
82
|
+
- `ProgrammingError` — a rejected statement (`statement_not_allowed`), a denied grant, or a
|
|
83
|
+
missing object.
|
|
84
|
+
- `OperationalError` — an unavailable/disconnected agent, a reaped or closed session
|
|
85
|
+
(reconnect), a timeout, or the session surface being disabled. `MaxRetryDurationError`
|
|
86
|
+
(a subtype) is raised when retries exhaust the configured time budget.
|
|
87
|
+
- `InterfaceError` — bad connection configuration or a malformed response.
|
|
88
|
+
|
|
89
|
+
Idempotent requests (poll/fetch/cancel) are retried on transient failures with capped
|
|
90
|
+
exponential backoff; a server `Retry-After` header is honored, and retries are bounded by
|
|
91
|
+
both a max-attempt count and a total-time budget (`RetryPolicy.max_elapsed`). Statement
|
|
92
|
+
submits are never auto-retried.
|
|
93
|
+
|
|
94
|
+
## Metadata
|
|
95
|
+
|
|
96
|
+
For relation introspection (as dbt and BI tools need), the cursor exposes metadata methods
|
|
97
|
+
that query the server's `information_schema`; fetch the rows as usual:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
cur.tables(schema_name="public")
|
|
101
|
+
for catalog, schema, name, table_type in cur.fetchall():
|
|
102
|
+
...
|
|
103
|
+
# also: cur.catalogs(), cur.schemas(catalog=…), cur.columns(table_name=…)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Arrow results
|
|
107
|
+
|
|
108
|
+
With the `arrow` extra, fetch results as a `pyarrow.Table`:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
cur.execute("SELECT * FROM sales.orders")
|
|
112
|
+
table = cur.fetch_arrow_table()
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Observability
|
|
116
|
+
|
|
117
|
+
- **`otel` extra** — each request emits a client span and injects a W3C `traceparent`, so
|
|
118
|
+
client spans join the DuckHaven server trace. It is a no-op when the extra isn't installed.
|
|
119
|
+
- **Hooks** — pass `connect(..., hooks=Hooks(...))` to observe request timings, retries, and
|
|
120
|
+
rows fetched without any OpenTelemetry dependency (a client library runs no metrics server).
|
|
121
|
+
|
|
122
|
+
## Compatibility
|
|
123
|
+
|
|
124
|
+
The exact server endpoints and fields this client depends on are pinned in
|
|
125
|
+
[`contract/duckhaven-openapi.subset.json`](contract/duckhaven-openapi.subset.json) and checked
|
|
126
|
+
by the contract test. Regenerate it against a running server with
|
|
127
|
+
`make refresh-contract HOST=https://duckhaven.internal` to detect API drift early.
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
Apache-2.0.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# duckhaven-sql-connector
|
|
2
|
+
|
|
3
|
+
A [PEP 249 (DB-API 2.0)](https://peps.python.org/pep-0249/) Python client for
|
|
4
|
+
[DuckHaven](https://github.com/tamasmrtn/duckhaven-clients)'s SQL **session** API.
|
|
5
|
+
|
|
6
|
+
It is a **pure HTTP client of DuckHaven's public REST API**: it authenticates with a
|
|
7
|
+
DuckHaven Personal Access Token (`dh_pat_…`), opens a SQL session bound to one compute
|
|
8
|
+
agent, runs statements against that session's persistent DuckDB connection, and fetches
|
|
9
|
+
results. It never talks to a compute node directly and depends on no DuckHaven server
|
|
10
|
+
internals.
|
|
11
|
+
|
|
12
|
+
This connector is the shared transport that `dbt-duckhaven`, the dlt `duckhaven`
|
|
13
|
+
destination, a future CLI, and Airflow operators build on.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
pip install duckhaven-sql-connector
|
|
19
|
+
# optional extras:
|
|
20
|
+
pip install "duckhaven-sql-connector[arrow]" # client-side Arrow tables
|
|
21
|
+
pip install "duckhaven-sql-connector[otel]" # OpenTelemetry trace propagation
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from duckhaven_sql_connector import connect
|
|
28
|
+
|
|
29
|
+
with connect(
|
|
30
|
+
host="https://duckhaven.internal",
|
|
31
|
+
workspace="analytics",
|
|
32
|
+
token="dh_pat_…",
|
|
33
|
+
catalog="sales", # optional default catalog
|
|
34
|
+
# agent="…-uuid-…", # optional explicit compute (an agent UUID); omit to auto-pick
|
|
35
|
+
) as conn:
|
|
36
|
+
with conn.cursor() as cur:
|
|
37
|
+
cur.execute("SELECT ? AS n", [1]) # qmark params, rendered safely client-side
|
|
38
|
+
print(cur.description, cur.fetchall())
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
A runnable version is in [`examples/quickstart.py`](examples/quickstart.py).
|
|
42
|
+
|
|
43
|
+
> **Note:** The DuckHaven SQL session surface is disabled unless the operator sets
|
|
44
|
+
> `SQL_SESSIONS_ENABLED=true` on the server. Against a server with it off, opening a
|
|
45
|
+
> session raises an `OperationalError`.
|
|
46
|
+
|
|
47
|
+
## Errors
|
|
48
|
+
|
|
49
|
+
Failures raise the standard [PEP 249 exceptions](https://peps.python.org/pep-0249/#exceptions),
|
|
50
|
+
carrying the server's `code`/`status_code`/`detail`:
|
|
51
|
+
|
|
52
|
+
- `ProgrammingError` — a rejected statement (`statement_not_allowed`), a denied grant, or a
|
|
53
|
+
missing object.
|
|
54
|
+
- `OperationalError` — an unavailable/disconnected agent, a reaped or closed session
|
|
55
|
+
(reconnect), a timeout, or the session surface being disabled. `MaxRetryDurationError`
|
|
56
|
+
(a subtype) is raised when retries exhaust the configured time budget.
|
|
57
|
+
- `InterfaceError` — bad connection configuration or a malformed response.
|
|
58
|
+
|
|
59
|
+
Idempotent requests (poll/fetch/cancel) are retried on transient failures with capped
|
|
60
|
+
exponential backoff; a server `Retry-After` header is honored, and retries are bounded by
|
|
61
|
+
both a max-attempt count and a total-time budget (`RetryPolicy.max_elapsed`). Statement
|
|
62
|
+
submits are never auto-retried.
|
|
63
|
+
|
|
64
|
+
## Metadata
|
|
65
|
+
|
|
66
|
+
For relation introspection (as dbt and BI tools need), the cursor exposes metadata methods
|
|
67
|
+
that query the server's `information_schema`; fetch the rows as usual:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
cur.tables(schema_name="public")
|
|
71
|
+
for catalog, schema, name, table_type in cur.fetchall():
|
|
72
|
+
...
|
|
73
|
+
# also: cur.catalogs(), cur.schemas(catalog=…), cur.columns(table_name=…)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Arrow results
|
|
77
|
+
|
|
78
|
+
With the `arrow` extra, fetch results as a `pyarrow.Table`:
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
cur.execute("SELECT * FROM sales.orders")
|
|
82
|
+
table = cur.fetch_arrow_table()
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Observability
|
|
86
|
+
|
|
87
|
+
- **`otel` extra** — each request emits a client span and injects a W3C `traceparent`, so
|
|
88
|
+
client spans join the DuckHaven server trace. It is a no-op when the extra isn't installed.
|
|
89
|
+
- **Hooks** — pass `connect(..., hooks=Hooks(...))` to observe request timings, retries, and
|
|
90
|
+
rows fetched without any OpenTelemetry dependency (a client library runs no metrics server).
|
|
91
|
+
|
|
92
|
+
## Compatibility
|
|
93
|
+
|
|
94
|
+
The exact server endpoints and fields this client depends on are pinned in
|
|
95
|
+
[`contract/duckhaven-openapi.subset.json`](contract/duckhaven-openapi.subset.json) and checked
|
|
96
|
+
by the contract test. Regenerate it against a running server with
|
|
97
|
+
`make refresh-contract HOST=https://duckhaven.internal` to detect API drift early.
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
Apache-2.0.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "duckhaven-sql-connector"
|
|
3
|
+
description = "DB-API 2.0 client for DuckHaven's SQL session/statement API"
|
|
4
|
+
readme = "README.md"
|
|
5
|
+
requires-python = ">=3.10"
|
|
6
|
+
license = "Apache-2.0"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [{ name = "DuckHaven" }]
|
|
9
|
+
keywords = ["duckhaven", "duckdb", "dbapi", "sql", "connector"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Development Status :: 3 - Alpha",
|
|
12
|
+
"Intended Audience :: Developers",
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Programming Language :: Python :: 3.10",
|
|
15
|
+
"Programming Language :: Python :: 3.11",
|
|
16
|
+
"Programming Language :: Python :: 3.12",
|
|
17
|
+
"Programming Language :: Python :: 3.13",
|
|
18
|
+
"Programming Language :: Python :: 3.14",
|
|
19
|
+
"Topic :: Database",
|
|
20
|
+
"Topic :: Database :: Front-Ends",
|
|
21
|
+
"Typing :: Typed",
|
|
22
|
+
]
|
|
23
|
+
dependencies = ["httpx>=0.28"]
|
|
24
|
+
dynamic = ["version"]
|
|
25
|
+
|
|
26
|
+
[project.optional-dependencies]
|
|
27
|
+
arrow = ["pyarrow>=15"]
|
|
28
|
+
otel = [
|
|
29
|
+
"opentelemetry-api>=1.36",
|
|
30
|
+
"opentelemetry-instrumentation-httpx>=0.48b0",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Homepage = "https://github.com/tamasmrtn/duckhaven-clients"
|
|
35
|
+
Source = "https://github.com/tamasmrtn/duckhaven-clients"
|
|
36
|
+
|
|
37
|
+
[build-system]
|
|
38
|
+
requires = ["hatchling", "hatch-vcs"]
|
|
39
|
+
build-backend = "hatchling.build"
|
|
40
|
+
|
|
41
|
+
# Version comes from git tags with a per-package prefix (sql-connector-vX.Y.Z), so
|
|
42
|
+
# every member versions independently from the same workspace repo. fallback-version
|
|
43
|
+
# keeps source checkouts (no tag) buildable.
|
|
44
|
+
[tool.hatch.version]
|
|
45
|
+
source = "vcs"
|
|
46
|
+
raw-options = { root = "../..", tag_regex = "^sql-connector-v(?P<version>.+)$", fallback_version = "0.0.0" }
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.hooks.vcs]
|
|
49
|
+
version-file = "src/duckhaven_sql_connector/_version.py"
|
|
50
|
+
|
|
51
|
+
[tool.hatch.build.targets.wheel]
|
|
52
|
+
packages = ["src/duckhaven_sql_connector"]
|
|
53
|
+
|
|
54
|
+
[tool.hatch.build.targets.sdist]
|
|
55
|
+
include = ["src", "README.md", "CHANGELOG.md"]
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""DuckHaven SQL connector — a DB-API 2.0 (PEP 249) client for DuckHaven's session API.
|
|
2
|
+
|
|
3
|
+
The package root *is* the DB-API module: it exposes ``connect`` plus the mandated
|
|
4
|
+
globals, exception hierarchy, and type objects.
|
|
5
|
+
|
|
6
|
+
from duckhaven_sql_connector import connect
|
|
7
|
+
|
|
8
|
+
with connect(host=..., workspace=..., token="dh_pat_...") as conn:
|
|
9
|
+
cur = conn.cursor()
|
|
10
|
+
cur.execute("SELECT 1")
|
|
11
|
+
print(cur.fetchall())
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
# Resolved first so submodules importing ``__version__`` during the import chain below
|
|
15
|
+
# see it already set (client.py builds its User-Agent from it).
|
|
16
|
+
try: # populated by hatch-vcs at build time
|
|
17
|
+
from ._version import __version__
|
|
18
|
+
except ImportError: # pragma: no cover - source checkout without a build
|
|
19
|
+
try:
|
|
20
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
21
|
+
|
|
22
|
+
__version__ = version("duckhaven-sql-connector")
|
|
23
|
+
except PackageNotFoundError: # pragma: no cover
|
|
24
|
+
__version__ = "0.0.0+unknown"
|
|
25
|
+
|
|
26
|
+
from ._telemetry import Hooks
|
|
27
|
+
from .config import ClientConfig, RetryPolicy
|
|
28
|
+
from .connection import Connection, connect
|
|
29
|
+
from .cursor import Cursor
|
|
30
|
+
from .dbapi import (
|
|
31
|
+
BINARY,
|
|
32
|
+
DATETIME,
|
|
33
|
+
NUMBER,
|
|
34
|
+
ROWID,
|
|
35
|
+
STRING,
|
|
36
|
+
Binary,
|
|
37
|
+
DatabaseError,
|
|
38
|
+
DataError,
|
|
39
|
+
Date,
|
|
40
|
+
DateFromTicks,
|
|
41
|
+
Error,
|
|
42
|
+
IntegrityError,
|
|
43
|
+
InterfaceError,
|
|
44
|
+
InternalError,
|
|
45
|
+
MaxRetryDurationError,
|
|
46
|
+
NotSupportedError,
|
|
47
|
+
OperationalError,
|
|
48
|
+
ProgrammingError,
|
|
49
|
+
Time,
|
|
50
|
+
TimeFromTicks,
|
|
51
|
+
Timestamp,
|
|
52
|
+
TimestampFromTicks,
|
|
53
|
+
Warning,
|
|
54
|
+
apilevel,
|
|
55
|
+
paramstyle,
|
|
56
|
+
threadsafety,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
__all__ = [
|
|
60
|
+
"__version__",
|
|
61
|
+
# entry point + objects
|
|
62
|
+
"connect",
|
|
63
|
+
"Connection",
|
|
64
|
+
"Cursor",
|
|
65
|
+
"ClientConfig",
|
|
66
|
+
"RetryPolicy",
|
|
67
|
+
"Hooks",
|
|
68
|
+
# globals
|
|
69
|
+
"apilevel",
|
|
70
|
+
"threadsafety",
|
|
71
|
+
"paramstyle",
|
|
72
|
+
# exceptions
|
|
73
|
+
"Warning",
|
|
74
|
+
"Error",
|
|
75
|
+
"InterfaceError",
|
|
76
|
+
"DatabaseError",
|
|
77
|
+
"DataError",
|
|
78
|
+
"OperationalError",
|
|
79
|
+
"MaxRetryDurationError",
|
|
80
|
+
"IntegrityError",
|
|
81
|
+
"InternalError",
|
|
82
|
+
"ProgrammingError",
|
|
83
|
+
"NotSupportedError",
|
|
84
|
+
# type objects + constructors
|
|
85
|
+
"STRING",
|
|
86
|
+
"BINARY",
|
|
87
|
+
"NUMBER",
|
|
88
|
+
"DATETIME",
|
|
89
|
+
"ROWID",
|
|
90
|
+
"Date",
|
|
91
|
+
"Time",
|
|
92
|
+
"Timestamp",
|
|
93
|
+
"DateFromTicks",
|
|
94
|
+
"TimeFromTicks",
|
|
95
|
+
"TimestampFromTicks",
|
|
96
|
+
"Binary",
|
|
97
|
+
]
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""Client-side JSON→Arrow conversion (the ``arrow`` optional extra).
|
|
2
|
+
|
|
3
|
+
v1 has no server-side Arrow result path, so ``Cursor.fetch_arrow_table`` builds a
|
|
4
|
+
``pyarrow.Table`` from the JSON rows the pager already returns. ``pyarrow`` is an optional
|
|
5
|
+
dependency; calling the Arrow path without it raises ``NotSupportedError``.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import importlib
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from .dbapi import NotSupportedError
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _load_pyarrow() -> Any:
|
|
17
|
+
try:
|
|
18
|
+
return importlib.import_module("pyarrow")
|
|
19
|
+
except ImportError as exc: # pragma: no cover - pyarrow is present in the test env
|
|
20
|
+
raise NotSupportedError(
|
|
21
|
+
"pyarrow is required for Arrow results; install duckhaven-sql-connector[arrow]"
|
|
22
|
+
) from exc
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def to_arrow_table(columns: list[str], rows: list[tuple[Any, ...]]) -> Any:
|
|
26
|
+
"""Build a ``pyarrow.Table`` from column names and row tuples (column order preserved)."""
|
|
27
|
+
pa = _load_pyarrow()
|
|
28
|
+
if not columns:
|
|
29
|
+
return pa.table({})
|
|
30
|
+
data = {col: [row[i] for row in rows] for i, col in enumerate(columns)}
|
|
31
|
+
return pa.table(data)
|