Skip to content

Fixes 38910: dbt column lineage for sources found in another database service - #38911

Open
tseporamaisa wants to merge 5 commits into
open-metadata:mainfrom
tseporamaisa:fix-dbt-query-lineage-upstream-services
Open

tseporamaisa wants to merge 5 commits into
open-metadata:mainfrom
tseporamaisa:fix-dbt-query-lineage-upstream-services

Conversation

@tseporamaisa

@tseporamaisa tseporamaisa commented Nov 26, 2026 •

Copy link
Copy Markdown

Describe your changes:

Fixes #38910

I worked on dbt query lineage (the step that produces column lineage) because models whose sources are ingested under a different database service get table lineage but never column lineage when the source is found through catalogMapping or searchAcrossDatabases.

Cause. create_dbt_query_lineage passes service_names=source_elements[0], the model's own service, to get_lineage_by_query, so the parser looks up every table in the compiled SQL in that one service. Table lineage resolves the same upstream through _get_table_entity_in_mapped_database (configured service, then *) or the searchAcrossDatabases fallback, and records it under the FQN it was found at, e.g. events.events.mobile_app.users. The parser then searches warehouse.events.mobile_app.users, finds nothing, and no column lineage is created. #35603 swaps a mapped catalog for its database in the SQL, which is enough when that database is in the model's service; the service was never addressed.

Fix. The parser now searches the model's own service first, then the service of each of the model's upstreams, read from datamodel.upstream: the FQNs table lineage already resolved. A wildcard upstream (BigQuery events_*) keeps the configured service's FQN even when its tables were found elsewhere, so the services of the tables its expansion found are added too. search_table_entities returns the first service with a match, so a table that is found in the model's service resolves exactly as before; the other services are searched only when it misses. Without cross-service upstreams the list is just the model's service, so nothing changes for anyone not using these settings.

The model itself is passed to the parser as the query's target (view_entity), so it is never looked up across the wider list. Otherwise a table of the model's name in an upstream's service could take its column lineage while search hasn't indexed the model yet, and with overrideLineage that table's dbt edges would be deleted first. This also saves one lookup per model.

I amended ADR:2026-10-14-dbt-catalog-mapping-resolves-engine-database-names (v3). Its query lineage paragraph says swapping the catalog lets column lineage find the sources, which doesn't hold when the mapped database is in another service, and the service order is a rule worth keeping in the tree.

Alternatives considered

  • service_names=[model service, "*"] when catalogMapping is set. One line, and the same * fallback table lookups use. I didn't pick it because it widens the lookup for databases nobody mapped: any db.schema.table in the SQL that misses in the model's service would match a namesake in whatever service has one, which is what searchAcrossDatabases is the opt-in for. It also needs a second condition to cover searchAcrossDatabases.
  • Looking up which services host each mapped database. Extra lookups per run, for an answer table lineage already has.

The chosen approach adds no lookups of its own: wildcard expansions are reused from the run's cache, which create_dbt_lineage fills before query lineage runs. The cost is one more search per extra upstream service for a table that misses in the model's service. Only hits are cached, so that miss is searched again for each model that reads the table. It only happens for models with upstreams in another service, and it never searches every service.

