Skip to content

Commit

Permalink
[CALCITE-4602] ClassCastException retrieving from ARRAY that has mixe…
Browse files Browse the repository at this point in the history
…d INTEGER and DECIMAL elements

Close #143
  • Loading branch information
snuyanzin authored and julianhyde committed Oct 3, 2021
1 parent 820edf6 commit d88f91a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,32 @@ public boolean getBoolean() throws SQLException {
}
}

/**
* Accessor that assumes that the underlying value is a {@link BigDecimal};
* corresponds to {@link java.sql.Types#DECIMAL}.
*/
private static class BigDecimalAccessor extends BigNumberAccessor {
private BigDecimalAccessor(Getter getter) {
super(getter);
}

protected Number getNumber() throws SQLException {
return (Number) getObject();
}

public BigDecimal getBigDecimal(int scale) throws SQLException {
Number number = getNumber();
return number == null || number instanceof BigDecimal
? (BigDecimal) number : BigDecimal.valueOf(number.longValue());
}

public BigDecimal getBigDecimal() throws SQLException {
Number number = getNumber();
return number == null || number instanceof BigDecimal
? (BigDecimal) number : BigDecimal.valueOf(number.longValue());
}
}

/**
* Accessor that assumes that the underlying value is a {@link Number};
* corresponds to {@link java.sql.Types#NUMERIC}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ public TestMetaImpl(AvaticaConnection connection) {
ColumnMetaData.Rep.NUMBER),
"ARRAY",
ColumnMetaData.Rep.ARRAY),
DatabaseMetaData.columnNoNulls),
columnMetaData("decimal_array", 18,
ColumnMetaData.array(
ColumnMetaData.scalar(Types.DECIMAL, "DECIMAL",
ColumnMetaData.Rep.PRIMITIVE_DOUBLE),
"ARRAY",
ColumnMetaData.Rep.ARRAY),
DatabaseMetaData.columnNoNulls));

List<Object> row = Collections.<Object>singletonList(
Expand All @@ -216,7 +223,8 @@ public TestMetaImpl(AvaticaConnection connection) {
null,
Arrays.asList(123, 18234),
Arrays.asList(1476130718123L, 1479123123242L),
Arrays.asList(1476123L, 147912242L)
Arrays.asList(1476123L, 147912242L),
Arrays.asList(1, 1.1)
});

CursorFactory factory = CursorFactory.deduce(columns, null);
Expand Down Expand Up @@ -596,6 +604,24 @@ private ArrayAccessorTestHelper(Getter g) {
}
}

/**
* Accessor test helper for decimal array column.
*/
private static final class DecimalArrayAccessorTestHelper extends AccessorTestHelper {
private DecimalArrayAccessorTestHelper(Getter g) {
super(g);
}

@Override public void testGetArray(ResultSet resultSet) throws SQLException {
ColumnMetaData.ScalarType intType =
ColumnMetaData.scalar(Types.DECIMAL, "DECIMAL", ColumnMetaData.Rep.PRIMITIVE_DOUBLE);
Array expectedArray =
new ArrayFactoryImpl(Unsafe.localCalendar().getTimeZone()).createArray(
intType, Arrays.asList(1, 1.1));
assertTrue(ArrayImpl.equalContents(expectedArray, g.getArray(resultSet)));
}
}

/**
* Accessor test helper for date array column.
*/
Expand Down Expand Up @@ -1150,7 +1176,9 @@ public static Collection<AccessorTestHelper> data() {
new TimestampArrayAccessorTestHelper(new OrdinalGetter(17)),
new TimestampArrayAccessorTestHelper(new LabelGetter("timestamp_array")),
new TimeArrayAccessorTestHelper(new OrdinalGetter(18)),
new TimeArrayAccessorTestHelper(new LabelGetter("time_array")));
new TimeArrayAccessorTestHelper(new LabelGetter("time_array")),
new DecimalArrayAccessorTestHelper(new OrdinalGetter(19)),
new DecimalArrayAccessorTestHelper(new LabelGetter("decimal_array")));
}

private final AccessorTestHelper testHelper;
Expand Down

0 comments on commit d88f91a

Please sign in to comment.