Skip to content

Reconcile schema nullability in BeamCoGBKJoinRel - #38868

Closed
damccorm wants to merge 1 commit into
apache:masterfrom
damccorm:feature/sql-join-schema-recon
Closed

Reconcile schema nullability in BeamCoGBKJoinRel#38868
damccorm wants to merge 1 commit into
apache:masterfrom
damccorm:feature/sql-join-schema-recon

Conversation

@damccorm

@damccorm damccorm commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Addresses a schema mismatch bug that occurs during outer joins on schemas with nested rows (structs).

The Problem

When performing a or outer join, Calcite's type derivation automatically marks all fields from the nullable side of the join as nullable. For nested rows, Calcite recursively marks all nested fields inside the struct as nullable as well, even if they are defined as in the actual data schema.

When this Calcite-derived schema is passed to Beam's physical transform, it compares it against the actual data schema. Because the actual data still has nested fields while the Calcite schema expects them to be nullable, it trips a schema compatibility guard and throws an exception.

The Fix

Modified to perform schema reconciliation before outputting:

  1. We preserve Calcite's top-level nullability for the outer join (which is correct, as the entire struct can be null).
  2. We walk the nested fields of the struct and reconcile their nullability with the actual underlying data schema, keeping the original nested nullability to avoid the type-guard failures.

Added to to verify the fix.

@damccorm
damccorm marked this pull request as ready for review June 11, 2026 19:26
@damccorm

Copy link
Copy Markdown
Contributor Author

R: @Abacn

@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

@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 resolves a schema mismatch issue encountered during outer joins involving nested row structures. By reconciling the Calcite-derived schema with the actual underlying data schema, the changes ensure that nested field nullability is respected while maintaining correct top-level nullability for outer join operations. This prevents unnecessary type-guard exceptions during the execution of Beam SQL joins.

Highlights

  • Schema Reconciliation: Implemented a reconciliation process in BeamCoGBKJoinRel to align Calcite-derived schema nullability with actual data schema, specifically addressing nested struct fields.
  • Type Compatibility Guard: Fixed a bug where overly aggressive nullability marking by Calcite caused schema compatibility failures in Beam's physical join transforms.
  • Regression Testing: Added a new test case in BeamSqlDslJoinTest to verify that left outer joins with nested rows handle nullability correctly.
New Features

🧠 You can now enable Memory (public preview) 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 the 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 counterproductive. 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.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request updates the join logic in BeamCoGBKJoinRel to reconcile Calcite's derived output schema with the actual data types, preventing type-equality guard failures caused by incorrect nullability reporting on nested struct fields. Additionally, a new test case is added to verify left outer joins with nested rows. Feedback suggests adding a defensive check to ensure the field count of the Calcite schema matches the size of the data fields before iterating, preventing a potential IndexOutOfBoundsException.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +219 to +220
Schema.Builder reconciled = Schema.builder();
for (int i = 0; i < calciteSchema.getFieldCount(); i++) {

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

To prevent an unexpected IndexOutOfBoundsException during the loop, it is safer to defensively check that the field count of calciteSchema matches the size of dataFields before iterating.

Suggested change
Schema.Builder reconciled = Schema.builder();
for (int i = 0; i < calciteSchema.getFieldCount(); i++) {
if (calciteSchema.getFieldCount() != dataFields.size()) {
throw new IllegalStateException(
String.format(
"Field count mismatch: Calcite schema has %d fields, but data schema has %d fields",
calciteSchema.getFieldCount(), dataFields.size()));
}
Schema.Builder reconciled = Schema.builder();
for (int i = 0; i < calciteSchema.getFieldCount(); i++) {

}

@Test
public void testLeftOuterJoinWithNestedRows() {

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.

I ran the test reverting BeamCoGBKJoinRel.java‎ change it still passes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, you're right - I think we don't actually need this backport after all, thanks

@damccorm
damccorm force-pushed the feature/sql-join-schema-recon branch from 73ee0de to 62e46f0 Compare June 11, 2026 20:23
@damccorm damccorm closed this Jun 11, 2026
@damccorm
damccorm deleted the feature/sql-join-schema-recon branch June 11, 2026 20:36
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