Skip to content

Fixes 32030: Look up Query entities by service-qualified FQN - #32031

Merged
mohittilala merged 10 commits into
open-metadata:mainfrom
larspettermadsstuen:fix-query-fqn-lookup
Sep 10, 2026
Merged

mohittilala merged 10 commits into
open-metadata:mainfrom
larspettermadsstuen:fix-query-fqn-lookup

Conversation

@larspettermadsstuen

@larspettermadsstuen larspettermadsstuen commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Describe your changes:

Fixes #32030

Usage ingestion looked up Query entities by the MD5 of the SQL only. The server stores them as service.hash, so a second table with the same query hit 409 and the usage sink skipped attaching usage / users / usedBy.

This looks up service.hash the same way query-cost already does. If create still 409s (two tables racing), we GET again and keep writing the relations.

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

Tests:

Use cases covered

  • Same SQL on a second table finds the existing Query (service.hash) and still PUTs /usage
  • Lookup never uses the bare hash
  • Missing Query is created
  • Create 409 retries GET by the qualified FQN, then usage still writes
  • Non-409 create errors still raise
  • Live: two tables, same SQL, share one Query whose FQN is service.hash and both have usage

Unit tests

  • I added unit tests for the new/changed logic.
  • Files added/updated: ingestion/tests/unit/ometa/test_query_mixin.py
  • Coverage on changed classes: 75% of query_mixin.py overall (get_entity_queries / publish_query_cost unchanged and not exercised here). Changed get-or-create / FQN lookup paths are covered.
  • Coverage %: 75% on query_mixin.py (5 tests, all passing locally)

Backend integration tests

  • Not applicable (no backend API changes).

Ingestion integration tests

  • I added/updated ingestion integration tests for connector changes.
  • Files added/updated: ingestion/tests/integration/ometa/test_ometa_table_api.py (test_same_query_on_second_table_attaches_usage)
  • Not applicable (no ingestion changes).

Playwright (UI) tests

  • Not applicable (no UI changes).

Manual testing performed

  1. pytest ingestion/tests/unit/ometa/test_query_mixin.py (5 passed)
  2. Local stack: quickstart images openmetadata/db:2.0.0 + openmetadata/server:2.0.0 (skip Maven; no JDK). GET /api/v1/system/version → 2.0.0
  3. pytest tests/integration/ometa/test_ometa_table_api.py::TestOMetaTableAPI::test_same_query_on_second_table_attaches_usage (1 passed)

UI screen recording / screenshots:

Not applicable.

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 UI changes: I attached a screen recording and/or screenshots above.

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

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

Greptile Summary

The PR updates ingestion to look up Query entities by their service-qualified FQN and retries that lookup after a create conflict.

  • Centralizes construction of the service.hash Query FQN.
  • Preserves usage relation writes when an existing Query is found.
  • Adds unit and integration coverage for shared SQL across multiple tables.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
ingestion/src/metadata/ingestion/ometa/mixins/query_mixin.py Uses service-qualified Query FQNs for lookup and retries lookup after a conflicting create.
ingestion/tests/unit/ometa/test_query_mixin.py Covers qualified lookup, creation, conflict recovery, non-conflict errors, and usage writes for existing Queries.
ingestion/tests/integration/ometa/test_ometa_table_api.py Verifies that two tables sharing the same SQL resolve to one Query and both retain usage relations.

Reviews (6): Last reviewed commit: "Merge branch 'main' into fix-query-fqn-l..." | Re-trigger Greptile

@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

Comment on lines +61 to +72
try:
resp = self.client.put(self.get_suffix(Query), data=query.model_dump_json())
if resp and resp.get("id"):
query_entity = Query(**resp)
except APIError as err:
# Concurrent create: look up the FQN we just raced on, then still
# attach usage/users/usedBy. Do not reuse __get_query_by_hash — it
# caches misses.
if err.status_code == 409:
query_entity = self.get_by_name(entity=Query, fqn=fqn)
else:
raise

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Edge Case: 409 retry only catches APIError, not raw HTTPError

The new retry only handles APIError, but _one_request only wraps HTTP errors in APIError when the response body contains a code field (client.py:391-396); otherwise it re-raises the raw requests HTTPError. A 409 conflict whose body lacks code would therefore escape the except APIError and propagate uncaught, defeating the concurrent-create recovery. OpenMetadata error responses normally include code, so this is unlikely in practice, but consider catching by HTTP status (e.g. inspecting err.response.status_code on HTTPError) or confirming the server always returns a coded 409 body.

