Skip to content

Conversation

@liferoad
Copy link
Contributor

@liferoad liferoad commented Jul 29, 2025

Fixes #35666

Use one simple rule to unify the schema: when the fields have different types, always use Optional[Any].

Tested:

cd sdks/python
pytest -v apache_beam/yaml/yaml_transform_test.py
pytest -v apache_beam/yaml/programming_guide_test.py
./gradlew :sdks:python:yamlIntegrationTests

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.

liferoad added 12 commits July 23, 2025 17:27
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
Copy link
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.

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 the Flatten transform. 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 to Optional[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 in None for missing fields and handling conversions for iterable types (e.g., ensuring they are lists).
  • Enhanced Flatten Transform: The expand method of the Flatten transform 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_schema function 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.py to thoroughly validate the schema unification logic for the Flatten transform. 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

  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.

Copy link
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 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.

@liferoad liferoad marked this pull request as ready for review July 29, 2025 22:26
@liferoad liferoad requested a review from damccorm July 29, 2025 22:27
@github-actions
Copy link
Contributor

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

Copy link
Contributor

@damccorm damccorm left a 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

Copy link
Contributor

@damccorm damccorm left a comment

Choose a reason for hiding this comment

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

Thanks!

@github-actions
Copy link
Contributor

Assigning reviewers:

R: @damccorm for label python.

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).

@liferoad liferoad merged commit f7e5ee5 into apache:master Aug 1, 2025
93 of 94 checks passed
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.

[Bug]: YAML Flatten incorrectly drops fields when input PCollections' schema are different

2 participants