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:从CREATE TABLE、CTAS 与乱序ALTER TABLE语句构建离线目录(支持三段式catalog.db.table命名空间与USE/USE CATALOG会话上下文),可选--dsn按缺失表只读补全 FE 元数据。audit:文件级预处理调度模板(${biz_date}、#{dt}、{{ ds }}、{{ ref(...) }}、{% if %})并保持精确行号,兼容 GB18030/UTF-8 与#注释;执行语法/DDL/DML 规则(LH001、LH101–LH212),输出显式coverage审计覆盖率契约(含缺失/歧义表、降级解析、不可判定模板谓词及带原因的行内豁免记录);支持--since/--changed-files增量审计、--baseline基线过滤、Text/json/ai-json/SARIF(及--sarif-out单遍双出)与可选--explain(LE001)。lineage/impact:穿透视图、CTE、LATERAL VIEW、SELECT *(遇未知*立即止步防错位)与UNION ALL构建表/字段级血缘,impact遇歧义裸表名直接报错提示而非静默返回空结果。profile:诊断 Doris(2.x/3.x/4.x 与传统节点)及 StarRocks 3.x Query Profile(含HashTableMemoryUsage等真实内存指标,LP001–LP009),支持覆盖率校验、不可解析输入非 0 退出码、--diff可比性判定与硬输出预算--format ai-json。dqc:结合主键约束与血缘依赖生成新鲜度、主键唯一性、非空、非负指标及上下游行数对账 SQL。mcp&skills/:内置标准库 stdio MCP Server(lh-doctor mcp,含常驻缓存、畸形 JSON-32700容错、structuredContent、annotations与rules/catalogresource)与 Agent Skills。
适用边界与工程定位
- 核心定位:离线静态检查依赖仓库内的
CREATE TABLE/CTAS/ALTERDDL(或--dsn补全);缺失/歧义表及模板占位造成的“不可判定”分支会明确记录在coverage字段,而非盲目报绿。 - 方言说明:Apache Flink SQL 与 Paimon 语法基于 Spark/Hive 方言回退及引擎语法归一化,适合结构、类型与表/列一致性审查;复杂水位线、状态后端及真实分区裁剪效果请配合
--explain或引擎自身验证。 - 渐进接入:存量仓库建议先用
lh-doctor audit ./sql --write-baseline .lh-baseline.json生成基线或--fail-on none报告模式试运行,再在 PR 中启用--since origin/main增量卡点。Health Score 仅为辅助启发式摘要,不建议用作优化目标。
快速上手
方式一:命令行安装与常用工作流
pip install lakehouse-doctor
# 如需启用只读 FE 建表补全与在线 EXPLAIN(--dsn / --explain)
pip install "lakehouse-doctor[online]"
# 或使用 uvx 免安装运行
uvx lakehouse-doctor --help
常用审计与排查命令:
# 仅审计本次 PR 变更 SQL(仍基于全仓 DDL 构建 Catalog),并同时导出 SARIF 与 JSON
lh-doctor audit ./sql --since origin/main --format json --sarif-out lh-audit.sarif
# 存量仓库生成基线并只拦截新增问题
lh-doctor audit ./sql --write-baseline .lh-baseline.json
lh-doctor audit ./sql --baseline .lh-baseline.json
# Profile 压缩摘要(单份或 Diff)
lh-doctor profile slow_profile.txt --format ai-json
lh-doctor profile before.txt --diff after.txt --format ai-json
运行仓库内置的 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
方式二:在 AI 编程助手中接入 MCP(可选)
可将 lh-doctor mcp(或 uvx lakehouse-doctor mcp)注册进支持 stdio MCP 的编程助手配置(参考 examples/mcp_config.json),搭配 skills/ 作为只读 SQL/Profile 取证工具使用:
{
"mcpServers": {
"lakehouse-doctor": {
"command": "uvx",
"args": ["lakehouse-doctor", "mcp"]
}
}
}
输出示例
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.9.1
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.9.1.tar.gz | 142.6 kB | With provenance | Details for lakehouse_doctor-0.9.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| lakehouse_doctor-0.9.1-py3-none-any.whl | Python 3 | none | any | Details for lakehouse_doctor-0.9.1-py3-none-any.whl |
Total release size: 255.9 kB
Release files / lakehouse_doctor-0.9.1.tar.gz
| Download URL | lakehouse_doctor-0.9.1.tar.gz |
|---|---|
| Size | 142.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6cec56e0e2f23b1d7dcd7744f216ca861aad4b9e4928857fd77c523c6b34ba14
|
|
BLAKE2b-256 checksum How to use checksums |
246d9593573758791af23ebc9ab1e0582394acdfdfe5754d8cf691945072e6fe
|
| 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.9.1-py3-none-any.whl
| Download URL | lakehouse_doctor-0.9.1-py3-none-any.whl |
|---|---|
| Size | 113.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2a8f0f8098bc7a0e0e60a9678f33e11c0eaa7ac5f66210dc6f8804e83e248d54
|
|
BLAKE2b-256 checksum How to use checksums |
5e3cb0c5ee68a630a96f7b60654e595c75eb475416c83de70b79da25ffa86466
|
| 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.9.1
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.9.1.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.