Repository navigation
Expand file tree
/
Copy pathBigQueryPreparedStatementSettersTest.java
More file actions
527 lines (451 loc) · 23.4 KB
/
Copy pathBigQueryPreparedStatementSettersTest.java
File metadata and controls
527 lines (451 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigquery.jdbc;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.FieldList;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
import com.google.gson.Gson;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import java.sql.Array;
import java.sql.Date;
import java.sql.ParameterMetaData;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
public class BigQueryPreparedStatementSettersTest {
private BigQueryConnection connection;
private BigQueryPreparedStatement preparedStatement;
@BeforeEach
public void setUp() throws Exception {
connection = mock(BigQueryConnection.class);
when(connection.getQueryDialect()).thenReturn("SQL");
when(connection.getConnectionId()).thenReturn("test-connection-id");
doReturn(BigQueryConnection.SessionState.empty()).when(connection).getSessionStateSnapshot();
preparedStatement = new BigQueryPreparedStatement(connection, "SELECT ?, ?, ?, ?, ?");
}
@Test
public void testSetNullWithSqlType() throws Exception {
preparedStatement.setNull(1, Types.VARCHAR);
assertNull(preparedStatement.parameterHandler.getParameter(1));
assertEquals(String.class, preparedStatement.parameterHandler.getType(1));
assertEquals(StandardSQLTypeName.STRING, preparedStatement.parameterHandler.getSqlType(1));
preparedStatement.setNull(2, Types.INTEGER);
assertNull(preparedStatement.parameterHandler.getParameter(2));
assertEquals(Integer.class, preparedStatement.parameterHandler.getType(2));
assertEquals(StandardSQLTypeName.INT64, preparedStatement.parameterHandler.getSqlType(2));
preparedStatement.setNull(3, Types.SMALLINT);
assertNull(preparedStatement.parameterHandler.getParameter(3));
assertEquals(Short.class, preparedStatement.parameterHandler.getType(3));
assertEquals(StandardSQLTypeName.INT64, preparedStatement.parameterHandler.getSqlType(3));
preparedStatement.setNull(4, Types.TINYINT);
assertNull(preparedStatement.parameterHandler.getParameter(4));
assertEquals(Byte.class, preparedStatement.parameterHandler.getType(4));
assertEquals(StandardSQLTypeName.INT64, preparedStatement.parameterHandler.getSqlType(4));
preparedStatement.setNull(5, Types.NULL);
assertNull(preparedStatement.parameterHandler.getParameter(5));
assertEquals(String.class, preparedStatement.parameterHandler.getType(5));
assertEquals(StandardSQLTypeName.STRING, preparedStatement.parameterHandler.getSqlType(5));
}
@Test
public void testSetNullWithTypeName() throws Exception {
preparedStatement.setNull(1, Types.VARCHAR, "VARCHAR");
assertNull(preparedStatement.parameterHandler.getParameter(1));
assertEquals(String.class, preparedStatement.parameterHandler.getType(1));
assertEquals(StandardSQLTypeName.STRING, preparedStatement.parameterHandler.getSqlType(1));
}
@Test
public void testSetNullWithUnsupportedSqlTypeThrowsException() {
assertThrows(java.sql.SQLException.class, () -> preparedStatement.setNull(1, 99999));
}
@Test
public void testSetShort() throws Exception {
preparedStatement.setShort(1, (short) 42);
assertEquals((short) 42, preparedStatement.parameterHandler.getParameter(1));
assertEquals(Short.class, preparedStatement.parameterHandler.getType(1));
assertEquals(StandardSQLTypeName.INT64, preparedStatement.parameterHandler.getSqlType(1));
}
@Test
public void testSetBytes() throws Exception {
byte[] bytes = new byte[] {1, 2, 3, 4};
preparedStatement.setBytes(1, bytes);
assertArrayEquals(bytes, (byte[]) preparedStatement.parameterHandler.getParameter(1));
assertEquals(byte[].class, preparedStatement.parameterHandler.getType(1));
assertEquals(StandardSQLTypeName.BYTES, preparedStatement.parameterHandler.getSqlType(1));
}
@Test
public void testSetObjectSingleArg() throws Exception {
preparedStatement.setObject(1, "hello");
assertEquals("hello", preparedStatement.parameterHandler.getParameter(1));
assertEquals(String.class, preparedStatement.parameterHandler.getType(1));
assertEquals(StandardSQLTypeName.STRING, preparedStatement.parameterHandler.getSqlType(1));
preparedStatement.setObject(2, 100L);
assertEquals(100L, preparedStatement.parameterHandler.getParameter(2));
assertEquals(Long.class, preparedStatement.parameterHandler.getType(2));
assertEquals(StandardSQLTypeName.INT64, preparedStatement.parameterHandler.getSqlType(2));
preparedStatement.setObject(3, null);
assertNull(preparedStatement.parameterHandler.getParameter(3));
assertEquals(String.class, preparedStatement.parameterHandler.getType(3));
}
@Test
public void testSetObjectWithTargetSqlType() throws Exception {
preparedStatement.setObject(1, 123, Types.INTEGER);
assertEquals(123, preparedStatement.parameterHandler.getParameter(1));
assertEquals(Integer.class, preparedStatement.parameterHandler.getType(1));
assertEquals(StandardSQLTypeName.INT64, preparedStatement.parameterHandler.getSqlType(1));
preparedStatement.setObject(2, null, Types.INTEGER);
assertNull(preparedStatement.parameterHandler.getParameter(2));
assertEquals(Integer.class, preparedStatement.parameterHandler.getType(2));
preparedStatement.setObject(3, 456, Types.INTEGER, 0);
assertEquals(456, preparedStatement.parameterHandler.getParameter(3));
assertEquals(Integer.class, preparedStatement.parameterHandler.getType(3));
}
@Test
public void testSetArray() throws Exception {
Array mockArray = mock(Array.class);
preparedStatement.setArray(1, mockArray);
assertEquals(mockArray, preparedStatement.parameterHandler.getParameter(1));
assertEquals(Array.class, preparedStatement.parameterHandler.getType(1));
assertEquals(StandardSQLTypeName.ARRAY, preparedStatement.parameterHandler.getSqlType(1));
}
@Test
public void testGetParameterMetaData() throws Exception {
preparedStatement.setString(1, "test");
preparedStatement.setInt(2, 42);
ParameterMetaData metaData = preparedStatement.getParameterMetaData();
assertEquals(5, metaData.getParameterCount());
assertEquals(Types.NVARCHAR, metaData.getParameterType(1));
assertEquals("STRING", metaData.getParameterTypeName(1));
assertEquals(String.class.getName(), metaData.getParameterClassName(1));
assertEquals(Types.BIGINT, metaData.getParameterType(2));
assertEquals("INT64", metaData.getParameterTypeName(2));
assertEquals(Long.class.getName(), metaData.getParameterClassName(2));
assertEquals(ParameterMetaData.parameterNullable, metaData.isNullable(1));
assertEquals(ParameterMetaData.parameterModeIn, metaData.getParameterMode(1));
assertThrows(SQLException.class, () -> metaData.getParameterType(0));
assertThrows(SQLException.class, () -> metaData.getParameterType(6));
}
@Test
public void testGetParameterModeWithInOutParameters() throws Exception {
preparedStatement.parameterHandler.setParameter(
1, "inParam", String.class, BigQueryParameterHandler.BigQueryStatementParameterType.IN, 0);
preparedStatement.parameterHandler.setParameter(
2, null, Integer.class, BigQueryParameterHandler.BigQueryStatementParameterType.OUT, 0);
preparedStatement.parameterHandler.setParameter(
3,
"inoutParam",
String.class,
BigQueryParameterHandler.BigQueryStatementParameterType.INOUT,
0);
ParameterMetaData metaData = preparedStatement.getParameterMetaData();
assertEquals(ParameterMetaData.parameterModeIn, metaData.getParameterMode(1));
assertEquals(ParameterMetaData.parameterModeOut, metaData.getParameterMode(2));
assertEquals(ParameterMetaData.parameterModeInOut, metaData.getParameterMode(3));
}
@Test
public void testSetDateWithCalendar() throws Exception {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTimeInMillis(1000000L);
long originalMillis = cal.getTimeInMillis();
Date date = new Date(1700000000000L); // 2023-11-14
preparedStatement.setDate(1, date, cal);
assertEquals(Date.class, preparedStatement.parameterHandler.getType(1));
assertEquals(originalMillis, cal.getTimeInMillis());
}
@ParameterizedTest
@ValueSource(strings = {"UTC", "America/New_York", "Asia/Tokyo"})
public void testSetTimeWithCalendar(String zoneId) throws Exception {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(zoneId));
cal.setTimeInMillis(1000000L);
long originalMillis = cal.getTimeInMillis();
Calendar sourceCal = Calendar.getInstance(TimeZone.getTimeZone(zoneId));
sourceCal.clear();
sourceCal.set(1970, Calendar.JANUARY, 1, 12, 34, 56);
Time time = new Time(sourceCal.getTimeInMillis());
preparedStatement.setTime(1, time, cal);
assertEquals(Time.class, preparedStatement.parameterHandler.getType(1));
assertEquals("12:34:56", preparedStatement.parameterHandler.getParameter(1).toString());
assertEquals(originalMillis, cal.getTimeInMillis());
}
@ParameterizedTest
@ValueSource(strings = {"America/New_York", "Asia/Tokyo"})
public void testSetTimestampWithCalendar(String zoneId) throws Exception {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(zoneId));
cal.setTimeInMillis(1000000L);
long originalMillis = cal.getTimeInMillis();
Timestamp ts = new Timestamp(1700000000000L);
preparedStatement.setTimestamp(1, ts, cal);
assertEquals(Timestamp.class, preparedStatement.parameterHandler.getType(1));
assertEquals(ts, preparedStatement.parameterHandler.getParameter(1));
assertEquals(originalMillis, cal.getTimeInMillis());
}
@Test
public void testGetMetaData() throws Exception {
// Until a dry run describes the query, there are no result columns to report.
assertNull(preparedStatement.getMetaData());
preparedStatement.resultSchema =
Schema.of(
Field.of("col1", StandardSQLTypeName.STRING),
Field.of("col2", StandardSQLTypeName.INT64));
ResultSetMetaData metaData = preparedStatement.getMetaData();
assertNotNull(metaData);
assertEquals(2, metaData.getColumnCount());
assertEquals("col1", metaData.getColumnName(1));
assertEquals(Types.NVARCHAR, metaData.getColumnType(1));
assertEquals("col2", metaData.getColumnName(2));
assertEquals(Types.BIGINT, metaData.getColumnType(2));
}
@Test
public void testGetMetaDataReturnsNullForInsert() throws Exception {
// An INSERT returns no ResultSet, so the columns it writes must not be reported as result
// metadata even though the Storage Write API path has captured them.
preparedStatement.insertSchema = Schema.of(Field.of("col1", StandardSQLTypeName.STRING));
assertNull(preparedStatement.getMetaData());
}
@Test
public void testSetObjectWithJavaTime() throws Exception {
preparedStatement = new BigQueryPreparedStatement(connection, "SELECT ?, ?, ?, ?, ?, ?");
LocalDate localDate = LocalDate.of(2025, 12, 3);
preparedStatement.setObject(1, localDate);
assertEquals(Date.class, preparedStatement.parameterHandler.getType(1));
assertEquals(Date.valueOf(localDate), preparedStatement.parameterHandler.getParameter(1));
LocalTime localTime = LocalTime.of(12, 30, 0);
preparedStatement.setObject(2, localTime);
assertEquals(Time.class, preparedStatement.parameterHandler.getType(2));
assertEquals(Time.valueOf(localTime), preparedStatement.parameterHandler.getParameter(2));
// LocalDateTime is bound as STRING so it is accepted by both DATETIME and TIMESTAMP columns.
LocalDateTime localDateTime = LocalDateTime.of(2025, 12, 3, 12, 30, 0);
preparedStatement.setObject(3, localDateTime);
assertEquals(String.class, preparedStatement.parameterHandler.getType(3));
assertEquals(
BigQueryParameterHandler.formatDateTimeDigits(localDateTime),
preparedStatement.parameterHandler.getParameter(3));
Instant instant = Instant.now();
preparedStatement.setObject(4, instant);
assertEquals(Timestamp.class, preparedStatement.parameterHandler.getType(4));
assertEquals(Timestamp.from(instant), preparedStatement.parameterHandler.getParameter(4));
OffsetDateTime offsetDateTime = OffsetDateTime.now();
preparedStatement.setObject(5, offsetDateTime);
assertEquals(Timestamp.class, preparedStatement.parameterHandler.getType(5));
assertEquals(
Timestamp.from(offsetDateTime.toInstant()),
preparedStatement.parameterHandler.getParameter(5));
ZonedDateTime zonedDateTime = ZonedDateTime.now();
preparedStatement.setObject(6, zonedDateTime);
assertEquals(Timestamp.class, preparedStatement.parameterHandler.getType(6));
assertEquals(
Timestamp.from(zonedDateTime.toInstant()),
preparedStatement.parameterHandler.getParameter(6));
}
// Verifies standard DML QueryJobConfiguration creates positional parameters with null values when
// setObject is called with null.
@Test
public void testBatchJobConfigurationWithSetObjectNull() throws Exception {
preparedStatement.setObject(1, null);
preparedStatement.setInt(2, 42);
preparedStatement.setString(3, "test");
preparedStatement.setDouble(4, 3.14);
preparedStatement.setBoolean(5, true);
preparedStatement.addBatch();
ArrayList<BigQueryJdbcParameter> batchParams =
preparedStatement.parameterHandler.parametersList;
QueryJobConfiguration config = preparedStatement.getWriteBatchJobConfiguration(batchParams);
assertNotNull(config);
assertEquals(5, config.getPositionalParameters().size());
assertNull(config.getPositionalParameters().get(0).getValue());
assertEquals(StandardSQLTypeName.STRING, config.getPositionalParameters().get(0).getType());
}
// Verifies Storage Write API JSON row serialization maps null parameter values to
// JsonNull.INSTANCE.
@Test
public void testCreateJsonRowWithSetObjectNull() throws Exception {
preparedStatement.setObject(1, null);
preparedStatement.setInt(2, 42);
preparedStatement.setString(3, "test");
preparedStatement.setDouble(4, 3.14);
preparedStatement.setBoolean(5, true);
FieldList fieldList =
FieldList.of(
Field.of("col1", StandardSQLTypeName.STRING),
Field.of("col2", StandardSQLTypeName.INT64),
Field.of("col3", StandardSQLTypeName.STRING),
Field.of("col4", StandardSQLTypeName.FLOAT64),
Field.of("col5", StandardSQLTypeName.BOOL));
ArrayList<BigQueryJdbcParameter> params = preparedStatement.parameterHandler.parametersList;
JsonObject jsonRow = BigQueryPreparedStatement.createJsonRow(fieldList, params, new Gson());
assertNotNull(jsonRow);
assertEquals(JsonNull.INSTANCE, jsonRow.get("col1"));
assertTrue(jsonRow.get("col1").isJsonNull());
assertEquals("42", jsonRow.get("col2").getAsString());
}
@Test
public void testInferredParameterTypeKnownBeforeSetters() throws Exception {
preparedStatement = new BigQueryPreparedStatement(connection, "SELECT ?");
// 1. Inferred type is known immediately without calling setInt/setString
preparedStatement.parameterHandler.setInferredParameterType(1, StandardSQLTypeName.INT64);
ParameterMetaData pmd = preparedStatement.getParameterMetaData();
assertEquals(Types.BIGINT, pmd.getParameterType(1));
assertEquals("INT64", pmd.getParameterTypeName(1));
// 2. configureParameters fails before value is supplied
QueryJobConfiguration.Builder configBuilder = QueryJobConfiguration.newBuilder("SELECT ?");
BigQueryJdbcException ex =
assertThrows(
BigQueryJdbcException.class,
() -> preparedStatement.parameterHandler.configureParameters(configBuilder));
assertTrue(ex.getMessage().contains("One or more parameters missing"));
// 3. Once setter is called, configureParameters succeeds and populates QueryJobConfiguration
preparedStatement.setLong(1, 42L);
assertDoesNotThrow(() -> preparedStatement.parameterHandler.configureParameters(configBuilder));
QueryJobConfiguration config = configBuilder.build();
assertEquals(1, config.getPositionalParameters().size());
assertEquals("42", config.getPositionalParameters().get(0).getValue());
assertEquals(StandardSQLTypeName.INT64, config.getPositionalParameters().get(0).getType());
}
@Test
public void testSetObjectWithTimestampStringAndTypesTimestamp_picosEnabled() throws Exception {
BigQueryConnection picosConnection = mock(BigQueryConnection.class);
doReturn(BigQueryConnection.SessionState.empty())
.when(picosConnection)
.getSessionStateSnapshot();
doReturn(true).when(picosConnection).isEnableTimestampPicos();
doReturn(BigQueryJdbcUrlUtility.DEFAULT_QUERY_DIALECT_VALUE)
.when(picosConnection)
.getQueryDialect();
BigQueryPreparedStatement ps =
new BigQueryPreparedStatement(picosConnection, "INSERT INTO t (col) VALUES (?)");
ps.setObject(1, "2024-01-01 12:34:56.123456789012", Types.TIMESTAMP);
assertEquals(Timestamp.class, ps.parameterHandler.getType(1));
assertEquals(StandardSQLTypeName.TIMESTAMP, ps.parameterHandler.getSqlType(1));
QueryJobConfiguration.Builder builder = QueryJobConfiguration.newBuilder("SELECT ?");
ps.parameterHandler.configureParameters(builder);
QueryJobConfiguration config = builder.build();
assertEquals(1, config.getPositionalParameters().size());
assertEquals(
"2024-01-01 12:34:56.123456789012", config.getPositionalParameters().get(0).getValue());
assertEquals(StandardSQLTypeName.TIMESTAMP, config.getPositionalParameters().get(0).getType());
}
@Test
public void testSetTimestamp_picosEnabledPreservesNanoseconds() throws Exception {
BigQueryConnection picosConnection = mock(BigQueryConnection.class);
doReturn(BigQueryConnection.SessionState.empty())
.when(picosConnection)
.getSessionStateSnapshot();
doReturn(true).when(picosConnection).isEnableTimestampPicos();
doReturn(BigQueryJdbcUrlUtility.DEFAULT_QUERY_DIALECT_VALUE)
.when(picosConnection)
.getQueryDialect();
BigQueryPreparedStatement ps =
new BigQueryPreparedStatement(picosConnection, "INSERT INTO t (col) VALUES (?)");
// Built from an Instant so the expected UTC rendering does not depend on the JVM zone.
Timestamp ts = Timestamp.from(Instant.parse("2024-01-01T12:34:56.123456789Z"));
ps.setTimestamp(1, ts);
QueryJobConfiguration.Builder builder = QueryJobConfiguration.newBuilder("SELECT ?");
ps.parameterHandler.configureParameters(builder);
QueryJobConfiguration config = builder.build();
assertEquals(1, config.getPositionalParameters().size());
assertEquals(
"2024-01-01 12:34:56.123456789Z", config.getPositionalParameters().get(0).getValue());
}
@Test
public void testSetTimestamp_picosDisabledTruncatesToMicroseconds() throws Exception {
BigQueryConnection nonPicosConnection = mock(BigQueryConnection.class);
doReturn(BigQueryConnection.SessionState.empty())
.when(nonPicosConnection)
.getSessionStateSnapshot();
doReturn(false).when(nonPicosConnection).isEnableTimestampPicos();
doReturn(BigQueryJdbcUrlUtility.DEFAULT_QUERY_DIALECT_VALUE)
.when(nonPicosConnection)
.getQueryDialect();
BigQueryPreparedStatement ps =
new BigQueryPreparedStatement(nonPicosConnection, "INSERT INTO t (col) VALUES (?)");
Timestamp ts = Timestamp.from(Instant.parse("2024-01-01T12:34:56.123456789Z"));
ps.setTimestamp(1, ts);
QueryJobConfiguration.Builder builder = QueryJobConfiguration.newBuilder("SELECT ?");
ps.parameterHandler.configureParameters(builder);
QueryJobConfiguration config = builder.build();
assertEquals(1, config.getPositionalParameters().size());
assertEquals("2024-01-01 12:34:56.123456Z", config.getPositionalParameters().get(0).getValue());
}
@Test
public void testBatchConfiguration_withEnableTimestampPicos() throws Exception {
BigQueryConnection picosConnection = mock(BigQueryConnection.class);
doReturn(BigQueryConnection.SessionState.empty())
.when(picosConnection)
.getSessionStateSnapshot();
doReturn(true).when(picosConnection).isEnableTimestampPicos();
doReturn(BigQueryJdbcUrlUtility.DEFAULT_QUERY_DIALECT_VALUE)
.when(picosConnection)
.getQueryDialect();
BigQueryPreparedStatement ps =
new BigQueryPreparedStatement(picosConnection, "INSERT INTO t (col) VALUES (?)");
ps.setTimestamp(1, Timestamp.from(Instant.parse("2024-01-01T12:34:56.123456789Z")));
ps.addBatch();
QueryJobConfiguration batchConfig =
ps.getStandardBatchJobConfiguration("INSERT INTO t (col) VALUES (?)");
assertEquals(1, batchConfig.getPositionalParameters().size());
assertEquals(
"2024-01-01 12:34:56.123456789Z", batchConfig.getPositionalParameters().get(0).getValue());
}
@ParameterizedTest
@ValueSource(strings = {"UTC", "America/New_York", "Asia/Kolkata"})
public void testSetObjectLocalDateTime_bindsDigitsAsString_inAnyJvmZone(String zone)
throws SQLException {
TimeZone previous = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone(zone));
preparedStatement.setObject(1, LocalDateTime.of(2025, 1, 1, 12, 34, 56, 123_000_000));
assertEquals(
"2025-01-01 12:34:56.123000", preparedStatement.parameterHandler.getParameter(1));
assertEquals(StandardSQLTypeName.STRING, preparedStatement.parameterHandler.getSqlType(1));
} finally {
TimeZone.setDefault(previous);
}
}
@Test
public void testSetObjectLocalDateTime_zeroSecondsStillHasFullDigits() throws SQLException {
// LocalDateTime.toString() would print "2025-01-01T12:00"; BigQuery needs full HH:mm:ss.SSSSSS.
preparedStatement.setObject(1, LocalDateTime.of(2025, 1, 1, 12, 0));
assertEquals("2025-01-01 12:00:00.000000", preparedStatement.parameterHandler.getParameter(1));
}
}