Skip to content

fix(bigquery-jdbc): avoid NPE for null values in Storage Write API batch insert - #14083

Open
rachitsgh wants to merge 2 commits into
googleapis:mainfrom
rachitsgh:fix/bigquery-jdbc-write-api-null-npe
Open

fix(bigquery-jdbc): avoid NPE for null values in Storage Write API batch insert#14083
rachitsgh wants to merge 2 commits into
googleapis:mainfrom
rachitsgh:fix/bigquery-jdbc-write-api-null-npe

Conversation

@rachitsgh

Copy link
Copy Markdown

Summary

  • BigQueryPreparedStatement.bulkInsertWithWriteAPI threw a NullPointerException when a batch parameter was null: STRING columns called .toString() on the null value directly, and other column types serialized null as the literal string "null" instead of a JSON null.
  • Both cases now write a proper JSON null via JsonNull.INSTANCE.

Fixes #14066

Test plan

  • Added BigQueryPreparedStatementBulkInsertNullValueTest reproducing the reported NPE against the fix
  • Ran full java-bigquery-jdbc unit test suite (1122 tests) — all pass, no regressions

…tch insert

BigQueryPreparedStatement.bulkInsertWithWriteAPI threw a NullPointerException
when a batch parameter was null: STRING columns called toString() on the null
value directly, and other column types serialized null as the literal string
"null" instead of a JSON null. Both cases now write a proper JSON null.

Fixes googleapis#14066

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@rachitsgh
rachitsgh requested review from a team as code owners August 16, 2026 15:34
@google-cla

google-cla Bot commented Aug 16, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@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 addresses a NullPointerException that occurs during Storage Write API batch inserts when a parameter value is null, specifically handling it by inserting a JsonNull.INSTANCE into the JSON row representation. A regression test was also introduced to verify this fix. The review feedback suggests improving the test implementation by avoiding catching exceptions from the reflective method call and instead ensuring the method can complete normally through proper mocking.

Comment on lines +92 to +98
try {
bulkInsertMethod.invoke(preparedStatement, mockWriteClient);
} catch (InvocationTargetException e) {
assertFalse(
e.getCause() instanceof NullPointerException,
"Row construction should not NPE on a null STRING parameter, but got: " + e.getCause());
}

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

Avoid catching NullPointerException and treating it as 'Expected' in tests, as it indicates a fragile testing pattern. Instead, mock necessary dependencies to allow the method to complete normally, or use Assertions.assertThrows if the exception is truly expected as part of the test scenario.

bulkInsertMethod.invoke(preparedStatement, mockWriteClient);
References
  1. Avoid catching NullPointerException and treating it as 'Expected' in tests, as it indicates a fragile testing pattern. Instead, mock necessary dependencies to allow the method to complete normally, or use Assertions.assertThrows if the exception is truly expected as part of the test scenario.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch — replaced the try/catch on InvocationTargetException with a full round trip through bulkInsertWithWriteAPI against an in-process fake Storage Write service (MockBigQueryWrite + gax-grpc testlib). The test now asserts the method completes normally (assertDoesNotThrow) rather than inspecting which exception type comes out.

…nd trip

Address review feedback on catching NullPointerException as an "expected"
outcome in the regression test. Replace the reflection-based try/catch with
a full round trip through bulkInsertWithWriteAPI against an in-process fake
Storage Write service (MockBigQueryWrite + gax-grpc testlib), so the test
now asserts the method completes normally instead of merely checking which
exception type comes out.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bigquery-jdbc: Storage Write batch throws NPE for setObject(index, null)

1 participant