Edge cases

  • A table the SQL reads that is not a dbt upstream (hard-coded rather than ref()/source()) and misses in the model's service is now also looked up in the upstreams' services. I think that's right, since the model already reads from those services, and the search never goes beyond them.
  • If the same db.schema.table exists under several of the services, the first in the list wins (the model's service, then upstream order), the same first-match rule as today.
  • A wildcard upstream whose expansion is incomplete (None) adds no service beyond its own FQN's.
  • The target is bound to the model, not looked up, so the wider list never applies to it.
  • A mapped catalog in backtick-quoted BigQuery SQL is not rewritten by _map_catalogs_in_sql (Fixes 34633: column lineage for dbt models whose catalog is mapped with catalogMapping #35603), so a BigQuery catalogMapping that renames the database still gets no column lineage. That predates this PR and is a separate change.

Type of change:

  • Bug fix

High-level design:

N/A — small change.

Tests:

Use cases covered

  • A dbt model on trino_svc reads a source that catalogMapping resolves under bq_svc: query lineage links its columns to the bq_svc table.
  • The same with searchAcrossDatabases and no mapping.
  • A BigQuery events_* source whose tables are found under another service, with catalogMapping and with searchAcrossDatabases: its columns are linked.
  • A table the SQL reads under both services resolves in the model's own service.
  • Before search has indexed the model, a table of the model's name in an upstream's service does not receive its column lineage.
  • The existing same-service mapped case and the unmapped case keep their results.

Unit tests

  • I added unit tests for the new/changed logic.
  • Files added/updated: ingestion/tests/unit/test_dbt.py, ingestion/tests/unit/entity_resolution/test_resolver.py
    • TestCatalogMapping::test_query_lineage_links_the_columns_of_a_source_found_in_another_service, for catalogMapping and for searchAcrossDatabases. It runs a manifest through yield_data_models and the resulting link through create_dbt_query_lineage with the real Trino lineage parser; only the search backend is faked, and it knows the source table only under bq_svc. Both cases fail on main (no lineage at all) and pass with this change.
    • test_dbt_query_lineage_links_the_columns_of_a_wildcard_source_in_another_service (test_resolver.py), for both settings, through the real SDK, BigQuery's table resolution and the lineage parser, with only the HTTP boundary faked. Both cases fail without the wildcard part of the change.
    • TestCatalogMapping::test_query_lineage_looks_in_the_models_own_service_first pins the service order (it fails if the upstreams' services go first), and test_query_lineage_keeps_the_model_as_its_target_before_search_has_indexed_it fails without view_entity.
    • TestCatalogMapping now clears the lineage search caches around each test. Without that, the target test passed in a full run only because a previous test had cached the model's table.
    • test_query_lineage_links_the_columns_of_the_mapped_source now sets upstream and upstream_table_ids on its link, as a real DataModelLink has. Its table and search helpers moved to the class so the tests share them.
  • Coverage %: 77% on dbt/metadata.py as a whole from the run below. The changed lines are all covered by the new tests.
  • Locally: 3980 passed, 2 skipped across tests/unit/test_dbt*.py, tests/unit/test_lineage_empty_result.py, tests/unit/lineage/, tests/unit/entity_resolution/ and tests/unit/topology/database/. ruff check and ruff format --check pass on the changed files. basedpyright reports no new finding in metadata.py. scripts/harness/check_decision_records.py is clean.

Backend integration tests

  • Not applicable (no backend API changes).

Ingestion integration tests

  • Not applicable: tests/integration/ has no dbt suite, so the unit tests above are the coverage for this connector.

Playwright (UI) tests

  • Not applicable (no UI changes).

Manual testing performed

I run 2.0.7, so I tested the same change applied to the 2.0.7.2 connector against a local 2.0.7 server holding my two Redshift services (warehouse and events; names changed here), with my real dbt artifacts and catalogMapping: {events: events}:

  1. Before: after a dbt ingestion run, warehouse.warehouse.mobile_app.users has the dbt edge from events.events.mobile_app.users with 0 column links.
  2. Real lineage parser on that model's compiled SQL, called as create_dbt_query_lineage does: service_names=["warehouse"] (current) gives no lineage; ["warehouse", "events"] (what this change passes) gives 1 edge events.events.mobile_app.users → warehouse.warehouse.mobile_app.users with 7 column links.
  3. After: a full dbt ingestion run with the patched connector. The same edge now has the 7 column links from step 2. The run's only other new errors were the local OpenSearch answering 429 to the extra lineage index updates.

UI screen recording:

Not applicable — ingestion-only bug fix with no UI change.

Checklist:

  • I have read CONTRIBUTING.md (the pre-review gate) and the contributor docs.

  • My PR title is Fixes <issue-number>: <short explanation>

  • My PR is linked to a GitHub issue via Fixes #<issue-number> above.

  • I have commented on my code, particularly in hard-to-understand areas.

  • For JSON Schema changes: I updated the migration scripts or explained why it is not needed.

  • For a UI feature or feature task: as part of PR creation, I ran the PR build in Docker with sample data and embedded a verified GitHub-hosted recording plus build/health evidence above (or explained why this requirement is not applicable).

  • I have added tests (unit / integration / Playwright as applicable) and listed them above.

  • Self-review: I reviewed the final diff against REVIEW.md (the pr-checklist skill runs it) and fixed every 🔴 finding, or answered it on the line below.
    Reviewed at 58d33a2591. Fixed: the first pass flagged the ADR amendment for saying non-upstream tables stay in the model's service, which the code doesn't keep; I corrected the text. After Copilot's wildcard finding, the next pass's nits were also fixed: the target is bound to the model (view_entity), the service order is pinned by a test, and the ADR Guard line names the new test. No 🔴 open. Placement: dbt runs whose upstreams (or wildcard tables) are found under another service through catalogMapping or searchAcrossDatabases; only the column lineage edges they send change. dbt Cloud's _yield_column_lineage has the same single-service lookup; that predates this PR and is left for a follow-up. Trade-off: weighed, holds because the search is bounded to the model's service and the services its upstreams were found in. Recommendation: approve.

  • Decision check: if this PR decides something the next contributor could undo — a schema field, REST path or format another component or repo must agree with, a new default or limit, an ordering that must hold, or a rule stated only in this description — it adds a record under docs/decisions/ (see its README.md). This description is gone from git log after the squash-merge.

  • I have added a test that covers the exact scenario we are fixing. For complex issues, comment the issue number in the test for future reference.

Questions for maintainers

  • I can't set the issue's Shipping project fields (Status, Source, Priority, Domain, Release) as an outside contributor. Could someone set them?
  • I run 2.0.x. Would a [2.0] backport be welcome? I'm happy to open one.

…were found

catalogMapping and searchAcrossDatabases can resolve a model's upstream under another
database service. Table lineage uses that table, but query lineage searched only the
model's own service, so the model never got column lineage from it.
@tseporamaisa
tseporamaisa requested a review from a team as a code owner November 26, 2026 19:21
@tseporamaisa
tseporamaisa requested review from Khairajani and a balanced review from Copilot November 26, 2026 19:21
@github-actions

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Cross-service BigQuery wildcard upstreams omit the resolved service from lineage lookup, leaving column lineage missing despite successful table lineage.

1 open issue
What changed in this PR

Extends #35603’s catalog-mapping fix so dbt column lineage can resolve sources ingested in another database service, addressing #38910.

Changes:

  • Searches the model’s service first, then deduplicated upstream services.
  • Adds real-parser regression tests for cross-service resolution through catalogMapping and searchAcrossDatabases.
  • Updates the decision record with lookup precedence and scope.

Placement: The change affects dbt ingestion’s column-lineage requests. Tests were inspected but not run; persisted server results were not verified.

Trade-off: weighed by the author, holds because additional lookups are limited to upstream services and stop at the first match.

Recommendation: request changes to complete handling of supported cross-service upstreams.

File Description
ingestion/​tests/​unit/​test_dbt.py Shares test helpers and adds cross-service column-lineage regressions.
ingestion/​src/​metadata/​ingestion/​source/​database/​dbt/​metadata.py Adds upstream services to query-lineage lookup candidates.
docs/​decisions/​2026-10-14-dbt-catalog-mapping-resolves-engine-database-names.md Documents cross-service lookup order and scope.

Review effort: Balanced

…les were found

A wildcard upstream such as BigQuery's events_* keeps the configured service's FQN, while
catalogMapping or searchAcrossDatabases can find its tables under another service. Query
lineage now adds the services of the tables its expansion found.
The model is the table its SQL creates, so query lineage now hands it to the parser
instead of looking it up across the wider service list, where a namesake in an
upstream's service could take its column lineage before search has indexed it.
Tests pin that the model's own service is searched first, and TestCatalogMapping
clears the lineage search caches between tests.
Copilot AI balanced review requested due to automatic review settings November 26, 2026 20:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the cross-service dbt column-lineage change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 27, 2026 13:14
@Khairajani Khairajani added the safe to test Add this label to run secure Github workflows on PRs label Nov 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified, although tests were not rerun during this review.

No open issues 🎉

Review effort: Balanced

@gitar-bot

gitar-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 🟡 Medium risk

Expanding dbt column-lineage searches to upstream services could associate SQL columns with a same-named table in the first matching service.

Fixes dbt query lineage so column lineage is created for models whose sources live in another database service, found via catalogMapping or searchAcrossDatabases. The parser now searches the model's own service first, then the services of its upstreams, while the model itself is bound as the query target. No issues found.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dbt: no column lineage when catalogMapping resolves a model's source in another database service

3 participants