Metadata-Version: 2.5
Name: icetrace
Version: 0.1.0
Summary: Scan-planning & metadata forensics engine for Apache Iceberg — EXPLAIN ANALYZE for Iceberg metadata without Spark.
Project-URL: Homepage, https://github.com/sanspareilsmyn/icetrace
Project-URL: Repository, https://github.com/sanspareilsmyn/icetrace
Project-URL: Issues, https://github.com/sanspareilsmyn/icetrace/issues
Project-URL: Changelog, https://github.com/sanspareilsmyn/icetrace/blob/main/CHANGELOG.md
Author-email: Sangmin Yoon <sanspareilsmyn@users.noreply.github.com>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: apache-iceberg,data-engineering,forensics,iceberg,lakehouse,mcp,metadata,query-planning
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Database
Classifier: Topic :: Software Development :: Debuggers
Requires-Python: >=3.11
Requires-Dist: mcp>=2.0.0
Requires-Dist: pydantic>=2.7.0
Requires-Dist: pyiceberg[sql-sqlite]>=0.9.0
Requires-Dist: rich>=13.7.0
Requires-Dist: sqlglot>=25.0.0
Requires-Dist: typer>=0.12.0
Provides-Extra: all
Requires-Dist: pyiceberg[gcsfs,glue,hive,s3fs,sql-postgres]>=0.9.0; extra == 'all'
Provides-Extra: dev
Requires-Dist: mypy>=1.11.0; extra == 'dev'
Requires-Dist: pyarrow>=16.0.0; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.23.0; extra == 'dev'
Requires-Dist: pytest>=8.2.0; extra == 'dev'
Requires-Dist: ruff>=0.6.0; extra == 'dev'
Provides-Extra: gcs
Requires-Dist: pyiceberg[gcsfs]>=0.9.0; extra == 'gcs'
Provides-Extra: glue
Requires-Dist: pyiceberg[glue,s3fs]>=0.9.0; extra == 'glue'
Provides-Extra: hive
Requires-Dist: pyiceberg[hive]>=0.9.0; extra == 'hive'
Provides-Extra: s3
Requires-Dist: pyiceberg[s3fs]>=0.9.0; extra == 's3'
Description-Content-Type: text/markdown

# icetrace

**`EXPLAIN ANALYZE` for Apache Iceberg metadata — debug why your queries scan 100% of files without Spark, Trino, or Athena.**

