Skip to content

fix(profiler): remap dunder/\_sa\_ column names to avoid SQLAlchemy 2.x declarative filter - #32805

Merged
TeddyCr merged 5 commits into
open-metadata:mainfrom
zerafachris:fix/profiler-dunder-column-names
Sep 9, 2026
Merged

TeddyCr merged 5 commits into
open-metadata:mainfrom
zerafachris:fix/profiler-dunder-column-names

Conversation

@zerafachris

Copy link
Copy Markdown
Contributor

Summary

Fixes #32508.

Columns whose names begin with __ or _sa_ are silently dropped by SQLAlchemy 2.x's declarative scan (decl_base._match_exclude_dunders). ometa_to_sqa_orm builds the ORM class by passing column names as class-attribute keys, so any such column never reaches the mapped Table. When it is the first column (the one build_orm_col marks as the synthetic primary key), SQLAlchemy raises a hard mapper error:

Mapper could not assemble any primary key columns for mapped table

Managed ELT tools such as Hevo prefix bookkeeping columns with __ (__hevo_id, __hevo__ingested_at). Whether a table breaks depends only on whether such a column comes first in the entity's column list.

Change

ometa_to_sqa_orm now calls a _safe_orm_attr() helper before using a column name as the class-attribute key:

column name class-attribute key Column.name / Column.key
normal_col normal_col (unchanged) normal_col / normal_col
metadata metadata_ (existing behaviour) metadata / metadata
__hevo_id om_col___hevo_id (new) __hevo_id / __hevo_id
_sa_special om_col__sa_special (new) _sa_special / _sa_special

Column.name and Column.key are unchanged, so profiler_interface.py's __table__.columns[column.key] lookups continue to work.

Collisions introduced by the renaming are resolved by appending underscores (same pattern as the existing SQA_RESERVED_ATTRIBUTES rename).

Test

test_dunder_prefixed_columns_are_mapped in test_converter.py verifies:

  1. All three columns (__hevo_id, normal_col, _sa_special) appear in orm.__table__.columns under their original names.
  2. __hevo_id (index 0, the synthetic PK) is the sole primary-key column — confirming that the crash path is closed.

Checklist

  • Unit test added covering the crash case and the silent-drop case
  • No changes to Column.name or Column.key — profiler column-lookup paths are unaffected
  • Existing tests pass (attribute-name assertions remain valid for non-dunder columns)

….x declarative filter

SQLAlchemy 2.x's declarative scan discards any class attribute whose name
matches ^(?:__|_sa_) (see decl_base._match_exclude_dunders). Columns stored
under such names are silently absent from the mapped Table; when the dropped
column is first it also carries the synthetic primary key, raising:

  Mapper could not assemble any primary key columns for mapped table

ometa_to_sqa_orm now uses a _safe_orm_attr() helper that prefixes dunder/
_sa_-named columns with 'om_col_' for the class-attribute key only. Column.name
and Column.key are unchanged, so the profiler still addresses columns by their
original names.

This subsumes and generalises the existing SQA_RESERVED_ATTRIBUTES rename
(which appended a trailing underscore for 'metadata').

Fixes open-metadata#32508.
@zerafachris
zerafachris requested a review from a team as a code owner September 7, 2026 06:44
@github-actions

github-actions Bot commented Sep 7, 2026

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.

@github-actions

github-actions Bot commented Sep 7, 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!

…_orm_attr

Previously the `while candidate in existing` loop only ran for reserved and
dunder/`_sa_`-prefixed column names; normal names were returned immediately
without checking `existing`.  This meant a normal column literally named
`om_col___hevo_id` and a dunder column `__hevo_id` (remapped to the same key)
would collide, silently overwriting the first column — the exact silent-drop
failure this PR fixes.

Move `candidate = name` into the shared path so the collision check always
runs, and add a unit test covering this contrived but valid edge case.

Addresses gitar-bot suggestion on PR open-metadata#32805.

Prepared with AI assistance (Claude Code, Anthropic), reviewed for correctness before submission.
@zerafachris

Copy link
Copy Markdown
Contributor Author

Addressed the edge case raised by gitar-bot (collision between a normal column and a remapped dunder key):

  • Root cause: _safe_orm_attr returned normal column names via a bare return name that bypassed the while candidate in existing collision loop. If a real column was named om_col___hevo_id and a dunder column __hevo_id was later remapped to the same key, the second write silently overwrote the first.

  • Fix (commit 0a63f8a): replaced return name with candidate = name so every path — reserved, dunder, and normal — goes through the same deduplication loop.

  • Test added: test_safe_orm_attr_collision_between_normal_and_remapped_dunder exercises this exact scenario and asserts the two keys come out distinct.

Prepared with AI assistance (Claude Code, Anthropic), reviewed for correctness before submission.

@github-actions

github-actions Bot commented Sep 7, 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!

@IceS2
IceS2 requested a review from TeddyCr September 8, 2026 08:46
TeddyCr
TeddyCr previously approved these changes Sep 8, 2026
@TeddyCr TeddyCr added the safe to test Add this label to run secure Github workflows on PRs label Sep 8, 2026
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

The Python checkstyle failed.

Please run make py_format and py_format_check in the root of your repository and commit the changes to this PR.
You can also use pre-commit to automate the Python code formatting.

You can install the pre-commit hooks with make install_test precommit_install.

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

github-actions Bot commented Sep 8, 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!

@gitar-bot

gitar-bot Bot commented Sep 8, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 1 resolved / 1 findings

Fixes SQLAlchemy 2.x silently dropping columns with __ or _sa_ prefixes by remapping dunder column names to safe class-attribute keys while preserving the original Column.name and Column.key for lookup compatibility. Collision handling between renamed dunder keys and unrenamed columns is resolved by appending underscores. Test coverage confirms all three cases (__hevo_id, normal_col, _sa_special) are correctly mapped and the synthetic primary key crash is prevented.

✅ 1 resolved
✅ Edge Case: Unrenamed column can collide with a remapped dunder key

📄 ingestion/src/metadata/profiler/orm/converter/base.py:61-69 📄 ingestion/src/metadata/profiler/orm/converter/base.py:198-201
_safe_orm_attr only runs the while candidate in existing collision loop for reserved/dunder names; a normal column returns its name immediately without checking existing. So if a dunder column (e.g. __hevo_id) is remapped to om_col___hevo_id and a later real column is literally named om_col___hevo_id, the second write to cols[attr] silently overwrites the first, dropping a column — the same silent-drop failure this PR fixes. This is contrived and low-probability, but could be closed by checking every returned name (including the unchanged path) against existing.

Options

Display: compact → Showing less information.

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

@TeddyCr TeddyCr added the safe to test Add this label to run secure Github workflows on PRs label Sep 8, 2026
@sonarqubecloud

sonarqubecloud Bot commented Sep 8, 2026

Copy link
Copy Markdown

Merged via the queue into open-metadata:main with commit f02e818 Sep 9, 2026
160 of 174 checks passed
TeddyCr added a commit that referenced this pull request Nov 9, 2026
…7295)

* fix(profiler): backport safe ORM attributes to 2.0

Backport #32805 (f02e818) to 2.0.

Keep the 2.0 build_orm_col signature and existing column-key behavior while preserving excluded column names and resolving attribute-name collisions.

Fixes #32508.

* Fix profiler and data quality source column names

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Profiler: table whose first column name begins with "__" fails with "could not assemble any primary key columns"

2 participants