Repository navigation
GH-1406: Read Float4 values as double and BigDecimal with correct precision #1407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,7 +99,7 @@ public float getFloat() { | |
|
|
||
| @Override | ||
| public double getDouble() { | ||
| return this.getFloat(); | ||
| return Double.parseDouble(Float.toString(this.getFloat())); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -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)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 | ||
|
|
@@ -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)))); | ||
| } | ||
| }); | ||
| } | ||
|
|
@@ -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))); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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) fis exact: it returns the value the float actually holds. SQL Server, DuckDB and PostgreSQL (binary protocol) return the same thing.ResultSet.getObject()goes throughAvaticaSite.get, which callsgetDouble()forTypes.FLOAT. So this also changesgetObject()for every REAL column. It adds aFloat.toStringplus aDouble.parseDoublefor eachcell, which was about 100 times slower than the cast in a rough local group (~430ns vs ~5ns per call on JDK 21).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, andtestShouldGetDoubleThatRoundTripsToTheSameFloatwouldn't catch it.The
getDoubleexpectations in the tests would then go back to the widened value.