Skip to content

support null primary key values in Spanner change streams to BigQuery - #4471

Open
mvanhorn wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
mvanhorn:fix/831-spanner-null-primary-keys
Open

mvanhorn wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
mvanhorn:fix/831-spanner-null-primary-keys

Conversation

@mvanhorn

Copy link
Copy Markdown

Background

BigQuery rejects the row because that field is not a record, so tables with a nullable primary key cannot be replicated. Change-stream rows whose primary key contains a null column fail to load into BigQuery.

The keys JSON parser represents a JSON null as the JSONObject.NULL sentinel, not Java null. That sentinel is copied onto the BigQuery row, and the typed key getters reject it. Non-key columns already store Java null, which BigQuery accepts.

Summary of the fix

If a primary-key name is present and JSON-null, key construction appends a null string and skips the typed getter, and the BigQuery row stores Java null instead of the sentinel. Missing keys still fail. Tests cover the composite key {"Col1":"123","Col2":null} for both the Spanner key and the table row.

Verification

Tests for the DataflowTemplates SpannerChangeStreamsUtils change live in v2/googlecloud-to-googlecloud/src/test/java/com/google/cloud/teleport/v2/templates/spannerchangestreamstobigquery/schemautils/SpannerChangeStreamsUtilsTest.java and v2/googlecloud-to-googlecloud/src/test/java/com/google/cloud/teleport/v2/templates/spannerchangestreamstobigquery/schemautils/SpannerToBigQueryUtilsTest.java. The targeted tests for the DataflowTemplates SpannerChangeStreamsUtils change pass locally.

Fixes #831

If a primary-key name is present and JSON-null, key construction appends
a null string and skips the typed getter, and the BigQuery row stores
Java null instead of the sentinel. Missing keys still fail. Tests cover
the composite key {"Col1":"123","Col2":null} for both the Spanner key
and the table row.

Fixes GoogleCloudPlatform#831
@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 addresses an issue where Spanner change streams containing nullable primary keys failed to load into BigQuery. The root cause was the JSON parser's use of a sentinel object (JSONObject.NULL) which BigQuery rejected as an invalid record type. The changes ensure that null primary key components are correctly translated to Java nulls, allowing successful data replication.

Highlights

  • Null Primary Key Support: Updated key construction logic to correctly handle JSON null values in Spanner change streams, preventing BigQuery ingestion failures.
  • Sentinel Value Handling: Replaced the JSONObject.NULL sentinel with actual Java nulls when populating BigQuery TableRows to ensure compatibility with BigQuery's schema requirements.
  • Regression Testing: Added comprehensive unit tests to verify correct handling of composite keys containing null values for both Spanner keys and BigQuery rows.
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 adds handling for explicit JSON null values (JSONObject.NULL) when processing Spanner change streams. In SpannerChangeStreamsUtils, a typed null is now appended to the Spanner key builder, and in SpannerToBigQueryUtils, a Java null is set in the TableRow to prevent BigQuery from rejecting the sentinel value. Corresponding unit tests have been added to verify these changes. I have no further feedback to provide as there are no review comments.

@mvanhorn

Copy link
Copy Markdown
Author

The required label check is failing because the PR has none of the release-note labels, and I don't have permission to add labels in this repo. Could a maintainer add bug-fix? The change fixes #831 (null primary key columns in Spanner change streams to BigQuery).

@jiangzzhu

Copy link
Copy Markdown
Contributor
@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 56.96%. Comparing base (308eb05) to head (f2bf152).
⚠️ Report is 45 commits behind head on main.

Files with missing lines Patch % Lines
...igquery/schemautils/SpannerChangeStreamsUtils.java 66.66% 0 Missing and 1 partial ⚠️

❗ There is a different number of reports uploaded between BASE (308eb05) and HEAD (f2bf152). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (308eb05) HEAD (f2bf152)
3 1
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #4471      +/-   ##
============================================
- Coverage     62.66%   56.96%   -5.71%     
- Complexity     3570     7579    +4009     
============================================
  Files           607     1158     +551     
  Lines         35213    73853   +38640     
  Branches       4001     8701    +4700     
============================================
+ Hits          22067    42069   +20002     
- Misses        12026    28982   +16956     
- Partials       1120     2802    +1682     
Components Coverage Δ
spanner-templates 84.15% <ø> (+0.02%) ⬆️
spanner-import-export 72.15% <ø> (∅)
spanner-live-forward-migration 87.79% <ø> (+0.05%) ⬆️
spanner-live-reverse-replication 80.10% <ø> (+0.04%) ⬆️
spanner-bulk-migration 88.35% <ø> (+0.03%) ⬆️
gcs-spanner-dv 86.68% <ø> (+0.06%) ⬆️
Files with missing lines Coverage Δ
...tobigquery/schemautils/SpannerToBigQueryUtils.java 72.00% <100.00%> (ø)
...igquery/schemautils/SpannerChangeStreamsUtils.java 80.63% <66.66%> (ø)

... and 571 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants