datasentry-core 0.5.1__tar.gz → 0.7.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.
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/PKG-INFO +1 -1
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/pyproject.toml +1 -1
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/__init__.py +1 -1
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/__init__.py +9 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/base.py +16 -1
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/csv.py +23 -5
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/duckdb.py +1 -1
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/file_based.py +48 -6
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/jsonl.py +5 -1
- datasentry_core-0.7.0/src/datasentry_core/connectors/mysql.py +266 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/parquet.py +5 -1
- datasentry_core-0.7.0/src/datasentry_core/connectors/postgres.py +256 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/registry.py +7 -1
- datasentry_core-0.7.0/src/datasentry_core/connectors/remote_file.py +263 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/spec.py +19 -1
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/sqlite.py +1 -1
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/engine/base.py +28 -1
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/repair/engine.py +1 -1
- datasentry_core-0.7.0/src/datasentry_core/secrets.py +116 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/storage/store.py +15 -1
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/.gitignore +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/README.md +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/errors.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/xlsx.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/contracts/__init__.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/contracts/exporters.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/__init__.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/base.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/common.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/cross_field.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/cross_table.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/datetime.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/initial/__init__.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/initial/anomaly_ml.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/initial/categorical.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/initial/formula.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/initial/missing.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/initial/numeric.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/initial/textual.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/initial/uniqueness.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/missingness.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/runner.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/safe_eval.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/detectors/textual_variants.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/drift/__init__.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/drift/engine.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/engine/__init__.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/engine/duckdb.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/engine/fusion.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/engine/profiler.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/engine/sql_guard.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/llm/provider.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/__init__.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/audit.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/contract.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/detector.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/drift.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/enums.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/evidence.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/feedback.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/fingerprint.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/issue.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/llm.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/profile.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/quality.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/repair.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/rules.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/scan.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/models/validation.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/plugins.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/privacy/redactor.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/repair/__init__.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/reporting/__init__.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/reporting/html.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/reporting/interactive.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/reporting/junit.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/reporting/markdown.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/reporting/sarif.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/rules/engine.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/scoring/__init__.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/scoring/engine.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/scoring/gate.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/scoring/quality.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/scoring/weights.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/storage/__init__.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/storage/paths.py +0 -0
- {datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/storage/schema.py +0 -0
|
@@ -19,8 +19,11 @@ from datasentry_core.connectors.errors import (
|
|
|
19
19
|
UnsupportedFormatError,
|
|
20
20
|
)
|
|
21
21
|
from datasentry_core.connectors.jsonl import JsonlConnector
|
|
22
|
+
from datasentry_core.connectors.mysql import MySQLConnector, MySQLDataHandle
|
|
22
23
|
from datasentry_core.connectors.parquet import ParquetConnector
|
|
24
|
+
from datasentry_core.connectors.postgres import PostgresConnector, PostgresDataHandle
|
|
23
25
|
from datasentry_core.connectors.registry import ConnectorRegistry, default_registry
|
|
26
|
+
from datasentry_core.connectors.remote_file import RemoteFileConnector, RemoteFileDataHandle
|
|
24
27
|
from datasentry_core.connectors.spec import DataSourceSpec, DataSourceType
|
|
25
28
|
from datasentry_core.connectors.sqlite import SqliteConnector
|
|
26
29
|
from datasentry_core.connectors.xlsx import XlsxConnector
|
|
@@ -40,7 +43,13 @@ __all__ = [
|
|
|
40
43
|
"FrameBatch",
|
|
41
44
|
"JsonlConnector",
|
|
42
45
|
"LoadWarning",
|
|
46
|
+
"MySQLConnector",
|
|
47
|
+
"MySQLDataHandle",
|
|
43
48
|
"ParquetConnector",
|
|
49
|
+
"PostgresConnector",
|
|
50
|
+
"PostgresDataHandle",
|
|
51
|
+
"RemoteFileConnector",
|
|
52
|
+
"RemoteFileDataHandle",
|
|
44
53
|
"SamplingMethod",
|
|
45
54
|
"SchemaInfo",
|
|
46
55
|
"SqliteConnector",
|
|
@@ -80,7 +80,7 @@ class DataHandle(Protocol):
|
|
|
80
80
|
@property
|
|
81
81
|
def source_type(self) -> DataSourceType: ...
|
|
82
82
|
@property
|
|
83
|
-
def source_path(self) -> Path | None: ...
|
|
83
|
+
def source_path(self) -> Path | str | None: ...
|
|
84
84
|
|
|
85
85
|
@property
|
|
86
86
|
def table_name(self) -> str | None:
|
|
@@ -93,6 +93,21 @@ class DataHandle(Protocol):
|
|
|
93
93
|
def sql_aggregate(self, sql: str, params: dict[str, Any] | None = None) -> FrameBatch: ...
|
|
94
94
|
def count_rows(self) -> int: ...
|
|
95
95
|
def fingerprint(self, mode: FingerprintMode = "full") -> DatasetFingerprint: ...
|
|
96
|
+
def content_fingerprint(self) -> str:
|
|
97
|
+
"""内容指纹(Step 55):文件源=文件 SHA-256;无文件字节源(如 PostgreSQL)
|
|
98
|
+
由子类实现内容哈希——调度器变更感知(Step 53/55)的跳过判定基准。"""
|
|
99
|
+
...
|
|
100
|
+
|
|
101
|
+
def stats_fingerprint(self) -> str:
|
|
102
|
+
"""统计层指纹(Step 58,ADR-058):schema_hash + row_count 组合,零内容读取。
|
|
103
|
+
|
|
104
|
+
调度两层快速失效第一层:统计层一致才计算内容指纹;统计层变化
|
|
105
|
+
立即判定变更。远程源(PG/MySQL/云文件)由 FileDataHandle 默认
|
|
106
|
+
实现(DESCRIBE 目录查询 + count),文件源不参与(沿用 Step 53
|
|
107
|
+
单层文件 SHA-256)。协议默认体:本地文件句柄不实现(调度器不会
|
|
108
|
+
对本地文件调用),实现者不覆盖即视为不适用。"""
|
|
109
|
+
raise NotImplementedError("stats_fingerprint is not supported by this handle")
|
|
110
|
+
|
|
96
111
|
def warnings(self) -> list[LoadWarning]: ...
|
|
97
112
|
def close(self) -> None: ...
|
|
98
113
|
|
|
@@ -27,7 +27,7 @@ from datasentry_core.connectors.base import (
|
|
|
27
27
|
SamplingMethod,
|
|
28
28
|
SchemaInfo,
|
|
29
29
|
)
|
|
30
|
-
from datasentry_core.connectors.errors import DataSourceNotFoundError
|
|
30
|
+
from datasentry_core.connectors.errors import ConnectorError, DataSourceNotFoundError
|
|
31
31
|
from datasentry_core.connectors.spec import DataSourceSpec, DataSourceType
|
|
32
32
|
from datasentry_core.engine import DuckDBExecutor
|
|
33
33
|
from datasentry_core.models.enums import Severity
|
|
@@ -91,8 +91,8 @@ class CsvDataHandle:
|
|
|
91
91
|
"""CSV 数据句柄:pyarrow 流式读取 + duckdb 只读聚合。"""
|
|
92
92
|
|
|
93
93
|
def __init__(self, spec: DataSourceSpec) -> None:
|
|
94
|
-
if spec.path is None:
|
|
95
|
-
raise DataSourceNotFoundError("csv source requires a path")
|
|
94
|
+
if spec.path is None or isinstance(spec.path, str):
|
|
95
|
+
raise DataSourceNotFoundError("csv source requires a local file path")
|
|
96
96
|
self._spec = spec
|
|
97
97
|
self._path = spec.path
|
|
98
98
|
self._encoding = _detect_encoding(spec.path, override=spec.options.get("encoding"))
|
|
@@ -108,7 +108,7 @@ class CsvDataHandle:
|
|
|
108
108
|
return self._spec.source_type
|
|
109
109
|
|
|
110
110
|
@property
|
|
111
|
-
def source_path(self) -> Path | None:
|
|
111
|
+
def source_path(self) -> Path | str | None:
|
|
112
112
|
return self._path
|
|
113
113
|
|
|
114
114
|
@property
|
|
@@ -286,6 +286,19 @@ class CsvDataHandle:
|
|
|
286
286
|
self._executor.close()
|
|
287
287
|
self._closed = True
|
|
288
288
|
|
|
289
|
+
def content_fingerprint(self) -> str:
|
|
290
|
+
"""内容指纹(Step 55):文件源 = 文件 SHA-256(Step 53 调度哈希语义)。"""
|
|
291
|
+
if not isinstance(self._path, Path):
|
|
292
|
+
raise ConnectorError("csv fingerprint requires a local file path")
|
|
293
|
+
return _file_sha256(self._path)
|
|
294
|
+
|
|
295
|
+
def stats_fingerprint(self) -> str:
|
|
296
|
+
"""统计层指纹(Step 58):本地文件源不参与两层指纹——调度器仅对
|
|
297
|
+
远程源(PG/MySQL/云文件)调用,本地文件沿用 Step 53 单层语义。"""
|
|
298
|
+
raise NotImplementedError(
|
|
299
|
+
"local file handles do not participate in two-layer fingerprinting"
|
|
300
|
+
)
|
|
301
|
+
|
|
289
302
|
|
|
290
303
|
def _file_sha256(path: Path) -> str:
|
|
291
304
|
"""流式文件 SHA-256(≤1GB 全量,大文件走 sampled 档)。"""
|
|
@@ -303,7 +316,12 @@ class CsvConnector:
|
|
|
303
316
|
display_name = "CSV"
|
|
304
317
|
|
|
305
318
|
def supports(self, source: DataSourceSpec) -> bool:
|
|
306
|
-
|
|
319
|
+
# Step 57:本地文件连接器排除云 URI(s3:// gs:// az:// 归 RemoteFileConnector)
|
|
320
|
+
return (
|
|
321
|
+
source.source_type == DataSourceType.CSV
|
|
322
|
+
and source.path is not None
|
|
323
|
+
and not isinstance(source.path, str)
|
|
324
|
+
)
|
|
307
325
|
|
|
308
326
|
def open(self, source: DataSourceSpec) -> DataHandle:
|
|
309
327
|
return CsvDataHandle(source)
|
|
@@ -35,7 +35,7 @@ class DuckDBDataHandle(FileDataHandle):
|
|
|
35
35
|
|
|
36
36
|
def __init__(self, spec: DataSourceSpec) -> None:
|
|
37
37
|
super().__init__(spec)
|
|
38
|
-
if spec.path is None or not spec.path.is_file():
|
|
38
|
+
if spec.path is None or isinstance(spec.path, str) or not spec.path.is_file():
|
|
39
39
|
raise DataSourceNotFoundError(f"duckdb file not found: {spec.path}")
|
|
40
40
|
if spec.table_name is None:
|
|
41
41
|
raise DataSourceNotFoundError("duckdb connector requires table_name")
|
{datasentry_core-0.5.1 → datasentry_core-0.7.0}/src/datasentry_core/connectors/file_based.py
RENAMED
|
@@ -12,6 +12,7 @@ import hashlib
|
|
|
12
12
|
import json
|
|
13
13
|
from collections.abc import Iterator
|
|
14
14
|
from pathlib import Path
|
|
15
|
+
from typing import ClassVar
|
|
15
16
|
|
|
16
17
|
import pyarrow as pa
|
|
17
18
|
import pyarrow.compute as pc
|
|
@@ -24,15 +25,33 @@ from datasentry_core.connectors.base import (
|
|
|
24
25
|
SamplingMethod,
|
|
25
26
|
SchemaInfo,
|
|
26
27
|
)
|
|
27
|
-
from datasentry_core.connectors.errors import DataSourceNotFoundError
|
|
28
|
+
from datasentry_core.connectors.errors import ConnectorError, DataSourceNotFoundError
|
|
28
29
|
from datasentry_core.connectors.spec import DataSourceSpec, DataSourceType
|
|
29
30
|
from datasentry_core.engine import DuckDBExecutor
|
|
31
|
+
from datasentry_core.engine.base import SetupExecutor
|
|
30
32
|
from datasentry_core.models.enums import Severity
|
|
31
33
|
from datasentry_core.models.fingerprint import DatasetFingerprint
|
|
32
34
|
|
|
33
35
|
FORMULA_INJECTION_PREFIXES: tuple[str, ...] = ("=", "+", "-", "@", "\t", "\r")
|
|
34
36
|
_WARNING_CAP = 100
|
|
35
37
|
|
|
38
|
+
#: DuckDB DESCRIBE 物理类型 → 规范名(Step 55:检测器/画像的类型集合按规范名精确匹配)
|
|
39
|
+
_TYPE_ALIASES = {
|
|
40
|
+
"TIMESTAMP WITH TIME ZONE": "TIMESTAMPTZ",
|
|
41
|
+
"TIMESTAMP WITHOUT TIME ZONE": "TIMESTAMP",
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _normalize_physical_type(typ: str) -> str:
|
|
46
|
+
"""物理类型归一化:DECIMAL(n,p) → DECIMAL;别名时间类型 → 规范名。
|
|
47
|
+
|
|
48
|
+
PG/Parquet 等源的 DECIMAL(10,2) 与 DuckDB 原生 TIMESTAMP WITH TIME ZONE
|
|
49
|
+
直接进 schema 时无法命中检测器/画像的精确类型集合(Step 55 实测发现)。
|
|
50
|
+
"""
|
|
51
|
+
if typ.startswith("DECIMAL(") and typ.endswith(")"):
|
|
52
|
+
return "DECIMAL"
|
|
53
|
+
return _TYPE_ALIASES.get(typ, typ)
|
|
54
|
+
|
|
36
55
|
|
|
37
56
|
def _schema_hash(column_signature: list[tuple[str, str]]) -> str:
|
|
38
57
|
"""schema_hash = 列签名规范 JSON 的 SHA-256(19.1)。"""
|
|
@@ -52,12 +71,15 @@ def _file_sha256(path: Path) -> str:
|
|
|
52
71
|
class FileDataHandle:
|
|
53
72
|
"""文件型数据句柄基类(DataHandle 协议共享实现)。"""
|
|
54
73
|
|
|
74
|
+
#: 需要文件路径的子类默认 True;无文件源的子类(如 PostgreSQL)置 False
|
|
75
|
+
requires_path: ClassVar[bool] = True
|
|
76
|
+
|
|
55
77
|
def __init__(self, spec: DataSourceSpec) -> None:
|
|
56
|
-
if spec.path is None:
|
|
57
|
-
raise DataSourceNotFoundError(f"{type(self).__name__} requires a path")
|
|
58
78
|
self._spec = spec
|
|
59
79
|
self._path = spec.path
|
|
60
|
-
self.
|
|
80
|
+
if self.requires_path and spec.path is None:
|
|
81
|
+
raise DataSourceNotFoundError(f"{type(self).__name__} requires a path")
|
|
82
|
+
self._executor: SetupExecutor = DuckDBExecutor()
|
|
61
83
|
self._view_ready = False
|
|
62
84
|
self._schema_info: SchemaInfo | None = None
|
|
63
85
|
self._warnings: list[LoadWarning] | None = None
|
|
@@ -68,7 +90,7 @@ class FileDataHandle:
|
|
|
68
90
|
return self._spec.source_type
|
|
69
91
|
|
|
70
92
|
@property
|
|
71
|
-
def source_path(self) -> Path | None:
|
|
93
|
+
def source_path(self) -> Path | str | None:
|
|
72
94
|
return self._path
|
|
73
95
|
|
|
74
96
|
@property
|
|
@@ -99,7 +121,7 @@ class FileDataHandle:
|
|
|
99
121
|
types = table.column(1).to_pylist()
|
|
100
122
|
self._schema_info = SchemaInfo(
|
|
101
123
|
columns=[
|
|
102
|
-
ColumnInfo(name=str(name), physical_type=str(typ))
|
|
124
|
+
ColumnInfo(name=str(name), physical_type=_normalize_physical_type(str(typ)))
|
|
103
125
|
for name, typ in zip(names, types, strict=True)
|
|
104
126
|
]
|
|
105
127
|
)
|
|
@@ -152,6 +174,8 @@ class FileDataHandle:
|
|
|
152
174
|
file_sha256: str | None = None
|
|
153
175
|
content_sample_hash: str | None = None
|
|
154
176
|
if mode == "full":
|
|
177
|
+
if not isinstance(self._path, Path):
|
|
178
|
+
raise ConnectorError("file fingerprint requires a local file path")
|
|
155
179
|
file_sha256 = _file_sha256(self._path)
|
|
156
180
|
elif mode == "sampled":
|
|
157
181
|
head = self._executor.execute("SELECT * FROM data LIMIT 1000")
|
|
@@ -173,6 +197,24 @@ class FileDataHandle:
|
|
|
173
197
|
content_sample_hash=content_sample_hash,
|
|
174
198
|
)
|
|
175
199
|
|
|
200
|
+
def content_fingerprint(self) -> str:
|
|
201
|
+
"""内容指纹(Step 55):文件源 = 文件 SHA-256(Step 53 调度哈希语义)。"""
|
|
202
|
+
if not isinstance(self._path, Path):
|
|
203
|
+
raise ConnectorError("file fingerprint requires a local file path")
|
|
204
|
+
return _file_sha256(self._path)
|
|
205
|
+
|
|
206
|
+
def stats_fingerprint(self) -> str:
|
|
207
|
+
"""统计层指纹(Step 58,ADR-058):schema_hash + row_count 组合。
|
|
208
|
+
|
|
209
|
+
零内容读取:DESCRIBE(目录查询,PG/MySQL 为扩展目录、云文件为
|
|
210
|
+
parquet footer/CSV 元数据)+ count(远程库为 count 扫描,云文件
|
|
211
|
+
为 footer 行数)。调度两层快速失效第一层:统计层一致才进内容层。
|
|
212
|
+
"""
|
|
213
|
+
self._check_open()
|
|
214
|
+
signature = [(c.name, c.physical_type) for c in self.schema().columns]
|
|
215
|
+
row_count = self.count_rows()
|
|
216
|
+
return hashlib.sha256(f"{_schema_hash(signature)}|{row_count}".encode()).hexdigest()
|
|
217
|
+
|
|
176
218
|
def warnings(self) -> list[LoadWarning]:
|
|
177
219
|
"""公式注入标记(11.7):遍历批处理扫描字符串列,前 _WARNING_CAP 条。"""
|
|
178
220
|
self._check_open()
|
|
@@ -59,7 +59,11 @@ class JsonlConnector:
|
|
|
59
59
|
display_name = "JSONL"
|
|
60
60
|
|
|
61
61
|
def supports(self, source: DataSourceSpec) -> bool:
|
|
62
|
-
return
|
|
62
|
+
return (
|
|
63
|
+
source.source_type == DataSourceType.JSONL
|
|
64
|
+
and source.path is not None
|
|
65
|
+
and not isinstance(source.path, str)
|
|
66
|
+
)
|
|
63
67
|
|
|
64
68
|
def open(self, source: DataSourceSpec) -> JsonlDataHandle:
|
|
65
69
|
return JsonlDataHandle(source)
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
"""MySQL 数据源连接器(Step 56,V5:多数据源第三落点,ADR-056)。
|
|
2
|
+
|
|
3
|
+
经 DuckDB mysql 扩展(LOAD mysql)只读 ATTACH 远程库
|
|
4
|
+
(TYPE mysql, READ_ONLY),注册只读 data 视图后复用 FileDataHandle
|
|
5
|
+
共享实现(schema/read_sample/sql_aggregate/count_rows/warnings)。
|
|
6
|
+
表名必填;MySQL 无独立 schema 层(database 已在 DSN 内)。
|
|
7
|
+
|
|
8
|
+
凭据红线(沿用 Step 55 全套):
|
|
9
|
+
- DSN 只存在于内存态(spec.options["dsn"])或经 connection_ref
|
|
10
|
+
指向的环境变量(如 DATASENTRY_MYSQL_DSN),不落库、不进日志/evidence/报告
|
|
11
|
+
- mysql 扩展连接失败时错误文本会原样回显 DSN 参数串
|
|
12
|
+
("Failed to connect to MySQL database with parameters ..."):
|
|
13
|
+
_RedactingExecutor 净化(DSN 整体 → mysql://***、密码 → ***)后转
|
|
14
|
+
ConnectorError 传播,CLI 错误面 / REST 400 只见净化文本
|
|
15
|
+
- DuckDB 1.5.x mysql 扩展已知 bug:视图之上的聚合(count/groupby)触发
|
|
16
|
+
聚合下推时报内部绑定错误(直连 attach 正常,PG 扩展无此问题)。连接器
|
|
17
|
+
_ensure_view 统一 SET mysql_aggregate_pushdown_enabled = false 绕过:
|
|
18
|
+
聚合改在 DuckDB 本地执行,语义不变(ADR-056)
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import hashlib
|
|
24
|
+
import re
|
|
25
|
+
from collections.abc import Iterator
|
|
26
|
+
from typing import ClassVar
|
|
27
|
+
from urllib.parse import urlparse
|
|
28
|
+
|
|
29
|
+
import duckdb
|
|
30
|
+
import pyarrow as pa
|
|
31
|
+
|
|
32
|
+
from datasentry_core.connectors.base import FingerprintMode, FrameBatch
|
|
33
|
+
from datasentry_core.connectors.errors import ConnectorError, DataSourceNotFoundError
|
|
34
|
+
from datasentry_core.connectors.file_based import FileDataHandle, _schema_hash
|
|
35
|
+
from datasentry_core.connectors.spec import DataSourceSpec, DataSourceType
|
|
36
|
+
from datasentry_core.engine import DuckDBExecutor
|
|
37
|
+
from datasentry_core.engine.base import SqlParams
|
|
38
|
+
from datasentry_core.models.fingerprint import DatasetFingerprint
|
|
39
|
+
from datasentry_core.secrets import lookup_secret
|
|
40
|
+
|
|
41
|
+
#: ATTACH 目标别名(连接器内部名,避开用户数据命名冲突概率极低)
|
|
42
|
+
_DB_ALIAS = "my"
|
|
43
|
+
|
|
44
|
+
#: 加载 mysql 扩展失败的提示(缺网络/离线/发行版未带扩展)
|
|
45
|
+
_MYSQL_EXTENSION_HINT = (
|
|
46
|
+
"failed to load duckdb mysql extension: network access is required on first use "
|
|
47
|
+
"(or the extension must be pre-provisioned for offline environments); "
|
|
48
|
+
"original error: {}"
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
#: 连接失败提示(凭据已净化)
|
|
52
|
+
_MYSQL_CONNECT_HINT = "mysql connection failed (check host/port/database/credentials): {}"
|
|
53
|
+
|
|
54
|
+
#: KV 形式 DSN 的密码键(mysql 扩展用 passwd 而非 password)
|
|
55
|
+
_KV_PASSWORD_PATTERN = re.compile(r"(passwd=\S+)")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _sql_string_literal(value: str) -> str:
|
|
59
|
+
return "'" + value.replace("'", "''") + "'"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _ident(value: str) -> str:
|
|
63
|
+
"""双引号标识符转义(防表名注入)。"""
|
|
64
|
+
return '"' + value.replace('"', '""') + '"'
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def redact_credentials(text: str, dsn: str) -> str:
|
|
68
|
+
"""净化错误文本:DSN 整体与密码不对外(凭据红线,可单测)。
|
|
69
|
+
|
|
70
|
+
mysql 扩展错误文本回显两种形式:URL 形式(mysql://user:pass@host/db)
|
|
71
|
+
与 KV 形式(host=... passwd=...)——两种都要打码。
|
|
72
|
+
"""
|
|
73
|
+
redacted = text.replace(dsn, "mysql://***")
|
|
74
|
+
redacted = _KV_PASSWORD_PATTERN.sub("passwd=***", redacted)
|
|
75
|
+
try:
|
|
76
|
+
parsed = urlparse(dsn)
|
|
77
|
+
except ValueError:
|
|
78
|
+
return redacted
|
|
79
|
+
if parsed.password:
|
|
80
|
+
redacted = redacted.replace(parsed.password, "***")
|
|
81
|
+
return redacted
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _resolve_dsn(spec: DataSourceSpec) -> str:
|
|
85
|
+
"""DSN 解析(Step 59,ADR-059):options["dsn"] 内存态 > connection_ref
|
|
86
|
+
统一解析链(进程环境变量 > secrets.env,`lookup_secret`)。"""
|
|
87
|
+
dsn = spec.options.get("dsn")
|
|
88
|
+
if isinstance(dsn, str) and dsn:
|
|
89
|
+
return dsn
|
|
90
|
+
if spec.connection_ref:
|
|
91
|
+
ref = spec.connection_ref
|
|
92
|
+
dsn = lookup_secret(ref)
|
|
93
|
+
if dsn:
|
|
94
|
+
return dsn
|
|
95
|
+
raise DataSourceNotFoundError(f"mysql connection_ref not set (env or secrets.env): {ref}")
|
|
96
|
+
raise DataSourceNotFoundError(
|
|
97
|
+
"mysql connector requires a dsn (options['dsn'] or connection_ref env/secrets)"
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class _RedactingExecutor:
|
|
102
|
+
"""MySQL 句柄专用执行器包装:DuckDB 异常净化后转 ConnectorError 传播。
|
|
103
|
+
|
|
104
|
+
只读守卫(sql_guard)在 DuckDBExecutor 内先于执行触发,其异常
|
|
105
|
+
(UnsafeSqlError 等连接器族)原样透传;查询期远程错误(断连等)
|
|
106
|
+
同样净化,避免 DSN 出现在 DetectorRun.error / 调度 summary 等
|
|
107
|
+
落库面。
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
def __init__(self, inner: DuckDBExecutor, dsn: str) -> None:
|
|
111
|
+
self._inner = inner
|
|
112
|
+
self._dsn = dsn
|
|
113
|
+
|
|
114
|
+
def execute_setup(self, sql: str) -> None:
|
|
115
|
+
try:
|
|
116
|
+
self._inner.execute_setup(sql)
|
|
117
|
+
except duckdb.Error as exc:
|
|
118
|
+
raise ConnectorError(redact_credentials(str(exc), self._dsn)) from exc
|
|
119
|
+
|
|
120
|
+
def register(self, name: str, obj: object) -> None:
|
|
121
|
+
self._inner.register(name, obj)
|
|
122
|
+
|
|
123
|
+
def execute(self, sql: str, params: SqlParams = None) -> pa.Table:
|
|
124
|
+
try:
|
|
125
|
+
return self._inner.execute(sql, params)
|
|
126
|
+
except duckdb.Error as exc:
|
|
127
|
+
raise ConnectorError(redact_credentials(str(exc), self._dsn)) from exc
|
|
128
|
+
|
|
129
|
+
def execute_stream(self, sql: str, batch_size: int = 65536) -> Iterator[pa.RecordBatch]:
|
|
130
|
+
try:
|
|
131
|
+
yield from self._inner.execute_stream(sql, batch_size)
|
|
132
|
+
except duckdb.Error as exc:
|
|
133
|
+
raise ConnectorError(redact_credentials(str(exc), self._dsn)) from exc
|
|
134
|
+
|
|
135
|
+
def close(self) -> None:
|
|
136
|
+
self._inner.close()
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class MySQLDataHandle(FileDataHandle):
|
|
140
|
+
"""MySQL 远程表数据句柄(只读,表名必填,无文件字节)。"""
|
|
141
|
+
|
|
142
|
+
requires_path: ClassVar[bool] = False
|
|
143
|
+
|
|
144
|
+
def __init__(self, spec: DataSourceSpec) -> None:
|
|
145
|
+
super().__init__(spec)
|
|
146
|
+
if spec.table_name is None:
|
|
147
|
+
raise DataSourceNotFoundError("mysql connector requires table_name")
|
|
148
|
+
self._dsn = _resolve_dsn(spec)
|
|
149
|
+
self._table_name = spec.table_name
|
|
150
|
+
assert isinstance(self._executor, DuckDBExecutor)
|
|
151
|
+
self._executor = _RedactingExecutor(self._executor, self._dsn)
|
|
152
|
+
|
|
153
|
+
@property
|
|
154
|
+
def table_name(self) -> str | None:
|
|
155
|
+
return self._table_name
|
|
156
|
+
|
|
157
|
+
def _ensure_view(self) -> None:
|
|
158
|
+
if self._view_ready:
|
|
159
|
+
return
|
|
160
|
+
try:
|
|
161
|
+
# DuckDB 1.5.x mysql 扩展绑定 bug:SQL 经「mysql-attach 之上的视图」触发
|
|
162
|
+
# 聚合下推时报 "Failed to bind column reference"(直连 attach 无此问题)。
|
|
163
|
+
# 关闭聚合下推后聚合在 DuckDB 本地计算(结果语义不变,仅少一次远端
|
|
164
|
+
# 聚合执行),视图 + count/groupby 全部正常(ADR-056 已知问题记录)。
|
|
165
|
+
self._executor.execute_setup("SET mysql_aggregate_pushdown_enabled = false")
|
|
166
|
+
self._executor.execute_setup("LOAD mysql")
|
|
167
|
+
except ConnectorError as exc:
|
|
168
|
+
raise ConnectorError(_MYSQL_EXTENSION_HINT.format(exc)) from exc
|
|
169
|
+
try:
|
|
170
|
+
self._executor.execute_setup(
|
|
171
|
+
f"ATTACH {_sql_string_literal(self._dsn)} AS {_DB_ALIAS} (TYPE mysql, READ_ONLY)"
|
|
172
|
+
)
|
|
173
|
+
except ConnectorError as exc:
|
|
174
|
+
raise ConnectorError(_MYSQL_CONNECT_HINT.format(exc)) from exc
|
|
175
|
+
table_ident = f"{_DB_ALIAS}.{_ident(self._table_name)}"
|
|
176
|
+
try:
|
|
177
|
+
self._executor.execute(f"SELECT 1 FROM {table_ident} LIMIT 0")
|
|
178
|
+
except ConnectorError as exc:
|
|
179
|
+
raise DataSourceNotFoundError(f"mysql table not found: {self._table_name}") from exc
|
|
180
|
+
self._executor.execute_setup(f"CREATE OR REPLACE VIEW data AS SELECT * FROM {table_ident}")
|
|
181
|
+
self._view_ready = True
|
|
182
|
+
|
|
183
|
+
def read_batches(self, batch_size: int = 65536) -> Iterator[FrameBatch]:
|
|
184
|
+
self._check_open()
|
|
185
|
+
if batch_size < 1:
|
|
186
|
+
raise ValueError("batch_size must be >= 1")
|
|
187
|
+
self._ensure_view()
|
|
188
|
+
row_offset = 0
|
|
189
|
+
for idx, batch in enumerate(
|
|
190
|
+
self._executor.execute_stream("SELECT * FROM data", batch_size)
|
|
191
|
+
):
|
|
192
|
+
table = pa.Table.from_batches([batch])
|
|
193
|
+
yield FrameBatch(
|
|
194
|
+
source=self._spec,
|
|
195
|
+
table=table,
|
|
196
|
+
batch_index=idx,
|
|
197
|
+
row_offset=row_offset,
|
|
198
|
+
)
|
|
199
|
+
row_offset += table.num_rows
|
|
200
|
+
|
|
201
|
+
def content_fingerprint(self) -> str:
|
|
202
|
+
"""内容指纹(Step 56):单查询全表哈希(行序无关、NULL 不折叠)。
|
|
203
|
+
|
|
204
|
+
语义与 PostgresDataHandle 一致(Step 55 模式):逐行
|
|
205
|
+
md5(concat_ws(chr(31), coalesce(col::VARCHAR, chr(0))...)) +
|
|
206
|
+
排序聚合 string_agg;并入 schema_hash 与行数。
|
|
207
|
+
"""
|
|
208
|
+
self._check_open()
|
|
209
|
+
self._ensure_view()
|
|
210
|
+
signature = [(c.name, c.physical_type) for c in self.schema().columns]
|
|
211
|
+
schema_hash = _schema_hash(signature)
|
|
212
|
+
exprs = ", ".join(
|
|
213
|
+
f"coalesce(cast({_ident(name)} AS VARCHAR), chr(0))" for name, _typ in signature
|
|
214
|
+
)
|
|
215
|
+
table = self._executor.execute(
|
|
216
|
+
"SELECT count(*) AS n, md5(string_agg(rh, '' ORDER BY rh)) AS h FROM "
|
|
217
|
+
f"(SELECT md5(concat_ws(chr(31), {exprs})) AS rh FROM data) t"
|
|
218
|
+
)
|
|
219
|
+
row_count = table.column(0).to_pylist()[0]
|
|
220
|
+
rows_hash = table.column(1).to_pylist()[0]
|
|
221
|
+
assert isinstance(row_count, int)
|
|
222
|
+
digest = hashlib.sha256(f"{schema_hash}|{row_count}|{rows_hash or ''}".encode()).hexdigest()
|
|
223
|
+
return digest
|
|
224
|
+
|
|
225
|
+
def fingerprint(self, mode: FingerprintMode = "full") -> DatasetFingerprint:
|
|
226
|
+
"""MySQL 无文件字节:file_sha256=None,内容指纹放 content_sample_hash。"""
|
|
227
|
+
self._check_open()
|
|
228
|
+
signature = [(c.name, c.physical_type) for c in self.schema().columns]
|
|
229
|
+
row_count = self.count_rows()
|
|
230
|
+
content_hash = self.content_fingerprint() if mode != "metadata_only" else None
|
|
231
|
+
return DatasetFingerprint(
|
|
232
|
+
dataset_id=self._spec.options.get("dataset_id", ""),
|
|
233
|
+
fingerprint_type=mode,
|
|
234
|
+
file_sha256=None,
|
|
235
|
+
schema_hash=_schema_hash(signature),
|
|
236
|
+
row_count=row_count,
|
|
237
|
+
column_count=len(signature),
|
|
238
|
+
column_signature=signature,
|
|
239
|
+
content_sample_hash=content_hash,
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
class MySQLConnector:
|
|
244
|
+
"""MySQL 数据源连接器(Step 56,V5,ADR-056)。"""
|
|
245
|
+
|
|
246
|
+
connector_id = "mysql"
|
|
247
|
+
display_name = "MySQL"
|
|
248
|
+
|
|
249
|
+
def supports(self, source: DataSourceSpec) -> bool:
|
|
250
|
+
if source.source_type != DataSourceType.MYSQL:
|
|
251
|
+
return False
|
|
252
|
+
dsn = source.options.get("dsn")
|
|
253
|
+
return (isinstance(dsn, str) and bool(dsn)) or source.connection_ref is not None
|
|
254
|
+
|
|
255
|
+
def open(self, source: DataSourceSpec) -> MySQLDataHandle:
|
|
256
|
+
if source.table_name is None:
|
|
257
|
+
raise DataSourceNotFoundError(
|
|
258
|
+
"mysql connector requires table_name (e.g. scan --table <table>)"
|
|
259
|
+
)
|
|
260
|
+
return MySQLDataHandle(source)
|
|
261
|
+
|
|
262
|
+
def close(self) -> None:
|
|
263
|
+
pass
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
__all__ = ["MySQLConnector", "MySQLDataHandle", "redact_credentials"]
|
|
@@ -52,7 +52,11 @@ class ParquetConnector:
|
|
|
52
52
|
display_name = "Parquet"
|
|
53
53
|
|
|
54
54
|
def supports(self, source: DataSourceSpec) -> bool:
|
|
55
|
-
return
|
|
55
|
+
return (
|
|
56
|
+
source.source_type == DataSourceType.PARQUET
|
|
57
|
+
and source.path is not None
|
|
58
|
+
and not isinstance(source.path, str)
|
|
59
|
+
)
|
|
56
60
|
|
|
57
61
|
def open(self, source: DataSourceSpec) -> ParquetDataHandle:
|
|
58
62
|
return ParquetDataHandle(source)
|