Skip to content

[Python][ML] Sanitize table_name for NamedTuple in RAG SQL/Spanner writers - #41817

Merged
jrmccluskey merged 3 commits into
apache:masterfrom
arijeetmukherjee3108-droid:fix-rag-table-name-and-column-specs
Dec 2, 2026
Merged

jrmccluskey merged 3 commits into
apache:masterfrom
arijeetmukherjee3108-droid:fix-rag-table-name-and-column-specs

Conversation

@arijeetmukherjee3108-droid

@arijeetmukherjee3108-droid arijeetmukherjee3108-droid commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Problem

In the RAG SQL/Spanner writers (mysql.py, postgres.py, spanner.py), dynamically generated NamedTuple schema classes derive their name from table_name. Schema-qualified or hyphenated table names (e.g., my_schema.my_table or my-table) are not valid Python identifiers and cause NamedTuple creation to fail[cite: 4].

Changes

  • Sanitize table_name into a valid Python identifier before using it as a NamedTuple name in the RAG MySQL, Postgres, and Spanner writers
  • Revert column_specs=None changes on this PR and track that discussion separately in #41849[cite: 2, 4]

Testing

  • Verified diff and tests in spanner_test.py (pytest apache_beam/ml/rag/ingestion/spanner_test.py)[cite: 4]

@github-actions github-actions Bot added python size/m M: 50-249 lines changed labels Nov 30, 2026

@github-actions github-actions Bot 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.

📋 Review Summary

This PR properly sanitizes table_name when constructing NamedTuple record types across the MySQL, Postgres, and Spanner RAG vector writers, preventing invalid identifier errors for schema-qualified or hyphenated table names. It also updates column_specs defaults from mutable list instances to None (resolving to default specs on initialization), aligning the implementation with the existing docstrings.

🔍 General Feedback

  • Prefixing the sanitized table_name with VectorRecord_ / SpannerVectorRecord_ after re.sub(r"\W+", "_", table_name) ensures the resulting NamedTuple type name is always a valid Python identifier even when table_name contains dots, hyphens, quotes, or leading digits.
  • The added unit tests in mysql_test.py, postgres_test.py, and spanner_test.py provide good coverage of table name sanitization, RowCoder round-trip encoding, and column_specs=None default behavior.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @jrmccluskey for label python.
Backup reviewer: tvalentyn (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 55.55556% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.79%. Comparing base (791aecf) to head (e3ce912).
⚠️ Report is 36 commits behind head on master.

Files with missing lines Patch % Lines
sdks/python/apache_beam/ml/rag/ingestion/mysql.py 33.33% 2 Missing ⚠️
...ks/python/apache_beam/ml/rag/ingestion/postgres.py 33.33% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #41817      +/-   ##
============================================
+ Coverage     58.78%   58.79%   +0.01%     
  Complexity     2296     2296              
============================================
  Files          1136     1136              
  Lines        170696   170766      +70     
  Branches       1490     1490              
============================================
+ Hits         100338   100404      +66     
- Misses        67811    67815       +4     
  Partials       2547     2547              
Flag Coverage Δ
python 80.75% <55.55%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jrmccluskey jrmccluskey 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.

What's the motivation behind making these changes? Was there a use case with Beam where these caused issues?

I'm a little wary of changing the default value for column_specs to None, that's a potentially breaking change for users and warrants further discussion. I'd recommend scoping this PR down to just the table_name sanitization and filing an issue for column_specs=None support so there can be more conversation about how to approach this.

@arijeetmukherjee3108-droid

Copy link
Copy Markdown
Contributor Author

Thanks for looking at this. I didn't hit any specific failure, I just ran across this while reading through the RAG ingestion writers. Passing a table name with dots or hyphens (like my-table) straight into NamedTuple(...) isn't a valid Python identifier, so I thought it was worth sanitizing table_name for the generated type name.

Agreed on column_specs=None—I've pushed a commit reverting those changes so this PR only covers table_name sanitization, and I'll open a separate issue for column_specs=None to discuss it there.

@github-actions github-actions Bot added size/s S: 10-49 lines changed and removed size/m M: 50-249 lines changed labels Dec 1, 2026
@arijeetmukherjee3108-droid

Copy link
Copy Markdown
Contributor Author

Opened #41849 for the column_specs=None discussion.

@jrmccluskey jrmccluskey 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.

LGTM, thanks!

@jrmccluskey
jrmccluskey merged commit 2223ac3 into apache:master Dec 2, 2026
85 of 86 checks passed
@arijeetmukherjee3108-droid
arijeetmukherjee3108-droid deleted the fix-rag-table-name-and-column-specs branch December 2, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants