Repository navigation
[Python][ML] Sanitize table_name for NamedTuple in RAG SQL/Spanner writers - #41817
Conversation
…AG SQL/Spanner writers
There was a problem hiding this comment.
📋 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_namewithVectorRecord_/SpannerVectorRecord_afterre.sub(r"\W+", "_", table_name)ensures the resultingNamedTupletype name is always a valid Python identifier even whentable_namecontains dots, hyphens, quotes, or leading digits. - The added unit tests in
mysql_test.py,postgres_test.py, andspanner_test.pyprovide good coverage of table name sanitization,RowCoderround-trip encoding, andcolumn_specs=Nonedefault behavior.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @jrmccluskey for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Codecov Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
jrmccluskey
left a comment
There was a problem hiding this comment.
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.
|
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. |
|
Opened #41849 for the column_specs=None discussion. |
Problem
In the RAG SQL/Spanner writers (
mysql.py,postgres.py,spanner.py), dynamically generatedNamedTupleschema classes derive their name fromtable_name. Schema-qualified or hyphenated table names (e.g.,my_schema.my_tableormy-table) are not valid Python identifiers and causeNamedTuplecreation to fail[cite: 4].Changes
table_nameinto a valid Python identifier before using it as aNamedTuplename in the RAG MySQL, Postgres, and Spanner writerscolumn_specs=Nonechanges on this PR and track that discussion separately in#41849[cite: 2, 4]Testing
spanner_test.py(pytest apache_beam/ml/rag/ingestion/spanner_test.py)[cite: 4]