Skip to content

Use registered type for Row#38108

Open
Abacn wants to merge 1 commit intoapache:masterfrom
Abacn:preserve-type
Open

Use registered type for Row#38108
Abacn wants to merge 1 commit intoapache:masterfrom
Abacn:preserve-type

Conversation

@Abacn
Copy link
Copy Markdown
Contributor

@Abacn Abacn commented Apr 8, 2026

Please add a meaningful description for your change here

Instead of generate named tuple every time


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request optimizes the handling of Row types in Apache Beam by leveraging the schema registry. By reusing registered types rather than dynamically generating new named tuples, the system improves efficiency and ensures better type consistency across pipeline operations.

Highlights

  • Schema Registry Integration: Updated the schema conversion logic to prioritize existing types from the schema registry instead of generating new named tuples for every request.
  • Test Validation: Adjusted the RowTypeTest to verify that types are correctly preserved after GroupByKey operations by checking for the presence of the Beam schema ID.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions bot added the python label Apr 8, 2026
@Abacn Abacn marked this pull request as ready for review April 9, 2026 20:51
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 9, 2026

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@Abacn
Copy link
Copy Markdown
Contributor Author

Abacn commented Apr 10, 2026

R: @claudevdm

@github-actions
Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

@Abacn
Copy link
Copy Markdown
Contributor Author

Abacn commented Apr 10, 2026

Actually this is not yet effective on Dataflow runner as schema registry hasn't been added to pickle

@Abacn Abacn marked this pull request as draft April 10, 2026 15:08
@Abacn
Copy link
Copy Markdown
Contributor Author

Abacn commented Apr 10, 2026

It becomes tricky. The main session is written to local file here:

artifacts = environments.python_sdk_dependencies(options)

as part of preparing python_sdk_dependencies

Then the schema is registered a few lines later,

self.proto_pipeline, self.proto_context = pipeline.to_runner_api(

it's not easy to redo save main session after this line, as the main session as a staged file has a checksum written in environment

@Abacn
Copy link
Copy Markdown
Contributor Author

Abacn commented Apr 10, 2026

nevertheless the current progress still fixes an inefficiency path such that generated Namedtuple is created every time named_tuple_from_schema is called, write to schema registry but never honors it.

Updated: solved by introducing a new "CoderRegistry.register_row" method

@Abacn Abacn marked this pull request as ready for review April 10, 2026 18:06
@github-actions github-actions bot added the io label Apr 10, 2026
@Abacn
Copy link
Copy Markdown
Contributor Author

Abacn commented Apr 13, 2026

"PreCommit Python ML tests with ML deps installed" also failing on HEAD, not related to the change. Ready for review for now.

* Introduce register_row to register with both coder and schema registry
  Save schema registry id->type mapping
@claudevdm
Copy link
Copy Markdown
Collaborator

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a register_row method to the type coder registry to streamline the registration of user types with Beam Rows and their schemas. It also updates session pickling to include the schema registry, ensuring schema IDs are preserved across sessions. Additionally, the logic for generating named tuples from schemas was updated to reuse existing types from the registry. Feedback was provided to rename variables in the load_registered_typings method to avoid shadowing the built-in id() function and to improve clarity regarding the mapping of IDs to typings.

Comment on lines +46 to +49
def load_registered_typings(self, by_id):
for id, typing in by_id.items():
if id not in self.by_id:
self.by_id[id] = (typing, None)
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.

medium

The variable name id shadows the built-in Python function id(). Additionally, the argument name by_id is slightly confusing because it contains a mapping of IDs to typings, whereas the class attribute self.by_id stores tuples of (typing, schema). Renaming these to id_to_typing and schema_id improves clarity and avoids potential conflicts with built-in names.

Suggested change
def load_registered_typings(self, by_id):
for id, typing in by_id.items():
if id not in self.by_id:
self.by_id[id] = (typing, None)
def load_registered_typings(self, id_to_typing):
for schema_id, typing in id_to_typing.items():
if schema_id not in self.by_id:
self.by_id[schema_id] = (typing, None)

Copy link
Copy Markdown
Collaborator

@claudevdm claudevdm left a comment

Choose a reason for hiding this comment

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

Very nice thanks!

Can we run a tap train on this before merging/importing?

self._register_coder_internal(
self._normalize_typehint_type(typehint_type), typehint_coder_class)

def register_row(self, typehint_type: Any) -> None:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What do you think of adding a decorator like @beam.row for conveinence?

Something like

@beam.row
@dataclass
class Order:
    id: int
    amount: float

def row(cls):
    cls._beam_row_pending = True
    return cls

Then when registering coders or RowTypeConstraint.from_user_type check for this attribute?


beam.coders.typecoders.registry.register_coder(
MyNamedTuple, beam.coders.RowCoder)
beam.coders.typecoders.registry.register_row(MyNamedTuple)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I am not sure the divegence between beam.coders.typecoders.registry.register_coder and beam.coders.typecoders.registry.register_row is a good thing. Most users probably want the benefit of this PR but rely on register_coder.

Can we either

  1. make register_coder act the same as register_row OR
  2. Deprecate register_coder(T, RowCoder) with a warning. DeprecationWarning: use register_row(T) instead when the coder class is RowCoder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants