Skip to content

[WIP] fix(bigquery-jdbc): gate Storage Write API on direct INSERT VALUES (?, ...) SQL and column alignment - #15186

Open
Neenu1995 wants to merge 1 commit into
fix/swa-v2-3-writer-batch-lifecyclefrom
fix/swa-v2-4-direct-insert-parser
Open

Neenu1995 wants to merge 1 commit into
fix/swa-v2-3-writer-batch-lifecyclefrom
fix/swa-v2-4-direct-insert-parser

Conversation

@Neenu1995

Copy link
Copy Markdown
Contributor

Stack: PR 4 of 6 (base: fix/swa-v2-3-writer-batch-lifecycle)
Bug: b/587228731

Summary

Add a zero-regex SQL whitelist parser (parseDirectInsertValues) and explicit column-order alignment (resolveInsertSchema) so Storage Write API is used strictly for direct INSERT [INTO] <table> [(<cols>)] VALUES (?, ...) [;] statements.

Design Decisions

  • Zero-Regex Token Whitelist Parser (b/587228731): Storage Write API streams raw row payloads directly into storage without BigQuery's SQL evaluator, previously causing silent UDF/expression bypass (MASK_UDF(?)) or AppendSerializationError crashes (UDF(CAST(? AS STRING), ...)). A deterministic character scanner (skipping SQL comments and enforcing strict \` / \\ backtick escapes) whitelists only single-tuple bare-? INSERT ... VALUES statements without regex backtracking risks, routing expressions, CASTs, UDFs, literals, DEFAULT, SELECT, multi-row tuples, and _SESSION tables to BigQuery SQL DML.
  • Explicit Column Reordering & Arity Alignment: resolveInsertSchema maps explicit (col_b, col_a) lists case-insensitively (Locale.ROOT) onto the table schema in placeholder order while rejecting partial column lists, unknown columns, or duplicate column targets so omitted/defaulted columns execute on the SQL engine.

…, ...) SQL and column alignment

Stack: PR 4 of 6 (base: `fix/swa-v2-3-writer-batch-lifecycle`)

### Summary
- Fix silent SQL expression/UDF bypass (`b/587228731`) and `STRING -> BYTES` transform crashes when `EnableWriteAPI=true` is used with SQL expressions, `CAST`s, or UDFs inside `INSERT ... VALUES (...)`.
- Add a zero-regex SQL whitelist parser (`parseDirectInsertValues` + `DirectInsertShape`) and `resolveInsertSchema` column alignment handling SQL comments, strict `` \` `` / `\\` backtick escapes, and explicit `(col_b, col_a)` column permutations so only single-row `INSERT [INTO] <table> [(<cols>)] VALUES (?, ...) [;]` statements with bare `?` placeholders and full-column arity use the Storage Write API; all other `INSERT` forms (`SELECT`, multi-row tuples, expressions/UDFs/casts/literals/`DEFAULT`, partial/duplicate column lists, and `_SESSION` tables) fall back automatically to BigQuery SQL DML.

### 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` (`BigQueryPreparedStatementWriteApiTest` parameterized SQL whitelist/rejection suite and `MASK_UDF(?)` / `UDF(CAST(? AS STRING), ...)` SQL fallback tests).
@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 introduces a lightweight SQL parser ('parseDirectInsertValues') in 'BigQueryPreparedStatement' to identify single-row 'INSERT INTO ... VALUES (?, ...)' statements with positional placeholders, enabling direct routing to the Storage Write API. It also adds extensive unit tests to verify the parser's correctness. The review feedback suggests strengthening the parser's validation of session-scoped temporary tables by ensuring that any table path containing '_session' (either as the first segment or any subsequent segment) is safely blocked and routed to the standard SQL engine.

Comment on lines +388 to +391
Tuple<String, Integer> segment = readTableSegment(sql, pos);
if (segment == null) {
return null;
}

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

Currently, the parser only blocks _session if it is the first segment of a 2-segment path or if it is backticked as a single segment. However, if a user specifies a fully-qualified path like project._session.temp_table, the second segment is _session but segmentCount is 3, which bypasses the check. Checking for _session on all subsequent segments in the loop ensures that any session-scoped temporary table is safely blocked and routed to the standard SQL engine.

Suggested change
Tuple<String, Integer> segment = readTableSegment(sql, pos);
if (segment == null) {
return null;
}
Tuple<String, Integer> segment = readTableSegment(sql, pos);
if (segment == null || "_session".equalsIgnoreCase(segment.x())) {
return null;
}

Comment on lines +399 to +401
if (segmentCount == 2 && "_session".equalsIgnoreCase(firstName)) {
return null;
}

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

The check for _session on the first segment is currently restricted to segmentCount == 2. Since _session is a reserved prefix for session-scoped resources and cannot be a valid project ID (which must start with a letter), we should block any table path where the first segment is _session regardless of the segment count. This simplifies the logic and prevents potential bypasses.

Suggested change
if (segmentCount == 2 && "_session".equalsIgnoreCase(firstName)) {
return null;
}
if ("_session".equalsIgnoreCase(firstName)) {
return null;
}

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