Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit bc31c62

Browse files
committed
fix: fix getLong on NUMERIC
1 parent c6ca732 commit bc31c62

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

src/main/java/com/google/cloud/spanner/jdbc/JdbcResultSet.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ public byte getByte(int columnIndex) throws SQLException {
307307
case ENUM:
308308
return isNull ? (byte) 0 : checkedCastToByte(spanner.getLong(spannerIndex));
309309
case NUMERIC:
310-
return isNull ? (byte) 0 : checkedCastToByte(spanner.getBigDecimal(spannerIndex));
310+
return isNull
311+
? (byte) 0
312+
: checkedCastToByte(spanner.getBigDecimal(spannerIndex).toBigInteger());
311313
case PG_NUMERIC:
312314
return isNull
313315
? (byte) 0
@@ -354,7 +356,9 @@ public short getShort(int columnIndex) throws SQLException {
354356
}
355357
return isNull ? 0 : checkedCastToShort(spanner.getLong(spannerIndex));
356358
case NUMERIC:
357-
return isNull ? 0 : checkedCastToShort(spanner.getBigDecimal(spannerIndex));
359+
return isNull
360+
? (short) 0
361+
: checkedCastToShort(spanner.getBigDecimal(spannerIndex).toBigInteger());
358362
case PG_NUMERIC:
359363
return isNull
360364
? 0
@@ -395,7 +399,7 @@ public int getInt(int columnIndex) throws SQLException {
395399
case ENUM:
396400
return isNull ? 0 : checkedCastToInt(spanner.getLong(spannerIndex));
397401
case NUMERIC:
398-
return isNull ? 0 : checkedCastToInt(spanner.getBigDecimal(spannerIndex));
402+
return isNull ? 0 : checkedCastToInt(spanner.getBigDecimal(spannerIndex).toBigInteger());
399403
case PG_NUMERIC:
400404
return isNull
401405
? 0
@@ -432,7 +436,7 @@ public long getLong(int columnIndex) throws SQLException {
432436
case ENUM:
433437
return isNull ? 0L : spanner.getLong(spannerIndex);
434438
case NUMERIC:
435-
return isNull ? 0 : checkedCastToLong(parseBigDecimal(spanner.getString(spannerIndex)));
439+
return isNull ? 0L : checkedCastToLong(spanner.getBigDecimal(spannerIndex).toBigInteger());
436440
case PG_NUMERIC:
437441
return isNull
438442
? 0L

src/main/java/com/google/cloud/spanner/jdbc/JdbcTypeConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,28 +127,28 @@ static Object convert(Object value, Type type, Class<?> targetType) throws SQLEx
127127
if (type.getCode() == Code.BOOL) return (Boolean) value ? 1L : 0L;
128128
if (type.getCode() == Code.INT64 || type.getCode() == Code.ENUM) return value;
129129
if (type.getCode() == Code.NUMERIC)
130-
return AbstractJdbcWrapper.checkedCastToLong((BigDecimal) value);
130+
return AbstractJdbcWrapper.checkedCastToLong(((BigDecimal) value).toBigInteger());
131131
}
132132
if (targetType.equals(Integer.class)) {
133133
if (type.getCode() == Code.BOOL) return (Boolean) value ? 1 : 0;
134134
if (type.getCode() == Code.INT64 || type.getCode() == Code.ENUM)
135135
return AbstractJdbcWrapper.checkedCastToInt((Long) value);
136136
if (type.getCode() == Code.NUMERIC)
137-
return AbstractJdbcWrapper.checkedCastToInt((BigDecimal) value);
137+
return AbstractJdbcWrapper.checkedCastToInt(((BigDecimal) value).toBigInteger());
138138
}
139139
if (targetType.equals(Short.class)) {
140140
if (type.getCode() == Code.BOOL) return (Boolean) value ? 1 : 0;
141141
if (type.getCode() == Code.INT64 || type.getCode() == Code.ENUM)
142142
return AbstractJdbcWrapper.checkedCastToShort((Long) value);
143143
if (type.getCode() == Code.NUMERIC)
144-
return AbstractJdbcWrapper.checkedCastToShort((BigDecimal) value);
144+
return AbstractJdbcWrapper.checkedCastToShort(((BigDecimal) value).toBigInteger());
145145
}
146146
if (targetType.equals(Byte.class)) {
147147
if (type.getCode() == Code.BOOL) return (Boolean) value ? 1 : 0;
148148
if (type.getCode() == Code.INT64 || type.getCode() == Code.ENUM)
149149
return AbstractJdbcWrapper.checkedCastToByte((Long) value);
150150
if (type.getCode() == Code.NUMERIC)
151-
return AbstractJdbcWrapper.checkedCastToByte((BigDecimal) value);
151+
return AbstractJdbcWrapper.checkedCastToByte(((BigDecimal) value).toBigInteger());
152152
}
153153
if (targetType.equals(BigInteger.class)) {
154154
if (type.getCode() == Code.BOOL) return (Boolean) value ? BigInteger.ONE : BigInteger.ZERO;

src/test/java/com/google/cloud/spanner/jdbc/JdbcResultSetTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,29 @@ public void testGetLongIndexForFloat64() throws SQLException {
594594
assertTrue(subject.wasNull());
595595
}
596596

597+
@Test
598+
public void testGetIntegerTypesOnNumeric() throws SQLException {
599+
assertEquals((byte) 0, subject.getByte(NUMERIC_COLINDEX_NULL));
600+
assertTrue(subject.wasNull());
601+
assertEquals((byte) 3, subject.getByte(NUMERIC_COLINDEX_NOTNULL));
602+
assertFalse(subject.wasNull());
603+
604+
assertEquals((short) 0, subject.getShort(NUMERIC_COLINDEX_NULL));
605+
assertTrue(subject.wasNull());
606+
assertEquals((short) 3, subject.getShort(NUMERIC_COLINDEX_NOTNULL));
607+
assertFalse(subject.wasNull());
608+
609+
assertEquals(0, subject.getInt(NUMERIC_COLINDEX_NULL));
610+
assertTrue(subject.wasNull());
611+
assertEquals(3, subject.getInt(NUMERIC_COLINDEX_NOTNULL));
612+
assertFalse(subject.wasNull());
613+
614+
assertEquals(0L, subject.getLong(NUMERIC_COLINDEX_NULL));
615+
assertTrue(subject.wasNull());
616+
assertEquals(3L, subject.getLong(NUMERIC_COLINDEX_NOTNULL));
617+
assertFalse(subject.wasNull());
618+
}
619+
597620
@Test
598621
public void testGetLongIndexForString() {
599622
try {

src/test/java/com/google/cloud/spanner/jdbc/JdbcTypeConverterTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,15 +452,13 @@ public void testConvertNumeric() throws SQLException {
452452
assertThat(convert(d, Type.numeric(), String.class)).isEqualTo(String.valueOf(d));
453453
assertThat(convert(d, Type.numeric(), Boolean.class)).isEqualTo(!d.equals(BigDecimal.ZERO));
454454
if (d.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0
455-
|| d.compareTo(BigDecimal.valueOf(Long.MIN_VALUE)) < 0
456-
|| d.scale() > 0) {
455+
|| d.compareTo(BigDecimal.valueOf(Long.MIN_VALUE)) < 0) {
457456
assertConvertThrows(d, Type.numeric(), Long.class, Code.OUT_OF_RANGE);
458457
} else {
459458
assertThat(convert(d, Type.numeric(), Long.class)).isEqualTo(d.longValue());
460459
}
461460
if (d.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0
462-
|| d.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) < 0
463-
|| d.scale() > 0) {
461+
|| d.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) < 0) {
464462
assertConvertThrows(d, Type.numeric(), Integer.class, Code.OUT_OF_RANGE);
465463
} else {
466464
assertThat(convert(d, Type.numeric(), Integer.class)).isEqualTo(d.intValue());

0 commit comments

Comments
 (0)