Repository navigation
Commit bc0126a
authored
fix(bigquery-jdbc): format nested ARRAY fields as valid JSON in Struct.toString() (#14720)
b/570669133
## Summary
Fixes `BigQueryBaseStruct.toString()` (and `ResultSet.getString()` on
`STRUCT` / `ARRAY<STRUCT>` columns) so that nested `ARRAY` fields inside
a `STRUCT` have their elements formatted as valid JSON values instead of
unquoted `Arrays.deepToString()` strings.
## Problem
`BigQueryBaseStruct.toString()` constructs a JSON object string
(`{...}`) and quotes scalar `STRING`, `DATE`, `TIME`, `DATETIME`,
`TIMESTAMP`, `GEOGRAPHY`, and Base64-encoded `BYTES` attributes.
However, when a `STRUCT` contained a nested `ARRAY` attribute
(`BigQueryBaseArray`), `BigQueryBaseStruct.toString()` fell through to
`value.toString()`, which called `BigQueryBaseArray.toString()`
(`Arrays.deepToString`). This produced unquoted array elements inside
the JSON object (e.g., `{"str_arr":[hello,
world],"date_arr":[2026-01-01, 2026-01-02]}`), resulting in
syntactically invalid JSON.
## Solution
- Extracted the existing scalar JSON value formatting logic in
`BigQueryBaseStruct.toString()` into a shared helper method
`appendJsonValue(...)`.
- When a `STRUCT` attribute is a `BigQueryBaseArray`, iterate over its
elements and format each element using `appendJsonValue(...)` inside
`[...]`.
- Top-level `BigQueryBaseArray.toString()` behavior remains unchanged
for backwards compatibility.
## Example Output
For `SELECT STRUCT(1 AS id, ['hello', 'world'] AS str_arr, [DATE
'2026-01-01'] AS date_arr, [TIME '12:34:56.789012'] AS time_arr,
[TIMESTAMP '2026-01-01 00:00:00 UTC'] AS ts_arr, [b'abc'] AS
bytes_arr)`:
- **Before:** `{"id":1,"str_arr":[hello,
world],"date_arr":[2026-01-01],"time_arr":[12:34:56.789012],"ts_arr":[2026-01-01
00:00:00.0],"bytes_arr":[YWJj]}` (Invalid JSON)
- **After:**
`{"id":1,"str_arr":["hello","world"],"date_arr":["2026-01-01"],"time_arr":["12:34:56.789012"],"ts_arr":["2026-01-01
00:00:00.0"],"bytes_arr":["YWJj"]}` (Valid JSON)
## Testing
- Added `struct.toString()` assertions covering all BigQuery array types
to `BigQueryJsonStructTest#structOfArrays` and
`BigQueryArrowStructTest#structOfArrays`.
- Added integration test
`ITBigQueryJDBCTest#testStructWithNestedArraysValidJson` verifying both
REST and Storage Read API (`EnableHighThroughputAPI=1`) paths.1 parent 13310c7 commit bc0126a
4 files changed
Lines changed: 106 additions & 23 deletions
File tree
- java-bigquery-jdbc/src
- main/java/com/google/cloud/bigquery/jdbc
- test/java/com/google/cloud/bigquery/jdbc
- it
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
86 | 87 | | |
87 | 88 | | |
88 | 89 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
111 | 106 | | |
112 | | - | |
| 107 | + | |
113 | 108 | | |
114 | 109 | | |
115 | 110 | | |
| |||
119 | 114 | | |
120 | 115 | | |
121 | 116 | | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
122 | 146 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
181 | 181 | | |
182 | 182 | | |
183 | 183 | | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
184 | 198 | | |
185 | 199 | | |
186 | 200 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
196 | 210 | | |
197 | 211 | | |
198 | 212 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
74 | 76 | | |
75 | 77 | | |
76 | 78 | | |
| |||
3095 | 3097 | | |
3096 | 3098 | | |
3097 | 3099 | | |
| 3100 | + | |
| 3101 | + | |
| 3102 | + | |
| 3103 | + | |
| 3104 | + | |
| 3105 | + | |
| 3106 | + | |
| 3107 | + | |
| 3108 | + | |
| 3109 | + | |
| 3110 | + | |
| 3111 | + | |
| 3112 | + | |
| 3113 | + | |
| 3114 | + | |
| 3115 | + | |
| 3116 | + | |
| 3117 | + | |
| 3118 | + | |
| 3119 | + | |
| 3120 | + | |
| 3121 | + | |
| 3122 | + | |
| 3123 | + | |
| 3124 | + | |
| 3125 | + | |
| 3126 | + | |
| 3127 | + | |
| 3128 | + | |
3098 | 3129 | | |
0 commit comments