-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(yaml): add a simple schema unification for Flatten transform #35728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Implement schema merging for Flatten transform to handle PCollections with different schemas. The unified schema contains all fields from input PCollections, making fields optional to handle missing values. Added a test case to verify the behavior.
…nal types Extract inner types from Optional when unifying schemas to properly handle type unions. Also improve code readability by breaking long lines and clarifying comments.
Fix type resolution for nested generic types by properly extracting inner types when comparing field types. This ensures correct type hints are generated for optional fields in YAML provider.
…ma unification Handle list types more carefully during schema unification to avoid unsupported Union types. Also ensure iterable values are properly converted to lists when needed for schema compatibility.
… tests add comprehensive test cases for schema unification in Flatten transform
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @liferoad, 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 enhances the Flatten transform within Apache Beam's YAML DSL by introducing robust schema unification capabilities. The primary goal is to allow Flatten to seamlessly combine PCollections that may have differing schemas, automatically resolving type conflicts and handling missing fields to produce a consistent output schema. This makes the Flatten transform more flexible and powerful when dealing with heterogeneous data sources.
Highlights
- Schema Unification Logic: I've introduced a new private method,
_merge_schemas, within theFlattentransform. This method is responsible for analyzing the schemas of multiple input PCollections. It identifies common fields and their types, making fields optional if they are missing in some inputs. Crucially, if a field exists across multiple inputs with different data types, its unified type is set toOptional[Any]to accommodate all possibilities. - Element Transformation for Schema Compliance: A new private helper,
_unify_element_with_schema, has been added. This function takes an individual element and a target schema, then transforms the element to conform to that schema. It ensures that all fields present in the target schema are represented in the output element, filling inNonefor missing fields and handling conversions for iterable types (e.g., ensuring they are lists). - Enhanced Flatten Transform: The
expandmethod of theFlattentransform has been updated to leverage the new schema unification capabilities. Before performing the actual flatten operation, it now attempts to derive a unified schema from its inputs. If a unified schema is successfully determined, each input PCollection is mapped through the_unify_element_with_schemafunction to ensure all elements adhere to the consistent schema before being flattened together. - Comprehensive Test Coverage: I've added six new unit tests to
yaml_transform_test.pyto thoroughly validate the schema unification logic for theFlattentransform. These tests cover various scenarios, including basic unification, handling optional fields, unifying fields with different types, list fields, missing fields, and complex mixed schemas, ensuring the new functionality behaves as expected.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| 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 issue 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 or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
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. ↩
There was a problem hiding this 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 valuable feature for schema unification in the YAML Flatten transform. The logic for merging schemas and unifying elements is well-implemented and handles various cases, including missing fields and differing types. The addition of comprehensive tests covering multiple scenarios is excellent and ensures the feature's robustness. My review includes a couple of suggestions focused on improving code readability and maintainability in both the implementation and the new tests. These are not critical but would enhance the long-term quality of the code.
|
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment |
damccorm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, this LGTM - just had one question, feel free to merge though
damccorm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
|
Assigning reviewers: R: @damccorm 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). |
Fixes #35666
Use one simple rule to unify the schema: when the fields have different types, always use Optional[Any].
Tested:
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
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, commentfixes #<ISSUE NUMBER>instead.CHANGES.mdwith noteworthy changes.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)
See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.