Skip to main content

Lakehouse Doctor (lh-doctor)

CI PyPI Python 3.10+ License: MIT

简体中文 | English

面向 Apache Doris、StarRocks、Apache Paimon 与 Flink SQL 的离线 SQL 静态审计、字段级血缘分析、Query Profile 诊断、DQC 生成工具与 MCP 服务。

lakehouse-doctor 无需连接真实数据库集群,通过解析项目中的 CREATE TABLE 建表语句构建内存元数据目录(Catalog),在此基础上执行 Schema-Aware AST 静态检查、跨文件字段血缘影响面追踪与执行计划剖析。

Lakehouse Doctor Demo


核心功能

  • catalog:解析 Doris、StarRocks、Paimon 及 Flink SQL 建表语句,提取字段类型族(含 DECIMALV3/DATEV2)、主键/分桶/分区键(含 AUTO PARTITION / date_trunc 表达式分区)与引擎属性;支持通过 --dsn 只读拉取线上 FE 缺失的 DDL 补全目录。
  • audit:兼容调度宏变量(${biz_date}、#{dt}、{{ ds }}、{{ ref(...) }} 与裸 $var)及 UTF-8/GB18030/latin-1 编码,运行语法/DDL/DML 审计规则(LH001、LH101–LH106、LH201–LH212),支持行内豁免注释(-- lh-doctor: disable=... / -- noqa)、健康分卡(Scorecard)、可选只读 --dsn ... --explain(LE001)校验及文本/JSON/SARIF 2.1.0 输出。
  • lineage / impact:穿透 CTE、CREATE VIEW、SELECT * 与 UNION ALL 构建 ODS → DWD → DWS → ADS 表级与字段级血缘,区分 DIRECT/DERIVED/FILTER 依赖边语义,支持上下游变更影响分析(--upstream 快捷溯源),并导出 Mermaid、JSON 或单文件交互式 HTML。
  • profile:解析 Doris(含 PipelineX MergedProfile)与 StarRocks 3.x 慢查询 Query Profile(LP001–LP009),定位实例倾斜、算子等待阻塞、低效 Scan、Hash Join Build/Probe 大小表颠倒、高基数预聚合失效、Broadcast 内存膨胀与磁盘 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: 9/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 与扩展能力


开源协议

MIT

Metadata

Release files for lakehouse-doctor 0.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lakehouse-doctor 0.7.1
File Size Uploaded Provenance Actions
lakehouse_doctor-0.7.1.tar.gz 103.2 kB With provenance Details for lakehouse_doctor-0.7.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lakehouse-doctor 0.7.1
File Interpreter ABI Platform Actions
lakehouse_doctor-0.7.1-py3-none-any.whl Python 3 none any Details for lakehouse_doctor-0.7.1-py3-none-any.whl

Total release size: 189.0 kB

Release files / lakehouse_doctor-0.7.1.tar.gz

Download URL lakehouse_doctor-0.7.1.tar.gz
Size 103.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e0cab56241599bf347df4f313344c1d39b04a281461ec3507133344dff72990c
BLAKE2b-256 checksum
How to use checksums
4e7991b2b40c8563f31a4a28c095d8f719b9454673b4a739faa0fdca0a4a0d20
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 log

Release files / lakehouse_doctor-0.7.1-py3-none-any.whl

Download URL lakehouse_doctor-0.7.1-py3-none-any.whl
Size 85.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b916e3e3b0bba1747ccc26f8499d35bbe688a25b55b500ee2179dea3e789def5
BLAKE2b-256 checksum
How to use checksums
ee844985831d58c91d5ecf49de11c48117c31f19904e000f4d38d668404238a5
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 log

Security for lakehouse-doctor 0.7.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.

Select from 2 files

Attestation for lakehouse_doctor-0.7.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 1, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page