Lakehouse Doctor (lh-doctor)
简体中文 | English
面向 Apache Doris / StarRocks 的离线湖仓诊断工具(CLI + stdio MCP + Agent Skills),兼顾 Paimon DDL 检查。
让数仓工程师和 AI Coding Agent(Cursor / Claude Code / Codex)在不暴露生产集群凭据的前提下,就能完成 Schema-Aware SQL 审计、字段影响面追踪与低 Token Query Profile 排障(无论 SQL 存在 Git 还是日常写在网页调度平台里)。
核心能力
- 为 Coding Agent 设计的只读取证接口(
mcp+skills/+ai-json)- 一条命令启动零重依赖 stdio MCP Server(
uvx lakehouse-doctor mcp),内置audit_sql、inspect_catalog、trace_impact、analyze_profile、compare_profiles与rules/catalogresource,支持工具annotations、-32700畸形帧容错与常驻项目缓存。工具结果始终返回文本 JSON,并在协商2025-06-18协议时附带structuredContent。 --format ai-json带硬字符预算裁剪(单份 Profile、Diff 与审计通用),避免几万行 Profile 把上下文窗口打爆;配套 4 套领域skills/与.claude/commands/引导 Agent 按“查证 → 改 SQL → 复审”闭环工作。- 防幻觉
coverage契约:缺失表、歧义表、降级解析、动态模板谓词或被丢弃的 Jinja{% elif / else %}分支均会显式标出complete = false,防止 Agent 把“没扫全”误判成“已通过”。
- 一条命令启动零重依赖 stdio MCP Server(
- Query Profile 离线诊断与对比(
profile)- 零前置依赖(无需 Git、无需 DDL、无需集群权限):直接解析 Doris(2.x/3.x/4.x)或 StarRocks 3.x 导出的 Profile 文本,识别算子倾斜(
LP001)、瓶颈算子(LP002)、大表低过滤率(LP003)、Broadcast 膨胀(LP004)、峰值内存与 Spill(LP005、HashTableMemoryUsage等)及等待阻塞(LP007);跨引擎/无重叠算子--diff会主动禁用误导性加速倍率。
- 零前置依赖(无需 Git、无需 DDL、无需集群权限):直接解析 Doris(2.x/3.x/4.x)或 StarRocks 3.x 导出的 Profile 文本,识别算子倾斜(
- Schema-Aware 离线 SQL 审计与字段影响面(
audit/catalog/impact/lineage)- 解析仓库内
CREATE TABLE、CTAS、乱序ALTER TABLE与USE CATALOG / USE db上下文构建内存 Catalog(DDL 不在仓库时可用可选--dsn按缺失表只读补齐 FE 元数据),捕捉跨表隐式转换(LH203)、分区裁剪失效(LH201)、漏分区过滤(LH202)、SELECT *溢出 /UNION ALL分支错位(LH211)、未知列(LH210)及跨层字段影响面。
- 解析仓库内
适用边界
- 推荐用法:建议作为本地开发自查、Agent 只读取证工具、导出 Profile 初诊与 PR 评论模式(
--fail-on none),而非唯一的强制合并阻断门禁;也请不要让 Agent 通过擅自添加-- lh-doctor: disable来强行“变绿”。 - 元数据来源:DDL 不在仓库里时,可配合只读测试库
--dsn自动按需补齐缺失表。 - Flink SQL / Paimon:基于 Spark/Hive 方言回退与语法归一化,适合表列/结构规范检查,不替代引擎原生的水位线、状态与物理计划验证。
- 增量审计与严格覆盖门禁:DDL 变更会触发多跳下游重审,
--since也会追踪 Git 中删除的 DDL。支持多重集--baseline基线过滤,可传入--strict-coverage在覆盖率不完整时返回失败。
快速上手
方式一:接入 AI 编程助手(MCP + Skills)
把 lh-doctor mcp 注册到支持 stdio MCP 的编程助手配置(参考 examples/mcp_config.json):
{
"mcpServers": {
"lakehouse-doctor": {
"command": "uvx",
"args": ["lakehouse-doctor", "mcp"]
}
}
}
方式二:命令行直接使用(CLI)
pip install lakehouse-doctor
# 可选:启用只读 FE 元数据补全与 EXPLAIN 校验(--dsn / --explain)
pip install "lakehouse-doctor[online]"
# 1. 零前置依赖:诊断导出的 Doris / StarRocks Profile(含低 Token ai-json 压缩)
lh-doctor profile slow_profile.txt
lh-doctor profile slow_profile.txt --format ai-json
lh-doctor profile before.txt --diff after.txt
# 2. 本地自查单文件或目录
lh-doctor audit ./sql --fail-on none
# 3. 存量仓库先建基线,PR 只看本次增量与下游影响(同步导出 SARIF)
lh-doctor audit ./sql --write-baseline .lh-baseline.json
lh-doctor audit ./sql --since origin/main --baseline .lh-baseline.json --format json --sarif-out lh-audit.sarif
使用旧版 v1 基线时,审计会显示全部诊断。请在原先验收的 SQL 版本上用 --write-baseline 生成 v2 基线。
运行仓库内置的 4 层 Paimon + Doris 示例与示例 Profile:
git clone https://github.com/tolgerx/lakehouse-doctor.git
cd lakehouse-doctor
lh-doctor audit examples/ecommerce_lakehouse --fail-on none
lh-doctor impact examples/ecommerce_lakehouse --column ods_orders.pay_amount
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 audit examples/ecommerce_lakehouse
Lakehouse Doctor Audit (files: 8, catalog tables: 5)
Summary: 3 CRITICAL | 9 WARNING | 1 INFO
Coverage: clean=8, normalized=0, parse_errors=0, missing_tables=0, ambiguous=0, suppressed=0, indeterminate=0
Health Score: 8/100 (Grade F) - heuristic summary
[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.
规则清单
| 规则编号 | 级别 | 适用范围 | 说明 |
|---|---|---|---|
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 目标列缺失、SELECT 或 UNION 列数不匹配、按位类型冲突或数值精度损失 |
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 优化器校验 |
在线 EXPLAIN 按语句的 Catalog 和数据库上下文运行。带未渲染变量或未绑定参数的语句会列入跳过记录。JSON 报告中的 coverage.online_explain 包含计划文本和跳过原因。
可选扩展与 CI 集成
- GitHub PR 建议评论工作流:复制
.github/workflows/sql-guard-template.yml即可在 PR 中上传 SARIF 并自动更新评论。 - 项目配置与行内豁免:
lh-doctor init生成.lh-doctor.toml(示例见.lh-doctor.toml.example);支持行内豁免注释(-- lh-doctor: disable=LH203 -- reason: ...、-- noqa)。 - 只读 stdio MCP(可选):
lh-doctor mcp(或uvx lakehouse-doctor mcp,配置示例examples/mcp_config.json)作为 AI 编程助手的只读取证工具;附带skills/与.claude/commands/。 - 基础 DQC SQL 模板(可选):
lh-doctor dqc <path> --table <table>按表结构生成基础断言。源表与目标表的粒度和分区映射明确,且目标由单源全量覆盖写入时,才生成等行数对账。
开源协议
Metadata
Release files for lakehouse-doctor 0.10.3
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.10.3.tar.gz | 159.9 kB | With provenance | Details for lakehouse_doctor-0.10.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| lakehouse_doctor-0.10.3-py3-none-any.whl | Python 3 | none | any | Details for lakehouse_doctor-0.10.3-py3-none-any.whl |
Total release size: 284.6 kB
Release files / lakehouse_doctor-0.10.3.tar.gz
| Download URL | lakehouse_doctor-0.10.3.tar.gz |
|---|---|
| Size | 159.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6dad0342f40b377c427611aaa6d0d1ac05f79515a62299c373edcb92ca766a7c
|
|
BLAKE2b-256 checksum How to use checksums |
f1b621d710885ae3c1fb5650ec13bc9cdc2e609a104f2707c43c03c5d43806a3
|
| 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 3, 2026.
Transparency logRelease files / lakehouse_doctor-0.10.3-py3-none-any.whl
| Download URL | lakehouse_doctor-0.10.3-py3-none-any.whl |
|---|---|
| Size | 124.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1f6e778361e3418763c5509dfe73af76de36e477575de060a4fff5ec25c46d28
|
|
BLAKE2b-256 checksum How to use checksums |
bc73505b13ba39c642ef31c529a5662242f48864b319e626ea87ceb154f6cad7
|
| 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 3, 2026.
Transparency logSecurity for lakehouse-doctor 0.10.3
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.10.3.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 3, 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.