Was this helpful? React with 👍 / 👎

@larspettermadsstuen larspettermadsstuen Aug 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

A 409 from this endpoint includes code, so it shows up as APIError — same thing the usage sink already handles. I'd leave the catch as-is unless we hit a good response without that.

@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@larspettermadsstuen larspettermadsstuen changed the title Fixes #32030: Look up Query entities by service-qualified FQN Fixes 32030: Look up Query entities by service-qualified FQN Aug 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@larspettermadsstuen
larspettermadsstuen marked this pull request as ready for review August 25, 2026 13:21
@larspettermadsstuen
larspettermadsstuen requested a review from a team as a code owner August 25, 2026 13:21
@github-actions

github-actions Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR checks passed

The linked issue has a description and all required Shipping project fields set. Thanks!

harshach
harshach previously approved these changes Aug 25, 2026
@harshach harshach added safe to test Add this label to run secure Github workflows on PRs To release Will cherry-pick this PR into the release branch labels Aug 25, 2026
larspettermadsstuen and others added 4 commits August 25, 2026 21:03
…d FQN

Usage get-or-create was asking for the bare SQL hash. The server stores
queries as service.hash, so a second table with the same SQL 409'd and
skipped usage/users/usedBy. Look up the same FQN query-cost already uses,
and retry GET after a create race.

Co-authored-by: Cursor <[email protected]>
@github-actions github-actions Bot removed the safe to test Add this label to run secure Github workflows on PRs label Aug 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@larspettermadsstuen

larspettermadsstuen commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor Author

@harshach I rebased the PR and it seems GHA removed the safe-to-test label - apologies!

@harshach harshach added the safe to test Add this label to run secure Github workflows on PRs label Aug 25, 2026
@github-actions github-actions Bot removed the safe to test Add this label to run secure Github workflows on PRs label Aug 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@harshach harshach added the safe to test Add this label to run secure Github workflows on PRs label Aug 27, 2026
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@mohittilala mohittilala added the safe to test Add this label to run secure Github workflows on PRs label Sep 9, 2026
@github-actions github-actions Bot removed the safe to test Add this label to run secure Github workflows on PRs label Sep 9, 2026
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@mohittilala mohittilala added the safe to test Add this label to run secure Github workflows on PRs label Sep 9, 2026
@gitar-bot

gitar-bot Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Code Review 👍 Approved with suggestions 0 resolved / 1 findings

Updates Query entity lookups to use service-qualified FQNs and handles duplicate entity creation errors with GET retries. Consider ensuring that the 409 retry catches raw HTTP errors alongside APIError instances.

💡 Edge Case: 409 retry only catches APIError, not raw HTTPError

📄 ingestion/src/metadata/ingestion/ometa/mixins/query_mixin.py:61-72

The new retry only handles APIError, but _one_request only wraps HTTP errors in APIError when the response body contains a code field (client.py:391-396); otherwise it re-raises the raw requests HTTPError. A 409 conflict whose body lacks code would therefore escape the except APIError and propagate uncaught, defeating the concurrent-create recovery. OpenMetadata error responses normally include code, so this is unlikely in practice, but consider catching by HTTP status (e.g. inspecting err.response.status_code on HTTPError) or confirming the server always returns a coded 409 body.

🤖 Prompt for agents
Code Review: Updates Query entity lookups to use service-qualified FQNs and handles duplicate entity creation errors with GET retries. Consider ensuring that the 409 retry catches raw HTTP errors alongside APIError instances.

1. 💡 Edge Case: 409 retry only catches APIError, not raw HTTPError
   Files: ingestion/src/metadata/ingestion/ometa/mixins/query_mixin.py:61-72

   The new retry only handles `APIError`, but `_one_request` only wraps HTTP errors in `APIError` when the response body contains a `code` field (client.py:391-396); otherwise it re-raises the raw `requests` `HTTPError`. A 409 conflict whose body lacks `code` would therefore escape the `except APIError` and propagate uncaught, defeating the concurrent-create recovery. OpenMetadata error responses normally include `code`, so this is unlikely in practice, but consider catching by HTTP status (e.g. inspecting `err.response.status_code` on `HTTPError`) or confirming the server always returns a coded 409 body.

Options

Display: compact → Counting what did not apply, without listing it.

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

@sonarqubecloud

