Metadata-Version: 2.4
Name: col-reaper
Version: 0.3.0
Summary: Schema management platform for data warehouses — dead columns, dead tables, compliance analysis, dbt integration, wide-table detection, and data debt scoring
Author-email: Sanjay Kandimalla <codewithsanjay-pixel@users.noreply.github.com>
License: MIT
Project-URL: Homepage, https://github.com/codewithsanjay-pixel/col-reaper
Project-URL: Repository, https://github.com/codewithsanjay-pixel/col-reaper
Project-URL: Issues, https://github.com/codewithsanjay-pixel/col-reaper/issues
Keywords: data-engineering,snowflake,bigquery,dead-columns,data-warehouse,schema,dbt,compliance,gdpr,hipaa,schema-management,data-debt
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Database
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: click>=8.0
Requires-Dist: rich>=13.0
Requires-Dist: sqlglot>=23.0
Provides-Extra: snowflake
Requires-Dist: snowflake-connector-python>=3.0; extra == "snowflake"
Provides-Extra: bigquery
Requires-Dist: google-cloud-bigquery>=3.0; extra == "bigquery"
Provides-Extra: all
Requires-Dist: snowflake-connector-python>=3.0; extra == "all"
Requires-Dist: google-cloud-bigquery>=3.0; extra == "all"
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0; extra == "dev"
Requires-Dist: ruff>=0.1; extra == "dev"
Dynamic: license-file

# ⚰ col-reaper

**Find dead columns in your data warehouse. Drop what nobody uses.**

