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

Commit e04ab3e

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

3 files changed

Lines changed: 31 additions & 8 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ 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 ? (byte) 0 : checkedCastToByte(spanner.getBigDecimal(spannerIndex).toBigInteger());
311311
case PG_NUMERIC:
312312
return isNull
313313
? (byte) 0
@@ -354,7 +354,7 @@ public short getShort(int columnIndex) throws SQLException {
354354
}
355355
return isNull ? 0 : checkedCastToShort(spanner.getLong(spannerIndex));
356356
case NUMERIC:
357-
return isNull ? 0 : checkedCastToShort(spanner.getBigDecimal(spannerIndex));
357+
return isNull ? (short) 0 : checkedCastToShort(spanner.getBigDecimal(spannerIndex).toBigInteger());
358358
case PG_NUMERIC:
359359
return isNull
360360
? 0
@@ -395,7 +395,7 @@ public int getInt(int columnIndex) throws SQLException {
395395
case ENUM:
396396
return isNull ? 0 : checkedCastToInt(spanner.getLong(spannerIndex));
397397
case NUMERIC:
398-
return isNull ? 0 : checkedCastToInt(spanner.getBigDecimal(spannerIndex));
398+
return isNull ? 0 : checkedCastToInt(spanner.getBigDecimal(spannerIndex).toBigInteger());
399399
case PG_NUMERIC:
400400
return isNull
401401
? 0
@@ -432,7 +432,7 @@ public long getLong(int columnIndex) throws SQLException {
432432
case ENUM:
433433
return isNull ? 0L : spanner.getLong(spannerIndex);
434434
case NUMERIC:
435-
return isNull ? 0 : checkedCastToLong(parseBigDecimal(spanner.getString(spannerIndex)));
435+
return isNull ? 0L : checkedCastToLong(spanner.getBigDecimal(spannerIndex).toBigInteger());
436436
case PG_NUMERIC:
437437
return isNull
438438
? 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 {

0 commit comments

Comments
 (0)