Skip to content

Fixes 38288: Reuse dbt tables already found during the run - #38291

Merged
akashverma0786 merged 4 commits into
open-metadata:mainfrom
larspettermadsstuen:dbt-table-lookup-cache
Nov 24, 2026
Merged

akashverma0786 merged 4 commits into
open-metadata:mainfrom
larspettermadsstuen:dbt-table-lookup-cache

Conversation

@larspettermadsstuen

@larspettermadsstuen larspettermadsstuen commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Describe your changes:

Fixes #38288

The dbt connector looked a table up as its own model, and again for every model, test, and source freshness result that names it. Nothing kept the table found the first time.

_resolve_table builds the FQN, looks the table up, and keeps what it finds in an LRUCache of LRU_CACHE_SIZE (4096) entries. The cache is keyed by the database, schema, and name the manifest uses, plus whether the name is literal. The model loop, upstream parsing (which dbt tests also go through), and add_dbt_sources all use it. The cache is reset per project in yield_data_models, like reported_unresolved_upstreams.

The cap has to hold a whole project to keep the saving: every dbt test comes after the last model in the manifest, so with a small cap the tests find their parents evicted. On the manifest below, with the search stubbed, 1000 entries made 16,139 searches and 2000 made 11,271; 3000 and 4096 both made 7,670. The cached tables are the same objects the data-model links keep for the whole run, so the cache adds no table data of its own.

Tables that are not found are not kept, so they are still looked up again. _get_table_entity returns None for a failed search as well as for a missing table. Keeping None would turn one transient search error into a missing table for the rest of the run.

On our production manifest (2,862 models, seeds, snapshots, and sources; run results for 5,256 tests), yield_data_models with es_search_from_fqn stubbed made 40,364 searches before this change and 7,670 after, for the same 2,844 distinct tables. The parsed data-model links, their upstreams, and the tests' upstreams were identical before and after.

The stub bypasses _search_es_entity, which is already lru_cache(maxsize=512), so it overstates what reaches the server. In the production run on 2.0.8, parsing sent 14,529 GET /api/v1/search/fieldQuery and 14,391 GET /api/v1/tables/name/... (from the server access log, about 9.5 minutes of server time). With this change parsing makes at most 7,670 searches, and at least one per distinct table (2,844), before that cache removes any repeats.

That cache does not cover this case for two reasons. It keys each search separately, and resolving one model makes two different searches (_build_table_fqn, then _get_table_entity with fields=sourceHash), plus the catalogMapping and searchAcrossDatabases branches. And 512 entries do not hold a project: every test comes after the last model, so by then the parents are evicted. Raising that cap would change every caller of es_search_from_fqn in every connector; this cache is scoped to the dbt source and holds one resolved table per manifest name.

Type of change:

  • Bug fix
  • Improvement
  • New feature
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation

High-level design:

N/A — small bug fix in one file. The two call sites that built an FQN and then looked the table up now share one helper, and add_dbt_sources uses it too.

Tests:

Use cases covered

  • A table that is a model and the parent of another model is searched for no more often than a table met once
  • The child model still gets the parent's id in upstream_table_ids
  • A source freshness result costs no extra search for its source table
  • Freshness tests still attach to the physical source table, and are still skipped when that table is not in OpenMetadata

Unit tests

  • I added unit tests for the new/changed logic.
  • Files added/updated: ingestion/tests/unit/test_dbt.py
    • TestDbtLineageReusesParsedUpstream::test_a_table_used_as_model_and_upstream_is_searched_for_once: before the change, raw_orders cost 4 searches and orders 2.
    • TestDbtSourceIdentifiers::test_freshness_reuses_the_source_table_lookup: before the change, a run with sources.json made 2 more searches than one without.
    • TestAddDbtSourceFreshnessResults._source and TestDbtLineageReusesParsedUpstream._source now set resolved_tables, since they build the source with __new__.
  • tests/unit/test_dbt.py, test_dbt_test_definition.py, test_dbt_test_descriptions.py: 346 passed, 1 failed. TestStorageStreamingBehavior::test_gcs_passes_generator_to_grouping also fails on main in my venv, because google-cloud-storage is not installed there.
  • Coverage: all 17 changed statements in metadata.py are covered (coverage run --include='*/dbt/metadata.py' -m pytest ...).