sonarqubecloud Bot commented Sep 9, 2026

Copy link
Copy Markdown

@mohittilala

Copy link
Copy Markdown
Contributor

Hey @larspettermadsstuen, thanks for this. Merged main in and fixed one thing that broke on the way: main had dropped from typing import List from test_ometa_table_api.py, so your two List[Query] annotations were resolving to an undefined name at runtime. They are list[Query] now.

@mohittilala
mohittilala added this pull request to the merge queue Sep 10, 2026
Merged via the queue into open-metadata:main with commit 5197d0a Sep 10, 2026
164 of 174 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Backport to 2.0 is under way. It is built from openmetadata-collate, which holds the credentials needed to open a PR here, and it will comment again with the result — nothing is needed from you unless it reports otherwise.

@github-actions

Copy link
Copy Markdown
Contributor

🤖 Backport to 1.13 is under way. It is built from openmetadata-collate, which holds the credentials needed to open a PR here, and it will comment again with the result — nothing is needed from you unless it reports otherwise.

@gh-bot-collate

Copy link
Copy Markdown

🤖 Backport to 1.13 failed. The conflict could not be resolved, so this change is not on that branch. Resolver run.

@gh-bot-collate

Copy link
Copy Markdown

🤖 Backport to 2.0 failed. The conflict could not be resolved, so this change is not on that branch. Resolver run.

@larspettermadsstuen

larspettermadsstuen commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Hey @larspettermadsstuen, thanks for this. Merged main in and fixed one thing that broke on the way: main had dropped from typing import List from test_ometa_table_api.py, so your two List[Query] annotations were resolving to an undefined name at runtime. They are list[Query] now.

Thank you!

OmRajput17 added a commit to OmRajput17/OpenMetadata that referenced this pull request Sep 21, 2026
…pen-metadata#32031) changed Query lookups to key on service.<hash> insteadof the bare hash, since the same SQL under two services must resolve totwo distinct Query entities. The cache here still keyed on the bare hash,so after merging main, identical SQL seen under a second service wouldincorrectly hit the first service's cached entity and attach usage to thewrong service.Key the cache on the same qualified FQN the lookup uses instead. Add aregression test covering two services sharing one SQL statement, and ranruff format/check on the touched files per review feedback.
akashverma0786 pushed a commit to zerafachris/OpenMetadata that referenced this pull request Sep 26, 2026
…pen-metadata#32254)

* fix(ingestion): cache Query and User lookups during usage ingestionWrap _get_or_create_query with a SkipNoneLRUCache keyed by query hash sorepeated tables referencing the same SQL skip the redundant GET/PUT, whilenever caching a miss (another table may have just created the Query).Add get_cached_user_reference, backed by an LRUCache keyed by username,shared between the staging get_by_name(User) call and the lifecycleget_entity_reference(User) call, caching misses too since a user absentat the start of a run will not appear mid-run.Fixes open-metadata#32210

* fix: address review feedback- Restore .root when extracting FQN from cached User EntityReference in  staging, fixing a bug where a model object was passed to  CreateQueryRequest(users=...) instead of a string.- Scope Query and User reference caches per-OpenMetadata-client-instance  instead of at the class level, so multiple clients/catalogs in the same  process never share cached data.- Reduce cache sizes from 4096 to 1000 to match repo convention.

* fix(ingestion): resolve usage cache review feedback

Cover failed query lookup and creation retries, and align the cache changes with current Python formatting.

* fix(ingestion): handle cached user references

* fix: key Query cache on service-qualified FQN, not bare hashUpstream (open-metadata#32031) changed Query lookups to key on service.<hash> insteadof the bare hash, since the same SQL under two services must resolve totwo distinct Query entities. The cache here still keyed on the bare hash,so after merging main, identical SQL seen under a second service wouldincorrectly hit the first service's cached entity and attach usage to thewrong service.Key the cache on the same qualified FQN the lookup uses instead. Add aregression test covering two services sharing one SQL statement, and ranruff format/check on the touched files per review feedback.

* fix: apply ruff formatting (trailing whitespace, EOF newline)

---------

Co-authored-by: Pablo Takara <[email protected]>
Co-authored-by: IceS2 <[email protected]>
Co-authored-by: Shailesh Parmar <[email protected]>

This branch was previously deployed

1 inactive deployment
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 To release Will cherry-pick this PR into the release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query usage looks up by SQL hash, but the entity FQN is service.hash

4 participants