Skip to main content

Lakehouse Doctor (lh-doctor)

CI PyPI Python 3.10+ License: MIT

简体中文 | 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 还是日常写在网页调度平台里)。

Lakehouse Doctor Demo


核心能力

  1. 为 Coding Agent 设计的只读取证接口(mcp + skills/ + ai-json)
    • 一条命令启动零重依赖 stdio MCP Server(uvx lakehouse-doctor mcp),内置 audit_sql、inspect_catalog、trace_impact、analyze_profile、compare_profiles 与 rules/catalog resource,返回 structuredContent 与工具 annotations,并自带 -32700 畸形帧容错与常驻项目缓存。
    • --format ai-json 带硬字符预算裁剪(单份 Profile、Diff 与审计通用),避免几万行 Profile 把上下文窗口打爆;配套 4 套领域 skills/ 与 .claude/commands/ 引导 Agent 按“查证 → 改 SQL → 复审”闭环工作。
    • 防幻觉 coverage 契约:缺失表、歧义表、降级解析、动态模板谓词或被丢弃的 Jinja {% elif / else %} 分支均会显式标出 complete = false,防止 Agent 把“没扫全”误判成“已通过”。
  2. 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 会主动禁用误导性加速倍率。
  3. 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 方言回退与语法归一化,适合表列/结构规范检查,不替代引擎原生的水位线、状态与物理计划验证。
  • 增量审计与严格覆盖门禁:--since / --changed-files 遇到 DDL 变更会自动扩展重审下游关联文件,支持多重集 --baseline 基线过滤;如需把任何 coverage.complete = false 也视作失败,可传入 --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

运行仓库内置的 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 目标列名不存在、列数不匹配或按位类型族不兼容
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 并自动更新评论。
  • 项目配置与行内豁免: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> 可按主键与血缘生成基础断言 SQL 起步模板。

开源协议

MIT

Metadata

Release files for lakehouse-doctor 0.10.2

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.10.2
File Size Uploaded Provenance Actions
lakehouse_doctor-0.10.2.tar.gz 150.9 kB With provenance Details for lakehouse_doctor-0.10.2.tar.gz

Built distribution (wheel)

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

Total release size: 270.0 kB

Release files / lakehouse_doctor-0.10.2.tar.gz

Download URL lakehouse_doctor-0.10.2.tar.gz
Size 150.9 kB
Tags Source
SHA-256 checksum
How to use checksums
33c650d7a1ae0cbe83459f1f5376f07384bc14f2c76840889bffac54495efa3a
BLAKE2b-256 checksum
How to use checksums
29ed967077cca9d377b42574d4a34067e5ebb2ab5f19d85576c264ff44539f0e
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 log

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

Download URL lakehouse_doctor-0.10.2-py3-none-any.whl
Size 119.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f501e7b5faa7924aa867f732376635f2e01278928771f41619ad4701c214da44
BLAKE2b-256 checksum
How to use checksums
c4ba76e8748e70b0f58276ebfd01e1eee0d888b857734288878e4efd38a3e88f
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 log

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

Select from 2 files

Attestation for lakehouse_doctor-0.10.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 3, 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