jbonofre commented on code in PR #1407:
URL: https://github.com/apache/arrow-java/pull/1407#discussion_r4728571380


##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat4VectorAccessor.java:
##########
@@ -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));

Review Comment:
   +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.



##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat4VectorAccessor.java:
##########
@@ -99,7 +99,7 @@ public float getFloat() {
 
   @Override
   public double getDouble() {
-    return this.getFloat();
+    return Double.parseDouble(Float.toString(this.getFloat()));

Review Comment:
   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.
   
   ```suggestion
       return this.getFloat();
   ```
   
   The `getDouble` expectations in the tests would then go back to the widened 
value.



##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat4VectorAccessorTest.java:
##########
@@ -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]);

Review Comment:
   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?
   
   ```java
   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"};
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to