Skip to content

Commit 8e464fe

Browse files
authored
fix(bigquery-jdbc): handle SQL NULLs in ResultSet primitive getters (#14383)
Fixed #14371
1 parent 438cda6 commit 8e464fe

2 files changed

Lines changed: 83 additions & 20 deletions

File tree

‎java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseResultSet.java‎

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
import io.opentelemetry.api.trace.SpanContext;
3333
import io.opentelemetry.context.Context;
3434
import io.opentelemetry.context.Scope;
35+
import java.io.ByteArrayInputStream;
3536
import java.io.InputStream;
3637
import java.io.Reader;
3738
import java.io.StringReader;
3839
import java.math.BigDecimal;
40+
import java.nio.charset.Charset;
3941
import java.nio.charset.StandardCharsets;
4042
import java.sql.Array;
4143
import java.sql.Blob;
@@ -51,6 +53,8 @@
5153
import java.time.ZoneId;
5254
import java.util.Calendar;
5355
import java.util.List;
56+
import javax.sql.rowset.serial.SerialBlob;
57+
import javax.sql.rowset.serial.SerialClob;
5458

5559
public abstract class BigQueryBaseResultSet extends BigQueryNoOpsResultSet
5660
implements BigQueryResultSet {
@@ -341,7 +345,8 @@ public boolean getBoolean(int columnIndex) throws SQLException {
341345

342346
try {
343347
Object value = getObject(columnIndex);
344-
return BigQueryTypeRegistry.convert(value, Boolean.class);
348+
Boolean result = BigQueryTypeRegistry.convert(value, Boolean.class);
349+
return result != null && result;
345350
} catch (BigQueryJdbcException e) {
346351
throw createCoercionException(columnIndex, Boolean.class, e);
347352
}
@@ -352,7 +357,8 @@ public byte getByte(int columnIndex) throws SQLException {
352357
LOG.finestTrace("getByte");
353358
try {
354359
Object value = getObject(columnIndex);
355-
return BigQueryTypeRegistry.convert(value, Byte.class);
360+
Byte result = BigQueryTypeRegistry.convert(value, Byte.class);
361+
return result != null ? result : 0;
356362
} catch (BigQueryJdbcException e) {
357363
throw createCoercionException(columnIndex, Byte.class, e);
358364
}
@@ -363,7 +369,8 @@ public short getShort(int columnIndex) throws SQLException {
363369
LOG.finestTrace("getShort");
364370
try {
365371
Object value = getObject(columnIndex);
366-
return BigQueryTypeRegistry.convert(value, Short.class);
372+
Short result = BigQueryTypeRegistry.convert(value, Short.class);
373+
return result != null ? result : 0;
367374
} catch (BigQueryJdbcException e) {
368375
throw createCoercionException(columnIndex, Short.class, e);
369376
}
@@ -374,7 +381,8 @@ public int getInt(int columnIndex) throws SQLException {
374381
LOG.finestTrace("getInt");
375382
try {
376383
Object value = getObject(columnIndex);
377-
return BigQueryTypeRegistry.convert(value, Integer.class);
384+
Integer result = BigQueryTypeRegistry.convert(value, Integer.class);
385+
return result != null ? result : 0;
378386
} catch (BigQueryJdbcException e) {
379387
throw createCoercionException(columnIndex, Integer.class, e);
380388
}
@@ -385,7 +393,8 @@ public long getLong(int columnIndex) throws SQLException {
385393
LOG.finestTrace("getLong");
386394
try {
387395
Object value = getObject(columnIndex);
388-
return BigQueryTypeRegistry.convert(value, Long.class);
396+
Long result = BigQueryTypeRegistry.convert(value, Long.class);
397+
return result != null ? result : 0L;
389398
} catch (BigQueryJdbcException e) {
390399
throw createCoercionException(columnIndex, Long.class, e);
391400
}
@@ -396,7 +405,8 @@ public float getFloat(int columnIndex) throws SQLException {
396405
LOG.finestTrace("getFloat");
397406
try {
398407
Object value = getObject(columnIndex);
399-
return BigQueryTypeRegistry.convert(value, Float.class);
408+
Float result = BigQueryTypeRegistry.convert(value, Float.class);
409+
return result != null ? result : 0.0f;
400410
} catch (BigQueryJdbcException e) {
401411
throw createCoercionException(columnIndex, Float.class, e);
402412
}
@@ -407,7 +417,8 @@ public double getDouble(int columnIndex) throws SQLException {
407417
LOG.finestTrace("getDouble");
408418
try {
409419
Object value = getObject(columnIndex);
410-
return BigQueryTypeRegistry.convert(value, Double.class);
420+
Double result = BigQueryTypeRegistry.convert(value, Double.class);
421+
return result != null ? result : 0.0d;
411422
} catch (BigQueryJdbcException e) {
412423
throw createCoercionException(columnIndex, Double.class, e);
413424
}
@@ -442,9 +453,9 @@ public Date getDate(int columnIndex) throws SQLException {
442453
LOG.finestTrace("getDate");
443454
try {
444455
Object value = getObject(columnIndex);
445-
return BigQueryTypeRegistry.convert(value, java.sql.Date.class);
456+
return BigQueryTypeRegistry.convert(value, Date.class);
446457
} catch (BigQueryJdbcException e) {
447-
throw createCoercionException(columnIndex, java.sql.Date.class, e);
458+
throw createCoercionException(columnIndex, Date.class, e);
448459
}
449460
}
450461

@@ -453,13 +464,13 @@ public Time getTime(int columnIndex) throws SQLException {
453464
LOG.finestTrace("getTime");
454465
StandardSQLTypeName type = getStandardSQLTypeName(columnIndex);
455466
if (type == StandardSQLTypeName.INT64) {
456-
throw createCoercionException(columnIndex, java.sql.Time.class, null);
467+
throw createCoercionException(columnIndex, Time.class, null);
457468
}
458469
try {
459470
Object value = getObject(columnIndex);
460-
return BigQueryTypeRegistry.convert(value, java.sql.Time.class);
471+
return BigQueryTypeRegistry.convert(value, Time.class);
461472
} catch (BigQueryJdbcException e) {
462-
throw createCoercionException(columnIndex, java.sql.Time.class, e);
473+
throw createCoercionException(columnIndex, Time.class, e);
463474
}
464475
}
465476

@@ -468,13 +479,13 @@ public Timestamp getTimestamp(int columnIndex) throws SQLException {
468479
LOG.finestTrace("getTimestamp");
469480
StandardSQLTypeName type = getStandardSQLTypeName(columnIndex);
470481
if (type == StandardSQLTypeName.INT64) {
471-
throw createCoercionException(columnIndex, java.sql.Timestamp.class, null);
482+
throw createCoercionException(columnIndex, Timestamp.class, null);
472483
}
473484
try {
474485
Object value = getObject(columnIndex);
475-
return BigQueryTypeRegistry.convert(value, java.sql.Timestamp.class);
486+
return BigQueryTypeRegistry.convert(value, Timestamp.class);
476487
} catch (BigQueryJdbcException e) {
477-
throw createCoercionException(columnIndex, java.sql.Timestamp.class, e);
488+
throw createCoercionException(columnIndex, Timestamp.class, e);
478489
}
479490
}
480491

@@ -503,14 +514,14 @@ public Array getArray(int columnIndex) throws SQLException {
503514
public Blob getBlob(int columnIndex) throws SQLException {
504515
LOG.finestTrace("getBlob");
505516
byte[] value = getBytes(columnIndex);
506-
return new javax.sql.rowset.serial.SerialBlob(value);
517+
return value == null ? null : new SerialBlob(value);
507518
}
508519

509520
@Override
510521
public Clob getClob(int columnIndex) throws SQLException {
511522
LOG.finestTrace("getClob");
512523
String value = getString(columnIndex);
513-
return new javax.sql.rowset.serial.SerialClob(value.toCharArray());
524+
return value == null ? null : new SerialClob(value.toCharArray());
514525
}
515526

516527
@Override
@@ -520,12 +531,12 @@ public Reader getCharacterStream(int columnIndex) throws SQLException {
520531
return value == null ? null : new StringReader(value);
521532
}
522533

523-
private InputStream getInputStream(String value, java.nio.charset.Charset charset) {
534+
private InputStream getInputStream(String value, Charset charset) {
524535
LOG.finestTrace("getInputStream");
525536
if (value == null) {
526537
return null;
527538
}
528-
return new java.io.ByteArrayInputStream(value.getBytes(charset));
539+
return new ByteArrayInputStream(value.getBytes(charset));
529540
}
530541

531542
@Override
@@ -546,7 +557,7 @@ public InputStream getUnicodeStream(int columnIndex) throws SQLException {
546557
public InputStream getBinaryStream(int columnIndex) throws SQLException {
547558
LOG.finestTrace("getBinaryStream");
548559
byte[] bytes = getBytes(columnIndex);
549-
return bytes == null ? null : new java.io.ByteArrayInputStream(bytes);
560+
return bytes == null ? null : new ByteArrayInputStream(bytes);
550561
}
551562

552563
@Override

‎java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBigQueryJDBCTest.java‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,46 @@ private void validate(
25112511
}
25122512
}
25132513

2514+
private void validateNull(
2515+
String method, BiFunction<ResultSet, Integer, Object> getter, Object expectedDefaultValue)
2516+
throws Exception {
2517+
2518+
try (Connection connection = DriverManager.getConnection(connection_uri);
2519+
Connection connectionHTAPI =
2520+
DriverManager.getConnection(
2521+
connection_uri
2522+
+ ";HighThroughputMinTableSize=0;HighThroughputActivationRatio=0;EnableHighThroughputAPI=1;");
2523+
Statement statement = connection.createStatement();
2524+
Statement statementHTAPI = connectionHTAPI.createStatement()) {
2525+
2526+
String query =
2527+
String.format(
2528+
"SELECT * FROM `%s.%s.all_bq_types` WHERE stringField is null", PROJECT_ID, DATASET);
2529+
ResultSet resultSetRegular = statement.executeQuery(query);
2530+
ResultSet resultSetArrow = statementHTAPI.executeQuery(query);
2531+
resultSetRegular.next();
2532+
resultSetArrow.next();
2533+
2534+
for (int i = 1; i <= resultSetRegular.getMetaData().getColumnCount(); i++) {
2535+
String columnName = resultSetRegular.getMetaData().getColumnName(i);
2536+
if (!columnName.contains("array")) {
2537+
String regularApiLabel =
2538+
String.format(
2539+
"[Method: %s] [Column: %s] [API: Regular] [Null Scenario]", method, columnName);
2540+
String htapiApiLabel =
2541+
String.format(
2542+
"[Method: %s] [Column: %s] [API: HTAPI] [Null Scenario]", method, columnName);
2543+
2544+
assertEquals(expectedDefaultValue, getter.apply(resultSetRegular, i), regularApiLabel);
2545+
assertTrue(resultSetRegular.wasNull(), regularApiLabel + " wasNull should be true");
2546+
2547+
assertEquals(expectedDefaultValue, getter.apply(resultSetArrow, i), htapiApiLabel);
2548+
assertTrue(resultSetArrow.wasNull(), htapiApiLabel + " wasNull should be true");
2549+
}
2550+
}
2551+
}
2552+
}
2553+
25142554
@Test
25152555
public void validateGetString() throws Exception {
25162556
DateTimeFormatter timestampFormatter =
@@ -2523,6 +2563,7 @@ public void validateGetString() throws Exception {
25232563
"[%s, %s]",
25242564
Timestamp.from(Instant.parse("2023-01-01T01:00:00Z")),
25252565
Timestamp.from(Instant.parse("2023-01-01T02:00:00Z")));
2566+
25262567
final ImmutableMap<String, Object> stringResults =
25272568
new ImmutableMap.Builder<String, Object>()
25282569
.put("stringField", "StringValue")
@@ -2567,6 +2608,7 @@ public void validateGetString() throws Exception {
25672608
}
25682609
};
25692610
validate("getString", getter, stringResults);
2611+
validateNull("getString", getter, null);
25702612
}
25712613

25722614
@Test
@@ -2587,6 +2629,7 @@ public void validateGetInt() throws Exception {
25872629
}
25882630
};
25892631
validate("getInt", getter, result);
2632+
validateNull("getInt", getter, 0);
25902633
}
25912634

25922635
@Test
@@ -2607,6 +2650,7 @@ public void validateGetLong() throws Exception {
26072650
}
26082651
};
26092652
validate("getLong", getter, result);
2653+
validateNull("getLong", getter, 0L);
26102654
}
26112655

26122656
@Test
@@ -2629,6 +2673,7 @@ public void validateGetBool() throws Exception {
26292673
}
26302674
};
26312675
validate("getBool", getter, result);
2676+
validateNull("getBool", getter, false);
26322677
}
26332678

26342679
@Test
@@ -2650,6 +2695,7 @@ public void validateGetFloat() throws Exception {
26502695
}
26512696
};
26522697
validate("getFloat", getter, result);
2698+
validateNull("getFloat", getter, 0.0f);
26532699
}
26542700

26552701
@Test
@@ -2671,6 +2717,7 @@ public void validateGetDouble() throws Exception {
26712717
}
26722718
};
26732719
validate("getDouble", getter, result);
2720+
validateNull("getDouble", getter, 0.0d);
26742721
}
26752722

