# 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

OpenLineage compatibility candidate, implemented and tested with MoonBit
`0.1.20261126` and `moonc v1.0.0-rc1`.

Interoperability baseline:

- OpenLineage core schema `2.0.2`
- `SchemaDatasetFacet` `1.2.0`
- `openlineage-python` `1.55.0`

The current foundation includes:

- Normalized OpenLineage `RunEvent` model.
- Required `schemaURL` and facet metadata validation.
- Run, Job, Dataset, Input Dataset, and Output Dataset facet preservation.
- Dataset schema facet support, including nested field metadata.
- Unknown extension properties are preserved instead of silently dropped.
- All standard RunEvent states: START, RUNNING, COMPLETE, ABORT, FAIL, OTHER.
- Idempotent event ingestion.
- Repeated RUNNING/OTHER events are retained unless the complete payload is a
  duplicate.
- 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
moon run cmd/main -- roundtrip compat/run-event.json _build/run-event.roundtrip.json
```

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",
  "schemaURL": "https://openlineage.io/spec/2-0-2/OpenLineage.json#/$defs/RunEvent",
  "run": { "runId": "11111111-1111-4111-8111-111111111111" },
  "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": {
      "_producer": "https://github.com/404-32/byc003",
      "_schemaURL": "https://openlineage.io/spec/facets/1-2-0/SchemaDatasetFacet.json#/$defs/SchemaDatasetFacet",
      "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.

## Known limitations

- The graph is in memory. Persistence and snapshot loading are planned.
- Job state reflects ingestion order; MoonLineage does not reorder historical
  events by timestamp.
- Graph query complexity grows with the number of known jobs and datasets.
  Large-scale indexing and benchmarks are planned.
- The pure core is tested on wasm, wasm-gc, and JS. The file-based CLI uses
  MoonBit async filesystem APIs and targets wasm/native.
- DatasetEvent and JobEvent ingestion is not implemented yet.

## Project documents

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

## Interoperability verification

The conformance fixture is validated in both directions:

```bash
python -m pip install -r compat/requirements.txt
mkdir -p _build/compat
moon run cmd/main -- roundtrip compat/run-event.json _build/compat/run-event.roundtrip.json
python scripts/verify_openlineage.py compat/run-event.json _build/compat/run-event.roundtrip.json
```

The verifier requires:

1. Input and output to pass the official OpenLineage `2.0.2` JSON Schema.
2. The output to reconstruct successfully through `openlineage-python 1.55.0`.
3. Input and MoonLineage output to be semantically identical JSON.

## 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.
