dlt-doctor
Provides static analysis and code-healing capabilities for Databricks Lakeflow Spark Declarative Pipelines, DLT, and PySpark, including DLT expectation quarantine routing and Unity Catalog migration checks.
Beta Release (v0.6.1): dlt-doctor is in active beta. Interfaces and CLI contracts may evolve prior to v1.0.0. Feedback and contributions are welcome via GitHub Issues and Discussions.
The Story
Every data engineer working on Databricks Lakeflow Spark Declarative Pipelines (formerly Delta Live Tables) knows the exact feeling:
It is 2 AM. Your streaming pipeline crashes with DeltaAnalysisException: Cannot perform Merge as multiple source rows matched. Or worse: native DLT expect_or_drop dropped 50,000 regulatory records with no record-level trace, and nobody noticed until audit season. You spin up an interactive cluster to debug, burning DBUs while waiting 4 minutes for workers to allocate, only to discover that someone chained 8 .withColumn() calls and wrote a row-level Python @udf that disabled Whole-Stage CodeGen and created massive JVM-to-Python socket serialization.
dlt-doctor is the antidote:
Zero cluster spend: Statically audits your PySpark AST and configurations locally or inside CI before code merges.
Helps prevent silent data loss: Intercepts destructive
expect_or_droppolicies and synthesizes complete, disjoint, three-valued-logic NULL-safe single-pass quarantine streams viadlt-doctor heal.Surgical healing: Calculates bounded late-data partition blast radii to restate historical rolling windows without full-table scans.
Related MCP server: CodeAnalysis MCP Server
Showcase Video
Benchmarks
Static Analyzer Throughput (Locally Measured)
Measured on an AMD Ryzen 7 5800H (16 vCPUs, 3.2GHz, 16GB RAM) running Python 3.11 with zero JVM cluster overhead across 25 production-scale pipeline files:
Benchmark Task | Workload Scale | Measured Latency | Throughput |
Single-Pass Heal | 10 DLT expectation tables | 4.2 ms | Sub-5ms per table code synthesis |
Multi-File Directory Scan | 25 pipeline source files | 35.9 ms | ~700 files/sec |
Where dlt-doctor Fits
Architectural Dimension | Native Lakeflow / DLT | DQX / Great Expectations | Commercial SaaS (Monte Carlo / Anomalo) | dlt-doctor |
Cluster Cost | Standard DLT compute | Executes assertions inside Spark tasks | Queries live tables post-run | Zero cluster cost (runs offline via AST in <5ms per file) |
Data Loss Prevention |
| Supports quarantine routing in DQX (runtime compute); GE typically halts or warns | Read-only alerting after corruption lands | Single-pass NULL-safe quarantine routing |
Actionability | Log metrics to event log | Assertion logs | Slack / PagerDuty alert notifications | Automated code synthesis ( |
Code Inspection | None (runtime engine) | Runtime dataframe assertions | Black-box table polling | Deep AST inspection (UDFs, cascades, spills) |
Licensing | Databricks runtime license | Open Source library | Commercial proprietary license | 100% Free & Open Source (Apache 2.0) |
Diagnostic Rules
All diagnostic rules are managed in dlt_doctor.rules and queryable via dlt-doctor rules and dlt-doctor explain <RULE_ID>.
Rules evaluated bydlt-doctor analyze: DD001, DD002, DD003, DD004, PYSPARK-001, PYSPARK-002, PYSPARK-004, PYSPARK-005, PYSPARK-006, and SYNTAX-001.
PLAN-001 through PLAN-004 are evaluated by PlanDoctor.analyze_explain_plan() on query explain outputs (not run by CLI analyze).
UC-001 through UC-004 are evaluated by dlt-doctor uc-migrate (not run by CLI analyze).
Rule ID | Evaluated By | Severity | Name | Description |
|
|
| Silent data loss from expect_or_drop policy |
|
|
|
| Quarantine filter is not NULL-safe complement | Quarantine filter does not handle SQL three-valued logic (NULL) for referenced columns. When a condition evaluates to NULL, both target and quarantine filters discard the row. |
|
|
| Dynamic expectation dictionary cannot be statically verified | Expectations are defined dynamically via runtime variables or external sources. Static analysis cannot resolve the expectation conditions. |
|
|
| Non-deterministic function in expectation condition | Expectation condition uses non-deterministic functions (such as current_timestamp, rand, uuid). Non-deterministic expectations produce inconsistent validation results across streaming micro-batches. |
|
|
| Python UDF serialization overhead | Row-by-row JVM-to-Python socket serialization disables Catalyst vectorization. |
|
|
| Deep withColumn cascade | Chained |
|
|
| Repartition triggers full shuffle | Explicit |
|
|
| Multiple action calls without DataFrame caching | Multiple actions on unpersisted DataFrame recompute upstream lineage from source. |
|
|
| Explicit Cartesian crossJoin |
|
|
|
| Source syntax error | Source file fails language parser validation. |
|
|
| Catalyst CartesianProduct operator in execution plan | Physical plan contains CartesianProduct operator without join condition. |
|
|
| SortMergeJoin utilized without Broadcast hints | SortMergeJoin executed where BroadcastExchange was feasible on smaller tables. |
|
|
| BatchEvalPython operator detected in physical plan | Catalyst is shipping rows across socket to Python worker daemon. |
|
|
| Empty PartitionFilters on partitioned table scan | Query scans partitioned table without partition pruning predicates. |
|
|
| Legacy DBFS root mount path detected | Code references deprecated |
|
|
| Legacy two-tier Hive Metastore table reference | Table references lack 3-level catalog namespace ( |
|
|
| Hardcoded workspace local file path | Direct local filesystem references ( |
|
|
| Legacy DBFS mount API call detected |
|
Terminal Scorecard Preview
Quickstart
Interactive Walkthrough Notebook
Import notebooks/01_dlt_doctor_walkthrough.ipynb directly into your Databricks workspace to run the interactive cell magic and AST optimizers live on your cluster.
1. Installation
pip install dlt-doctor2. Pre-Commit & GitHub Actions CI Gate
Catch PySpark anti-patterns in pull requests before they merge:
# .github/workflows/pyspark-audit.yml
name: PySpark Quality Gate
on: [pull_request]
jobs:
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run dlt-doctor Quality Audit
uses: MsMansiDhruv/dlt-doctor@v0.6.1
with:
path: "src/pipelines"
format: "text"3. Essential CLI Commands
# Statically audit a pipeline script, SQL file, notebook (.ipynb), or directory
dlt-doctor analyze src/pipelines/orders_silver.py
dlt-doctor analyze notebooks/01_ingest.ipynb
dlt-doctor analyze src/pipelines/orders.sql
dlt-doctor analyze src/pipelines --format markdown
# Launch Model Context Protocol (MCP) stdio server for Claude, Cursor, and agentic workflows
dlt-doctor mcp
# Auto-heal expect_or_drop anti-patterns into single-pass quarantine streams (preview diff)
dlt-doctor heal src/pipelines/orders_silver.py
# Apply healed quarantine routing directly to file or directory
dlt-doctor heal src/pipelines/orders_silver.py --write
dlt-doctor heal src/pipelines/ --write
# CI Compliance Gate: Check files or entire directory for unhealed drop rules
# Exit codes: 0 = Compliant, 1 = Needs healing, 2 = Usage/path error, 3 = Cannot heal
dlt-doctor heal src/pipelines/ --check
# List and explain diagnostic rules
dlt-doctor rules
dlt-doctor explain DD001
# Calculate late-arriving partition blast radius and emit pruned MERGE SQL
dlt-doctor restate --table main.gold.orders --late-date 2026-10-25 --window 7 --source main.silver.orders_recalculated
# Generate streaming CDC deduplication and SCD merge templates
dlt-doctor cdc --table main.silver.customers --keys customer_id --event-time updated_at
# Generate idempotent replay SQL to reprocess quarantined records
dlt-doctor replay --quarantine-table main.silver.orders_quarantine --target-table main.silver.orders --keys order_id --valid-filter "amount > 0" --erasure-table main.silver.orders_tombstones
# Audit legacy DBFS mounts and two-tier Hive Metastore references for Unity Catalog
dlt-doctor uc-migrate src/pipelines/orders.py --catalog prod_lakehouse --schema bronze
# Forecast Databricks DBU burn and cluster sizing (Serverless vs Classic DLT)
dlt-doctor cost --files-per-hour 25000 --avg-file-mb 12 --joins 2
# Inspect live Delta Lake table telemetry in Databricks (compaction debt, commit velocity, vacuum safety)
dlt-doctor inspect-table main.gold.orders --warehouse-id <warehouse_id>4. Interactive Databricks Notebook Cell Magic
Audit PySpark code interactively inside Databricks or Jupyter notebook cells before cluster execution:
# Load the extension in notebook setup cell
%load_ext dlt_doctor
%%dlt_doctor
# Audits cell Catalyst AST and flags anti-patterns before cluster execution
from pyspark.sql import functions as F
from pyspark.sql.functions import udf
@udf
def parse_status(x):
return x.lower() if x else "unknown"
df_orders = (
spark.readStream.table("bronze_orders")
.withColumn("c1", F.col("id"))
.withColumn("c2", F.col("ts"))
.withColumn("c3", F.col("amt"))
.withColumn("c4", F.col("code"))
.withColumn("status_clean", parse_status(F.col("status")))
)5. Model Context Protocol (MCP) Server
Run dlt-doctor as a native, zero-dependency stdio MCP server for Claude Desktop, Cursor, and agentic AI tools:
# 1-Click Install via Smithery
npx -y @smithery/cli install dlt-doctor --client claudeOr configure manually in claude_desktop_config.json or .cursor/mcp.json:
{
"mcpServers": {
"dlt-doctor": {
"command": "dlt-doctor",
"args": ["mcp"]
}
}
}Provides 5 diagnostic tools (analyze_pyspark_source, analyze_sql_source, scan_pipeline_directory, forecast_dbu_cost, audit_compaction_debt) and the dlt-doctor://rules catalog resource. See the MCP Documentation for full protocol schemas.
Production Walkthrough
Act 1: Pre-Flight Code & Ingestion Health
Catch Catalyst compilation degradation, Auto Loader backpressure, and legacy storage paths before code touches an active cluster:
from dlt_doctor import PlanDoctor, AdvisorDoctor, AutoLoaderDoctor
# 1. Statically audit PySpark code for chained withColumn calls and row-level UDFs
doctor = PlanDoctor()
findings = doctor.analyze_source_code("""
from pyspark.sql import functions as F
from pyspark.sql.functions import udf
@udf
def clean_it(x):
return x.strip() if x else None
df_clean = (
df.withColumn("c1", F.col("a"))
.withColumn("c2", F.col("b"))
.withColumn("c3", F.col("c"))
.withColumn("c4", F.col("d"))
.withColumn("clean_val", clean_it(F.col("raw")))
.repartition(4)
)
""")
report = AdvisorDoctor().compile_report("orders_etl", findings)
# Render scorecard directly inside a Databricks notebook cell or export HTML
scorecard_html = AdvisorDoctor().to_databricks_html(report)
# displayHTML(scorecard_html)
# 2. Audit Auto Loader ingestion stream progress for backpressure
al_doctor = AutoLoaderDoctor()
al_health = al_doctor.diagnose_stream_progress(
{
"numInputRows": 50000,
"inputRowsPerSecond": 1200.0,
"processedRowsPerSecond": 450.0, # Falling behind ingestion rate
}
)
if al_health.is_backpressured:
print(f"Stream backpressured: Backlog ~{al_health.estimated_backlog_rows:,} rows.")Act 2: Streaming Execution & Zero-Loss Quality
Replace destructive expect_or_drop and prevent streaming CDC multiple-match merge failures:
from dlt_doctor import ExpectationDoctor, ExpectationRule, CdcDoctor
# 1. Single-Pass NULL-Safe Quarantine (Valid -> Clean, Invalid -> Quarantine Table)
exp_doctor = ExpectationDoctor()
rules = [
ExpectationRule("valid_id", "order_id IS NOT NULL"),
ExpectationRule("positive_amount", "amount > 0"),
]
plan = exp_doctor.build_quarantine_routing("silver_orders", rules)
# Synthesizes complete single-pass pipeline code with failed rules tracking
pipeline_code = exp_doctor.generate_pipeline_code(plan, source_table="bronze_orders", flavor="dp")
print(pipeline_code)
# 2. Streaming CDC Micro-Batch Window Deduplication
cdc = CdcDoctor().generate_cdc_templates(
target_table="main.silver.customers",
primary_keys=["customer_id"],
event_time_col="updated_at",
)
# Inside foreachBatch handler: dedupes micro-batch to eliminate Delta multiple-match exception
print(cdc.microbatch_dedup_code)
print(cdc.scd1_merge_sql)Act 3: Lakehouse Storage & Surgical Healing
Restate historical rolling metrics with surgical partition pruning and optimize Delta table storage:
from dlt_doctor import RestatementDoctor, ClusterDoctor, ColumnProfile, VacuumDoctor
# 1. Surgical Late Data Restatement (Bounded Blast Radius)
reconciler = RestatementDoctor()
scope = reconciler.calculate_blast_radius(
target_table="main.gold.fact_orders",
late_date_iso="2026-10-25",
window_size_days=7,
source_table="main.silver.orders_recalculated",
partition_col="order_date",
match_key="order_id",
)
# Restates only the 7 affected partition days, skipping 358 unaffected days
spark.sql(scope.delta_merge_sql)
# 2. Select Optimal Liquid Clustering (CLUSTER BY) Keys
cluster_rec = ClusterDoctor().advise_clustering(
table_name="main.gold.fact_orders",
columns=[
ColumnProfile("customer_id", "string", distinct_count=25000, filter_frequency_score=0.9),
ColumnProfile("order_date", "date", distinct_count=1800, filter_frequency_score=0.95),
ColumnProfile("order_uuid", "string", distinct_count=9900000), # Rejected: near-unique
],
total_rows=10000000,
)
spark.sql(cluster_rec.alter_cluster_ddl)
# 3. Safe Delta VACUUM Retention Guard
vac_report = VacuumDoctor().evaluate_vacuum_safety(
table_name="main.silver.orders",
requested_retention_hours=24,
active_streaming_readers=2,
cdf_enabled=True,
)
if vac_report.safety_level.value != "DANGEROUS":
spark.sql(vac_report.recommended_vacuum_sql)Model Context Protocol (MCP) Server
Run dlt-doctor as a zero-dependency MCP stdio server for Claude Desktop, Cursor, VS Code, or Antigravity:
{
"mcpServers": {
"dlt-doctor": {
"command": "dlt-doctor",
"args": ["mcp"]
}
}
}Exposed MCP Tools & Resources:
analyze_pyspark_source— Static AST audit of PySpark / DLT Python code for Catalyst bloat, skew, and missing quarantine gates.analyze_sql_source— Databricks SQL / DLT SQL quality & expectation auditing.scan_pipeline_directory— Recursive repository scan returning structured JSON findings.forecast_dbu_cost— Classic vs. Serverless DBU burn rate and cluster sizing advisor.audit_compaction_debt— Delta small-file fragmentation audit and targetedOPTIMIZESQL generation.dlt-doctor://rules(Resource) — Live catalog of all registered diagnostic rules.
Verification & Test Suite
The test suite validates all diagnostic engines and integration components across Python 3.10, 3.11, and 3.12:
# Fast unit test suite (zero external cluster dependencies)
pytest -m "not integration"
# 197 passed in 0.52s
# Integration test suite (executed on local PySpark + Delta Lake)
REQUIRE_SPARK=1 pytest -m integration -rs -v
# 12 passed (executed Delta MERGE, SCD1/SCD2 CDC microbatches, and expectation pipelines)All source code is audited with ruff check src tests, type-checked with mypy src, and verified against security vulnerabilities with bandit -r src.
For environment setup (including Windows JAVA_HOME and Hadoop binaries) and contributor guidelines, see CONTRIBUTING.md.
Limitations & Operational Boundaries
Static AST Analysis: Static analysis inspects source AST without cluster execution. Dynamic constructs (e.g.
eval(), runtime-generated expectation dicts, or dynamic SQL queries) cannot be resolved statically (emitsDD003INFO).SQL Pipeline Auto-Heal:
dlt-doctor healsynthesizes Python AST definitions. SQL pipelines usingON VIOLATION DROP ROWmust be manually migrated to dual-table quarantine structures.Derived Non-Deterministic Rules: Expectation conditions referencing runtime boundary calls (such as
current_date()) emit a warning (UserWarning) because records evaluated near midnight boundaries may evaluate differently upon replay.Configuration: Audits and CI gates can be customized using
pyproject.tomlordlt-doctor.toml. For complete options, see the Configuration Guide and Limitations Guide.
Disclaimers
dlt-doctor is an independent open-source project and is not affiliated with, sponsored by, or endorsed by Databricks, Inc. Apache Spark, Delta Lake, and Databricks are trademarks of their respective owners. Use of dlt-doctor does not guarantee regulatory compliance (such as GDPR, CCPA, or HIPAA); data engineering teams remain responsible for their own retention, masking, and governance policies.
License
Distributed under the Apache 2.0 License. See LICENSE for details.
Available Tools
5 toolsanalyze_pyspark_sourceARead-onlyIdempotent
Statically analyze PySpark or Delta Live Tables (DLT) Python source code for Catalyst plan bloat, anti-patterns, skew risks, watermark reordering, and missing quarantine gates.
Usage Guidelines:
Use for individual Python (.py) source strings or pipeline scripts.
For SQL queries or DLT SQL statements, use analyze_sql_source instead.
For scanning entire directories or multiple repository files, use scan_pipeline_directory instead.
Behavior:
Runs 100% offline via local Python AST inspection. Does NOT connect to Databricks clusters, execute code, or require JVM/cloud credentials.
Deterministic and idempotent. Returns a structured JSON report containing finding counts and diagnostic items.
Syntax errors or invalid Python constructs are surfaced as diagnostic findings rather than raising unhandled exceptions.
Inputs: source_code (required), file_name. Returns: findings, file_name, finding_count
analyze_sql_sourceARead-onlyIdempotent
Audit Databricks SQL or Delta Live Tables (DLT) SQL source code for quality expectation risks, Cartesian joins, schema drift traps, and optimization opportunities.
Usage Guidelines:
Use for individual SQL (.sql) query text or DLT SQL statements (e.g., CREATE STREAMING TABLE, expect_or_drop).
For PySpark Python pipelines, use analyze_pyspark_source instead.
For scanning an entire repository or folder of mixed SQL and Python files, use scan_pipeline_directory instead.
Behavior:
Pure offline static AST parsing via sqlglot. Does NOT execute queries, access databases, or connect to Databricks SQL warehouses.
Deterministic and idempotent. Returns a structured diagnostic report with violation counts and remediation advice.
Syntax errors are surfaced as structured diagnostic findings.
Inputs: sql_code (required). Returns: findings, finding_count
audit_compaction_debtARead-onlyIdempotent
Evaluate Delta Lake table file-size distribution for small-file fragmentation debt and synthesize targeted OPTIMIZE, ZORDER BY, Liquid Clustering, and TBLPROPERTIES SQL commands.
Usage Guidelines:
Use when troubleshooting slow Delta Lake queries, read amplification, or excessive metadata overhead.
Prerequisites: Obtain file sizes in bytes (e.g. from DESCRIBE DETAIL table_name, Delta transaction logs, or cloud storage inventory).
To forecast pipeline compute cluster DBU burn instead, use forecast_dbu_cost.
Behavior:
Purely analytical calculation; parses file size array and computes fragmentation statistics.
Does NOT connect to cloud object storage or execute the generated OPTIMIZE/ALTER TABLE statements against a live warehouse.
Returns small-file ratios, compaction urgency rating, and copy-pasteable SQL remediation scripts.
Inputs: table_name (required), file_sizes_bytes (required), small_file_threshold_mb. Returns: table_name, total_files, total_size_mb, recommended_sql, +3 more
forecast_dbu_costARead-onlyIdempotent
Forecast Databricks DBU consumption and compute costs (Classic compute vs. Serverless) and recommend optimal worker sizing based on ingestion throughput and shuffle complexity.
Usage Guidelines:
Use during architectural sizing, capacity planning, and cost optimization before deploying Lakeflow or DLT pipelines.
For Delta Lake table storage layout and file size optimization, use audit_compaction_debt instead.
Behavior:
Pure deterministic mathematical calculation based on Databricks official DBU pricing and node SKU specifications.
Does NOT query Databricks billing APIs, account consoles, or live workspace compute.
Returns hourly, daily, and monthly DBU and USD cost projections across Classic and Serverless options with cluster node recommendations.
Inputs: files_per_hour (required), avg_file_mb, shuffle_join_count, active_hours_per_day, +1 more. Returns: classic_daily_dbu, estimated_daily_gb, serverless_daily_dbu, recommended_worker…
scan_pipeline_directoryARead-onlyIdempotent
Recursively scan a local repository or directory of PySpark (.py), SQL (.sql), and Databricks notebook (.ipynb) files to produce an aggregated lakehouse quality audit report.
Usage Guidelines:
Use to audit an entire repository, dbt/DLT project, or folder containing multiple pipeline files.
For auditing a single Python pipeline script, prefer analyze_pyspark_source.
For auditing an isolated SQL query, prefer analyze_sql_source.
Behavior:
Recursively inspects the filesystem at directory_path.
Completely offline; runs without cloud credentials, JVM, or active cluster connections.
Evaluates all supported files, computes individual and aggregate health scores (0-100), and reports per-file findings.
Individual file errors are captured inside per-file reports without halting the directory scan.
Inputs: directory_path (required), recursive. Returns: reports, clean_files, flagged_files, overall_score, +2 more
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 5 tools
Each tool has a clearly distinct target: PySpark source, SQL source, directory scanning, Delta compaction debt, and DBU cost forecasting. The descriptions explicitly cross-reference alternatives when the scope might overlap, such as single-file analysis versus full-directory scanning.
All names use a consistent snake_case verb_noun pattern: analyze_pyspark_source, analyze_sql_source, audit_compaction_debt, scan_pipeline_directory, and forecast_dbu_cost. The verbs are varied but predictable and readable.
Five tools is well-scoped for an offline lakehouse pipeline audit toolkit. Each tool covers a distinct practical need, with no redundant or filler operations.
The surface covers the apparent domain well: single-file PySpark analysis, single-file SQL analysis, full-directory/repository scanning, Delta table compaction remediation, and DBU cost forecasting. The tools work together as a coherent advisory suite without obvious dead ends.
Maintenance
Related MCP Connectors
Ovanek MCP: Model Context Protocol server for Ovanek platform tools; writes are dry-run by default.
Lean 4 MCP server: compile, prove theorems, and formalize math with Mathlib.
SQL validation, formatting, parsing, metadata extraction, linting, and security analysis.
Model Context Protocol server for the Apideck Unified API. Connect any MCP-compatible agent framework to 100+ accounting systems, HRIS platforms, file storage providers, and more through one integration. More information https://www.apideck.com/mcp-server
Related MCP Servers
- AlicenseBqualityDmaintenanceA server that implements the Model Completion Protocol (MCP) to allow LLMs to interact with Databricks resources including clusters, jobs, notebooks, and SQL execution through natural language.1150MIT
- FlicenseDqualityDmaintenanceA comprehensive Model Context Protocol server for advanced code analysis that provides tools for syntax analysis, dependency visualization, and AI-assisted development workflow support.287-
- AlicenseBqualityBmaintenanceA MCP (Model Context Protocol) server for interacting with dbt.1623,033 PyPI624Apache 2.0
- FlicenseNot gradedqualityDmaintenanceA lightning-fast, language-agnostic code analysis MCP (Model Context Protocol) server built in Rust9-