continuo-python-runtime 0.11.0__py3-none-any.whl → 0.13.0__py3-none-any.whl
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.
- continuo_python_runtime/cli.py +108 -37
- continuo_python_runtime/content_check.py +73 -0
- continuo_python_runtime/contract/kinds.py +37 -21
- continuo_python_runtime/contract/loader.py +207 -389
- continuo_python_runtime/contract/model.py +13 -6
- continuo_python_runtime/contract/pack.py +299 -0
- continuo_python_runtime/contract/paths.py +34 -1
- continuo_python_runtime/contract/read_tables.py +85 -0
- continuo_python_runtime/errors.py +13 -1
- continuo_python_runtime/harness.py +21 -6
- continuo_python_runtime/hash_inputs.py +63 -0
- continuo_python_runtime/hashing.py +54 -72
- continuo_python_runtime/lint.py +194 -6
- continuo_python_runtime/packages.py +56 -0
- continuo_python_runtime/validation/preflight.py +410 -0
- continuo_python_runtime/validation/runner.py +20 -3
- continuo_python_runtime-0.13.0.dist-info/METADATA +600 -0
- continuo_python_runtime-0.13.0.dist-info/RECORD +36 -0
- continuo_python_runtime/contract/merge.py +0 -197
- continuo_python_runtime/contract/secret_ref.py +0 -31
- continuo_python_runtime-0.11.0.dist-info/METADATA +0 -411
- continuo_python_runtime-0.11.0.dist-info/RECORD +0 -32
- {continuo_python_runtime-0.11.0.dist-info → continuo_python_runtime-0.13.0.dist-info}/WHEEL +0 -0
- {continuo_python_runtime-0.11.0.dist-info → continuo_python_runtime-0.13.0.dist-info}/entry_points.txt +0 -0
- {continuo_python_runtime-0.11.0.dist-info → continuo_python_runtime-0.13.0.dist-info}/licenses/LICENSE +0 -0
- {continuo_python_runtime-0.11.0.dist-info → continuo_python_runtime-0.13.0.dist-info}/licenses/NOTICE +0 -0
|
@@ -1,118 +1,71 @@
|
|
|
1
|
-
"""
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
.
|
|
5
|
-
|
|
1
|
+
"""The contract loader: one ODCS contract per node.
|
|
2
|
+
|
|
3
|
+
A contracts folder holds one ODCS v3.2.0 contract per node, in a file named
|
|
4
|
+
``<table>.odcs.yaml``, or ``<schema>.<table>.odcs.yaml`` when two nodes of one
|
|
5
|
+
service share a table name. continuo-contracts reads each file strictly, checks
|
|
6
|
+
it and maps it to its ``PythonNode``; the runtime then applies the rules it
|
|
7
|
+
keeps for itself and builds its :class:`~continuo_python_runtime.contract.model
|
|
8
|
+
.Node`. Every refusal is a :class:`ContractError` that names the file.
|
|
6
9
|
"""
|
|
7
10
|
|
|
8
11
|
from __future__ import annotations
|
|
9
12
|
|
|
13
|
+
from collections.abc import Iterable, Mapping
|
|
10
14
|
from pathlib import Path
|
|
11
15
|
from typing import Any
|
|
12
16
|
|
|
13
|
-
import
|
|
17
|
+
from continuo_contracts import yaml_strict
|
|
18
|
+
from continuo_contracts.errors import StrictYamlError
|
|
19
|
+
from continuo_contracts.python import PythonNode, node_from_odcs
|
|
20
|
+
from continuo_contracts.validate import validate_python_contract
|
|
21
|
+
from continuo_contracts.vocabulary import continuo_values
|
|
14
22
|
from sqlglot import Dialect
|
|
15
23
|
|
|
16
24
|
from continuo_python_runtime.contract.kinds import rules_for
|
|
17
|
-
from continuo_python_runtime.contract.model import
|
|
18
|
-
CRITICALITIES,
|
|
19
|
-
DEFAULT_KIND,
|
|
20
|
-
DEFAULT_MODE_TYPE,
|
|
21
|
-
EXTRA_COLUMNS_POLICIES,
|
|
22
|
-
MODE_TYPES,
|
|
23
|
-
Column,
|
|
24
|
-
Node,
|
|
25
|
-
)
|
|
26
|
-
from continuo_python_runtime.contract.secret_ref import validate_secret_ref
|
|
25
|
+
from continuo_python_runtime.contract.model import Column, Node
|
|
27
26
|
from continuo_python_runtime.errors import ContractError
|
|
28
|
-
from continuo_python_runtime.types import parse_sql_type
|
|
29
|
-
|
|
30
|
-
_ALLOWED_KEYS = {
|
|
31
|
-
"schema",
|
|
32
|
-
"table",
|
|
33
|
-
"description",
|
|
34
|
-
"owner",
|
|
35
|
-
"schedule",
|
|
36
|
-
"criticality",
|
|
37
|
-
"kind",
|
|
38
|
-
"script",
|
|
39
|
-
"extra_columns",
|
|
40
|
-
"reads",
|
|
41
|
-
"output_columns",
|
|
42
|
-
"config",
|
|
43
|
-
"content_hash",
|
|
44
|
-
"secret_ref",
|
|
45
|
-
"mode_type",
|
|
46
|
-
"primary_key",
|
|
47
|
-
"timeout_minutes",
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
# Bounds of a node's declared task timeout, in minutes (one minute to one week).
|
|
51
|
-
MIN_TIMEOUT_MINUTES = 1
|
|
52
|
-
MAX_TIMEOUT_MINUTES = 10_080
|
|
53
|
-
|
|
54
|
-
_REQUIRED_STRING_FIELDS = ("schema", "table", "owner", "schedule")
|
|
55
|
-
|
|
56
|
-
_ALLOWED_OUTPUT_COLUMN_KEYS = {"name", "type", "nullable"}
|
|
57
|
-
|
|
58
27
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
raise ContractError(f"duplicate key {key!r} at {key_node.start_mark}")
|
|
71
|
-
seen.add(key)
|
|
72
|
-
return yaml.SafeLoader.construct_mapping(loader, node, deep=deep)
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
_StrictLoader.add_constructor(
|
|
76
|
-
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, _strict_construct_mapping
|
|
28
|
+
CONTRACT_SUFFIX = ".odcs.yaml"
|
|
29
|
+
# The endings of a YAML file's name, compared in lower case: any file that ends
|
|
30
|
+
# in one of them and not exactly in CONTRACT_SUFFIX is refused.
|
|
31
|
+
_YAML_SUFFIXES = (".yml", ".yaml")
|
|
32
|
+
|
|
33
|
+
# Where a refused file's author finds how to convert it: the refusal is read in
|
|
34
|
+
# the CI of the domain team that owns the repository, which does not hold this
|
|
35
|
+
# repository's docs.
|
|
36
|
+
_CONVERSION_GUIDE = (
|
|
37
|
+
"https://github.com/carolsimone/continuo-python-runtime/blob/main/"
|
|
38
|
+
"docs/boundary-contract.md#converting-a-contract-file"
|
|
77
39
|
)
|
|
78
40
|
|
|
79
|
-
|
|
80
|
-
def _node_label(raw: dict[str, Any], source: str) -> str:
|
|
81
|
-
"""Build a `source (schema.table)`-style label for error messages."""
|
|
82
|
-
schema = raw.get("schema")
|
|
83
|
-
table = raw.get("table")
|
|
84
|
-
if isinstance(schema, str) and schema and isinstance(table, str) and table:
|
|
85
|
-
return f"{source} ({schema}.{table})"
|
|
86
|
-
return source
|
|
87
|
-
|
|
88
|
-
|
|
89
41
|
_JSON_SCALARS = (str, int, float, bool, type(None))
|
|
90
42
|
|
|
43
|
+
# What a document that is not a mapping is, as the words of a message.
|
|
44
|
+
_DOCUMENT_KINDS = {
|
|
45
|
+
list: "a list",
|
|
46
|
+
str: "a string",
|
|
47
|
+
bool: "a boolean",
|
|
48
|
+
int: "a number",
|
|
49
|
+
float: "a number",
|
|
50
|
+
}
|
|
51
|
+
|
|
91
52
|
|
|
92
53
|
def _validate_config(raw: Any, label: str) -> dict[str, Any]:
|
|
93
|
-
"""Validate the node's physical-layout
|
|
94
|
-
|
|
95
|
-
The engine's adapter
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
every dict/list container currently being descended into; a container is
|
|
108
|
-
added just before recursing into its items and discarded right after, so
|
|
109
|
-
re-entering one still on the stack is a genuine back-edge (a cycle), while
|
|
110
|
-
the *same* container reached twice at sibling positions — a legal DAG,
|
|
111
|
-
e.g. one list aliased under two different keys — is not: its first visit
|
|
112
|
-
finishes (and is discarded) before the second begins.
|
|
54
|
+
"""Validate the node's physical-layout ``config`` as a JSON-shaped mapping.
|
|
55
|
+
|
|
56
|
+
The engine's adapter, not this loader, owns the vocabulary, so the only
|
|
57
|
+
rules here are the ones the hash and the adapters need: it is a mapping,
|
|
58
|
+
every key at every level is a string, and every value is a JSON scalar, a
|
|
59
|
+
list or a mapping. A contract file always passes, since ``yaml_strict``
|
|
60
|
+
reads nothing else; a document built in code can hold a key that is not a
|
|
61
|
+
string, a value JSON cannot write or a container that contains itself, and
|
|
62
|
+
each is refused here, naming the node.
|
|
63
|
+
|
|
64
|
+
``active`` holds the ``id()`` of every dict or list being descended into:
|
|
65
|
+
a container is added just before its items are checked and discarded right
|
|
66
|
+
after, so meeting one that is still in ``active`` is a cycle, while one
|
|
67
|
+
container reached twice at sibling positions is not.
|
|
113
68
|
"""
|
|
114
|
-
if raw is None:
|
|
115
|
-
return {}
|
|
116
69
|
if not isinstance(raw, dict):
|
|
117
70
|
raise ContractError(f"{label}: 'config' must be a mapping")
|
|
118
71
|
|
|
@@ -149,291 +102,162 @@ def _validate_config(raw: Any, label: str) -> dict[str, Any]:
|
|
|
149
102
|
return raw
|
|
150
103
|
|
|
151
104
|
|
|
152
|
-
def
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
``replace`` would look enforced when it is not. Names keep their order,
|
|
159
|
-
which is what the wire entry carries. Each must be spelled exactly as an
|
|
160
|
-
output column (postgres quotes identifiers, so ``Rate_Date`` is not
|
|
161
|
-
``rate_date``) declared ``nullable: false``, because a NULL key never
|
|
162
|
-
matches an existing row. Repeats are refused case-insensitively too, so a
|
|
163
|
-
key cannot name two columns that differ only in case. A floating-point
|
|
164
|
-
column is refused as well: the warehouse treats ``0.0`` and ``-0.0`` as
|
|
165
|
-
one key, so ``ensure_unique_key`` would reject the rows at write time --
|
|
166
|
-
failing here instead keeps an unwritable upsert out of the contract.
|
|
105
|
+
def _upsert_key(python: PythonNode, label: str) -> tuple[str, ...]:
|
|
106
|
+
"""Return the key the harness upserts on: the node's key when it upserts, else none.
|
|
107
|
+
|
|
108
|
+
On a replace or append node a key only describes the table. The names of
|
|
109
|
+
an upsert key are compared case-insensitively too, so a key cannot name
|
|
110
|
+
two columns that differ only in case.
|
|
167
111
|
"""
|
|
168
|
-
if mode_type != "upsert":
|
|
169
|
-
if "primary_key" in raw:
|
|
170
|
-
raise ContractError(
|
|
171
|
-
f"{label}: 'primary_key' is only allowed when mode_type is upsert "
|
|
172
|
-
f"(got {mode_type})"
|
|
173
|
-
)
|
|
112
|
+
if python.mode_type != "upsert":
|
|
174
113
|
return ()
|
|
175
|
-
if "primary_key" not in raw:
|
|
176
|
-
raise ContractError(f"{label}: 'primary_key' is required when mode_type is upsert")
|
|
177
|
-
raw_key = raw["primary_key"]
|
|
178
|
-
if not isinstance(raw_key, list) or not raw_key:
|
|
179
|
-
raise ContractError(
|
|
180
|
-
f"{label}: 'primary_key' must be a non-empty list of output column names"
|
|
181
|
-
)
|
|
182
|
-
by_name = {column.name: column for column in columns}
|
|
183
114
|
seen: set[str] = set()
|
|
184
|
-
for name in
|
|
185
|
-
|
|
186
|
-
raise ContractError(
|
|
187
|
-
f"{label}: 'primary_key' entry {name!r} must be a non-empty string"
|
|
188
|
-
)
|
|
189
|
-
folded = name.lower() # same folding as continuo's parser
|
|
115
|
+
for name in python.primary_key:
|
|
116
|
+
folded = name.lower()
|
|
190
117
|
if folded in seen:
|
|
191
118
|
raise ContractError(
|
|
192
|
-
f"{label}:
|
|
119
|
+
f"{label}: the upsert key names {name!r} more than once "
|
|
193
120
|
"(names are compared case-insensitively)"
|
|
194
121
|
)
|
|
195
122
|
seen.add(folded)
|
|
196
|
-
|
|
197
|
-
if column is None:
|
|
198
|
-
raise ContractError(
|
|
199
|
-
f"{label}: 'primary_key' column {name!r} is not in output_columns"
|
|
200
|
-
)
|
|
201
|
-
if column.nullable:
|
|
202
|
-
raise ContractError(
|
|
203
|
-
f"{label}: 'primary_key' column {name!r} must be declared nullable: false"
|
|
204
|
-
)
|
|
205
|
-
if parse_sql_type(column.type).base == "DOUBLE_PRECISION":
|
|
206
|
-
raise ContractError(
|
|
207
|
-
f"{label}: 'primary_key' column {name!r} is floating-point "
|
|
208
|
-
f"({column.type}); a floating-point column cannot be a primary key"
|
|
209
|
-
)
|
|
210
|
-
return tuple(raw_key)
|
|
123
|
+
return tuple(python.primary_key)
|
|
211
124
|
|
|
212
125
|
|
|
213
|
-
def
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
*,
|
|
217
|
-
dialect: str | None = None,
|
|
218
|
-
check_reads: bool = True,
|
|
219
|
-
) -> Node:
|
|
220
|
-
"""Validate a single raw mapping and build a :class:`Node`.
|
|
221
|
-
|
|
222
|
-
``source`` is the originating filename; it appears in every error message.
|
|
223
|
-
``dialect`` is a sqlglot dialect name (e.g. ``"postgres"``, ``"trino"``)
|
|
224
|
-
each declared read is checked against via
|
|
225
|
-
:func:`~continuo_engine_contract.sql.ensure_single_read`; ``None``
|
|
226
|
-
(the default) uses sqlglot's dialect-neutral parser.
|
|
227
|
-
|
|
228
|
-
``check_reads=False`` skips *only* that :func:`ensure_single_read` call —
|
|
229
|
-
the read-shape gate — leaving every other rule here (required fields,
|
|
230
|
-
criticality, the ``reads`` map's own shape, output-column types and
|
|
231
|
-
uniqueness, ``mode_type`` and ``primary_key``, ``config``,
|
|
232
|
-
``content_hash``) in force. It exists for
|
|
233
|
-
:func:`~continuo_python_runtime.harness.run_node`; see
|
|
234
|
-
:func:`load_contract_dir` for why the runtime opts out.
|
|
235
|
-
"""
|
|
236
|
-
if not isinstance(raw, dict):
|
|
237
|
-
raise ContractError(
|
|
238
|
-
f"{source}: node must be a mapping, got {type(raw).__name__}"
|
|
239
|
-
)
|
|
240
|
-
|
|
241
|
-
label = _node_label(raw, source)
|
|
126
|
+
def issue_lines(file: Path | str, issues: Iterable[object]) -> list[str]:
|
|
127
|
+
"""One line per issue of the contract in ``file``, ``f"{file}: {issue}"``."""
|
|
128
|
+
return [f"{file}: {issue}" for issue in issues]
|
|
242
129
|
|
|
243
|
-
unknown = set(raw) - _ALLOWED_KEYS
|
|
244
|
-
if unknown:
|
|
245
|
-
raise ContractError(f"{label}: unknown key(s) {sorted(unknown)}")
|
|
246
130
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
)
|
|
282
|
-
|
|
283
|
-
extra_columns = raw.get("extra_columns", "raise")
|
|
284
|
-
if (
|
|
285
|
-
not isinstance(extra_columns, str)
|
|
286
|
-
or extra_columns not in EXTRA_COLUMNS_POLICIES
|
|
287
|
-
):
|
|
288
|
-
raise ContractError(
|
|
289
|
-
f"{label}: 'extra_columns' must be one of {sorted(EXTRA_COLUMNS_POLICIES)}, "
|
|
290
|
-
f"got {extra_columns!r}"
|
|
291
|
-
)
|
|
292
|
-
|
|
293
|
-
reads = rules.reads.validate(
|
|
294
|
-
raw.get("reads"), label, dialect=dialect, check_reads=check_reads
|
|
131
|
+
def node_from_contract(
|
|
132
|
+
doc: Mapping[str, Any], file: Path, *, dialect: str | None, check_reads: bool
|
|
133
|
+
) -> Node:
|
|
134
|
+
"""Check the ODCS contract ``doc``, read from ``file``, and build its :class:`Node`.
|
|
135
|
+
|
|
136
|
+
continuo-contracts checks the contract first
|
|
137
|
+
(``validate_python_contract``, with no service, of which only the base
|
|
138
|
+
name of ``file`` counts for the file-name rule). When it finds issues they
|
|
139
|
+
become one :class:`ContractError` with one :func:`issue_lines` line per
|
|
140
|
+
issue, and nothing else runs.
|
|
141
|
+
|
|
142
|
+
The runtime's own rules follow, each naming ``file`` and the relation:
|
|
143
|
+
the kind's reads rule (:mod:`~continuo_python_runtime.contract.kinds`),
|
|
144
|
+
which, unless ``check_reads`` is false, parses each read of a python-node
|
|
145
|
+
as one read query under ``dialect`` and refuses a read that names a table
|
|
146
|
+
without its schema (a reference to a CTE in scope excepted); the shape of ``config``;
|
|
147
|
+
and the case-insensitive uniqueness of an upsert key's names.
|
|
148
|
+
|
|
149
|
+
The node takes ``owner`` from ``team.name``, ``description`` from
|
|
150
|
+
``description.purpose`` (``""`` when absent), and ``schedule``,
|
|
151
|
+
``criticality`` and ``timeout_minutes`` (``None`` when absent) from the
|
|
152
|
+
contract's ``continuo`` custom properties. Every other field is the
|
|
153
|
+
library's ``PythonNode``, kept as ``python``: the kind as the library
|
|
154
|
+
reads it, the script, the reads, the columns (nullable unless
|
|
155
|
+
``required``), ``extra_columns``, ``config``, ``secret_ref``,
|
|
156
|
+
``mode_type``, the key when the node upserts and the assets.
|
|
157
|
+
"""
|
|
158
|
+
issues = validate_python_contract(doc, path=str(file), service=None)
|
|
159
|
+
if issues:
|
|
160
|
+
raise ContractError("\n".join(issue_lines(file, issues)))
|
|
161
|
+
python = node_from_odcs(doc)
|
|
162
|
+
label = f"{file} ({python.relation})"
|
|
163
|
+
|
|
164
|
+
reads = rules_for(python.kind, label).reads.validate(
|
|
165
|
+
dict(python.reads), label, dialect=dialect, check_reads=check_reads
|
|
295
166
|
)
|
|
167
|
+
config = _validate_config(python.config, label)
|
|
168
|
+
primary_key = _upsert_key(python, label)
|
|
296
169
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
if not rules.secret_allowed:
|
|
300
|
-
raise ContractError(f"{label}: 'secret_ref' is not allowed for kind {kind}")
|
|
301
|
-
secret_ref = validate_secret_ref(raw["secret_ref"], label)
|
|
302
|
-
|
|
303
|
-
timeout_minutes = raw.get("timeout_minutes")
|
|
304
|
-
if "timeout_minutes" in raw and (
|
|
305
|
-
type(timeout_minutes) is not int
|
|
306
|
-
or not MIN_TIMEOUT_MINUTES <= timeout_minutes <= MAX_TIMEOUT_MINUTES
|
|
307
|
-
):
|
|
308
|
-
raise ContractError(
|
|
309
|
-
f"{label}: 'timeout_minutes' must be an integer between "
|
|
310
|
-
f"{MIN_TIMEOUT_MINUTES} and {MAX_TIMEOUT_MINUTES}, got {timeout_minutes!r}"
|
|
311
|
-
)
|
|
312
|
-
|
|
313
|
-
mode_type = raw.get("mode_type", DEFAULT_MODE_TYPE)
|
|
314
|
-
if not isinstance(mode_type, str) or mode_type not in MODE_TYPES:
|
|
315
|
-
raise ContractError(
|
|
316
|
-
f"{label}: 'mode_type' must be one of {sorted(MODE_TYPES)}, got {mode_type!r}"
|
|
317
|
-
)
|
|
318
|
-
if mode_type not in rules.mode_types:
|
|
319
|
-
raise ContractError(
|
|
320
|
-
f"{label}: 'mode_type' {mode_type!r} is not allowed for kind {kind!r} "
|
|
321
|
-
f"(allowed: {sorted(rules.mode_types)})"
|
|
322
|
-
)
|
|
323
|
-
|
|
324
|
-
raw_columns = raw.get("output_columns")
|
|
325
|
-
if not isinstance(raw_columns, list) or not raw_columns:
|
|
326
|
-
raise ContractError(f"{label}: 'output_columns' must be a non-empty list")
|
|
327
|
-
|
|
328
|
-
seen_names: set[str] = set()
|
|
329
|
-
columns: list[Column] = []
|
|
330
|
-
for entry in raw_columns:
|
|
331
|
-
if not isinstance(entry, dict):
|
|
332
|
-
raise ContractError(
|
|
333
|
-
f"{label}: each 'output_columns' entry must be a mapping, got {entry!r}"
|
|
334
|
-
)
|
|
335
|
-
unknown_col_keys = set(entry) - _ALLOWED_OUTPUT_COLUMN_KEYS
|
|
336
|
-
if unknown_col_keys:
|
|
337
|
-
raise ContractError(
|
|
338
|
-
f"{label}: output column has unknown key(s) {sorted(unknown_col_keys)}"
|
|
339
|
-
)
|
|
340
|
-
name = entry.get("name")
|
|
341
|
-
if not isinstance(name, str) or not name.strip():
|
|
342
|
-
raise ContractError(f"{label}: output column missing non-empty 'name'")
|
|
343
|
-
col_type = entry.get("type")
|
|
344
|
-
if not isinstance(col_type, str):
|
|
345
|
-
raise ContractError(
|
|
346
|
-
f"{label}: output column '{name}' has unsupported 'type' {col_type!r}"
|
|
347
|
-
)
|
|
348
|
-
try:
|
|
349
|
-
parse_sql_type(col_type)
|
|
350
|
-
except ContractError as e:
|
|
351
|
-
raise ContractError(
|
|
352
|
-
f"{label}: output column '{name}' has unsupported {str(e)}"
|
|
353
|
-
) from e
|
|
354
|
-
nullable = entry.get("nullable", True)
|
|
355
|
-
if not isinstance(nullable, bool):
|
|
356
|
-
raise ContractError(
|
|
357
|
-
f"{label}: output column '{name}' has non-boolean 'nullable' {nullable!r}"
|
|
358
|
-
)
|
|
359
|
-
if name in seen_names:
|
|
360
|
-
raise ContractError(f"{label}: duplicate column '{name}' in output_columns")
|
|
361
|
-
seen_names.add(name)
|
|
362
|
-
columns.append(Column(name=name, type=col_type, nullable=nullable))
|
|
363
|
-
|
|
364
|
-
primary_key = _validate_primary_key(raw, mode_type, columns, label)
|
|
365
|
-
|
|
366
|
-
description = raw.get("description", "")
|
|
367
|
-
if not isinstance(description, str):
|
|
368
|
-
raise ContractError(f"{label}: 'description' must be a string")
|
|
369
|
-
|
|
370
|
-
config = _validate_config(raw.get("config"), label)
|
|
371
|
-
|
|
372
|
-
content_hash = raw.get("content_hash")
|
|
373
|
-
if content_hash is not None and not isinstance(content_hash, str):
|
|
374
|
-
raise ContractError(f"{label}: 'content_hash' must be a string")
|
|
375
|
-
|
|
170
|
+
values = continuo_values(doc.get("customProperties"))
|
|
171
|
+
description = doc.get("description", {}).get("purpose", "")
|
|
376
172
|
return Node(
|
|
377
|
-
schema=schema,
|
|
378
|
-
table=table,
|
|
379
|
-
owner=
|
|
380
|
-
schedule=schedule,
|
|
381
|
-
criticality=criticality,
|
|
382
|
-
script=script,
|
|
173
|
+
schema=python.schema,
|
|
174
|
+
table=python.table,
|
|
175
|
+
owner=doc["team"]["name"],
|
|
176
|
+
schedule=values["schedule"],
|
|
177
|
+
criticality=values["criticality"],
|
|
178
|
+
script=python.script,
|
|
383
179
|
reads=reads,
|
|
384
|
-
output_columns=tuple(
|
|
180
|
+
output_columns=tuple(
|
|
181
|
+
Column(name=column.name, type=column.type, nullable=column.nullable)
|
|
182
|
+
for column in python.output_columns
|
|
183
|
+
),
|
|
184
|
+
python=python,
|
|
185
|
+
path=file,
|
|
385
186
|
description=description,
|
|
386
|
-
extra_columns=extra_columns,
|
|
187
|
+
extra_columns=python.extra_columns,
|
|
387
188
|
config=config,
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
mode_type=mode_type,
|
|
189
|
+
kind=python.kind,
|
|
190
|
+
secret_ref=python.secret_ref,
|
|
191
|
+
mode_type=python.mode_type,
|
|
392
192
|
primary_key=primary_key,
|
|
393
|
-
timeout_minutes=
|
|
193
|
+
timeout_minutes=values.get("timeoutMinutes"),
|
|
194
|
+
assets=tuple(python.assets),
|
|
394
195
|
)
|
|
395
196
|
|
|
396
197
|
|
|
198
|
+
def read_contract_text(file: Path) -> str:
|
|
199
|
+
"""Return the text of the contract ``file`` exactly as written, line endings included."""
|
|
200
|
+
try:
|
|
201
|
+
return file.read_bytes().decode("utf-8")
|
|
202
|
+
except UnicodeDecodeError as exc:
|
|
203
|
+
raise ContractError(f"{file}: the file is not UTF-8 text ({exc})") from None
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def parse_contract(text: str, file: Path | str) -> Mapping[str, Any]:
|
|
207
|
+
"""Return the document of the contract ``text``, read strictly by continuo-contracts.
|
|
208
|
+
|
|
209
|
+
``file`` is only the name the refusals carry.
|
|
210
|
+
"""
|
|
211
|
+
try:
|
|
212
|
+
doc = yaml_strict.load(text)
|
|
213
|
+
except StrictYamlError as exc:
|
|
214
|
+
raise ContractError(f"{file}: {exc}") from None
|
|
215
|
+
if not isinstance(doc, Mapping):
|
|
216
|
+
holds = "nothing" if doc is None else _DOCUMENT_KINDS.get(type(doc), type(doc).__name__)
|
|
217
|
+
raise ContractError(
|
|
218
|
+
f"{file}: a contract file holds one ODCS contract, a mapping; this one holds {holds}"
|
|
219
|
+
)
|
|
220
|
+
return doc
|
|
221
|
+
|
|
222
|
+
|
|
397
223
|
def load_contract_dir(
|
|
398
224
|
path: Path, *, dialect: str | None = None, check_reads: bool = True
|
|
399
225
|
) -> list[Node]:
|
|
400
|
-
"""Load and
|
|
401
|
-
|
|
402
|
-
``
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
226
|
+
"""Load and check every ``*.odcs.yaml`` contract in ``path``, in file-name order.
|
|
227
|
+
|
|
228
|
+
Any other file whose name ends in ``.yml`` or ``.yaml``, in any case
|
|
229
|
+
(``legacy.YML``, ``t.odcs.YAML``, ``t.odcs.yml``), is refused: one
|
|
230
|
+
:class:`ContractError` names every such file, one line each, with how to
|
|
231
|
+
convert it. An image pairs a runtime with its contracts, so a contract in
|
|
232
|
+
another format is a mistake, never a node to skip. Files with any other
|
|
233
|
+
extension are not read.
|
|
234
|
+
|
|
235
|
+
``check_reads=False`` skips only the checks of the reads of a python-node:
|
|
236
|
+
that each parses as one read query and that each names its tables with
|
|
237
|
+
their schemas. Every other rule of :func:`node_from_contract` and every
|
|
238
|
+
rule here (the dialect name, the duplicate relations, the empty folder)
|
|
239
|
+
still runs. The harness
|
|
407
240
|
(:func:`~continuo_python_runtime.harness.run_node`) passes it, because
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
- CI already
|
|
412
|
-
|
|
413
|
-
parser
|
|
414
|
-
- The
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
the per-read ``ensure_single_read`` call matters: an unrecognized
|
|
429
|
-
dialect name (e.g. a typo'd ``--dialect POSTGRES`` instead of
|
|
430
|
-
``postgres``) raises the same bare ``ValueError`` a genuinely
|
|
431
|
-
unparseable read would, and would otherwise be misreported as a
|
|
432
|
-
rejected read instead of a bad flag -- blaming an innocent, valid read.
|
|
433
|
-
|
|
434
|
-
Raises `ContractError` if ``dialect`` is not a sqlglot dialect name, if
|
|
435
|
-
no nodes are found, if any file's document is malformed, or if two nodes
|
|
436
|
-
across files share the same `(schema, table)`.
|
|
241
|
+
checking the reads again at container start can only disagree with the
|
|
242
|
+
checks already passed, never catch a new problem:
|
|
243
|
+
|
|
244
|
+
- CI already checked the reads (``continuo-runtime validate``) under the
|
|
245
|
+
repository's ``--dialect``, and continuo checked them again with its own
|
|
246
|
+
parser before promoting the release.
|
|
247
|
+
- The harness has no ``--dialect`` of its own, so it would parse with
|
|
248
|
+
sqlglot's dialect-neutral grammar, which refuses ordinary postgres such
|
|
249
|
+
as ``a ~ 'x'`` that parses under ``--dialect postgres``.
|
|
250
|
+
- Nothing at run time reads the parse: ``ctx.read`` finds a read by name
|
|
251
|
+
and hands its SQL to the adapter as written.
|
|
252
|
+
|
|
253
|
+
``dialect`` is checked once, before any file is read: sqlglot raises the
|
|
254
|
+
same ``ValueError`` for an unknown dialect name as for a read it cannot
|
|
255
|
+
parse, so a typo such as ``--dialect POSTGRES`` is reported as a bad flag
|
|
256
|
+
instead of blaming a valid read.
|
|
257
|
+
|
|
258
|
+
Raises ``ContractError`` for an unknown ``dialect``, a file in another
|
|
259
|
+
format, a folder with no contract, a contract that is refused, and two
|
|
260
|
+
files that declare the same ``<schema>.<table>``, compared in lower case.
|
|
437
261
|
"""
|
|
438
262
|
if dialect is not None:
|
|
439
263
|
try:
|
|
@@ -441,43 +265,37 @@ def load_contract_dir(
|
|
|
441
265
|
except ValueError as exc:
|
|
442
266
|
raise ContractError(f"unknown --dialect {dialect!r}: {exc}") from exc
|
|
443
267
|
|
|
444
|
-
|
|
268
|
+
entries = sorted(path.iterdir()) if path.is_dir() else []
|
|
269
|
+
yaml_files = [
|
|
270
|
+
entry for entry in entries if entry.name.lower().endswith(_YAML_SUFFIXES)
|
|
271
|
+
]
|
|
272
|
+
files = [entry for entry in yaml_files if entry.name.endswith(CONTRACT_SUFFIX)]
|
|
273
|
+
others = [entry for entry in yaml_files if not entry.name.endswith(CONTRACT_SUFFIX)]
|
|
274
|
+
if others:
|
|
275
|
+
raise ContractError(
|
|
276
|
+
"\n".join(
|
|
277
|
+
f"{other}: runtime 0.13.0 reads only ODCS contracts named "
|
|
278
|
+
f"<table>.odcs.yaml; convert this file ({_CONVERSION_GUIDE})"
|
|
279
|
+
for other in others
|
|
280
|
+
)
|
|
281
|
+
)
|
|
282
|
+
if not files:
|
|
283
|
+
raise ContractError(f"no contract files found in {path}")
|
|
445
284
|
|
|
446
285
|
nodes: list[Node] = []
|
|
447
|
-
|
|
448
|
-
|
|
286
|
+
relation_files: dict[str, Path] = {}
|
|
449
287
|
for file in files:
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
if
|
|
458
|
-
document = {}
|
|
459
|
-
if not isinstance(document, dict):
|
|
288
|
+
doc = parse_contract(read_contract_text(file), file)
|
|
289
|
+
node = node_from_contract(doc, file, dialect=dialect, check_reads=check_reads)
|
|
290
|
+
# Relations are compared in lower case, as continuo compares them: a
|
|
291
|
+
# warehouse folds the case of an unquoted name, so two contracts whose
|
|
292
|
+
# tables differ only in case would build one table.
|
|
293
|
+
key = node.relation.lower()
|
|
294
|
+
existing = relation_files.get(key)
|
|
295
|
+
if existing is not None:
|
|
460
296
|
raise ContractError(
|
|
461
|
-
f"{
|
|
462
|
-
f"got {type(document).__name__}"
|
|
463
|
-
)
|
|
464
|
-
raw_nodes = document.get("nodes")
|
|
465
|
-
if not isinstance(raw_nodes, list):
|
|
466
|
-
raise ContractError(f"{file.name}: 'nodes' must be a list")
|
|
467
|
-
|
|
468
|
-
for raw_node in raw_nodes:
|
|
469
|
-
node = parse_node(
|
|
470
|
-
raw_node, file.name, dialect=dialect, check_reads=check_reads
|
|
297
|
+
f"duplicate node {node.relation} (in {existing} and {file})"
|
|
471
298
|
)
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
raise ContractError(
|
|
475
|
-
f"duplicate node {node.relation} (in {existing_source} and {file.name})"
|
|
476
|
-
)
|
|
477
|
-
relation_sources[node.relation] = file.name
|
|
478
|
-
nodes.append(node)
|
|
479
|
-
|
|
480
|
-
if not nodes:
|
|
481
|
-
raise ContractError(f"no contract files found in {path}")
|
|
482
|
-
|
|
299
|
+
relation_files[key] = file
|
|
300
|
+
nodes.append(node)
|
|
483
301
|
return nodes
|