[![License: Apache-2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](LICENSE)
[![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
[![MCP Ready](https://img.shields.io/badge/MCP-Cursor%20%7C%20Claude%20Code-purple.svg)](#agent-native-setup-cursor--claude-code)

`icetrace` is a **strictly read-only, metadata-only** scan-planning and pruning forensics engine for Apache Iceberg. Point it at any Iceberg table (AWS Glue, REST / Polaris / Lakekeeper / Nessie / S3 Tables, Hive, or SQL) from **Cursor**, **Claude Code**, or the CLI, and in seconds it simulates Iceberg's 4-stage pruning pipeline and proves **exactly why** files were not skipped:

- **32-column metrics cutoff (`MISSING_METRICS_COL_LIMIT`)** — Iceberg silently stops recording min/max bounds after column #32 by default.
- **16-char string truncation collisions (`TRUNCATED_BOUNDS`)** — default `truncate(16)` collapses prefixed IDs, tenant keys, or URIs across files, turning data skipping to 0%.
- **Destroyed clustering (`OVERLAPPING_RANGES`)** — computes exact $O(N \log N)$ domain-weighted **sweep-line interval overlap** (`0.00` = sorted, `1.00` = every file spans the full domain) to expose when binpack compaction or arrival-order writes broke pruning.
- **Zero-copy / writer stats omission (`MISSING_METRICS_WRITER`)** — detects files registered via `add_files` or custom writers with missing Parquet footer statistics.
- **Manifest interleaving & evolved partition specs** — catches Stage 1 manifest-list overlap, historical `spec_id` mismatches, and V2/V3 delete-file amplification.

---

## Try It in 10 Seconds (Zero Credentials Required)

```bash
uvx icetrace demo
```

Creates a temporary local SQLite Iceberg table with 3 classic production pruning traps (`truncate(16)` prefix collision on `tenant_id`, 32-column metrics cutoff on col #35 `trace_key`, and unsorted `amount` vs sorted `event_id`) and runs `explain` + `audit` live:

```text
╭───────────────────────────────────────────────────────────────────╮
│ icetrace explain  demo.events                                     │
│ Catalog: demo  |  Snapshot: 634953721215018604  |  Sampled: False │
│ Predicate: tenant_id = 'org_prod_us_east_03_0042'                 │
╰───────────────────────────────────────────────────────────────────╯
Stage 1 (Manifest List):   8 evaluated -> 0 pruned -> 8 surviving (0.0%)
Stage 2 (Partitions):      8 evaluated -> 0 pruned -> 8 surviving (0.0%)
Stage 3 (Column Bounds):   8 evaluated -> 0 pruned -> 8 surviving (0.0%)

Survival Attribution: TRUNCATED_BOUNDS: 8
HIGH [TRUNCATED_BOUNDS]: 8/8 surviving files matched a truncated prefix of length 16
  (metrics mode: truncate(16)) on column 'tenant_id'.
  Fix: ALTER TABLE ... SET TBLPROPERTIES ('write.metadata.metrics.column.tenant_id' = 'full')
       and run sort compaction on 'tenant_id'.
```

```text
                          Per-Column Pruning Readiness
  #  Column      Mode          Bounds   Overlap   Trunc%  Grade  Issues
  1  event_id    truncate(16)    100%      0.00       0%    A    ok
  2  tenant_id   truncate(16)    100%      1.00     100%    F    TRUNCATED_BOUNDS; OVERLAPPING_RANGES
  3  amount      truncate(16)    100%      0.98       0%    F    OVERLAPPING_RANGES (0.98)
 35  trace_key   none              0%         -       0%    F    MISSING_METRICS_COL_LIMIT (35 > 32)
```

---

## Why `icetrace`?

| Tool | Needs Cluster / Server? | 4-Stage Predicate Pruning Simulation | Per-File Root-Cause Attribution | Sweep-Line Bounds Overlap | Agent-Native MCP |
|---|:---:|:---:|:---:|:---:|:---:|
| **Spark `EXPLAIN` / `ScanReport`** | Yes (Spark JVM) | Post-execution counts only | No | No | No |
| **Nimtable / Amoro / Floe** | Yes (Docker/Postgres/JVM) | No | No | No | No |
| **`iceberg-meta` / `iceberg-doctor`** | No | No | No (static thresholds) | No | Basic (2 tools) |
| **Existing Iceberg MCP servers** | No | No (catalog listing / SQL) | No | No | Yes |
| **`icetrace`** | **No (zero-infra)** | **Yes (100% PyIceberg parity)** | **Yes (6 root causes)** | **Yes ($O(N \log N)$)** | **Yes (5 read-only tools + Skill)** |

---

## Installation

### 1. Zero-Install via `uvx` (Recommended for MCP)
```bash
uvx icetrace demo
uvx icetrace-mcp
```

### 2. Homebrew (macOS / Linux)
```bash
brew tap sanspareilsmyn/icetrace https://github.com/sanspareilsmyn/icetrace
brew install icetrace
```

### 3. `uv tool` / `pip`
```bash
# Core (REST, SQL/SQLite)
uv tool install icetrace

# With AWS Glue & S3 support
uv tool install "icetrace[glue]"
```

---

## Agent-Native Setup (Cursor & Claude Code)

`icetrace` is designed to run directly inside your AI coding agent while your SQL queries, dbt models, Spark jobs, or table YAMLs are open.

### Cursor (`.cursor/mcp.json` or `~/.cursor/mcp.json`)
```json
{
  "mcpServers": {
    "icetrace": {
      "command": "uvx",
      "args": ["--from", "icetrace[glue]", "icetrace-mcp"],
      "env": {
        "AWS_PROFILE": "${env:AWS_PROFILE}",
        "AWS_REGION": "us-east-1"
      }
    }
  }
}
```

### Claude Code
```bash
claude mcp add --transport stdio --scope user icetrace -- uvx --from "icetrace[glue]" icetrace-mcp
```

### Example Prompts
- *"Run `explain_scan` on `prod.events` for `WHERE event_ts >= '2026-11-20' AND tenant_id = 'org_prod_8491'`. Why did Athena scan 180 GB?"*
- *"Audit `prod.events` and tell me which columns have broken pruning bounds or high overlap."*
- *"Compare the last two snapshots of `prod.events` — did yesterday's compaction hurt clustering?"*

### Exposed MCP Tools (All `readOnlyHint: true`, `destructiveHint: false`)
1. `list_tables` — Discover namespaces and Iceberg tables.
2. `explain_scan` — 4-stage scan-planning simulator + per-file survival root-cause attribution.
3. `audit_pruning_health` — Predicate-free per-column bounds coverage, truncation collision, sweep-line overlap ratio, and layout health.
4. `diff_snapshots` — Snapshot-to-snapshot manifest growth, bounds coverage, clustering, and pruning regression diff.
5. `inspect_table_layout` — Zero-manifest-read inspection of `metadata.json` (format version, column ordinals, evolved partition specs, sort order, properties).

A companion Agent Skill is bundled in [`skills/icetrace/SKILL.md`](skills/icetrace/SKILL.md).

---

## CLI Usage

```bash
# Simulate 4-stage pruning for a SQL WHERE clause
icetrace explain prod.events --where "event_ts >= '2026-11-20' AND tenant_id = 'org_prod_01'"

# Audit all columns for bounds coverage, truncation traps, and overlap
icetrace audit prod.events

# Diff current snapshot vs parent (with optional predicate regression check)
icetrace diff prod.events --where "tenant_id = 'org_prod_01'"

# Inspect metadata.json only (zero manifest reads)
icetrace inspect prod.events

# JSON output for scripting / CI
icetrace audit prod.events --json
```

### Catalog Configuration
`icetrace` reuses standard [`~/.pyiceberg.yaml`](https://py.iceberg.apache.org/configuration/) or environment variables:
- `ICETRACE_CATALOG_TYPE` (`glue`, `rest`, `sql`, `hive`)
- `ICETRACE_CATALOG_URI`, `ICETRACE_WAREHOUSE`, `ICETRACE_TOKEN`
- Standard AWS credentials (`AWS_PROFILE`, `AWS_REGION`, IAM role / SSO) for Glue & S3.

---

## Architecture & Security Guarantees

```text
Cursor / Claude Code (MCP stdio)  |  CLI (icetrace)
                    v
      Evidence & Diagnosis Layer (Facts != Inferences)
                    v
   Core Forensics Engine (SQL AST -> 4-Stage Planner -> Sweep-Line Overlap -> Diff)
                    v
   PyIceberg Catalog Adapter (Glue, REST, SQL, Hive) — Metadata Only
```

- **Strictly Read-Only:** `icetrace` never writes, compacts, expires, or mutates table metadata or data.
- **Metadata-Only:** Only reads `metadata.json`, manifest list Avro, and manifest Avro files — never downloads Parquet/ORC data files.
- **Bounded Cost:** Enforces `max_manifests` (default `200`) so huge tables never silently hang or trigger unbounded S3 GETs.
- **Zero Telemetry:** Your credentials and metadata never leave your machine.

---

## Roadmap & Contributing

- See [ROADMAP.md](ROADMAP.md) for the 3-month release plan (v0.1 → v0.4) and 1–2 year vision.
- See [CHANGELOG.md](CHANGELOG.md) for release notes.
- See [CONTRIBUTING.md](CONTRIBUTING.md) to run the full test suite locally in `< 2s` with zero cloud credentials.

Licensed under the [Apache License 2.0](LICENSE).
