Repository navigation
[WIP] fix(bigquery-jdbc): gate Storage Write API on direct INSERT VALUES (?, ...) SQL and column alignment - #15186
Conversation
…, ...) 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).
There was a problem hiding this comment.
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.
| Tuple<String, Integer> segment = readTableSegment(sql, pos); | ||
| if (segment == null) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
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.
| 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; | |
| } |
| if (segmentCount == 2 && "_session".equalsIgnoreCase(firstName)) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
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.
| if (segmentCount == 2 && "_session".equalsIgnoreCase(firstName)) { | |
| return null; | |
| } | |
| if ("_session".equalsIgnoreCase(firstName)) { | |
| return null; | |
| } |
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 directINSERT [INTO] <table> [(<cols>)] VALUES (?, ...) [;]statements.Design Decisions
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(?)) orAppendSerializationErrorcrashes (UDF(CAST(? AS STRING), ...)). A deterministic character scanner (skipping SQL comments and enforcing strict\`/\\backtick escapes) whitelists only single-tuple bare-?INSERT ... VALUESstatements without regex backtracking risks, routing expressions,CASTs, UDFs, literals,DEFAULT,SELECT, multi-row tuples, and_SESSIONtables to BigQuery SQL DML.resolveInsertSchemamaps 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.