# MoonLineage

MoonLineage is an OpenLineage-compatible data lineage graph and change-impact
analysis library written primarily in MoonBit.

It turns lineage events emitted by data jobs into an incrementally maintained
graph, then answers practical governance questions:

- Which datasets are downstream of a changed source?
- Which jobs, reports, feature sets, or models are affected?
- What schema fields were added, removed, or changed?
- Can the same lineage data be consumed by existing OpenLineage tooling?

MoonLineage is not a data file parser or a dashboard. It is reusable
infrastructure for MoonBit data applications, pipeline tools, and CI gates.

## Status

Initial-review candidate, implemented and tested with MoonBit `0.1.20261126`
and `moonc v1.0.0-rc1`.

The current foundation includes:

- Normalized OpenLineage `RunEvent` model.
- JSON parsing, validation, and serialization.
- Dataset schema facet support.
- Idempotent event ingestion.
- Incremental job and dataset graph.
- Direct and transitive upstream/downstream queries.
- Affected-job analysis.
- Deterministic schema diff.
- A CLI that validates NDJSON event files and reports impact.

## Quick start

```bash
moon check
moon test
moon run cmd/main
```

Run a realistic impact query:

```bash
moon run cmd/main -- validate examples/orders.ndjson
moon run cmd/main -- impact examples/orders.ndjson s3 raw/orders
```

Expected impact from the included example:

```text
Downstream impact: 3
  - s3/curated/orders
  - warehouse/ml/orders_features
  - ml/fraud-model-v3
Affected jobs: 3
  - orders/clean
  - ml/build_features
  - ml/train
```

## Library example

```moonbit
let event = @moonlineage.decode_run_event_text(json_text) catch {
  error => fail(error.to_string())
}
```

After collecting events:

```moonbit
let graph = @moonlineage.LineageGraph::new()
ignore(graph.apply(event))
let downstream = graph.downstream_datasets(
  @moonlineage.DatasetId::new("s3", "raw/orders"),
)
```

## Input format

The CLI accepts one OpenLineage RunEvent JSON object per line:

```json
{
  "eventType": "COMPLETE",
  "eventTime": "2026-12-03T08:05:00Z",
  "producer": "https://github.com/404-32/byc003",
  "run": { "runId": "run-clean-001" },
  "job": { "namespace": "orders", "name": "clean" },
  "inputs": [{ "namespace": "s3", "name": "raw/orders" }],
  "outputs": [{ "namespace": "s3", "name": "curated/orders" }]
}
```

Optional output schema facets use the OpenLineage schema shape:

```json
{
  "facets": {
    "schema": {
      "fields": [
        { "name": "id", "type": "LONG", "nullable": false },
        { "name": "amount", "type": "DECIMAL", "nullable": false }
      ]
    }
  }
}
```

## Development roadmap

The initial-review milestone is intentionally bounded. The remaining contest
work focuses on:

1. All official OpenLineage event variants and fixture conformance.
2. Persistent graph snapshots and deterministic replay.
3. Column-level lineage for a documented SQL subset.
4. Schema compatibility rules and CI policy output.
5. Adapters for MoonBit DataFrame and Parquet/SQL processing packages.
6. Dependency-graph and external OpenLineage event benchmarks.

## Project documents

- [Initial-review proposal](docs/proposal.md)
- [Architecture](docs/architecture.md)

## Originality and licensing

MoonLineage is an original MoonBit implementation built against the public
OpenLineage event specification. It does not vendor or translate the reference
implementation. The project is licensed under Apache-2.0.
