sqlload-api 0.85.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.
- sqlload_api-0.85.0/.gitignore +34 -0
- sqlload_api-0.85.0/LICENSE +201 -0
- sqlload_api-0.85.0/PKG-INFO +34 -0
- sqlload_api-0.85.0/README.md +18 -0
- sqlload_api-0.85.0/pyproject.toml +30 -0
- sqlload_api-0.85.0/python/sqlload_api/__init__.py +68 -0
- sqlload_api-0.85.0/python/sqlload_api/_data.py +213 -0
- sqlload_api-0.85.0/python/sqlload_api/_http.py +428 -0
- sqlload_api-0.85.0/python/sqlload_api/_http_paths.py +218 -0
- sqlload_api-0.85.0/python/sqlload_api/_registry.py +103 -0
- sqlload_api-0.85.0/python/sqlload_api/_transform.py +113 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/__init__.py +149 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/checkpoint.json +6 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/fan_out_batch.json +53 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/fan_out_nested.json +85 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/fan_out_values.json +28 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/json_document.json +17 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/ndjson_lines.json +17 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/paginate_cells.json +34 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/paginate_cursor.json +23 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/paginate_header_row.json +29 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/paginate_link_header.json +24 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/paginate_next_link.json +21 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/paginate_offset.json +27 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/paginate_page_number_post.json +30 -0
- sqlload_api-0.85.0/python/sqlload_api/_wire/paginate_projected.json +53 -0
- sqlload_api-0.85.0/python/sqlload_api/py.typed +0 -0
- sqlload_api-0.85.0/tests/test_context_clients.py +51 -0
- sqlload_api-0.85.0/tests/test_job_wire.py +93 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Rust
|
|
2
|
+
target/
|
|
3
|
+
**/*.rs.bk
|
|
4
|
+
|
|
5
|
+
# Tool caches
|
|
6
|
+
.fensu/cache/
|
|
7
|
+
|
|
8
|
+
# Local configuration and credentials
|
|
9
|
+
.env
|
|
10
|
+
.env.*
|
|
11
|
+
.envrc
|
|
12
|
+
*.local.toml
|
|
13
|
+
*.pem
|
|
14
|
+
*.p8
|
|
15
|
+
|
|
16
|
+
# Editors
|
|
17
|
+
.idea/
|
|
18
|
+
.vscode/
|
|
19
|
+
|
|
20
|
+
# Python packaging
|
|
21
|
+
dist/
|
|
22
|
+
# unixODBC's source, fetched into each package for its release sdist
|
|
23
|
+
packages/*/unixODBC-*.tar.gz
|
|
24
|
+
.venv/
|
|
25
|
+
__pycache__/
|
|
26
|
+
|
|
27
|
+
# Native extension built in place by maturin develop
|
|
28
|
+
packages/sqlload/python/sqlload/*.so
|
|
29
|
+
packages/sqlload/python/sqlload/*.pyd
|
|
30
|
+
benchmarks/results/
|
|
31
|
+
benchmarks/.wheels/
|
|
32
|
+
|
|
33
|
+
# Agent working files
|
|
34
|
+
.amp/
|
|
@@ -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
|
|
95
|
+
Derivative 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 [yyyy] [name of copyright owner]
|
|
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,34 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: sqlload-api
|
|
3
|
+
Version: 0.85.0
|
|
4
|
+
Summary: SQLLoad's Python API for describing data and HTTP requests, without the engine.
|
|
5
|
+
Project-URL: Homepage, https://github.com/sqlstack-io/sqlload
|
|
6
|
+
Project-URL: Issues, https://github.com/sqlstack-io/sqlload/issues
|
|
7
|
+
Project-URL: Repository, https://github.com/sqlstack-io/sqlload
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Database
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# sqlload-api
|
|
18
|
+
|
|
19
|
+
SQLLoad's Python API for describing data and HTTP requests, in pure Python with no compiled code: `http(...)` jobs and
|
|
20
|
+
their paginators, `json(...)` / `ndjson(...)`, `field`, `parent`, `key`, `checkpoint` and `transform`.
|
|
21
|
+
|
|
22
|
+
You rarely install it yourself: [`sqlload`](https://pypi.org/project/sqlload/) depends on it and re-exports it
|
|
23
|
+
(`import sqlload as sl; sl.http(...)`). Tools that run SQLLoad's engine in their own process use it to build the same jobs
|
|
24
|
+
without loading a second engine:
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
import sqlload_api as sl
|
|
28
|
+
|
|
29
|
+
api = sl.http(connection="shop_api")
|
|
30
|
+
job = api.paginate("/orders", paginator=sl.next_link("$.next"), select="$.items")
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
A job built this way is identical to one built from a SQLLoad source with the same connection. Released together with
|
|
34
|
+
`sqlload`, at the same version; an engine reads the API of its own version.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# sqlload-api
|
|
2
|
+
|
|
3
|
+
SQLLoad's Python API for describing data and HTTP requests, in pure Python with no compiled code: `http(...)` jobs and
|
|
4
|
+
their paginators, `json(...)` / `ndjson(...)`, `field`, `parent`, `key`, `checkpoint` and `transform`.
|
|
5
|
+
|
|
6
|
+
You rarely install it yourself: [`sqlload`](https://pypi.org/project/sqlload/) depends on it and re-exports it
|
|
7
|
+
(`import sqlload as sl; sl.http(...)`). Tools that run SQLLoad's engine in their own process use it to build the same jobs
|
|
8
|
+
without loading a second engine:
|
|
9
|
+
|
|
10
|
+
```python
|
|
11
|
+
import sqlload_api as sl
|
|
12
|
+
|
|
13
|
+
api = sl.http(connection="shop_api")
|
|
14
|
+
job = api.paginate("/orders", paginator=sl.next_link("$.next"), select="$.items")
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
A job built this way is identical to one built from a SQLLoad source with the same connection. Released together with
|
|
18
|
+
`sqlload`, at the same version; an engine reads the API of its own version.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.26"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sqlload-api"
|
|
7
|
+
version = "0.85.0" # x-release-please-version
|
|
8
|
+
description = "SQLLoad's Python API for describing data and HTTP requests, without the engine."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Intended Audience :: Developers",
|
|
15
|
+
"License :: OSI Approved :: Apache Software License",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Topic :: Database",
|
|
18
|
+
]
|
|
19
|
+
dependencies = []
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/sqlstack-io/sqlload"
|
|
23
|
+
Issues = "https://github.com/sqlstack-io/sqlload/issues"
|
|
24
|
+
Repository = "https://github.com/sqlstack-io/sqlload"
|
|
25
|
+
|
|
26
|
+
[tool.hatch.build.targets.wheel]
|
|
27
|
+
packages = ["python/sqlload_api"]
|
|
28
|
+
|
|
29
|
+
[tool.hatch.build.targets.sdist]
|
|
30
|
+
include = ["python/sqlload_api", "tests", "README.md", "LICENSE"]
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""SQLLoad's Python API for describing data and requests, without the engine.
|
|
2
|
+
|
|
3
|
+
A table function (or another tool's loader) yields what these build, and the engine that reads them runs it:
|
|
4
|
+
|
|
5
|
+
- `http(...)` jobs (`paginate`, `fan_out`) that make every request and parse every page in Rust;
|
|
6
|
+
- `json(...)` and `ndjson(...)` bytes, parsed without the GIL;
|
|
7
|
+
- `checkpoint(...)` positions between a logged table's rows.
|
|
8
|
+
|
|
9
|
+
Each crosses as plain data through a method the engine looks for (`__sqlload_http_job__`, `__sqlload_json__`,
|
|
10
|
+
`__sqlload_checkpoint__`), so this package holds no compiled code. `sqlload` re-exports it: `import sqlload as sl;
|
|
11
|
+
sl.http` is `sqlload_api.http`. Without a SQLLoad source, name the connection: `http(connection="shop_api")`.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from importlib.metadata import version
|
|
15
|
+
|
|
16
|
+
from sqlload_api._data import Checkpoint, Field, JsonData, checkpoint, field, json, key, ndjson, parent
|
|
17
|
+
from sqlload_api._http import (
|
|
18
|
+
CellName,
|
|
19
|
+
Cells,
|
|
20
|
+
ConnectionOwner,
|
|
21
|
+
Http,
|
|
22
|
+
Job,
|
|
23
|
+
cells,
|
|
24
|
+
cursor,
|
|
25
|
+
header_row,
|
|
26
|
+
http,
|
|
27
|
+
link_header,
|
|
28
|
+
listed,
|
|
29
|
+
next_link,
|
|
30
|
+
offset,
|
|
31
|
+
page_number,
|
|
32
|
+
single,
|
|
33
|
+
)
|
|
34
|
+
from sqlload_api._http_paths import literal_paths
|
|
35
|
+
from sqlload_api._transform import TransformContext, transform
|
|
36
|
+
|
|
37
|
+
__version__: str = version("sqlload-api")
|
|
38
|
+
|
|
39
|
+
__all__ = [
|
|
40
|
+
"CellName",
|
|
41
|
+
"Cells",
|
|
42
|
+
"Checkpoint",
|
|
43
|
+
"ConnectionOwner",
|
|
44
|
+
"Field",
|
|
45
|
+
"Http",
|
|
46
|
+
"Job",
|
|
47
|
+
"JsonData",
|
|
48
|
+
"TransformContext",
|
|
49
|
+
"__version__",
|
|
50
|
+
"cells",
|
|
51
|
+
"checkpoint",
|
|
52
|
+
"cursor",
|
|
53
|
+
"field",
|
|
54
|
+
"header_row",
|
|
55
|
+
"http",
|
|
56
|
+
"json",
|
|
57
|
+
"key",
|
|
58
|
+
"link_header",
|
|
59
|
+
"listed",
|
|
60
|
+
"literal_paths",
|
|
61
|
+
"ndjson",
|
|
62
|
+
"next_link",
|
|
63
|
+
"offset",
|
|
64
|
+
"page_number",
|
|
65
|
+
"parent",
|
|
66
|
+
"single",
|
|
67
|
+
"transform",
|
|
68
|
+
]
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"""What a table function can yield besides rows: JSON bytes, which Rust parses without the GIL, and checkpoints.
|
|
2
|
+
|
|
3
|
+
Each crosses into the engine through a method returning plain data (`__sqlload_json__`, `__sqlload_checkpoint__`), so
|
|
4
|
+
this module needs no compiled code; the engine checks what it reads.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import json as _json
|
|
8
|
+
from collections.abc import Mapping
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
BytesLike = bytes | bytearray | memoryview | str
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
#: What `sl.field(parse=...)` takes, the shape of a table's `parse` entry: an epoch word, or a mapping with `timestamp` or `date` (a format or a list of formats), and optionally `tz` and `on_error`; anything else belongs in a `transform`.
|
|
16
|
+
Parse = str | Mapping[str, Any]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class Field:
|
|
21
|
+
"""One projected column's path, whether every row must have a value there, and how that value converts."""
|
|
22
|
+
|
|
23
|
+
path: str
|
|
24
|
+
required: bool = False
|
|
25
|
+
parse: Parse | None = None
|
|
26
|
+
on_error: str = "fail"
|
|
27
|
+
#: Where `path` starts when not at the row: `sl.parent`'s enclosing object or `sl.key`'s request key, as the spec's `from`.
|
|
28
|
+
source: Mapping[str, Any] | None = None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
#: `fields=`: each column's path, such as `"$.address.city"` or `"$.items[0].id"`, or an `sl.field(...)`.
|
|
32
|
+
Fields = Mapping[str, "str | Field"]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def field(path: str, *, required: bool = False, parse: Parse | None = None, on_error: str = "fail") -> Field:
|
|
36
|
+
"""The value at `path` (such as `"$.updated-at"` or `"$.items[0].id"`) for `fields=`; `required=True` fails the table on a
|
|
37
|
+
row without one. A path is dotted keys after `$`, each optionally followed by list indexes: `[0]` is the first item and
|
|
38
|
+
`[-1]` the last. A missing key, an index past the end of the list or on a value that isn't a list gives NULL. Other
|
|
39
|
+
JSONPath syntax (`[*]`, filters, slices, `..`) is refused, naming the field.
|
|
40
|
+
|
|
41
|
+
`parse` converts the value, as a table's `parse` setting does: `"epoch_s"`, `"epoch_ms"` or `"epoch_us"` turn a number
|
|
42
|
+
(or numeric text) into a UTC timestamp; `{"timestamp": ["iso", "%m/%d/%Y %H:%M"]}` and `{"date": "%d/%m/%Y"}` read
|
|
43
|
+
text with a format, or each of a list of formats in turn, the first that reads it winning. A time without an offset
|
|
44
|
+
is read in `"tz"` (such as `{"timestamp": "%d/%m/%Y %H:%M", "tz": "Europe/Paris"}`), else the table's `timezone`,
|
|
45
|
+
else UTC, and loads as `timestamp_tz`. A value that doesn't convert fails the table, naming the field and the value,
|
|
46
|
+
or loads NULL with `on_error="null"`. Other conversions belong in a `transform`."""
|
|
47
|
+
return Field(path, required, parse, on_error)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def parent(path: str, *, levels: int = 1, required: bool = False, parse: Parse | None = None, on_error: str = "fail") -> Field:
|
|
51
|
+
"""In an `sl.http` job's `fields=`, the value at `path` in the object around each row that `select` passes through:
|
|
52
|
+
the nearest with `levels=1`, further out with more, the response document itself outermost (so a page's envelope
|
|
53
|
+
works too). `required`, `parse` and `on_error` are as for `sl.field`; `plan` refuses more levels than `select` has."""
|
|
54
|
+
if isinstance(levels, bool) or not isinstance(levels, int):
|
|
55
|
+
raise TypeError(f"sl.parent levels takes a whole number, not {levels!r}")
|
|
56
|
+
return Field(path, required, parse, on_error, {"kind": "parent", "levels": levels})
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def key(name: str, *, required: bool = False) -> Field:
|
|
60
|
+
"""In a `fan_out`'s `fields=`, the value of key field `name` its request was made for, from any level above
|
|
61
|
+
(each key keeps the fields of the keys above it). `plan` refuses a name no level provides, and `batch=`, where
|
|
62
|
+
one request carries many keys."""
|
|
63
|
+
if not isinstance(name, str):
|
|
64
|
+
raise TypeError(f"sl.key takes a key field's name, not {name!r}")
|
|
65
|
+
return Field("$", required, None, "fail", {"kind": "key", "name": name})
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class JsonData:
|
|
69
|
+
"""JSON or NDJSON bytes yielded by a table function (`sl.json(...)`, `sl.ndjson(...)`), parsed by the engine.
|
|
70
|
+
|
|
71
|
+
It crosses as `__sqlload_json__()`: the bytes, and how to read them as JSON. The engine checks the read when the
|
|
72
|
+
item crosses (a field's path syntax among it), failing the table with the problem."""
|
|
73
|
+
|
|
74
|
+
__slots__ = ("_bytes", "_fields", "_keep_record", "_lines", "_path")
|
|
75
|
+
|
|
76
|
+
def __init__(
|
|
77
|
+
self,
|
|
78
|
+
data: bytes | bytearray,
|
|
79
|
+
*,
|
|
80
|
+
lines: bool = False,
|
|
81
|
+
path: str | None = None,
|
|
82
|
+
fields: list[dict[str, Any]] | None = None,
|
|
83
|
+
keep_record: str | None = None,
|
|
84
|
+
) -> None:
|
|
85
|
+
if lines and path is not None:
|
|
86
|
+
raise ValueError("path picks rows out of a JSON document; NDJSON has none")
|
|
87
|
+
if keep_record is not None and fields is None:
|
|
88
|
+
raise ValueError("keep_record needs fields: name the columns to load beside the whole record")
|
|
89
|
+
self._bytes = data
|
|
90
|
+
self._lines = lines
|
|
91
|
+
self._path = path
|
|
92
|
+
self._fields = fields
|
|
93
|
+
self._keep_record = keep_record
|
|
94
|
+
|
|
95
|
+
@property
|
|
96
|
+
def lines(self) -> bool:
|
|
97
|
+
"""Whether the bytes are NDJSON."""
|
|
98
|
+
return self._lines
|
|
99
|
+
|
|
100
|
+
@property
|
|
101
|
+
def path(self) -> str | None:
|
|
102
|
+
"""The dotted path to the rows inside the document, if any."""
|
|
103
|
+
return self._path
|
|
104
|
+
|
|
105
|
+
def __len__(self) -> int:
|
|
106
|
+
return len(self._bytes)
|
|
107
|
+
|
|
108
|
+
def __sqlload_json__(self) -> tuple[bytes | bytearray, str]:
|
|
109
|
+
"""The bytes, and the read as the engine takes it: layout, path, fields and keep_record."""
|
|
110
|
+
read = {
|
|
111
|
+
"layout": "lines" if self._lines else "document",
|
|
112
|
+
"path": self._path,
|
|
113
|
+
"fields": self._fields,
|
|
114
|
+
"keep_record": self._keep_record,
|
|
115
|
+
}
|
|
116
|
+
return self._bytes, _json.dumps(read)
|
|
117
|
+
|
|
118
|
+
def __repr__(self) -> str:
|
|
119
|
+
kind = "sl.ndjson" if self._lines else "sl.json"
|
|
120
|
+
return f"{kind}({len(self._bytes)} bytes)"
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def json(data: BytesLike, path: str | None = None, *, fields: Fields | None = None, keep_record: str | None = None) -> JsonData:
|
|
124
|
+
"""Rows in a JSON document: an array of objects (at dotted `path`, such as `"data.items"`), or objects.
|
|
125
|
+
|
|
126
|
+
`fields` loads only those values, renamed (`{"account_code": "$.code"}`); `keep_record` adds each row's whole
|
|
127
|
+
object as a JSON column."""
|
|
128
|
+
return JsonData(_as_bytes(data), path=path, fields=_json_fields(fields), keep_record=keep_record)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def ndjson(data: BytesLike, *, fields: Fields | None = None, keep_record: str | None = None) -> JsonData:
|
|
132
|
+
"""Rows as newline-delimited JSON, one object per line; `fields` and `keep_record` as for `sl.json`."""
|
|
133
|
+
return JsonData(_as_bytes(data), lines=True, fields=_json_fields(fields), keep_record=keep_record)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def field_list(fields: Fields | None) -> list[dict[str, Any]] | None:
|
|
137
|
+
"""`fields` as the specs Rust reads (name, path, required, and parse and on_error when set), in the order given."""
|
|
138
|
+
if fields is None:
|
|
139
|
+
return None
|
|
140
|
+
if not isinstance(fields, Mapping):
|
|
141
|
+
raise TypeError(f"fields takes a mapping of column name to path, such as {{\"id\": \"$.id\"}}, not {fields!r}")
|
|
142
|
+
listed = []
|
|
143
|
+
for name, spec in fields.items():
|
|
144
|
+
projected = spec if isinstance(spec, Field) else Field(spec)
|
|
145
|
+
if not isinstance(name, str):
|
|
146
|
+
raise TypeError(f"fields names columns with text, not {name!r}")
|
|
147
|
+
if not isinstance(projected.path, str):
|
|
148
|
+
raise TypeError(f"fields[{name!r}] takes a path such as \"$.id\" or sl.field(...), not {spec!r}")
|
|
149
|
+
if not isinstance(projected.required, bool):
|
|
150
|
+
raise TypeError(f"fields[{name!r}]: required takes True or False, not {projected.required!r}")
|
|
151
|
+
source = {} if projected.source is None else {"from": dict(projected.source)}
|
|
152
|
+
listed.append({"name": name, "path": projected.path, "required": projected.required, **_conversion(name, projected), **source})
|
|
153
|
+
return listed
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _conversion(name: str, projected: Field) -> dict[str, Any]:
|
|
157
|
+
"""`projected`'s parse and on_error as spec keys, left out when unset so specs without them are unchanged."""
|
|
158
|
+
if projected.on_error not in ("fail", "null"):
|
|
159
|
+
raise ValueError(f'fields[{name!r}]: on_error must be "fail" or "null", not {projected.on_error!r}')
|
|
160
|
+
parse = projected.parse
|
|
161
|
+
if parse is None:
|
|
162
|
+
if projected.on_error != "fail":
|
|
163
|
+
raise ValueError(f"fields[{name!r}]: on_error applies to values parse converts; set parse too")
|
|
164
|
+
return {}
|
|
165
|
+
if not isinstance(parse, (str, Mapping)):
|
|
166
|
+
raise ValueError(
|
|
167
|
+
f'fields[{name!r}]: parse takes "epoch_s", "epoch_ms", "epoch_us", or {{"timestamp": formats}} or {{"date": formats}}, not {parse!r}; '
|
|
168
|
+
"other conversions belong in a transform"
|
|
169
|
+
)
|
|
170
|
+
# Rust reads the rest, as it reads a table's parse setting.
|
|
171
|
+
return {"parse": parse if isinstance(parse, str) else dict(parse), **({"on_error": "null"} if projected.on_error == "null" else {})}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _json_fields(fields: Fields | None) -> list[dict[str, Any]] | None:
|
|
175
|
+
"""`fields` as the list of specs `JsonData` crosses with."""
|
|
176
|
+
listed = field_list(fields)
|
|
177
|
+
for spec in listed or []:
|
|
178
|
+
if "from" in spec:
|
|
179
|
+
raise ValueError(f"fields[{spec['name']!r}]: sl.parent and sl.key read around the rows of an sl.http job; sl.json and sl.ndjson rows have no request")
|
|
180
|
+
return listed
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _as_bytes(data: BytesLike) -> bytes | bytearray:
|
|
184
|
+
if isinstance(data, str):
|
|
185
|
+
return data.encode()
|
|
186
|
+
if isinstance(data, memoryview):
|
|
187
|
+
return data.tobytes()
|
|
188
|
+
return data
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
class Checkpoint:
|
|
192
|
+
"""A position yielded among a logged table's rows; see `checkpoint`."""
|
|
193
|
+
|
|
194
|
+
def __init__(self, state: Any) -> None:
|
|
195
|
+
try:
|
|
196
|
+
self._state = _json.dumps(state)
|
|
197
|
+
except (TypeError, ValueError) as error:
|
|
198
|
+
raise TypeError(f"sl.checkpoint state is not JSON: {error}") from error
|
|
199
|
+
|
|
200
|
+
def __sqlload_checkpoint__(self) -> str:
|
|
201
|
+
return self._state
|
|
202
|
+
|
|
203
|
+
def __repr__(self) -> str:
|
|
204
|
+
return f"sl.checkpoint({self._state})"
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def checkpoint(state: Any) -> Checkpoint:
|
|
208
|
+
"""The position (any JSON value) that covers exactly the rows yielded before it, for a `state="logged"`
|
|
209
|
+
table: `yield sl.checkpoint(page.next_token)` after a page's rows. A commit records the latest checkpoint
|
|
210
|
+
whose rows are all in it, so a run that fails part-way resumes there without loading any row twice; the
|
|
211
|
+
next run reads it as `ctx.state`. Rows yielded after the last checkpoint are loaded again by the next run
|
|
212
|
+
unless the function leaves the final position in `ctx.state`; `sl.checkpoint(None)` clears the position."""
|
|
213
|
+
return Checkpoint(state)
|