Lakehouse Doctor (lh-doctor)
简体中文 | English
面向 Apache Doris、StarRocks、Apache Paimon 与 Flink SQL 的离线 SQL 静态审计、字段级血缘分析、Query Profile 诊断、DQC 生成工具与 MCP 服务。
lakehouse-doctor 无需连接真实数据库集群,通过解析项目中的 CREATE TABLE 建表语句构建内存元数据目录(Catalog),在此基础上执行 Schema-Aware AST 静态检查、跨文件字段血缘影响面追踪与执行计划剖析。
核心功能
catalog:从 Doris、StarRocks、Paimon 与 Flink SQL 建表及ALTER TABLE语句构建离线目录(字段类型、主/分区/分桶键与引擎属性),可选--dsn只读拉取 FE 缺失 DDL。audit:兼容调度宏变量(${biz_date}、#{dt}、{{ ds }}、{{ ref(...) }})与 GBK/UTF-8 文件,运行语法/DDL/DML 静态规则(LH001、LH101–LH212),支持行内豁免注释、归一化健康评分与文本/JSON/SARIF 输出(可选--explain做 FE 只读验证LE001)。lineage/impact:穿透视图、CTE、SELECT *与UNION ALL构建表级和字段级血缘(标注DIRECT/DERIVED/FILTER),支持--upstream逆向溯源,导出 Mermaid、JSON 或交互式 HTML。profile:解析 Doris(2.x/3.x/4.x 与传统节点)及 StarRocks Query Profile(LP001–LP009),定位倾斜、阻塞、错位 Join 与内存/Spill 问题,支持--diff前后对比、--format ai-json压缩与 HTML 火焰图。dqc:结合主键约束与血缘依赖,一键生成新鲜度、主键唯一性、非空、非负指标及上下游行数对账 SQL。mcp&skills/:内置标准库 stdio MCP Server(lh-doctor mcp)与开放标准 Agent Skills。
快速上手
方式一:发给你的 AI Agent 自动装好(推荐)
直接复制下面这段话发给 Claude Code / Codex / WorkBuddy / Qoder / DeepSeek Harness 等支持 MCP 和 Skills 的编程助手,让它直接从 PyPI 安装、配好 MCP 并跑通首轮体检:
请帮我把 lakehouse-doctor 接入到当前环境(仓库地址:https://github.com/tolgerx/lakehouse-doctor):
1. 直接从 PyPI 安装(`pip install lakehouse-doctor`,如需 `--dsn` 只读连 FE 则装 `pip install "lakehouse-doctor[online]"`;也可使用 `uvx lakehouse-doctor` 免安装运行);
2. 将 `lh-doctor mcp`(或 `uvx lakehouse-doctor mcp`)配进我的 MCP Server 配置,如需使用内置 `skills/*` 可从仓库拉取并关联到 Skills 目录;
3. 完成后调用 `lh-doctor audit`(或 MCP 工具 `audit_sql`)对我当前项目的 SQL 目录做一次检查并汇报结果。
方式二:命令行安装
# 推荐直接从 PyPI 安装
pip install lakehouse-doctor
# 如需启用只读 FE 建表补全与在线 EXPLAIN(--dsn / --explain)
pip install "lakehouse-doctor[online]"
# 或使用 uvx 免安装运行(MCP 可直接配 command: "uvx", args: ["lakehouse-doctor", "mcp"])
uvx lakehouse-doctor --help
如需运行仓库内置的 4 层 Paimon + Doris 示例项目(examples/ecommerce_lakehouse)与示例 Profile(examples/profiles):
git clone https://github.com/tolgerx/lakehouse-doctor.git
cd lakehouse-doctor
lh-doctor catalog examples/ecommerce_lakehouse
lh-doctor audit examples/ecommerce_lakehouse
lh-doctor impact examples/ecommerce_lakehouse --column ods_orders.pay_amount
lh-doctor impact examples/ecommerce_lakehouse --column ads_gmv_dashboard.total_gmv --upstream
lh-doctor lineage examples/ecommerce_lakehouse --format html --output lineage.html
lh-doctor profile examples/profiles/doris_skewed_broadcast_join.txt
lh-doctor profile examples/profiles/doris_skewed_broadcast_join.txt --diff examples/profiles/doris_skewed_broadcast_join_tuned.txt
lh-doctor dqc examples/ecommerce_lakehouse --table dwd_order_detail
输出示例
lh-doctor audit examples/ecommerce_lakehouse
Lakehouse Doctor Audit (files: 8, catalog tables: 5)
Summary: 3 CRITICAL | 9 WARNING | 1 INFO
Health Score: 8/100 (Grade F)
[CRITICAL] LH203 dml/01_dwd_load_order_detail.sql:13 - Implicit type cast in JOIN/WHERE comparison
SQL: ON o.user_id = u.id
Why: Implicit cast in `o.user_id = u.id`: `ods_orders.user_id` (VARCHAR(64)) vs `ods_users.id` (BIGINT).
Fix: Align column types in DDL or explicitly `CAST(o.user_id AS BIGINT)` before joining.
[CRITICAL] LH201 dml/01_dwd_load_order_detail.sql:15 - Partition pruning disabled by function wrap
SQL: WHERE DATE_FORMAT(o.dt, '%Y-%m') = '2026-11'
Why: Partition column `o.dt` is wrapped in `TIME_TO_STR(CAST(o.dt AS TIMESTAMP), '%Y-%m')`, disabling partition pruning.
Fix: Rewrite using a half-open range predicate directly on `o.dt` (e.g. `o.dt >= '2026-11-01' AND o.dt < '2026-12-01'`).
[CRITICAL] LH202 dml/03_ads_gmv_report.sql:4 - Missing partition filter on partitioned table
SQL: SELECT * FROM dws_user_order_1d
Why: Partitioned table `dws_user_order_1d` (partitioned by `dt`) is scanned without a partition predicate.
Fix: Add a filter on `dt` in `WHERE` (or `JOIN ... ON` for outer-joined tables).
lh-doctor impact examples/ecommerce_lakehouse --column ods_orders.pay_amount
Impact Analysis (downstream): ods_orders.pay_amount -> 3 affected target(s)
[Hop 1 | DWD] dwd_order_detail.pay_amount [DIRECT]
Path: ods_orders.pay_amount -> dwd_order_detail.pay_amount
[Hop 2 | DWS] dws_user_order_1d.total_gmv [DERIVED]
Path: ods_orders.pay_amount -> dwd_order_detail.pay_amount -> dws_user_order_1d.total_gmv
[Hop 3 | ADS] ads_gmv_dashboard.total_gmv [DERIVED]
Path: ods_orders.pay_amount -> dwd_order_detail.pay_amount -> dws_user_order_1d.total_gmv -> ads_gmv_dashboard.total_gmv
lh-doctor profile examples/profiles/doris_skewed_broadcast_join.txt
Lakehouse Doctor Query Profile (engine: DORIS, total: 14.82s, operators: 5, findings: 6)
Query ID: 9f4c2b8a-7e11-4d3a-9c52-88a1e72b1104
[CRITICAL] LP001 Fragment 1 / HASH_JOIN_OPERATOR (id=4) - Severe instance data skew
Why: Operator `HASH_JOIN_OPERATOR (id=4)` exhibits 7.9x time skew (max=9820ms vs avg=1240ms) and 8.7x row skew.
[CRITICAL] LP002 Fragment 1 / HASH_JOIN_OPERATOR (id=4) - Dominant execution bottleneck operator
Why: Operator `HASH_JOIN_OPERATOR (id=4)` accounts for 64.8% of operator max time (max=9820ms).
[CRITICAL] LP004 Fragment 1 / HASH_JOIN_OPERATOR (id=4) - Heavy BROADCAST join memory/fan-out risk
Why: BROADCAST join `HASH_JOIN_OPERATOR (id=4)` reached 2.85 GB peak memory and processed 84.5M probe/read rows.
lh-doctor dqc examples/ecommerce_lakehouse --table dwd_order_detail
SELECT
'DQC_PK_UNIQUE_DWD_ORDER_DETAIL' AS check_id,
'dwd_order_detail' AS table_name,
COUNT(*) - COUNT(DISTINCT CONCAT_WS('#', order_id, dt)) AS metric_value,
CASE WHEN COUNT(*) = COUNT(DISTINCT CONCAT_WS('#', order_id, dt)) THEN 'PASS' ELSE 'FAIL' END AS status
FROM dwd_order_detail WHERE dt = '${biz_date}';
规则清单
| 规则编号 | 级别 | 适用范围 | 说明 |
|---|---|---|---|
LH001 |
CRITICAL |
语法 | SQL 解析失败或存在未识别的关键字/拼写错误 |
LH101 |
WARNING |
DDL (Doris/SR) | UNIQUE KEY 表未开启 enable_unique_key_merge_on_write = "true" |
LH102 |
WARNING |
DDL (Doris/SR) | BUCKETS > 64(Tablet 膨胀)或非维表使用 BUCKETS 1 |
LH103 |
WARNING |
DDL | 分层表(ods_/dwd_/dws_/ads_)缺失分区定义 |
LH104 |
WARNING |
DDL (Paimon) | 缺失 changelog-producer 或 snapshot.time-retained / snapshot.num-retained.max |
LH105 |
INFO |
DDL | 缺失表注释或超过 50% 字段缺失 COMMENT |
LH106 |
WARNING |
DDL (Doris/SR) | DISTRIBUTED BY HASH(...) 仅使用分区/日期/低基数列 |
LH201 |
CRITICAL |
DML | WHERE/JOIN ON 中对分区字段套用函数或算术运算导致分区裁剪失效 |
LH202 |
CRITICAL |
DML | 扫描已注册的分区表时未过滤任何分区字段 |
LH203 |
CRITICAL |
DML | JOIN/WHERE 等值比较存在跨表不兼容类型的隐式转换 |
LH204 |
WARNING |
DML | ETL 查询中使用 SELECT *(不含 COUNT(*)) |
LH205 |
WARNING |
DML | INSERT INTO / INSERT OVERWRITE 未显式指定目标列名 |
LH206 |
CRITICAL |
DML | 无约束 CROSS JOIN、缺失 ON/USING 或 ON 1 = 1 笛卡尔积风险 |
LH207 |
WARNING |
DML | 顶层 ORDER BY 未配合 LIMIT |
LH208 |
WARNING |
DML | 单条 SELECT 中包含多个 COUNT(DISTINCT ...) |
LH209 |
INFO |
DML | 前导通配符 LIKE '%...' |
LH210 |
CRITICAL |
DML | 引用了表目录中不存在的列名(含带别名列引用) |
LH211 |
CRITICAL/WARNING |
DML | INSERT 目标列名不存在、列数不匹配或按位类型族不兼容 |
LH212 |
WARNING |
DML | 对分区表执行 INSERT OVERWRITE 但未指定 PARTITION (...) 子句 |
LP001 |
CRITICAL/WARNING |
Profile | 实例间时间或行数倾斜度 >= 3x |
LP002 |
CRITICAL/WARNING |
Profile | 主导耗时瓶颈算子(耗时占比 >= 40% 或 >= 5s) |
LP003 |
WARNING |
Profile | 大表 Scan 过滤率过低(读取 >= 10M 行且过滤保留率 < 20%) |
LP004 |
CRITICAL/WARNING |
Profile | BROADCAST Join 内存膨胀或探测行数过高 |
LP005 |
CRITICAL/WARNING |
Profile | 算子峰值内存过高(>= 1.5 GB)或发生磁盘 Spill |
LP006 |
WARNING |
Profile | 网络 Shuffle / Exchange 流量过大(>= 1 GB 或耗时占比 >= 25%) |
LP007 |
CRITICAL/WARNING |
Profile | Pipeline 依赖或 Runtime Filter 等待阻塞(>= 3s 且超过自身执行耗时 2 倍) |
LP008 |
CRITICAL/WARNING |
Profile | Hash Join Build/Probe 大小表颠倒(Build 行数 >= 500K 且超过 Probe 的 2 倍) |
LP009 |
CRITICAL/WARNING |
Profile | 预聚合裁剪率过低(AGG 输入 >= 1M 行且输出保留率 >= 80%) |
LE001 |
CRITICAL |
在线 EXPLAIN | 启用 --dsn --explain 时未通过 FE 只读 EXPLAIN 优化器校验 |
CI 与扩展能力
- GitHub PR 自动门禁:复制
.github/workflows/sql-guard-template.yml即可在 PR 中上传 SARIF 扫描结果并自动评论违规 SQL。 - 项目级配置与行内豁免:运行
lh-doctor init生成.lh-doctor.toml(示例见.lh-doctor.toml.example),可自定义规则开关ignore、严重级别覆盖、max_buckets、分区列名与目录排除exclude;同时支持 SQL 行内豁免注释(-- lh-doctor: disable=LH203、disable-file、disable-next-line、-- noqa)。 - 内置 Agent Skills (
skills/) 与斜杠命令 (.claude/commands/):包含doris-starrocks-modeling、paimon-flink-lakehouse、lakehouse-sql-and-profile-tuning、lakehouse-lineage-and-dqc及/lh-audit、/lh-profile、/lh-impact。 - MCP 工具清单 (
lh-doctor mcp):参考examples/mcp_config.json,暴露audit_sql、inspect_catalog、trace_impact、analyze_profile、compare_profiles、generate_dqc。
开源协议
Metadata
Release files for lakehouse-doctor 0.7.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| lakehouse_doctor-0.7.2.tar.gz | 110.0 kB | With provenance | Details for lakehouse_doctor-0.7.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| lakehouse_doctor-0.7.2-py3-none-any.whl | Python 3 | none | any | Details for lakehouse_doctor-0.7.2-py3-none-any.whl |
Total release size: 199.4 kB
Release files / lakehouse_doctor-0.7.2.tar.gz
| Download URL | lakehouse_doctor-0.7.2.tar.gz |
|---|---|
| Size | 110.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
132f811eb6f6a8df86c2adf487a808221728234b5867214417e74f29c8ffb952
|
|
BLAKE2b-256 checksum How to use checksums |
d8881cb0454c508dcb1bfa2b63c921f6e0d5364da0861f41693332ab26fca8b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.
Transparency logRelease files / lakehouse_doctor-0.7.2-py3-none-any.whl
| Download URL | lakehouse_doctor-0.7.2-py3-none-any.whl |
|---|---|
| Size | 89.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
73d5035ff5807d402204b1bbea5f6a2da486549a8c1ebddceb8d32768788a7e4
|
|
BLAKE2b-256 checksum How to use checksums |
58cf689fbfafbf5a0371f6609c4dc0e1cbef49818b3ebe84315a468866982d00
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.
Transparency logSecurity for lakehouse-doctor 0.7.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for lakehouse_doctor-0.7.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.