Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import static org.apache.arrow.driver.jdbc.accessor.impl.numeric.ArrowFlightJdbcNumericGetter.createGetter;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.sql.SQLException;
import java.util.function.IntSupplier;
import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor;
import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory;
Expand Down Expand Up @@ -161,25 +163,43 @@ public int getInt() {

@Override
public float getFloat() {
return (float) getLong();
final long value = getLong();
return exceedsLongRange(value)
? new BigDecimal(Long.toUnsignedString(value)).floatValue()
: (float) value;
}

@Override
public double getDouble() {
return (double) getLong();
final long value = getLong();
return exceedsLongRange(value)
? new BigDecimal(Long.toUnsignedString(value)).doubleValue()
: (double) value;
}

@Override
public BigDecimal getBigDecimal() {
final BigDecimal value = BigDecimal.valueOf(getLong());
return this.wasNull ? null : value;
final long value = getLong();
if (this.wasNull) {
return null;
}
return exceedsLongRange(value)
? new BigDecimal(Long.toUnsignedString(value))
: BigDecimal.valueOf(value);
}

@Override
public BigDecimal getBigDecimal(int scale) {
final BigDecimal value =
BigDecimal.valueOf(this.getDouble()).setScale(scale, RoundingMode.HALF_UP);
return this.wasNull ? null : value;
final BigDecimal value = getBigDecimal();
return value == null ? null : value.setScale(scale, RoundingMode.HALF_UP);
}

/**
* Whether {@code value} is a UInt64 above {@link Long#MAX_VALUE}. Narrower unsigned values are
* zero-extended by the getter, so only UInt64 can have a negative {@code long} representation.
*/
private boolean exceedsLongRange(long value) {
return isUnsigned && value < 0;
}

@Override
Expand Down Expand Up @@ -208,6 +228,15 @@ public Number getObject() {
return wasNull ? null : number;
}

@Override
public <T> T getObject(final Class<T> type) throws SQLException {
if (type == BigInteger.class) {
final BigDecimal value = getBigDecimal();
return value == null ? null : type.cast(value.toBigInteger());
}
return super.getObject(type);
}

@Override
public boolean getBoolean() {
final long value = getLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static Getter createGetter(UInt1Vector vector) {
vector.get(index, nullableUInt1Holder);

holder.isSet = nullableUInt1Holder.isSet;
holder.value = nullableUInt1Holder.value;
holder.value = Byte.toUnsignedLong(nullableUInt1Holder.value);
};
}

Expand Down Expand Up @@ -121,7 +121,7 @@ private static Getter createGetter(UInt4Vector vector) {
vector.get(index, nullableUInt4Holder);

holder.isSet = nullableUInt4Holder.isSet;
holder.value = nullableUInt4Holder.value;
holder.value = Integer.toUnsignedLong(nullableUInt4Holder.value);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public static List<ColumnMetaData> convertArrowFieldsToColumnMetaDataList(
Common.ColumnMetaData.newBuilder()
.setOrdinal(index)
.setColumnName(field.getName())
.setLabel(field.getName());
.setLabel(field.getName())
.setSigned(isSignedNumber(fieldType));

setOnColumnMetaDataBuilder(builder, field.getMetadata());

Expand All @@ -93,6 +94,23 @@ public static List<ColumnMetaData> convertArrowFieldsToColumnMetaDataList(
.collect(Collectors.toList());
}

/**
* Whether values of {@code type} are signed numbers, as reported by {@link
* java.sql.ResultSetMetaData#isSigned}. Matches the parameter metadata built by the {@code
* AvaticaParameterConverter}s.
*/
private static boolean isSignedNumber(final ArrowType type) {
switch (type.getTypeID()) {
case Int:
return ((ArrowType.Int) type).getIsSigned();
case FloatingPoint:
case Decimal:
return true;
default:
return false;
}
}

/**
* Set on Column MetaData Builder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.arrow.driver.jdbc.utils.CoreMockedSqlProducers;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -212,6 +216,33 @@ public void testShouldIsSearchable() throws SQLException {
assertThat(metadata.isSearchable(3), equalTo(true));
}

/**
* {@code isSigned} for the {@link CoreMockedSqlProducers#LEGACY_REGULAR_SQL_CMD} columns: {@code
* ID} (signed BIGINT), {@code Name} (VARCHAR), {@code Age} (unsigned INTEGER), {@code Salary}
* (DOUBLE), {@code Hire Date} (DATE) and {@code Last Sale} (TIMESTAMP). Checked on the prepared
* statement, whose metadata comes from the prepare response, and on its result set, whose
* metadata comes from the stream's schema as for every executed query.
*/
@Test
public void testShouldIsSigned() throws SQLException {
final List<Boolean> expected = Arrays.asList(true, false, false, true, false, false);
try (PreparedStatement preparedStatement =
connection.prepareStatement(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) {
assertThat(isSigned(preparedStatement.getMetaData()), equalTo(expected));
try (ResultSet resultSet = preparedStatement.executeQuery()) {
assertThat(isSigned(resultSet.getMetaData()), equalTo(expected));
}
}
}

private static List<Boolean> isSigned(final ResultSetMetaData metaData) throws SQLException {
final List<Boolean> signed = new ArrayList<>();
for (int column = 1; column <= metaData.getColumnCount(); column++) {
signed.add(metaData.isSigned(column));
}
return signed;
}

/**
* Test if {@link ResultSetMetaData#getColumnTypeName(int)} passing an column index that does not
* exist.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import static org.junit.jupiter.api.Assertions.*;

import com.google.common.collect.ImmutableSet;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
Expand Down Expand Up @@ -1068,4 +1070,36 @@ public void testPreparedStatementUpdateWithUuid() throws SQLException {
assertThat(updated, is(1));
}
}

/**
* Unsigned values read through the driver must match the value the server sent, including those
* with the type's sign bit set and UInt64 values above {@link Long#MAX_VALUE}.
*/
@Test
public void testShouldGetExactUnsignedValues() throws SQLException {
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(CoreMockedSqlProducers.UNSIGNED_SQL_CMD)) {
for (final String[] row : CoreMockedSqlProducers.UNSIGNED_VALUES) {
assertTrue(resultSet.next());
for (int column = 1; column <= row.length; column++) {
final String expected = row[column - 1];
final String label = "column " + column + " = " + expected;
assertEquals(expected, resultSet.getString(column), label);
assertEquals(new BigDecimal(expected), resultSet.getBigDecimal(column), label);
assertEquals(
new BigDecimal(expected).setScale(2), resultSet.getBigDecimal(column, 2), label);
assertEquals(Double.parseDouble(expected), resultSet.getDouble(column), label);
assertEquals(
new BigInteger(expected), resultSet.getObject(column, BigInteger.class), label);
assertEquals(
new BigDecimal(expected), resultSet.getObject(column, BigDecimal.class), label);
if (column < row.length) {
// UInt64 values above Long.MAX_VALUE don't fit a long.
assertEquals(Long.parseLong(expected), resultSet.getLong(column), label);
}
}
}
assertFalse(resultSet.next());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static org.hamcrest.CoreMatchers.equalTo;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory;
Expand Down Expand Up @@ -116,6 +118,48 @@ public void setup(Supplier<BaseIntVector> vectorSupplier) {
this.vector = vectorSupplier.get();
}

/**
* The value stored at {@code row}, read from the vector itself; unsigned values stay positive.
*/
private BigDecimal exactValue(int row) {
return vector instanceof UInt8Vector
? new BigDecimal(((UInt8Vector) vector).getObjectNoOverflow(row))
: BigDecimal.valueOf(vector.getValueAsLong(row));
}

@ParameterizedTest
@MethodSource("data")
public void testShouldGetBigDecimalMethodFromBaseIntVector(Supplier<BaseIntVector> vectorSupplier)
throws Exception {
setup(vectorSupplier);
accessorIterator.assertAccessorGetter(
vector,
ArrowFlightJdbcBaseIntVectorAccessor::getBigDecimal,
(accessor, currentRow) -> equalTo(exactValue(currentRow)));
}

@ParameterizedTest
@MethodSource("data")
public void testShouldGetStringMethodFromBaseIntVector(Supplier<BaseIntVector> vectorSupplier)
throws Exception {
setup(vectorSupplier);
accessorIterator.assertAccessorGetter(
vector,
ArrowFlightJdbcBaseIntVectorAccessor::getString,
(accessor, currentRow) -> equalTo(exactValue(currentRow).toPlainString()));
}

@ParameterizedTest
@MethodSource("data")
public void testShouldGetBigIntegerFromBaseIntVector(Supplier<BaseIntVector> vectorSupplier)
throws Exception {
setup(vectorSupplier);
accessorIterator.assertAccessorGetter(
vector,
accessor -> accessor.getObject(BigInteger.class),
(accessor, currentRow) -> equalTo(exactValue(currentRow).toBigIntegerExact()));
}

@AfterEach
public void tearDown() {
this.vector.close();
Expand All @@ -129,7 +173,7 @@ public void testShouldConvertToByteMethodFromBaseIntVector(Supplier<BaseIntVecto
accessorIterator.assertAccessorGetter(
vector,
ArrowFlightJdbcBaseIntVectorAccessor::getByte,
(accessor, currentRow) -> equalTo((byte) accessor.getLong()));
(accessor, currentRow) -> equalTo(exactValue(currentRow).byteValue()));
}

@ParameterizedTest
Expand All @@ -140,7 +184,7 @@ public void testShouldConvertToShortMethodFromBaseIntVector(
accessorIterator.assertAccessorGetter(
vector,
ArrowFlightJdbcBaseIntVectorAccessor::getShort,
(accessor, currentRow) -> equalTo((short) accessor.getLong()));
(accessor, currentRow) -> equalTo(exactValue(currentRow).shortValue()));
}

@ParameterizedTest
Expand All @@ -151,7 +195,18 @@ public void testShouldConvertToIntegerMethodFromBaseIntVector(
accessorIterator.assertAccessorGetter(
vector,
ArrowFlightJdbcBaseIntVectorAccessor::getInt,
(accessor, currentRow) -> equalTo((int) accessor.getLong()));
(accessor, currentRow) -> equalTo(exactValue(currentRow).intValue()));
}

@ParameterizedTest
@MethodSource("data")
public void testShouldConvertToLongMethodFromBaseIntVector(Supplier<BaseIntVector> vectorSupplier)
throws Exception {
setup(vectorSupplier);
accessorIterator.assertAccessorGetter(
vector,
ArrowFlightJdbcBaseIntVectorAccessor::getLong,
(accessor, currentRow) -> equalTo(exactValue(currentRow).longValue()));
}

@ParameterizedTest
Expand All @@ -162,7 +217,7 @@ public void testShouldConvertToFloatMethodFromBaseIntVector(
accessorIterator.assertAccessorGetter(
vector,
ArrowFlightJdbcBaseIntVectorAccessor::getFloat,
(accessor, currentRow) -> equalTo((float) accessor.getLong()));
(accessor, currentRow) -> equalTo(exactValue(currentRow).floatValue()));
}

@ParameterizedTest
Expand All @@ -173,7 +228,7 @@ public void testShouldConvertToDoubleMethodFromBaseIntVector(
accessorIterator.assertAccessorGetter(
vector,
ArrowFlightJdbcBaseIntVectorAccessor::getDouble,
(accessor, currentRow) -> equalTo((double) accessor.getLong()));
(accessor, currentRow) -> equalTo(exactValue(currentRow).doubleValue()));
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static org.hamcrest.CoreMatchers.equalTo;

import java.math.BigDecimal;
import java.math.BigInteger;
import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory;
import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils;
import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestExtension;
Expand Down Expand Up @@ -141,6 +143,26 @@ public void testShouldGetStringFromUnsignedValue() throws Exception {
equalTo("18446744073709551615"));
}

@Test
public void testShouldGetBigDecimalWithScaleFromBigIntAboveDoublePrecision() throws Exception {
final long value = (1L << 53) + 1; // 9007199254740993, the nearest double is 9007199254740992
try (BigIntVector vector =
new BigIntVector("ID", rootAllocatorTestExtension.getRootAllocator())) {
vector.setSafe(0, value);
vector.setValueCount(1);
accessorIterator.assertAccessorGetter(
vector, accessor -> accessor.getBigDecimal(0), equalTo(BigDecimal.valueOf(value)));
}
}

@Test
public void testShouldGetBigIntegerFromIntVectorWithNull() throws Exception {
accessorIterator.assertAccessorGetter(
intVectorWithNull,
accessor -> accessor.getObject(BigInteger.class),
CoreMatchers.nullValue());
}

@Test
public void testShouldGetBytesFromIntVectorThrowsSqlException() throws Exception {
accessorIterator.assertAccessorGetterThrowingException(
Expand Down
Loading