Skip to content

[WIP] fix(bigquery-jdbc): validate stream TableSchema and gate sessions, transactions, and target projects - #15187

Open
Neenu1995 wants to merge 1 commit into
fix/swa-v2-4-direct-insert-parserfrom
fix/swa-v2-5-schema-session-gating
Open

Neenu1995 wants to merge 1 commit into
fix/swa-v2-4-direct-insert-parserfrom
fix/swa-v2-5-schema-session-gating

Conversation

@Neenu1995

Copy link
Copy Markdown
Contributor

Stack: PR 5 of 6 (base: fix/swa-v2-4-direct-insert-parser)
Bug: b/587228731, b/587340667

Summary

Validate destination stream TableSchema compatibility before appending, propagate DefaultDataset target projects accurately, guard Legacy SQL and deferred-table retry dry runs, and gate out active SQL transactions and session temporary datasets.

Design Decisions

  • Pre-Poll Stream TableSchema Validation: Check the authoritative TableSchema returned by createWriteStream (isSupportedStreamTableSchema and isWriteStreamCompatible) before polling batchParameters; if unreferenced columns (such as DDL DEFAULT or ARRAY/STRUCT/RANGE columns) exist, return -1L and fall back cleanly to SQL DML with batchParameters intact.
  • Unqualified vs. Dataset-Qualified Project Resolution: Propagate DefaultDataset in BigQueryStatement.generateBigQuerySettings() and apply DefaultDataset.getProject() in resolveTargetProject strictly to 1-part unqualified tables while resolving 2-part dataset.table targets against catalog / bigQuery.getOptions().getProjectId().
  • Transaction, Session & At-Most-Once Dry-Run Guards: Route active multi-statement SQL transactions and active-session _ temporary datasets to SQL DML so pending stream commits cannot violate transactional/session visibility, skip positional dry runs under Legacy SQL, and retry unbound dry runs at most once (insertMetadataEvaluated) for tables created after prepareStatement().

…ansactions, and target projects

Stack: PR 5 of 6 (base: `fix/swa-v2-4-direct-insert-parser`)

### Summary
- Validate the authoritative destination `TableSchema` returned by `createWriteStream` (`isSupportedStreamTableSchema` / `isWriteStreamCompatible`) before opening `StreamWriter` or polling `batchParameters`, falling back cleanly to SQL DML when unreferenced columns (such as DDL `DEFAULT` or `ARRAY`/`STRUCT`/`RANGE` columns) are present on the target table, and verify end-to-end reordered `(col_b, col_a)` `ProtoRows` streaming.
- Propagate `DefaultDataset` in `BigQueryStatement.generateBigQuerySettings()`, make `getUseLegacySql()` null-safe and skip positional dry runs in Legacy SQL mode, and resolve target projects (`resolveTargetProject`) using `DefaultDataset` strictly for 1-part unqualified table names while resolving 2-part names against `catalog` / `bigQuery.getOptions().getProjectId()`.
- Retry unbound positional dry runs at most once in `executeBatch()` when a target table is created after `prepareStatement()` (removing the obsolete bound `getWriteBatchJobConfiguration` helper), and fall back to SQL DML inside active multi-statement transactions or active-session anonymous `_` temp datasets.

### Testing
- `make lint`, `mvn -B -ntp dependency:analyze -Pquick-build -DfailOnWarning=true -Dmdep.analyze.skip=false`, `mvn -q -Denforcer.skip=true -Dclirr.skip=true test-compile`, and `make unittest` (`BigQueryStatementTest` and `BigQueryPreparedStatementWriteApiTest` covering reordered `ProtoRows` streaming, `createWriteStream` table-schema mismatch SQL fallback, unqualified `DefaultDataset` vs. 2-part `catalog` / ADC `options` project resolution, deferred-table retry dry runs, non-repeated persistent dry-run errors, Legacy SQL, and session/transaction gating).
@Neenu1995
Neenu1995 requested review from a team as code owners November 24, 2026 01:42

@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 enhances the BigQuery JDBC driver's batch insertion capabilities using the Storage Write API. Key changes include validating stream table schemas, dynamically resolving target project IDs, and adding safety checks to prevent Write API usage during active transactions, legacy SQL execution, or incompatible session states. Additionally, the PR refactors parameter handling and improves robustness. Feedback on the changes suggests a safer way to iterate over batch parameters in getStandardBatchJobConfiguration to avoid a potential IndexOutOfBoundsException if the parameter list is not fully populated.

Comment on lines +1029 to +1030
for (int i = 0; i < this.parameterCount; i++) {
BigQueryJdbcParameter parameter = parameterList.get(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

Using a hardcoded loop up to this.parameterCount can throw an IndexOutOfBoundsException if parameterList is not fully populated (e.g., if some parameters were not set). It is safer to limit the loop to the actual size of the parameter list.

Suggested change
for (int i = 0; i < this.parameterCount; i++) {
BigQueryJdbcParameter parameter = parameterList.get(i);
int paramsToBind = Math.min(this.parameterCount, parameterList.size());
for (int i = 0; i < paramsToBind; i++) {
BigQueryJdbcParameter parameter = parameterList.get(i);

This branch has not been deployed

No deployments
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.

1 participant