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 @@ -99,7 +99,7 @@ public float getFloat() {

@Override
public double getDouble() {
return this.getFloat();
return Double.parseDouble(Float.toString(this.getFloat()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep the widening cast here, for a few reasons:

  • (double) f is exact: it returns the value the float actually holds. SQL Server, DuckDB and PostgreSQL (binary protocol) return the same thing.
  • For a REAL column, ResultSet.getObject() goes through AvaticaSite.get, which calls getDouble() for Types.FLOAT. So this also changes getObject() for every REAL column. It adds a Float.toString plus a Double.parseDouble for each cell, which was about 100 times slower than the cast in a rough local group (~430ns vs ~5ns per call on JDK 21).
  • Going through the string doesn't always convert back: for Float.intBitsToFloat(0x15ae43fd) (7.038531E-26f) and its negative, (float) getDouble() != getFloat()on both JDK 17 and 21, because the decimal is rounded to a double first and then to a float. It's the only such value, but it shows the conversion is subtler than it looks, and testShouldGetDoubleThatRoundTripsToTheSameFloat wouldn't catch it.
Suggested change
return Double.parseDouble(Float.toString(this.getFloat()));
return this.getFloat();

The getDouble expectations in the tests would then go back to the widened value.

}

@Override
Expand All @@ -110,7 +110,7 @@ public BigDecimal getBigDecimal() throws SQLException {
throw new SQLException("BigDecimal doesn't support Infinite/NaN.");
}

return this.wasNull ? null : BigDecimal.valueOf(value);
return this.wasNull ? null : new BigDecimal(Float.toString(value));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on this. One thing worth noting in the Javadoc or the PR description: the result now follows Float.toString, whose output changed in JDK 19. For large values the digits differ by JDK. For example, 2147483648f gives 2147483650 on JDK 17 and 2147483600 on JDK 21. Both convert back to the same float, and the Float8 accessor's BigDecimal.valueOf(double) already behaves this way, so I'm fine with it. It just should not surprise users.

}

@Override
Expand All @@ -119,7 +119,9 @@ public BigDecimal getBigDecimal(int scale) throws SQLException {
if (Float.isInfinite(value) || Float.isNaN(value)) {
throw new SQLException("BigDecimal doesn't support Infinite/NaN.");
}
return this.wasNull ? null : BigDecimal.valueOf(value).setScale(scale, RoundingMode.HALF_UP);
return this.wasNull
? null
: new BigDecimal(Float.toString(value)).setScale(scale, RoundingMode.HALF_UP);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,80 @@ public void testShouldGetDoubleMethodFromFloat4Vector() throws Exception {
accessorIterator.assertAccessorGetter(
vector,
ArrowFlightJdbcFloat4VectorAccessor::getDouble,
accessor -> is((double) accessor.getFloat()));
accessor -> is(Double.parseDouble(Float.toString(accessor.getFloat()))));
}

@Test
public void testShouldGetDoubleThatRoundTripsToTheSameFloat() throws Exception {
accessorIterator.iterate(
vector,
(accessor, currentRow) ->
assertThat((float) accessor.getDouble(), is(accessor.getFloat())));
}

@Test
public void testShouldGetDecimalValuesWithoutFloatWideningArtifacts() throws Exception {
final float[] values = {0.1f, -0.1f, 0.2f, 0.3f, 1.1f, 123.456f, 3.4e-5f, -0.0f};
try (final Float4Vector float4Vector =
new Float4Vector("ID", rootAllocatorTestExtension.getRootAllocator())) {
for (int i = 0; i < values.length; i++) {
float4Vector.setSafe(i, values[i]);
}
float4Vector.setValueCount(values.length);

accessorIterator.iterate(
float4Vector,
(accessor, currentRow) -> {
final String expected = Float.toString(values[currentRow]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These expectations are built with the same expression as the implementation, so they pass whatever Float.toString returns. These inputs are all small, so their shortest form is the same on every JDK.

Could we spell out the expected values instead?

final float[] values = {0.1f, -0.1f, 0.2f, 0.3f, 1.1f, 123.456f, 3.4e-5f, -0.0f};
final String[] expected = {"0.1", "-0.1", "0.2", "0.3", "1.1", "123.456", "3.4E-5", "-0.0"};

assertThat(accessor.getDouble(), is(Double.parseDouble(expected)));
assertThat(accessor.getObject(Double.class), is(Double.parseDouble(expected)));
assertThat(accessor.getBigDecimal(), is(new BigDecimal(expected)));
assertThat(
accessor.getBigDecimal(9),
is(new BigDecimal(expected).setScale(9, RoundingMode.HALF_UP)));
});
}
}

@Test
public void testShouldGetDoubleFromFloat4VectorWithZeroPointOne() throws Exception {
try (final Float4Vector float4Vector =
new Float4Vector("ID", rootAllocatorTestExtension.getRootAllocator())) {
float4Vector.setSafe(0, 0.1f);
float4Vector.setValueCount(1);

accessorIterator.iterate(
float4Vector,
(accessor, currentRow) -> {
assertThat(accessor.getDouble(), is(0.1));
assertThat(accessor.getBigDecimal(), is(new BigDecimal("0.1")));
assertThat(accessor.getBigDecimal(9), is(new BigDecimal("0.100000000")));
});
}
}

@Test
public void testShouldGetDoubleMethodFromFloat4VectorWithNull() throws Exception {
try (final Float4Vector float4Vector =
new Float4Vector("ID", rootAllocatorTestExtension.getRootAllocator())) {
float4Vector.setNull(0);
float4Vector.setValueCount(1);

accessorIterator.assertAccessorGetter(
float4Vector, ArrowFlightJdbcFloat4VectorAccessor::getDouble, is(0.0));
}
}

@Test
public void testShouldGetDoubleMethodFromFloat4VectorWithNaN() throws Exception {
try (final Float4Vector float4Vector =
new Float4Vector("ID", rootAllocatorTestExtension.getRootAllocator())) {
float4Vector.setSafe(0, Float.NaN);
float4Vector.setValueCount(1);

accessorIterator.assertAccessorGetter(
float4Vector, ArrowFlightJdbcFloat4VectorAccessor::getDouble, is(Double.NaN));
}
}

@Test
Expand All @@ -191,7 +264,7 @@ public void testShouldGetBigDecimalMethodFromFloat4Vector() throws Exception {
if (Float.isInfinite(value) || Float.isNaN(value)) {
assertThrows(SQLException.class, accessor::getBigDecimal);
} else {
assertThat(accessor.getBigDecimal(), is(BigDecimal.valueOf(value)));
assertThat(accessor.getBigDecimal(), is(new BigDecimal(Float.toString(value))));
}
});
}
Expand All @@ -207,7 +280,7 @@ public void testShouldGetBigDecimalWithScaleMethodFromFloat4Vector() throws Exce
} else {
assertThat(
accessor.getBigDecimal(9),
is(BigDecimal.valueOf(value).setScale(9, RoundingMode.HALF_UP)));
is(new BigDecimal(Float.toString(value)).setScale(9, RoundingMode.HALF_UP)));
}
});
}
Expand Down
Loading