26762723
@Test
@@ -2691,6 +2738,7 @@ public void validateGetShort() throws Exception {
26912738
}
26922739
};
26932740
validate("getShort", getter, result);
2741+
validateNull("getShort", getter, (short) 0);
26942742
}
26952743

26962744
@Test
@@ -2712,6 +2760,7 @@ public void validateGetTime() throws Exception {
27122760
}
27132761
};
27142762
validate("getTime", getter, result);
2763+
validateNull("getTime", getter, null);
27152764
}
27162765

27172766
@Test
@@ -2731,6 +2780,7 @@ public void validateGetDate() throws Exception {
27312780
}
27322781
};
27332782
validate("getDate", getter, result);
2783+
validateNull("getDate", getter, null);
27342784
}
27352785

27362786
@Test
@@ -2751,6 +2801,7 @@ public void validateGetTimestamp() throws Exception {
27512801
}
27522802
};
27532803
validate("getTimestamp", getter, result);
2804+
validateNull("getTimestamp", getter, null);
27542805
}
27552806

27562807
@Test
@@ -2770,6 +2821,7 @@ public void validateGetByte() throws Exception {
27702821
}
27712822
};
27722823
validate("getByte", getter, result);
2824+
validateNull("getByte", getter, (byte) 0);
27732825
}
27742826

27752827
@Test

0 commit comments

Comments
 (0)