Repository navigation
data: fix #1318 type mismatch + enforce contract on collab silver + dbt-coverage gate #1320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
SharedQA
wants to merge
6
commits into
constructorfabric:main
from
SharedQA:claude/data-correctness-collab
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cb5b334
data: fix #1318 type mismatch, enforce collab-silver contract, add db…
SharedQA 8e73749
Merge branch 'main' into claude/data-correctness-collab
SharedQA 6fab20e
data: address review on dbt-coverage gate
59ab13e
Merge remote-tracking branch 'origin/main' into u-1320
SharedQA 4b490a4
ci(data): move dbt-coverage gate to the consolidated data-quality.yml…
SharedQA 7acb534
Merge branch 'main' into claude/data-correctness-collab
SharedQA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Data-contract & test-completeness coverage — the QA gate that MEASURES, in CI, | ||
| # how much of the metric path is under an enforced dbt contract + key tests, and | ||
| # how many bronze sources have a freshness check. | ||
| # | ||
| # Runs on `dbt parse` only — NO database, NO credentials, NO seeded data needed | ||
| # (contract.enforced, tests, and source freshness all live in the parsed | ||
| # manifest). So every MR gets the numbers + the per-model gap list as a | ||
| # downloadable artifact and a job summary. | ||
| # | ||
| # Today it is REPORT-ONLY (informational): the baseline is 1/124 contracted, so | ||
| # blocking would red every PR. Dev burns the debt down (adds contracts); flip | ||
| # the marked step to `--check` per domain to make it blocking as coverage rises. | ||
| # Tracked as a QA-logged bug; QA owns this gate, Dev owns filling the contracts. | ||
|
|
||
| name: Data Contracts | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [main] | ||
| paths: | ||
| - "src/ingestion/**" | ||
| - "scripts/ci/dbt_coverage.py" | ||
| - ".github/workflows/data-contracts.yml" | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| dbt-coverage: | ||
| name: dbt-coverage | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 15 | ||
| steps: | ||
| - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0 | ||
| with: | ||
| python-version: "3.12" | ||
|
|
||
| - name: Install dbt (core + clickhouse adapter) | ||
| run: pip install --quiet 'dbt-core~=1.10' 'dbt-clickhouse~=1.9' | ||
|
|
||
| - name: dbt parse (manifest only — no warehouse connection) | ||
| working-directory: src/ingestion/dbt | ||
| env: | ||
| # parse does NOT connect; a placeholder satisfies the profile's env_var. | ||
| CLICKHOUSE_PASSWORD: ci-noconnect | ||
| DBT_PROFILES_DIR: ${{ github.workspace }}/src/ingestion/dbt | ||
| run: | | ||
| if [ -f packages.yml ]; then | ||
| dbt deps | ||
| fi | ||
| dbt parse --no-version-check | ||
|
|
||
| - name: Data-contract & test coverage report | ||
| run: | | ||
| python scripts/ci/dbt_coverage.py src/ingestion/dbt/target/manifest.json \ | ||
| | tee /tmp/dbt-coverage.txt | ||
| { | ||
| echo '## dbt data-contract coverage' | ||
| echo '```' | ||
| grep -E 'metric-path models|enforced contract|key tests|^sources:|freshness declared' /tmp/dbt-coverage.txt || true | ||
| echo '```' | ||
| echo '_Report-only. Dev fills contracts; flip the gate step to `--check` per domain to enforce._' | ||
| } >> "$GITHUB_STEP_SUMMARY" | ||
|
|
||
| # ── Flip to blocking per domain as contracts land (QA ratchet) ── | ||
| # - name: Enforce (blocking) | ||
| # run: python scripts/ci/dbt_coverage.py src/ingestion/dbt/target/manifest.json --check | ||
|
|
||
| - name: Upload coverage report + manifest (downloadable results) | ||
| if: always() | ||
| uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3 | ||
| with: | ||
| name: dbt-coverage | ||
| path: | | ||
| /tmp/dbt-coverage.txt | ||
| src/ingestion/dbt/target/manifest.json | ||
| if-no-files-found: warn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| #!/usr/bin/env python3 | ||
| """dbt_coverage.py — data-contract & test-completeness gate (the path to dbt 100%). | ||
|
|
||
| Walks the compiled dbt manifest and asserts that every model in the path to an | ||
| user-facing metric (schema silver/gold, plus staging models that feed a | ||
| `silver:*` tag-union) carries: | ||
|
|
||
| 1. an ENFORCED contract — config.contract.enforced == true, i.e. declared | ||
| column names + data_types. This is what turns a type drift like issue | ||
| #1318 (Float64 vs Int64) into a BUILD failure instead of a runtime | ||
| ClickHouse NO_COMMON_TYPE error; and | ||
| 2. key data tests — not_null + unique on its key. | ||
|
|
||
| "100%" = zero metric-path models below this bar. | ||
|
|
||
| Usage: | ||
| dbt_coverage.py [MANIFEST] # report coverage, exit 0 | ||
| dbt_coverage.py [MANIFEST] --check # exit 1 if any metric-path model has a gap | ||
|
|
||
| Needs a compiled manifest (default src/ingestion/dbt/target/manifest.json) — | ||
| run `dbt parse` (or any build/compile) in the dbt project first. | ||
| """ | ||
| import json | ||
| import pathlib | ||
| import sys | ||
|
|
||
| DEFAULT_MANIFEST = pathlib.Path("src/ingestion/dbt/target/manifest.json") | ||
|
|
||
|
|
||
| def is_metric_path(node: dict) -> bool: | ||
| """A dbt model on the path to an user-facing metric: silver/gold, the | ||
| silver:* tag-union feeders, AND bronze-promotion views (the first typed | ||
| boundary). NOTE: gold serving tables here are ClickHouse views built by | ||
| analytics-api migrations, not dbt — their contract lives in the API-contract | ||
| track (openapi/schemathesis), not this gate.""" | ||
| schema = (node.get("config") or {}).get("schema") or node.get("schema") or "" | ||
| name = node.get("name") or "" | ||
| tags = node.get("tags") or [] | ||
| return ( | ||
| schema in ("silver", "gold") | ||
| or name.endswith("__bronze_promoted") | ||
| or any(t == "silver" or t.startswith("silver:") for t in tags) | ||
| ) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| check = "--check" in sys.argv | ||
| positional = [a for a in sys.argv[1:] if not a.startswith("--")] | ||
| manifest_path = pathlib.Path(positional[0]) if positional else DEFAULT_MANIFEST | ||
|
|
||
| if not manifest_path.exists(): | ||
| print(f"✗ manifest not found: {manifest_path}") | ||
| print(" compile it first: (cd src/ingestion/dbt && dbt parse)") | ||
| sys.exit(2) | ||
|
|
||
| manifest = json.loads(manifest_path.read_text()) | ||
| nodes = manifest.get("nodes", {}) | ||
|
|
||
| # --- Sources: every declared source should carry a freshness check --- | ||
| sources = manifest.get("sources", {}) | ||
| if sources: | ||
| fresh = sum(1 for s in sources.values() if s.get("freshness") and s.get("loaded_at_field")) | ||
| print(f"sources: {len(sources)} — freshness declared: {fresh}/{len(sources)} " | ||
| f"({100 * fresh // len(sources)}%)") | ||
| for s in sorted(sources.values(), key=lambda s: s.get("name", "")): | ||
| if not (s.get("freshness") and s.get("loaded_at_field")): | ||
| print(f" ✗ source {s.get('source_name')}.{s.get('name')}: no freshness check") | ||
|
|
||
| models = { | ||
| uid: n | ||
| for uid, n in nodes.items() | ||
| if n.get("resource_type") == "model" and is_metric_path(n) | ||
| } | ||
|
|
||
| # (model uid, column) -> set of test kinds, so we can check the ACTUAL key | ||
| # column is tested, not just that the model has *some* not_null + *some* | ||
| # unique somewhere (which a model can satisfy on unrelated columns). | ||
| col_tests: dict[tuple, set] = {} | ||
| for n in nodes.values(): | ||
| if n.get("resource_type") != "test": | ||
| continue | ||
| kind = (n.get("test_metadata") or {}).get("name") or "" | ||
| col = n.get("column_name") or ( | ||
| (n.get("test_metadata") or {}).get("kwargs") or {} | ||
| ).get("column_name") | ||
| if not col: | ||
| continue | ||
| for dep in (n.get("depends_on") or {}).get("nodes", []): | ||
| col_tests.setdefault((dep, col), set()).add(kind) | ||
|
|
||
| def key_columns(node: dict) -> list: | ||
| key = (node.get("config") or {}).get("unique_key") | ||
| if isinstance(key, str): | ||
| return [key] | ||
| if isinstance(key, list) and key: | ||
| return key | ||
| return ["unique_key"] | ||
|
|
||
| gaps, contract_ok, tests_ok = [], 0, 0 | ||
| for uid, n in sorted(models.items(), key=lambda kv: kv[1].get("name", "")): | ||
| # contract.enforced lives at the node top level AND under config in | ||
| # current dbt; read both so the check is correct across versions. | ||
| enforced = bool( | ||
| (n.get("contract") or {}).get("enforced") | ||
| or ((n.get("config") or {}).get("contract") or {}).get("enforced") | ||
| ) | ||
| keys = key_columns(n) | ||
| has_keys = all( | ||
| "not_null" in col_tests.get((uid, col), set()) | ||
| and "unique" in col_tests.get((uid, col), set()) | ||
| for col in keys | ||
| ) | ||
| contract_ok += enforced | ||
| tests_ok += has_keys | ||
| problems = [] | ||
| if not enforced: | ||
| problems.append("no enforced contract") | ||
| if not has_keys: | ||
| problems.append(f"missing not_null+unique on key column(s) {keys}") | ||
| if problems: | ||
| gaps.append((n.get("name"), problems)) | ||
|
|
||
| total = len(models) | ||
| print(f"metric-path models: {total}") | ||
| if total: | ||
| print(f" enforced contract: {contract_ok}/{total} ({100 * contract_ok // total}%)") | ||
| print(f" key tests : {tests_ok}/{total} ({100 * tests_ok // total}%)") | ||
| for name, problems in gaps: | ||
| print(f" ✗ {name}: {', '.join(problems)}") | ||
|
|
||
| if check and gaps: | ||
| print(f"\n✗ data-contract gate FAILED: {len(gaps)} metric-path model(s) below the contract.") | ||
| sys.exit(1) | ||
| if check: | ||
| print("✓ data-contract gate passed — 100% of metric-path models contracted + key-tested") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.