umf-access 0.1.0.dev8__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.
- umf_access/__init__.py +109 -0
- umf_access/adoption.py +41 -0
- umf_access/build/__init__.py +2 -0
- umf_access/build/approximate.py +34 -0
- umf_access/build/catalog.py +1626 -0
- umf_access/build/commands.py +394 -0
- umf_access/build/pg_privileges.py +1544 -0
- umf_access/build/privileges.py +134 -0
- umf_access/build/search.py +365 -0
- umf_access/build/unity_search.py +104 -0
- umf_access/bulk.py +227 -0
- umf_access/canonical.py +23 -0
- umf_access/capture.py +410 -0
- umf_access/catalog.py +325 -0
- umf_access/codec_support.py +427 -0
- umf_access/commands.py +93 -0
- umf_access/delta_read.py +758 -0
- umf_access/dsl/__init__.py +148 -0
- umf_access/dsl/_context.py +36 -0
- umf_access/dsl/_expressions.py +625 -0
- umf_access/dsl/_joined.py +4 -0
- umf_access/dsl/_nodes.py +207 -0
- umf_access/dsl/_query.py +5559 -0
- umf_access/dsl/_selection.py +143 -0
- umf_access/dsl/navigation.py +302 -0
- umf_access/dsl/render.py +516 -0
- umf_access/dsl/search.py +343 -0
- umf_access/engine_catalog.py +563 -0
- umf_access/generated_core.py +5 -0
- umf_access/lifecycle/__init__.py +46 -0
- umf_access/lifecycle/_darwin_process_observer.c +306 -0
- umf_access/lifecycle/approval.py +366 -0
- umf_access/lifecycle/backfills.py +79 -0
- umf_access/lifecycle/catalog.py +289 -0
- umf_access/lifecycle/cli.py +231 -0
- umf_access/lifecycle/concurrent.py +196 -0
- umf_access/lifecycle/conversion_policy.py +188 -0
- umf_access/lifecycle/correspondence.py +203 -0
- umf_access/lifecycle/generation_authority_bootstrap.py +1491 -0
- umf_access/lifecycle/generation_authority_session.py +125 -0
- umf_access/lifecycle/generation_enrollment_authority.py +1108 -0
- umf_access/lifecycle/generation_enrollment_ledger.py +698 -0
- umf_access/lifecycle/generation_ingress.py +185 -0
- umf_access/lifecycle/generation_root_registry.py +138 -0
- umf_access/lifecycle/head.py +250 -0
- umf_access/lifecycle/head_view.py +81 -0
- umf_access/lifecycle/host.py +1183 -0
- umf_access/lifecycle/ingress_custody.py +750 -0
- umf_access/lifecycle/ingress_local_postgres.py +7990 -0
- umf_access/lifecycle/ingress_store.py +1488 -0
- umf_access/lifecycle/ledger.py +276 -0
- umf_access/lifecycle/ledger_schema.py +116 -0
- umf_access/lifecycle/mixed.py +593 -0
- umf_access/lifecycle/outcome_schema.py +136 -0
- umf_access/lifecycle/owner_approval_core.py +1810 -0
- umf_access/lifecycle/owner_authority.py +472 -0
- umf_access/lifecycle/owner_bootstrap.py +2077 -0
- umf_access/lifecycle/owner_wire.py +161 -0
- umf_access/lifecycle/phase_fence.py +52 -0
- umf_access/lifecycle/phase_policy.py +422 -0
- umf_access/lifecycle/privileges.py +145 -0
- umf_access/lifecycle/probe.py +39 -0
- umf_access/lifecycle/progress.py +497 -0
- umf_access/lifecycle/roles.py +96 -0
- umf_access/lifecycle/row_originals.py +1712 -0
- umf_access/lifecycle/row_originals_creation.py +759 -0
- umf_access/lifecycle/row_originals_receipt.py +413 -0
- umf_access/lifecycle/seeds.py +99 -0
- umf_access/lifecycle/sql_containment.py +358 -0
- umf_access/lifecycle/sql_containment_cli.py +33 -0
- umf_access/lifecycle/sql_containment_parser.py +20 -0
- umf_access/lifecycle/supervisor_journal_codec.py +1251 -0
- umf_access/lifecycle/table_boundary.py +22 -0
- umf_access/lifecycle/target_custody_codec.py +788 -0
- umf_access/lifecycle/witness.py +72 -0
- umf_access/managed_actions.py +271 -0
- umf_access/managed_bulk.py +353 -0
- umf_access/managed_delta.py +150 -0
- umf_access/managed_support.py +166 -0
- umf_access/manifest_commands.py +278 -0
- umf_access/native_actions.py +1007 -0
- umf_access/native_program.py +570 -0
- umf_access/pages.py +125 -0
- umf_access/postgres_bulk.py +509 -0
- umf_access/postgres_read.py +1230 -0
- umf_access/postgres_search.py +639 -0
- umf_access/protocols.py +665 -0
- umf_access/py.typed +0 -0
- umf_access/related_keys.py +233 -0
- umf_access/reviewed.py +706 -0
- umf_access/search_catalog.py +122 -0
- umf_access/targets.py +312 -0
- umf_access/types.py +220 -0
- umf_access/unified/__init__.py +187 -0
- umf_access/unified/_binding.py +654 -0
- umf_access/unified/head_session.py +144 -0
- umf_access/unified/native_binding.py +143 -0
- umf_access/unified/operation_values.py +173 -0
- umf_access/unified/outcome_values.py +76 -0
- umf_access/unified/session_async.py +161 -0
- umf_access/unified/session_binding.py +108 -0
- umf_access/unified/session_policy.py +152 -0
- umf_access/unified/session_psycopg2.py +70 -0
- umf_access/unified/session_sqlalchemy.py +161 -0
- umf_access/unified/session_sync.py +148 -0
- umf_access/unified/table_reads.py +314 -0
- umf_access/unified/table_writes.py +193 -0
- umf_access/unity_search.py +312 -0
- umf_access/values.py +280 -0
- umf_access/verification.py +64 -0
- umf_access-0.1.0.dev8.dist-info/METADATA +15 -0
- umf_access-0.1.0.dev8.dist-info/RECORD +117 -0
- umf_access-0.1.0.dev8.dist-info/WHEEL +4 -0
- umf_access-0.1.0.dev8.dist-info/entry_points.txt +4 -0
- umf_access-0.1.0.dev8.dist-info/licenses/LICENSE-APACHE +201 -0
- umf_access-0.1.0.dev8.dist-info/licenses/LICENSE-MIT +21 -0
- umf_access-0.1.0.dev8.dist-info/licenses/LICENSE.md +239 -0
umf_access/dsl/search.py
ADDED
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT OR Apache-2.0
|
|
2
|
+
"""Typed bounded ranked-search declarations. No native queries or embedders."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
from dataclasses import dataclass, replace
|
|
8
|
+
from math import isfinite
|
|
9
|
+
from typing import Any, Generic, Literal, Self, TypeVar, cast, overload
|
|
10
|
+
|
|
11
|
+
from pydantic import BaseModel
|
|
12
|
+
|
|
13
|
+
from ..types import Vector
|
|
14
|
+
from ._context import CaptureError, require_capture
|
|
15
|
+
from ._expressions import Column, Param, operand
|
|
16
|
+
from ._nodes import JoinProjection, Node, ProjectionRef, RecordRef, Terminal
|
|
17
|
+
|
|
18
|
+
RANKED_MODEL_PROFILE = "umf.ranked-generated-model/0.1"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def require_ranked_model_profile(actual: str, expected: str) -> None:
|
|
22
|
+
"""Inert generated-module ABI admission; no capture or external work."""
|
|
23
|
+
if actual != RANKED_MODEL_PROFILE or expected != RANKED_MODEL_PROFILE:
|
|
24
|
+
raise RuntimeError(
|
|
25
|
+
"RANKED_MODEL_PROFILE_MISMATCH: generated/runtime profile differs"
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
OutputT = TypeVar("OutputT")
|
|
30
|
+
R = TypeVar("R")
|
|
31
|
+
P = TypeVar("P", bound=BaseModel)
|
|
32
|
+
T1 = TypeVar("T1")
|
|
33
|
+
T2 = TypeVar("T2")
|
|
34
|
+
T3 = TypeVar("T3")
|
|
35
|
+
T4 = TypeVar("T4")
|
|
36
|
+
T5 = TypeVar("T5")
|
|
37
|
+
T6 = TypeVar("T6")
|
|
38
|
+
T7 = TypeVar("T7")
|
|
39
|
+
T8 = TypeVar("T8")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True)
|
|
43
|
+
class Fusion:
|
|
44
|
+
kind: Literal["rrf", "target-defined"]
|
|
45
|
+
constant: int = 60
|
|
46
|
+
depth: int | None = None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def rrf(*, k: int = 60, depth: int | None = None) -> Fusion:
|
|
50
|
+
if (
|
|
51
|
+
type(k) is not int
|
|
52
|
+
or k <= 0
|
|
53
|
+
or depth is not None
|
|
54
|
+
and (type(depth) is not int or not 1 <= depth <= 1000)
|
|
55
|
+
):
|
|
56
|
+
raise CaptureError(
|
|
57
|
+
"SEARCH_LIMIT", "bounded positive RRF constant/depth required"
|
|
58
|
+
)
|
|
59
|
+
return Fusion("rrf", k, depth)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def target_defined() -> Fusion:
|
|
63
|
+
return Fusion("target-defined")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@dataclass(frozen=True)
|
|
67
|
+
class SearchLeg(Generic[OutputT]):
|
|
68
|
+
record: RecordRef
|
|
69
|
+
kind: Literal["text-vector", "vector", "keyword", "hybrid"]
|
|
70
|
+
index_json: str | None
|
|
71
|
+
value: Node | None
|
|
72
|
+
legs: tuple[SearchLeg[OutputT], ...] = ()
|
|
73
|
+
fusion: Fusion | None = None
|
|
74
|
+
|
|
75
|
+
def _owner_type(self, value: OutputT) -> OutputT:
|
|
76
|
+
# Keep owner identity invariant even for a leg whose data is recursive.
|
|
77
|
+
return value
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@dataclass(frozen=True)
|
|
81
|
+
class KeywordIndex(Generic[OutputT]):
|
|
82
|
+
record: RecordRef
|
|
83
|
+
declaration_json: str
|
|
84
|
+
|
|
85
|
+
def matches(self, text: Param[str]) -> SearchLeg[OutputT]:
|
|
86
|
+
require_capture()
|
|
87
|
+
if not isinstance(text, Param):
|
|
88
|
+
raise CaptureError("SEARCH_DECLARATION", "declared text parameter required")
|
|
89
|
+
return SearchLeg(self.record, "keyword", self.declaration_json, operand(text))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclass(frozen=True)
|
|
93
|
+
class VectorIndex(Generic[OutputT]):
|
|
94
|
+
record: RecordRef
|
|
95
|
+
declaration_json: str
|
|
96
|
+
|
|
97
|
+
def near(self, text: Param[str]) -> SearchLeg[OutputT]:
|
|
98
|
+
require_capture()
|
|
99
|
+
if not isinstance(text, Param):
|
|
100
|
+
raise CaptureError("SEARCH_DECLARATION", "declared text parameter required")
|
|
101
|
+
return SearchLeg(
|
|
102
|
+
self.record, "text-vector", self.declaration_json, operand(text)
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
def near_vector(self, vector: Param[Vector]) -> SearchLeg[OutputT]:
|
|
106
|
+
require_capture()
|
|
107
|
+
if not isinstance(vector, Param):
|
|
108
|
+
raise CaptureError(
|
|
109
|
+
"SEARCH_DECLARATION", "declared Vector parameter required"
|
|
110
|
+
)
|
|
111
|
+
return SearchLeg(self.record, "vector", self.declaration_json, operand(vector))
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def hybrid(
|
|
115
|
+
first: SearchLeg[OutputT],
|
|
116
|
+
second: SearchLeg[OutputT],
|
|
117
|
+
*rest: SearchLeg[OutputT],
|
|
118
|
+
fusion: Fusion,
|
|
119
|
+
) -> SearchLeg[OutputT]:
|
|
120
|
+
require_capture()
|
|
121
|
+
legs = (first, second, *rest)
|
|
122
|
+
if len(legs) > 8 or any(
|
|
123
|
+
leg.record != first.record or leg.kind == "hybrid" for leg in legs
|
|
124
|
+
):
|
|
125
|
+
raise CaptureError(
|
|
126
|
+
"SEARCH_DECLARATION",
|
|
127
|
+
"bounded legs must use one Record without nested fusion",
|
|
128
|
+
)
|
|
129
|
+
return SearchLeg(first.record, "hybrid", None, None, legs, fusion)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
@dataclass(frozen=True)
|
|
133
|
+
class Score:
|
|
134
|
+
value: float
|
|
135
|
+
kind: Literal[
|
|
136
|
+
"cosine-distance",
|
|
137
|
+
"dot-similarity",
|
|
138
|
+
"l2-distance",
|
|
139
|
+
"bm25",
|
|
140
|
+
"rrf",
|
|
141
|
+
"implementation-defined",
|
|
142
|
+
]
|
|
143
|
+
|
|
144
|
+
def __post_init__(self) -> None:
|
|
145
|
+
if (
|
|
146
|
+
type(self.value) is not float
|
|
147
|
+
or not isfinite(self.value)
|
|
148
|
+
or self.kind
|
|
149
|
+
not in (
|
|
150
|
+
"cosine-distance",
|
|
151
|
+
"dot-similarity",
|
|
152
|
+
"l2-distance",
|
|
153
|
+
"bm25",
|
|
154
|
+
"rrf",
|
|
155
|
+
"implementation-defined",
|
|
156
|
+
)
|
|
157
|
+
):
|
|
158
|
+
raise ValueError("finite labeled score required")
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
@dataclass(frozen=True)
|
|
162
|
+
class Scored(Generic[R]):
|
|
163
|
+
item: R
|
|
164
|
+
rank: int
|
|
165
|
+
score: Score | None
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
@dataclass(frozen=True)
|
|
169
|
+
class SearchResult(Sequence[Scored[R]], Generic[R]):
|
|
170
|
+
items: tuple[Scored[R], ...]
|
|
171
|
+
stale: int = 0
|
|
172
|
+
approximate: bool = False
|
|
173
|
+
partial: bool = False
|
|
174
|
+
|
|
175
|
+
@overload
|
|
176
|
+
def __getitem__(self, index: int) -> Scored[R]: ...
|
|
177
|
+
@overload
|
|
178
|
+
def __getitem__(self, index: slice) -> tuple[Scored[R], ...]: ...
|
|
179
|
+
def __getitem__(self, index: int | slice) -> Scored[R] | tuple[Scored[R], ...]:
|
|
180
|
+
return self.items[index]
|
|
181
|
+
|
|
182
|
+
def __len__(self) -> int:
|
|
183
|
+
return len(self.items)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
@dataclass(frozen=True)
|
|
187
|
+
class RankedQuery(Generic[OutputT, R]):
|
|
188
|
+
ref: RecordRef
|
|
189
|
+
steps: tuple[Node, ...]
|
|
190
|
+
leg: SearchLeg[OutputT]
|
|
191
|
+
allow_partial: bool = False
|
|
192
|
+
|
|
193
|
+
@classmethod
|
|
194
|
+
def _from_query(cls, query: RankedQuery[OutputT, R]) -> Self:
|
|
195
|
+
return cls(query.ref, query.steps, query.leg, query.allow_partial)
|
|
196
|
+
|
|
197
|
+
def into(self, projection: JoinProjection[OutputT, P]) -> RankedQuery[OutputT, P]:
|
|
198
|
+
return cast("RankedQuery[OutputT, P]", self._into(projection))
|
|
199
|
+
|
|
200
|
+
def _into(self, projection: object) -> RankedQuery[OutputT, BaseModel]:
|
|
201
|
+
require_capture()
|
|
202
|
+
model = (
|
|
203
|
+
projection.model if isinstance(projection, JoinProjection) else projection
|
|
204
|
+
)
|
|
205
|
+
if not isinstance(model, type) or not issubclass(model, BaseModel):
|
|
206
|
+
raise CaptureError(
|
|
207
|
+
"SEARCH_DECLARATION", "generated projection model required"
|
|
208
|
+
)
|
|
209
|
+
fields = getattr(model, "UMF_PROJECTION_FIELDS", None)
|
|
210
|
+
if not isinstance(fields, tuple) or any(
|
|
211
|
+
(f.module, f.element) != (self.ref.module, self.ref.element) for f in fields
|
|
212
|
+
):
|
|
213
|
+
raise CaptureError(
|
|
214
|
+
"SEARCH_DECLARATION", "search projection belongs to another Record"
|
|
215
|
+
)
|
|
216
|
+
return RankedQuery(
|
|
217
|
+
self.ref,
|
|
218
|
+
self.steps
|
|
219
|
+
+ (
|
|
220
|
+
Node(
|
|
221
|
+
"into",
|
|
222
|
+
(
|
|
223
|
+
ProjectionRef(
|
|
224
|
+
model.__module__,
|
|
225
|
+
model.__name__,
|
|
226
|
+
fields,
|
|
227
|
+
tuple(model.model_fields),
|
|
228
|
+
),
|
|
229
|
+
),
|
|
230
|
+
),
|
|
231
|
+
),
|
|
232
|
+
self.leg,
|
|
233
|
+
self.allow_partial,
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
@overload
|
|
237
|
+
def select(self, c1: Column[OutputT, T1], /) -> RankedQuery[OutputT, tuple[T1]]: ...
|
|
238
|
+
@overload
|
|
239
|
+
def select(
|
|
240
|
+
self, c1: Column[OutputT, T1], c2: Column[OutputT, T2], /
|
|
241
|
+
) -> RankedQuery[OutputT, tuple[T1, T2]]: ...
|
|
242
|
+
@overload
|
|
243
|
+
def select(
|
|
244
|
+
self,
|
|
245
|
+
c1: Column[OutputT, T1],
|
|
246
|
+
c2: Column[OutputT, T2],
|
|
247
|
+
c3: Column[OutputT, T3],
|
|
248
|
+
/,
|
|
249
|
+
) -> RankedQuery[OutputT, tuple[T1, T2, T3]]: ...
|
|
250
|
+
@overload
|
|
251
|
+
def select(
|
|
252
|
+
self,
|
|
253
|
+
c1: Column[OutputT, T1],
|
|
254
|
+
c2: Column[OutputT, T2],
|
|
255
|
+
c3: Column[OutputT, T3],
|
|
256
|
+
c4: Column[OutputT, T4],
|
|
257
|
+
/,
|
|
258
|
+
) -> RankedQuery[OutputT, tuple[T1, T2, T3, T4]]: ...
|
|
259
|
+
@overload
|
|
260
|
+
def select(
|
|
261
|
+
self,
|
|
262
|
+
c1: Column[OutputT, T1],
|
|
263
|
+
c2: Column[OutputT, T2],
|
|
264
|
+
c3: Column[OutputT, T3],
|
|
265
|
+
c4: Column[OutputT, T4],
|
|
266
|
+
c5: Column[OutputT, T5],
|
|
267
|
+
/,
|
|
268
|
+
) -> RankedQuery[OutputT, tuple[T1, T2, T3, T4, T5]]: ...
|
|
269
|
+
@overload
|
|
270
|
+
def select(
|
|
271
|
+
self,
|
|
272
|
+
c1: Column[OutputT, T1],
|
|
273
|
+
c2: Column[OutputT, T2],
|
|
274
|
+
c3: Column[OutputT, T3],
|
|
275
|
+
c4: Column[OutputT, T4],
|
|
276
|
+
c5: Column[OutputT, T5],
|
|
277
|
+
c6: Column[OutputT, T6],
|
|
278
|
+
/,
|
|
279
|
+
) -> RankedQuery[OutputT, tuple[T1, T2, T3, T4, T5, T6]]: ...
|
|
280
|
+
@overload
|
|
281
|
+
def select(
|
|
282
|
+
self,
|
|
283
|
+
c1: Column[OutputT, T1],
|
|
284
|
+
c2: Column[OutputT, T2],
|
|
285
|
+
c3: Column[OutputT, T3],
|
|
286
|
+
c4: Column[OutputT, T4],
|
|
287
|
+
c5: Column[OutputT, T5],
|
|
288
|
+
c6: Column[OutputT, T6],
|
|
289
|
+
c7: Column[OutputT, T7],
|
|
290
|
+
/,
|
|
291
|
+
) -> RankedQuery[OutputT, tuple[T1, T2, T3, T4, T5, T6, T7]]: ...
|
|
292
|
+
@overload
|
|
293
|
+
def select(
|
|
294
|
+
self,
|
|
295
|
+
c1: Column[OutputT, T1],
|
|
296
|
+
c2: Column[OutputT, T2],
|
|
297
|
+
c3: Column[OutputT, T3],
|
|
298
|
+
c4: Column[OutputT, T4],
|
|
299
|
+
c5: Column[OutputT, T5],
|
|
300
|
+
c6: Column[OutputT, T6],
|
|
301
|
+
c7: Column[OutputT, T7],
|
|
302
|
+
c8: Column[OutputT, T8],
|
|
303
|
+
/,
|
|
304
|
+
) -> RankedQuery[OutputT, tuple[T1, T2, T3, T4, T5, T6, T7, T8]]: ...
|
|
305
|
+
def select(self, *columns: Column[OutputT, Any]) -> Any:
|
|
306
|
+
return self._select(*columns)
|
|
307
|
+
|
|
308
|
+
def _select(self, *columns: object) -> Any:
|
|
309
|
+
require_capture()
|
|
310
|
+
if not 1 <= len(columns) <= 8 or any(
|
|
311
|
+
not isinstance(c, Column)
|
|
312
|
+
or (c.ref.module, c.ref.element) != (self.ref.module, self.ref.element)
|
|
313
|
+
for c in columns
|
|
314
|
+
):
|
|
315
|
+
raise CaptureError(
|
|
316
|
+
"SEARCH_DECLARATION", "bounded typed search projection required"
|
|
317
|
+
)
|
|
318
|
+
return replace(
|
|
319
|
+
self,
|
|
320
|
+
steps=self.steps
|
|
321
|
+
+ (Node("select", tuple(c.ref for c in columns if isinstance(c, Column))),),
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
def top(self, k: int, *, allow_partial: bool = False) -> Terminal[SearchResult[R]]:
|
|
325
|
+
require_capture()
|
|
326
|
+
if type(allow_partial) is not bool:
|
|
327
|
+
raise CaptureError(
|
|
328
|
+
"SEARCH_DECLARATION", "explicit partial-search opt-in required"
|
|
329
|
+
)
|
|
330
|
+
if type(k) is not int or not 1 <= k <= 1000:
|
|
331
|
+
raise CaptureError("SEARCH_LIMIT", "k must be in 1..1000")
|
|
332
|
+
return Terminal(
|
|
333
|
+
Node(
|
|
334
|
+
"search_top",
|
|
335
|
+
(
|
|
336
|
+
self.ref,
|
|
337
|
+
self.steps,
|
|
338
|
+
self.leg,
|
|
339
|
+
k,
|
|
340
|
+
self.allow_partial or allow_partial,
|
|
341
|
+
),
|
|
342
|
+
)
|
|
343
|
+
)
|