Backend integration tests

  • Not applicable (no backend API changes).

Ingestion integration tests

  • Not applicable. A live server would answer the repeated searches correctly, so an integration test that checks the result stays green with the bug. The unit tests count catalog searches instead.

Playwright (UI) tests

  • Not applicable (no UI changes).

Manual testing performed

  1. Ran yield_data_models over our production manifest and 149 run-results files with es_search_from_fqn stubbed to answer every search, counting calls: 40,364 on main, 7,670 on this branch. The stub bypasses the 512-entry _search_es_entity cache; production numbers are above.
  2. Hashed the resulting data-model links (FQN, upstream list, upstream ids) and test upstreams on both: identical.
  3. Ran both new tests before making each change: both failed with the counts given above.

UI screen recording:

Not applicable — ingestion-only change.

Checklist:

  • I have read the CONTRIBUTING document.

  • 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.
    No 🔴. Two 🟡: add_dbt_sources repeated the lookup (fixed here); tables that are not found are looked up again (left, see above). Gitar asked for a bounded cache; it now uses LRUCache(LRU_CACHE_SIZE).

  • 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.
    Nothing to record. The cap reuses the existing LRU_CACHE_SIZE default, and the rule "do not keep misses" is stated in a comment next to the code.

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

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge.

Summary

Reuses successful dbt table lookups across models, upstream references, and source freshness checks.

  • The dbt parser reuses found tables across models, lineage, and freshness checks.

Reviews (6) · Last reviewed commit: "Bound the dbt table cache with LRUCache(..." · Reviewed by Greptile

larspettermadsstuen and others added 2 commits November 20, 2026 11:00
…e them.

A table is looked up as its own model and again for every model and test
that depends on it. Keep each table found in the run and reuse it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
add_dbt_sources looked the source table up itself, and the source's own
lookup right after it did the same search again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A smaller cap loses most of the saving: dbt tests come after the last
model in the manifest, so their parents would be evicted by then.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@larspettermadsstuen
larspettermadsstuen marked this pull request as ready for review November 20, 2026 11:03
@larspettermadsstuen
larspettermadsstuen requested a review from a team as a code owner November 20, 2026 11:03
@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.

# Conflicts:
#	ingestion/tests/unit/test_dbt.py
@ulixius9 ulixius9 added the safe to test Add this label to run secure Github workflows on PRs label Nov 24, 2026
@gitar-bot

gitar-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 🟡 Medium risk 1 closed / 1 findings

Caching resolved dbt tables changes lookup reuse across models and freshness results; incorrectly keyed entries could affect lineage associations.

Reuses dbt tables already found during a run by caching lookups in a bounded LRUCache(LRU_CACHE_SIZE), so models, upstream parents, tests, and source freshness results stop repeating catalog searches. The unbounded table cache finding was resolved by switching to the bounded LRU cache. No open issues found.

✅ 1 closed
✅ Quality: resolved_tables is an unbounded dict cache of full Table entities

📄 ingestion/src/metadata/ingestion/source/database/dbt/metadata.py:184-187 📄 ingestion/src/metadata/ingestion/source/database/dbt/metadata.py:977-991 📄 ingestion/src/metadata/ingestion/source/database/dbt/metadata.py:1008
self.resolved_tables is a plain dict that keeps every Table found during a project's parse. Each entry is a full Table entity, columns included. It is cleared only when the next project starts, so a manifest with thousands of models and sources keeps all of them in memory for the whole parse. The repository's hard rule says every cache must be bounded (100–1000 entries by default, using OrderedDict + popitem(last=False), lru_cache, or cachetools.LRUCache with hit/miss counts). Most reuse happens between a model and the models that depend on it, so an LRU cache of about 1000 entries keeps nearly all the savings with a fixed memory ceiling. The cap is a judgment call, so the PR should also record its value and the rule that misses are not cached, for example in a dbt decision record.

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

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 ingestion looks up the same table again for every model and test that depends on it

4 participants