semantic-context-engine 0.3.0a1__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.
- semantic_context_engine/__init__.py +136 -0
- semantic_context_engine/artifacts.py +98 -0
- semantic_context_engine/cli.py +197 -0
- semantic_context_engine/compiler.py +316 -0
- semantic_context_engine/composition.py +31 -0
- semantic_context_engine/config.py +199 -0
- semantic_context_engine/diff.py +93 -0
- semantic_context_engine/domain_contracts.py +386 -0
- semantic_context_engine/engine.py +250 -0
- semantic_context_engine/enrichment.py +1416 -0
- semantic_context_engine/errors.py +291 -0
- semantic_context_engine/evaluation.py +88 -0
- semantic_context_engine/hierarchies.py +185 -0
- semantic_context_engine/instructions/semantic_catalog.md +2 -0
- semantic_context_engine/integration.py +22 -0
- semantic_context_engine/integrations/__init__.py +11 -0
- semantic_context_engine/integrations/langchain.py +275 -0
- semantic_context_engine/models.py +282 -0
- semantic_context_engine/normalization.py +351 -0
- semantic_context_engine/pipeline.py +1685 -0
- semantic_context_engine/prompts/v1/column_classification.md +15 -0
- semantic_context_engine/prompts/v1/grain_correction.md +4 -0
- semantic_context_engine/prompts/v1/relationship_proposal.md +10 -0
- semantic_context_engine/prompts/v1/synthesis.md +5 -0
- semantic_context_engine/prompts/v1/taxonomy.md +5 -0
- semantic_context_engine/publication.py +98 -0
- semantic_context_engine/registry.py +764 -0
- semantic_context_engine/relations.py +1114 -0
- semantic_context_engine/runtime.py +1193 -0
- semantic_context_engine/sources/base.py +130 -0
- semantic_context_engine/sources/duckdb.py +1172 -0
- semantic_context_engine/sources/fake.py +107 -0
- semantic_context_engine/storage.py +248 -0
- semantic_context_engine-0.3.0a1.dist-info/METADATA +263 -0
- semantic_context_engine-0.3.0a1.dist-info/RECORD +39 -0
- semantic_context_engine-0.3.0a1.dist-info/WHEEL +5 -0
- semantic_context_engine-0.3.0a1.dist-info/entry_points.txt +2 -0
- semantic_context_engine-0.3.0a1.dist-info/licenses/LICENSE +202 -0
- semantic_context_engine-0.3.0a1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"""Optional bridge from an application-owned LangChain chat model to SCE.
|
|
2
|
+
|
|
3
|
+
This module imports ``langchain-core`` only when the bridge is constructed.
|
|
4
|
+
The supplied chat model owns authentication, transport, timeout and retry
|
|
5
|
+
configuration; the bridge does not mutate it or promise uniform transport
|
|
6
|
+
timeouts across integrations.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import json
|
|
12
|
+
import re
|
|
13
|
+
from time import perf_counter
|
|
14
|
+
from typing import Any, Mapping
|
|
15
|
+
|
|
16
|
+
from pydantic import ValidationError as PydanticValidationError
|
|
17
|
+
|
|
18
|
+
from semantic_context_engine.errors import ProviderDiagnosticError
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _safe_identity(value: Any) -> str | None:
|
|
22
|
+
if (
|
|
23
|
+
type(value) is str
|
|
24
|
+
and 0 < len(value) <= 128
|
|
25
|
+
and re.fullmatch(r"[A-Za-z0-9_.:/+-]+", value)
|
|
26
|
+
and not re.search(
|
|
27
|
+
r"(?i)(secret|token|api[_-]?key|bearer|credential|password|private[_-]?key|authorization)",
|
|
28
|
+
value,
|
|
29
|
+
)
|
|
30
|
+
and not re.fullmatch(r"[A-Za-z0-9_-]{32,}", value)
|
|
31
|
+
):
|
|
32
|
+
return value
|
|
33
|
+
return None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _http_status(exc: BaseException) -> int | None:
|
|
37
|
+
value = getattr(exc, "status_code", None)
|
|
38
|
+
if isinstance(value, int) and not isinstance(value, bool) and 100 <= value <= 599:
|
|
39
|
+
return value
|
|
40
|
+
response = getattr(exc, "response", None)
|
|
41
|
+
value = getattr(response, "status_code", None)
|
|
42
|
+
if isinstance(value, int) and not isinstance(value, bool) and 100 <= value <= 599:
|
|
43
|
+
return value
|
|
44
|
+
return None
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _contains_pydantic_validation_error(error: Any) -> bool:
|
|
48
|
+
seen: set[int] = set()
|
|
49
|
+
current = error
|
|
50
|
+
for _ in range(5):
|
|
51
|
+
if isinstance(current, PydanticValidationError):
|
|
52
|
+
return True
|
|
53
|
+
if current is None or id(current) in seen:
|
|
54
|
+
return False
|
|
55
|
+
seen.add(id(current))
|
|
56
|
+
current = getattr(current, "__cause__", None)
|
|
57
|
+
return False
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _provider_error(stage: str, exc: BaseException) -> ProviderDiagnosticError:
|
|
61
|
+
status = _http_status(exc)
|
|
62
|
+
if status in {401, 403}:
|
|
63
|
+
category, code, retryable = "provider_authentication", "authentication_rejected", False
|
|
64
|
+
elif status == 429:
|
|
65
|
+
category, code, retryable = "rate_limited", "rate_limited", True
|
|
66
|
+
elif status is not None and 400 <= status < 500:
|
|
67
|
+
category, code, retryable = "provider_request_rejected", "http_4xx", False
|
|
68
|
+
elif status is not None and status >= 500:
|
|
69
|
+
category, code, retryable = "transport_failure", "http_5xx", True
|
|
70
|
+
else:
|
|
71
|
+
# A timeout class is safe to distinguish without reading the message.
|
|
72
|
+
cls_name = type(exc).__name__.lower()
|
|
73
|
+
if "timeout" in cls_name:
|
|
74
|
+
category, code, retryable = "transport_failure", "timeout", True
|
|
75
|
+
else:
|
|
76
|
+
category, code, retryable = "transport_failure", "transport_error", True
|
|
77
|
+
return ProviderDiagnosticError(stage, category, code, retryable=retryable, http_status=status)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class LangChainStructuredLLM:
|
|
81
|
+
"""Use ``BaseChatModel.with_structured_output`` with SCE's Pydantic schemas."""
|
|
82
|
+
|
|
83
|
+
prompt_version = "sce-semantic-v1"
|
|
84
|
+
|
|
85
|
+
def __init__(
|
|
86
|
+
self,
|
|
87
|
+
model: Any,
|
|
88
|
+
*,
|
|
89
|
+
provider_name: str | None = None,
|
|
90
|
+
model_name: str | None = None,
|
|
91
|
+
) -> None:
|
|
92
|
+
try:
|
|
93
|
+
from langchain_core.language_models.chat_models import BaseChatModel # type: ignore[import-not-found]
|
|
94
|
+
except ImportError:
|
|
95
|
+
raise ProviderDiagnosticError(
|
|
96
|
+
"unknown_stage",
|
|
97
|
+
"provider_configuration",
|
|
98
|
+
"configuration_rejected",
|
|
99
|
+
retryable=False,
|
|
100
|
+
) from None
|
|
101
|
+
if not isinstance(model, BaseChatModel):
|
|
102
|
+
raise TypeError("model must be an instantiated langchain_core BaseChatModel")
|
|
103
|
+
self._model = model
|
|
104
|
+
self.configured_provider_name = _safe_identity(provider_name)
|
|
105
|
+
self.configured_model_name = _safe_identity(model_name) or _safe_identity(
|
|
106
|
+
getattr(model, "model_name", None) or getattr(model, "model", None)
|
|
107
|
+
)
|
|
108
|
+
self.observed_provider_name: str | None = None
|
|
109
|
+
self.observed_model_name: str | None = None
|
|
110
|
+
self.last_finish_reason = "unknown"
|
|
111
|
+
self.provider_name = self.configured_provider_name or "unknown"
|
|
112
|
+
self.model_name = self.configured_model_name or "unknown"
|
|
113
|
+
self.request_count = 0
|
|
114
|
+
self.retry_count = 0
|
|
115
|
+
self.usage_totals: dict[str, int] = {}
|
|
116
|
+
self.last_usage: dict[str, int] = {}
|
|
117
|
+
self.last_duration_seconds: float | None = None
|
|
118
|
+
self.total_duration_seconds = 0.0
|
|
119
|
+
self.stage_calls: dict[str, int] = {}
|
|
120
|
+
self.last_provider_metadata: dict[str, str] = {}
|
|
121
|
+
|
|
122
|
+
def generate(
|
|
123
|
+
self,
|
|
124
|
+
stage: str,
|
|
125
|
+
context: Mapping[str, Any],
|
|
126
|
+
schema: Mapping[str, Any],
|
|
127
|
+
*,
|
|
128
|
+
timeout_seconds: float,
|
|
129
|
+
) -> Mapping[str, Any]:
|
|
130
|
+
del schema # The canonical Pydantic schema below owns validation/encoding.
|
|
131
|
+
from semantic_context_engine.enrichment import _STAGE_MODELS
|
|
132
|
+
|
|
133
|
+
output_model = _STAGE_MODELS.get(stage)
|
|
134
|
+
if output_model is None:
|
|
135
|
+
raise ProviderDiagnosticError(
|
|
136
|
+
stage, "provider_configuration", "configuration_rejected", retryable=False
|
|
137
|
+
)
|
|
138
|
+
if timeout_seconds <= 0:
|
|
139
|
+
raise ProviderDiagnosticError(
|
|
140
|
+
stage, "provider_configuration", "configuration_rejected", retryable=False
|
|
141
|
+
)
|
|
142
|
+
try:
|
|
143
|
+
runnable = self._model.with_structured_output(output_model, include_raw=True)
|
|
144
|
+
except Exception:
|
|
145
|
+
raise ProviderDiagnosticError(
|
|
146
|
+
stage,
|
|
147
|
+
"unsupported_structured_output",
|
|
148
|
+
"structured_output_unsupported",
|
|
149
|
+
retryable=False,
|
|
150
|
+
) from None
|
|
151
|
+
from langchain_core.messages import HumanMessage, SystemMessage # type: ignore[import-not-found]
|
|
152
|
+
|
|
153
|
+
system_prompt = context.get("system_prompt")
|
|
154
|
+
payload = {key: value for key, value in context.items() if key not in {"system_prompt"}}
|
|
155
|
+
if not isinstance(system_prompt, str):
|
|
156
|
+
system_prompt = "Return only the requested SCE structured output."
|
|
157
|
+
messages = [
|
|
158
|
+
SystemMessage(content=system_prompt),
|
|
159
|
+
HumanMessage(
|
|
160
|
+
content=json.dumps(payload, sort_keys=True, ensure_ascii=False, default=str)
|
|
161
|
+
),
|
|
162
|
+
]
|
|
163
|
+
self.request_count += 1
|
|
164
|
+
self.stage_calls[stage] = self.stage_calls.get(stage, 0) + 1
|
|
165
|
+
started = perf_counter()
|
|
166
|
+
try:
|
|
167
|
+
response = runnable.invoke(messages)
|
|
168
|
+
except (NotImplementedError, AttributeError, TypeError):
|
|
169
|
+
self._record_duration(started)
|
|
170
|
+
raise ProviderDiagnosticError(
|
|
171
|
+
stage,
|
|
172
|
+
"unsupported_structured_output",
|
|
173
|
+
"structured_output_unsupported",
|
|
174
|
+
retryable=False,
|
|
175
|
+
) from None
|
|
176
|
+
except Exception as exc:
|
|
177
|
+
self._record_duration(started)
|
|
178
|
+
raise _provider_error(stage, exc) from None
|
|
179
|
+
self._record_duration(started)
|
|
180
|
+
if not isinstance(response, Mapping):
|
|
181
|
+
raise ProviderDiagnosticError(
|
|
182
|
+
stage, "structured_output_parse_failure", "response_parse_failed", retryable=False
|
|
183
|
+
)
|
|
184
|
+
parsing_error = response.get("parsing_error")
|
|
185
|
+
parsed = response.get("parsed")
|
|
186
|
+
raw = response.get("raw")
|
|
187
|
+
self._collect_metadata(raw)
|
|
188
|
+
finish_reason = self.last_finish_reason
|
|
189
|
+
if finish_reason in {"length", "max_tokens", "max_output_tokens"}:
|
|
190
|
+
raise ProviderDiagnosticError(
|
|
191
|
+
stage, "incomplete_response", "response_incomplete", retryable=False
|
|
192
|
+
)
|
|
193
|
+
if parsed is not None and hasattr(parsed, "model_dump") and callable(parsed.model_dump):
|
|
194
|
+
parsed = parsed.model_dump(mode="python")
|
|
195
|
+
if parsing_error is not None:
|
|
196
|
+
if _contains_pydantic_validation_error(parsing_error):
|
|
197
|
+
category, code = "pydantic_schema_validation_failure", "schema_validation_failed"
|
|
198
|
+
else:
|
|
199
|
+
category, code = "structured_output_parse_failure", "response_parse_failed"
|
|
200
|
+
raise ProviderDiagnosticError(stage, category, code, retryable=False)
|
|
201
|
+
if not isinstance(parsed, Mapping):
|
|
202
|
+
raise ProviderDiagnosticError(
|
|
203
|
+
stage, "incomplete_response", "response_incomplete", retryable=False
|
|
204
|
+
)
|
|
205
|
+
try:
|
|
206
|
+
validated = output_model.model_validate(dict(parsed)).model_dump(mode="python")
|
|
207
|
+
except PydanticValidationError:
|
|
208
|
+
raise ProviderDiagnosticError(
|
|
209
|
+
stage,
|
|
210
|
+
"pydantic_schema_validation_failure",
|
|
211
|
+
"schema_validation_failed",
|
|
212
|
+
retryable=False,
|
|
213
|
+
) from None
|
|
214
|
+
except Exception:
|
|
215
|
+
raise ProviderDiagnosticError(
|
|
216
|
+
stage, "internal_error", "unexpected_provider_error", retryable=False
|
|
217
|
+
) from None
|
|
218
|
+
return validated
|
|
219
|
+
|
|
220
|
+
def _record_duration(self, started: float) -> None:
|
|
221
|
+
from time import perf_counter
|
|
222
|
+
|
|
223
|
+
self.last_duration_seconds = perf_counter() - started
|
|
224
|
+
self.total_duration_seconds += self.last_duration_seconds
|
|
225
|
+
|
|
226
|
+
def _collect_metadata(self, raw: Any) -> None:
|
|
227
|
+
usage = getattr(raw, "usage_metadata", None)
|
|
228
|
+
if not isinstance(usage, Mapping):
|
|
229
|
+
metadata = getattr(raw, "response_metadata", {})
|
|
230
|
+
usage = metadata.get("token_usage", {}) if isinstance(metadata, Mapping) else {}
|
|
231
|
+
self.last_usage = {}
|
|
232
|
+
if isinstance(usage, Mapping):
|
|
233
|
+
for dest, keys in {
|
|
234
|
+
"input_tokens": ("input_tokens", "prompt_tokens"),
|
|
235
|
+
"output_tokens": ("output_tokens", "completion_tokens"),
|
|
236
|
+
"total_tokens": ("total_tokens",),
|
|
237
|
+
}.items():
|
|
238
|
+
value = next((usage.get(key) for key in keys if key in usage), None)
|
|
239
|
+
if isinstance(value, int) and not isinstance(value, bool) and value >= 0:
|
|
240
|
+
self.last_usage[dest] = value
|
|
241
|
+
self.usage_totals[dest] = self.usage_totals.get(dest, 0) + value
|
|
242
|
+
metadata = getattr(raw, "response_metadata", {})
|
|
243
|
+
self.last_provider_metadata = {}
|
|
244
|
+
self.observed_provider_name = None
|
|
245
|
+
self.observed_model_name = None
|
|
246
|
+
self.last_finish_reason = "unknown"
|
|
247
|
+
if isinstance(metadata, Mapping):
|
|
248
|
+
reason = metadata.get("finish_reason") or metadata.get("stop_reason")
|
|
249
|
+
self.last_finish_reason = (
|
|
250
|
+
reason
|
|
251
|
+
if reason
|
|
252
|
+
in {
|
|
253
|
+
"stop",
|
|
254
|
+
"length",
|
|
255
|
+
"max_tokens",
|
|
256
|
+
"max_output_tokens",
|
|
257
|
+
"tool_calls",
|
|
258
|
+
"content_filter",
|
|
259
|
+
"error",
|
|
260
|
+
}
|
|
261
|
+
else "unknown"
|
|
262
|
+
)
|
|
263
|
+
for key in ("model_name", "model", "provider"):
|
|
264
|
+
value = metadata.get(key)
|
|
265
|
+
identity = _safe_identity(value)
|
|
266
|
+
if identity:
|
|
267
|
+
self.last_provider_metadata[key] = identity
|
|
268
|
+
actual_provider = metadata.get("provider")
|
|
269
|
+
actual_model = metadata.get("model_name") or metadata.get("model")
|
|
270
|
+
self.observed_provider_name = _safe_identity(actual_provider)
|
|
271
|
+
self.observed_model_name = _safe_identity(actual_model)
|
|
272
|
+
if self.observed_provider_name:
|
|
273
|
+
self.provider_name = self.observed_provider_name
|
|
274
|
+
if self.observed_model_name:
|
|
275
|
+
self.model_name = self.observed_model_name
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from enum import Enum
|
|
4
|
+
from typing import Dict, List, Optional, Set, Tuple
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# ReviewStatus enum
|
|
8
|
+
class ReviewStatus(str, Enum):
|
|
9
|
+
proposed = "proposed"
|
|
10
|
+
verified = "verified"
|
|
11
|
+
rejected = "rejected"
|
|
12
|
+
deprecated = "deprecated"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# RelationshipType enum (includes forbidden)
|
|
16
|
+
class RelationshipType(str, Enum):
|
|
17
|
+
# Enum members intentionally shadow str.join; preserve the existing public member.
|
|
18
|
+
join = "join" # type: ignore[assignment]
|
|
19
|
+
temporal = "temporal"
|
|
20
|
+
lineage = "lineage"
|
|
21
|
+
forbidden = "forbidden"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# ColumnMeta: nome, tipo, nullable, comentário
|
|
25
|
+
class ColumnMeta:
|
|
26
|
+
__slots__ = ("name", "data_type", "nullable", "comment")
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
name: str,
|
|
31
|
+
data_type: str,
|
|
32
|
+
nullable: bool = True,
|
|
33
|
+
comment: Optional[str] = None,
|
|
34
|
+
):
|
|
35
|
+
self.name = name
|
|
36
|
+
self.data_type = data_type
|
|
37
|
+
self.nullable = nullable
|
|
38
|
+
self.comment = comment
|
|
39
|
+
|
|
40
|
+
def __eq__(self, other):
|
|
41
|
+
if not isinstance(other, ColumnMeta):
|
|
42
|
+
return NotImplemented
|
|
43
|
+
return (
|
|
44
|
+
self.name == other.name
|
|
45
|
+
and self.data_type == other.data_type
|
|
46
|
+
and self.nullable == other.nullable
|
|
47
|
+
and self.comment == other.comment
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
def __hash__(self):
|
|
51
|
+
return hash((self.name, self.data_type, self.nullable, self.comment))
|
|
52
|
+
|
|
53
|
+
def __repr__(self):
|
|
54
|
+
return f"ColumnMeta({self.name!r}, {self.data_type!r})"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# VariantKey: para perfil VARIANT opt-in
|
|
58
|
+
class VariantKey:
|
|
59
|
+
__slots__ = ("column", "discriminator", "key", "value_type", "rows", "present", "status")
|
|
60
|
+
|
|
61
|
+
def __init__(
|
|
62
|
+
self,
|
|
63
|
+
column: str,
|
|
64
|
+
discriminator: str,
|
|
65
|
+
key: str,
|
|
66
|
+
value_type: str,
|
|
67
|
+
rows: int | None = None,
|
|
68
|
+
present: int | None = None,
|
|
69
|
+
status: str = "complete",
|
|
70
|
+
):
|
|
71
|
+
self.column = column
|
|
72
|
+
self.discriminator = discriminator
|
|
73
|
+
self.key = key
|
|
74
|
+
self.value_type = value_type
|
|
75
|
+
self.rows = rows
|
|
76
|
+
self.present = present
|
|
77
|
+
self.status = status
|
|
78
|
+
|
|
79
|
+
def __getitem__(self, name: str) -> str:
|
|
80
|
+
"""Keep the historical mapping-style read access for variant metadata."""
|
|
81
|
+
if name not in {
|
|
82
|
+
"column",
|
|
83
|
+
"discriminator",
|
|
84
|
+
"key",
|
|
85
|
+
"value_type",
|
|
86
|
+
"rows",
|
|
87
|
+
"present",
|
|
88
|
+
"status",
|
|
89
|
+
}:
|
|
90
|
+
raise KeyError(name)
|
|
91
|
+
return getattr(self, name)
|
|
92
|
+
|
|
93
|
+
def __eq__(self, other):
|
|
94
|
+
if not isinstance(other, VariantKey):
|
|
95
|
+
return NotImplemented
|
|
96
|
+
return (
|
|
97
|
+
self.column == other.column
|
|
98
|
+
and self.discriminator == other.discriminator
|
|
99
|
+
and self.key == other.key
|
|
100
|
+
and self.value_type == other.value_type
|
|
101
|
+
and self.rows == other.rows
|
|
102
|
+
and self.present == other.present
|
|
103
|
+
and self.status == other.status
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
def __hash__(self):
|
|
107
|
+
return hash(
|
|
108
|
+
(
|
|
109
|
+
self.column,
|
|
110
|
+
self.discriminator,
|
|
111
|
+
self.key,
|
|
112
|
+
self.value_type,
|
|
113
|
+
self.rows,
|
|
114
|
+
self.present,
|
|
115
|
+
self.status,
|
|
116
|
+
)
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
def __repr__(self):
|
|
120
|
+
return f"VariantKey({self.column!r}, {self.discriminator!r})"
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
# ObjectSnapshot: contrato observável (spec §5.5)
|
|
124
|
+
class ObjectSnapshot:
|
|
125
|
+
__slots__ = (
|
|
126
|
+
"key",
|
|
127
|
+
"fqn",
|
|
128
|
+
"object_type",
|
|
129
|
+
"description",
|
|
130
|
+
"hint",
|
|
131
|
+
"table_comment",
|
|
132
|
+
"columns",
|
|
133
|
+
"ddl_canonical",
|
|
134
|
+
"ddl_raw",
|
|
135
|
+
"ddl_error",
|
|
136
|
+
"dependencies",
|
|
137
|
+
"variant_keys",
|
|
138
|
+
"declared_constraints",
|
|
139
|
+
"fingerprint",
|
|
140
|
+
"discovered_at",
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
def __init__(
|
|
144
|
+
self,
|
|
145
|
+
key: str,
|
|
146
|
+
fqn: str,
|
|
147
|
+
object_type: str,
|
|
148
|
+
description: Optional[str] = None,
|
|
149
|
+
hint: Optional[str] = None,
|
|
150
|
+
table_comment: Optional[str] = None,
|
|
151
|
+
columns: Optional[Dict[str, ColumnMeta]] = None,
|
|
152
|
+
ddl_canonical: str = "",
|
|
153
|
+
ddl_raw: str = "",
|
|
154
|
+
ddl_error: Optional[str] = None,
|
|
155
|
+
dependencies: Optional[Set[str]] = None,
|
|
156
|
+
variant_keys: Optional[List[VariantKey]] = None,
|
|
157
|
+
declared_constraints: Optional[List[dict]] = None,
|
|
158
|
+
fingerprint: str = "",
|
|
159
|
+
discovered_at: str = "",
|
|
160
|
+
):
|
|
161
|
+
self.key = key
|
|
162
|
+
self.fqn = fqn
|
|
163
|
+
self.object_type = object_type
|
|
164
|
+
self.description = description
|
|
165
|
+
self.hint = hint
|
|
166
|
+
self.table_comment = table_comment
|
|
167
|
+
self.columns = columns if columns is not None else {}
|
|
168
|
+
self.ddl_canonical = ddl_canonical
|
|
169
|
+
self.ddl_raw = ddl_raw
|
|
170
|
+
self.ddl_error = ddl_error
|
|
171
|
+
self.dependencies = dependencies if dependencies is not None else set()
|
|
172
|
+
self.variant_keys = variant_keys if variant_keys is not None else []
|
|
173
|
+
self.declared_constraints = declared_constraints if declared_constraints is not None else []
|
|
174
|
+
self.fingerprint = fingerprint
|
|
175
|
+
self.discovered_at = discovered_at
|
|
176
|
+
|
|
177
|
+
def __eq__(self, other):
|
|
178
|
+
if not isinstance(other, ObjectSnapshot):
|
|
179
|
+
return NotImplemented
|
|
180
|
+
return (
|
|
181
|
+
self.key == other.key
|
|
182
|
+
and self.fqn == other.fqn
|
|
183
|
+
and self.object_type == other.object_type
|
|
184
|
+
and self.description == other.description
|
|
185
|
+
and self.hint == other.hint
|
|
186
|
+
and self.table_comment == other.table_comment
|
|
187
|
+
and self.columns == other.columns
|
|
188
|
+
and self.ddl_canonical == other.ddl_canonical
|
|
189
|
+
and self.declared_constraints == other.declared_constraints
|
|
190
|
+
and self.fingerprint == other.fingerprint
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
def __hash__(self):
|
|
194
|
+
return hash((self.key, self.fqn, self.object_type, self.fingerprint))
|
|
195
|
+
|
|
196
|
+
def __repr__(self):
|
|
197
|
+
return f"ObjectSnapshot({self.key!r}, {self.fqn!r})"
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
# StructuralSnapshot: conjunto imutável de ObjectSnapshots
|
|
201
|
+
class StructuralSnapshot:
|
|
202
|
+
__slots__ = ("version", "published_at", "objects", "discovered_order")
|
|
203
|
+
|
|
204
|
+
def __init__(
|
|
205
|
+
self,
|
|
206
|
+
version: str,
|
|
207
|
+
published_at: Optional[str] = None,
|
|
208
|
+
objects: Optional[Dict[str, ObjectSnapshot]] = None,
|
|
209
|
+
discovered_order: Optional[Tuple[str, ...]] = None,
|
|
210
|
+
):
|
|
211
|
+
self.version = version
|
|
212
|
+
self.published_at = published_at
|
|
213
|
+
self.objects = objects if objects is not None else {}
|
|
214
|
+
self.discovered_order = discovered_order if discovered_order is not None else tuple()
|
|
215
|
+
|
|
216
|
+
def __eq__(self, other):
|
|
217
|
+
if not isinstance(other, StructuralSnapshot):
|
|
218
|
+
return NotImplemented
|
|
219
|
+
return (
|
|
220
|
+
self.version == other.version
|
|
221
|
+
and self.published_at == other.published_at
|
|
222
|
+
and self.objects == other.objects
|
|
223
|
+
and self.discovered_order == other.discovered_order
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
def __repr__(self):
|
|
227
|
+
return f"StructuralSnapshot(v={self.version}, n={len(self.objects)})"
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
# Override models (sem pydantic)
|
|
231
|
+
class RelationshipOverride:
|
|
232
|
+
__slots__ = (
|
|
233
|
+
"id",
|
|
234
|
+
"type",
|
|
235
|
+
"source",
|
|
236
|
+
"target",
|
|
237
|
+
"source_columns",
|
|
238
|
+
"target_columns",
|
|
239
|
+
"reason",
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
def __init__(
|
|
243
|
+
self,
|
|
244
|
+
id: str,
|
|
245
|
+
type: RelationshipType,
|
|
246
|
+
source: str,
|
|
247
|
+
target: str,
|
|
248
|
+
source_columns: List[str],
|
|
249
|
+
target_columns: List[str],
|
|
250
|
+
reason: str,
|
|
251
|
+
):
|
|
252
|
+
self.id = id
|
|
253
|
+
self.type = type
|
|
254
|
+
self.source = source
|
|
255
|
+
self.target = target
|
|
256
|
+
self.source_columns = source_columns
|
|
257
|
+
self.target_columns = target_columns
|
|
258
|
+
self.reason = reason
|
|
259
|
+
|
|
260
|
+
def __eq__(self, other):
|
|
261
|
+
if not isinstance(other, RelationshipOverride):
|
|
262
|
+
return NotImplemented
|
|
263
|
+
return (
|
|
264
|
+
self.id == other.id
|
|
265
|
+
and self.type == other.type
|
|
266
|
+
and self.source == other.source
|
|
267
|
+
and self.target == other.target
|
|
268
|
+
and self.source_columns == other.source_columns
|
|
269
|
+
and self.target_columns == other.target_columns
|
|
270
|
+
and self.reason == other.reason
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
class Overrides:
|
|
275
|
+
def __init__(self, relationships: Optional[Dict[str, RelationshipOverride]] = None):
|
|
276
|
+
self.relationships = relationships if relationships is not None else {}
|
|
277
|
+
|
|
278
|
+
def add(self, override: RelationshipOverride) -> None:
|
|
279
|
+
self.relationships[override.id] = override
|
|
280
|
+
|
|
281
|
+
def get(self, override_id: str):
|
|
282
|
+
return self.relationships.get(override_id)
|