[![CI](https://github.com/codewithsanjay-pixel/col-reaper/actions/workflows/ci.yml/badge.svg)](https://github.com/codewithsanjay-pixel/col-reaper/actions)
[![PyPI](https://img.shields.io/pypi/v/col-reaper.svg)](https://pypi.org/project/col-reaper/)
[![Python 3.9+](https://img.shields.io/badge/python-3.9%2B-blue.svg)](https://python.org)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

col-reaper scans your Snowflake or BigQuery query history and finds columns that
nobody has queried recently. It outputs a report and a ready-to-run SQL migration
script so your team can safely clean up the schema.

**Zero data leaves your warehouse.** col-reaper is strictly read-only and only
reads column metadata and query text — never your actual data.

---

## Why col-reaper?

Over time, data warehouses accumulate **ghost columns** — columns created for a
one-off analysis, a deprecated feature, or a migration that never finished. These
columns:

- **Cost money** — cloud warehouses charge for storage and scan bytes
- **Slow down queries** — wider tables = more bytes scanned per full scan
- **Confuse analysts** — "is `referral_source_v1` or `referral_source_v2` correct?"
- **Hide in plain sight** — no warehouse has a built-in "who uses this column?" view

col-reaper fills that gap.

---

## Try it in 30 seconds (no credentials needed)

```bash
pip install col-reaper

# Run the demo — synthetic e-commerce data, no warehouse required
col-reaper demo

# See it as HTML
col-reaper demo --format html --output demo.html && open demo.html

# See the migration SQL
col-reaper demo --format sql
```

---

## Installation

```bash
# Base install (demo mode + all analysis, no warehouse drivers)
pip install col-reaper

# With Snowflake support
pip install "col-reaper[snowflake]"

# With BigQuery support
pip install "col-reaper[bigquery]"

# Everything
pip install "col-reaper[all]"
```

---

## Quick start

### Snowflake

```bash
export SNOWFLAKE_ACCOUNT=xy12345.us-east-1
export SNOWFLAKE_USER=col_reaper_svc
export SNOWFLAKE_PASSWORD=...
export SNOWFLAKE_DATABASE=ANALYTICS
export SNOWFLAKE_WAREHOUSE=COMPUTE_WH

# Scan — terminal report
col-reaper scan --warehouse snowflake --days 90

# Scan with dry-run (review before committing to drops)
col-reaper scan --warehouse snowflake --days 90 --dry-run --format sql

# Generate migration SQL
col-reaper scan --warehouse snowflake --days 90 --format sql --output cleanup.sql

# Shareable HTML report
col-reaper scan --warehouse snowflake --days 90 --format html --output report.html
```

### BigQuery

```bash
export GOOGLE_CLOUD_PROJECT=my-gcp-project
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json

col-reaper scan --warehouse bigquery --days 90
```

---

## Output formats

### `terminal` (default) — rich colour table

```
⚰  col-reaper  |  ANALYTICS  |  lookback: 90d  |  found: 7 dead columns

╭──────────┬────────────────┬─────────────────────┬─────────┬────────────┬────────────╮
│ Schema   │ Table          │ Column              │ Type    │  Last Seen │ Confidence │
├──────────┼────────────────┼─────────────────────┼─────────┼────────────┼────────────┤
│ PUBLIC   │ USERS          │ referral_source      │ VARCHAR │ never      │    high    │
│ PUBLIC   │ USERS          │ legacy_user_flag     │ BOOLEAN │ never      │    high    │
│ PUBLIC   │ ORDERS         │ shipping_v1_address  │ VARCHAR │ never      │    high    │
╰──────────┴────────────────┴─────────────────────┴─────────┴────────────┴────────────╯
```

### `sql` — ready-to-run migration script

```sql
-- col-reaper migration script
-- Warehouse : ANALYTICS (snowflake)
-- Generated : 2026-11-01 09:30 UTC
-- Lookback  : 90 days
-- REVIEW CAREFULLY before running.

-- Table: ANALYTICS.PUBLIC.USERS
ALTER TABLE ANALYTICS.PUBLIC.USERS DROP COLUMN referral_source;
ALTER TABLE ANALYTICS.PUBLIC.USERS DROP COLUMN legacy_user_flag;

-- Table: ANALYTICS.PUBLIC.ORDERS
ALTER TABLE ANALYTICS.PUBLIC.ORDERS DROP COLUMN shipping_v1_address;
```

### `sql` with `--dry-run` — deprecation comments (safe review mode)

```sql
-- col-reaper DRY RUN — deprecation comments only
-- Step 1: Paste these comments into your migration tool to mark columns.
-- Step 2: Monitor for 30 days. If no complaints, re-run without --dry-run.

-- Table: ANALYTICS.PUBLIC.USERS
-- [DEPRECATED 2026-11-01] ANALYTICS.PUBLIC.USERS.referral_source — not queried in 90 days (confidence: high). Review before dropping.
-- [DEPRECATED 2026-11-01] ANALYTICS.PUBLIC.USERS.legacy_user_flag — not queried in 90 days (confidence: high). Review before dropping.
```

### `json` — machine-readable

```json
{
  "meta": {
    "generated_at": "2026-11-01T09:30:00+00:00",
    "warehouse": "snowflake",
    "lookback_days": 90,
    "dead_column_count": 7,
    "dry_run": false
  },
  "dead_columns": [
    {
      "database": "ANALYTICS",
      "schema": "PUBLIC",
      "table": "USERS",
      "column": "referral_source",
      "data_type": "VARCHAR",
      "last_seen": null,
      "days_since_last_use": null,
      "confidence": "high",
      "drop_statement": "ALTER TABLE ANALYTICS.PUBLIC.USERS DROP COLUMN referral_source;"
    }
  ]
}
```

### `html` — shareable report with filtering and sorting

```bash
col-reaper scan --warehouse snowflake --format html --output report.html
# Open report.html in any browser — no server needed, fully self-contained
```

The HTML report includes:
- Summary stats (dead count, confidence breakdown)
- Filterable, sortable table
- One-click copy of the DROP statement per column
- Dark theme, works offline

### `markdown` — paste into GitHub / Slack / Notion

```bash
col-reaper scan --warehouse snowflake --format markdown
```

---

## Dry-run mode

**Always run with `--dry-run` first.**

Dry-run mode outputs review comments instead of DROP statements. Use it to:
1. Share the list with your team for review
2. Watch for complaints for 30 days (BI tools, notebooks, apps)
3. Only then run the real migration

```bash
# Step 1: Review
col-reaper scan --warehouse snowflake --days 90 --dry-run --format sql --output review.sql

# Step 2 (after 30+ days): Actually drop
col-reaper scan --warehouse snowflake --days 90 --format sql --output cleanup.sql
```

---

## Filtering

```bash
# Skip entire schemas
col-reaper scan --warehouse snowflake --exclude-schema STAGING --exclude-schema RAW

# Skip specific tables
col-reaper scan --warehouse snowflake --exclude-table TEMP_MIGRATION

# Skip columns matching a pattern (substring, case-insensitive)
col-reaper scan --warehouse snowflake --exclude-column _deprecated --exclude-column legacy_

# Combine filters
col-reaper scan --warehouse snowflake \
  --exclude-schema STAGING \
  --exclude-column _v1 \
  --days 60
```

---

## CI/CD integration

Add col-reaper to your CI pipeline to enforce a maximum number of dead columns:

```yaml
# .github/workflows/warehouse-hygiene.yml
name: Warehouse hygiene check
on:
  schedule:
    - cron: '0 9 * * 1'   # every Monday at 9am UTC
  workflow_dispatch:

jobs:
  col-reaper:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install col-reaper
        run: pip install "col-reaper[snowflake]"

      - name: Scan for dead columns
        env:
          SNOWFLAKE_ACCOUNT: ${{ secrets.SNOWFLAKE_ACCOUNT }}
          SNOWFLAKE_USER: ${{ secrets.SNOWFLAKE_USER }}
          SNOWFLAKE_PASSWORD: ${{ secrets.SNOWFLAKE_PASSWORD }}
          SNOWFLAKE_DATABASE: ANALYTICS
          SNOWFLAKE_WAREHOUSE: COMPUTE_WH
        run: |
          col-reaper scan \
            --warehouse snowflake \
            --days 90 \
            --exclude-schema STAGING \
            --format markdown \
            --output dead-columns.md

      - name: Upload report
        uses: actions/upload-artifact@v4
        with:
          name: dead-columns-report
          path: dead-columns.md

      # Optional: fail the build if dead columns are found
      # - name: Fail if dead columns exist
      #   run: col-reaper scan --warehouse snowflake --days 90 --fail-on-found
```

---

## How it works

1. **Fetch schema** — col-reaper reads `INFORMATION_SCHEMA.COLUMNS` to get every column in scope (read-only, metadata only).

2. **Fetch query history** — it reads recent SELECT queries from:
   - Snowflake: `SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY` (365-day history, requires IMPORTED PRIVILEGES on SNOWFLAKE DB) — falls back to `INFORMATION_SCHEMA.QUERY_HISTORY` (7-day history, no special grants needed)
   - BigQuery: `region-us.INFORMATION_SCHEMA.JOBS_BY_PROJECT`

3. **Parse SQL** — each query's text is parsed with [sqlglot](https://github.com/tobymao/sqlglot) to extract every column reference. A regex fallback handles edge cases.

4. **Dead = not referenced** — a column is "dead" if it appears in zero parsed queries. A confidence score (high/medium/low) accounts for ambiguous short names.

5. **Report** — results are rendered in your chosen format with optional DROP statements.

---

## Confidence scores

| Score | Meaning | Example column names |
|-------|---------|----------------------|
| **high** | Specific name, low false-positive risk | `shipping_v1_address`, `fb_pixel_id_deprecated` |
| **medium** | Short name (≤ 3 chars), slightly ambiguous | `qty`, `sku`, `ref` |
| **low** | Very common name — might match unrelated queries | `id`, `name`, `created_at` |

**Recommendation:** review "low" confidence columns manually before dropping.

---

## Security

col-reaper is **strictly read-only**. It never writes anything to your warehouse.

See [SECURITY.md](SECURITY.md) for:
- Exact minimum-permission SQL scripts for Snowflake and BigQuery
- Network traffic audit instructions
- Complete list of what col-reaper reads and what it never reads

---

## Supported warehouses

| Warehouse | Status | Query history source | History depth |
|-----------|--------|---------------------|---------------|
| Snowflake | ✅ Stable | `ACCOUNT_USAGE.QUERY_HISTORY` (or `INFORMATION_SCHEMA` fallback) | 365 days (7 fallback) |
| BigQuery | ✅ Stable | `INFORMATION_SCHEMA.JOBS_BY_PROJECT` | 180 days |
| Redshift | 🔜 Planned | `STL_QUERY` | 7 days |
| Databricks | 🔜 Planned | `system.access.audit` | 365 days |
| DuckDB | 🔜 Planned | — | — |

---

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md) — it includes a step-by-step guide for adding a new warehouse connector.

---

## License

MIT © 2026 Sanjay Kandimalla
