|
| 1 | +# sqlmesh-openlineage |
| 2 | + |
| 3 | +OpenLineage integration for SQLMesh. Emits per-model lineage events during SQLMesh runs without modifying SQLMesh itself. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Table-level lineage**: Track which models depend on which upstream models |
| 8 | +- **Column-level lineage**: Track which columns flow from source to destination |
| 9 | +- **Schema capture**: Column names and types for each model |
| 10 | +- **Execution stats**: Duration, rows processed, bytes processed |
| 11 | +- **Per-model events**: START/COMPLETE/FAIL events for each model evaluation |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +pip install sqlmesh-openlineage |
| 17 | +``` |
| 18 | + |
| 19 | +Or with uv: |
| 20 | + |
| 21 | +```bash |
| 22 | +uv add sqlmesh-openlineage |
| 23 | +``` |
| 24 | + |
| 25 | +## Quick Start (CLI Users) |
| 26 | + |
| 27 | +Add this to your `config.py`: |
| 28 | + |
| 29 | +```python |
| 30 | +import sqlmesh_openlineage |
| 31 | + |
| 32 | +sqlmesh_openlineage.install( |
| 33 | + url="http://localhost:5000", |
| 34 | + namespace="my_project", |
| 35 | + # api_key="...", # optional |
| 36 | +) |
| 37 | + |
| 38 | +from sqlmesh.core.config import Config |
| 39 | + |
| 40 | +config = Config( |
| 41 | + # ... your existing config |
| 42 | +) |
| 43 | +``` |
| 44 | + |
| 45 | +Then run `sqlmesh run` as normal. OpenLineage events will be emitted for each model evaluation. |
| 46 | + |
| 47 | +## Environment Variables |
| 48 | + |
| 49 | +You can also configure via environment variables: |
| 50 | + |
| 51 | +```bash |
| 52 | +export OPENLINEAGE_URL=http://localhost:5000 |
| 53 | +export OPENLINEAGE_NAMESPACE=my_project |
| 54 | +export OPENLINEAGE_API_KEY=... # optional |
| 55 | +``` |
| 56 | + |
| 57 | +Then in `config.py`: |
| 58 | + |
| 59 | +```python |
| 60 | +import sqlmesh_openlineage |
| 61 | +sqlmesh_openlineage.install() # reads from env vars |
| 62 | +``` |
| 63 | + |
| 64 | +## How It Works |
| 65 | + |
| 66 | +This package uses SQLMesh's `set_console()` API to inject a custom Console wrapper. The wrapper intercepts per-snapshot lifecycle events and emits corresponding OpenLineage events: |
| 67 | + |
| 68 | +- `START` event when a model evaluation begins |
| 69 | +- `COMPLETE` event when evaluation succeeds (includes execution stats) |
| 70 | +- `FAIL` event when evaluation fails or audits fail |
| 71 | + |
| 72 | +## Events Emitted |
| 73 | + |
| 74 | +| SQLMesh Event | OpenLineage Event | Data Included | |
| 75 | +|---------------|-------------------|---------------| |
| 76 | +| Model evaluation start | RunEvent(START) | Input datasets, output dataset with schema, column lineage | |
| 77 | +| Model evaluation success | RunEvent(COMPLETE) | Execution stats (rows, bytes, duration) | |
| 78 | +| Model evaluation failure | RunEvent(FAIL) | Error message | |
| 79 | +| Audit failure | RunEvent(FAIL) | Audit failure details | |
| 80 | + |
| 81 | +## Column-Level Lineage |
| 82 | + |
| 83 | +The integration automatically extracts column-level lineage using SQLMesh's built-in lineage analysis. For example, if you have: |
| 84 | + |
| 85 | +```sql |
| 86 | +-- customers.sql |
| 87 | +SELECT customer_id, name, email FROM raw_customers |
| 88 | + |
| 89 | +-- customer_summary.sql |
| 90 | +SELECT |
| 91 | + c.customer_id, |
| 92 | + c.name as customer_name, |
| 93 | + COUNT(o.order_id) as total_orders |
| 94 | +FROM customers c |
| 95 | +LEFT JOIN orders o ON c.customer_id = o.customer_id |
| 96 | +GROUP BY c.customer_id, c.name |
| 97 | +``` |
| 98 | + |
| 99 | +The lineage will show that `customer_summary.customer_name` traces back to `customers.name`. |
| 100 | + |
| 101 | +## Testing with Marquez |
| 102 | + |
| 103 | +```bash |
| 104 | +# Start Marquez (requires Docker) |
| 105 | +docker compose up -d |
| 106 | + |
| 107 | +# Configure and run SQLMesh |
| 108 | +export OPENLINEAGE_URL=http://localhost:5001 |
| 109 | +sqlmesh run |
| 110 | + |
| 111 | +# View lineage at http://localhost:3000 |
| 112 | +``` |
| 113 | + |
| 114 | +## Development |
| 115 | + |
| 116 | +```bash |
| 117 | +# Install dependencies |
| 118 | +uv sync --dev |
| 119 | + |
| 120 | +# Run tests (unit + integration) |
| 121 | +uv run pytest tests/ -v |
| 122 | + |
| 123 | +# Run Marquez integration test (requires Docker) |
| 124 | +docker compose up -d |
| 125 | +uv run pytest tests/test_marquez_integration.py -v -s |
| 126 | +docker compose down |
| 127 | +``` |
| 128 | + |
| 129 | +## License |
| 130 | + |
| 131 | +MIT |
0 commit comments