lidavidm commented on code in PR #39681:
URL: https://github.com/apache/arrow/pull/39681#discussion_r1466766217


##########
java/memory/memory-core/src/main/java/org/apache/arrow/memory/ArrowBuf.java:
##########
@@ -346,6 +347,29 @@ public void setLong(long index, long value) {
     MemoryUtil.UNSAFE.putLong(addr(index), value);
   }
 
+  /**
+   * Get float16 value stored at a particular index in the
+   * underlying memory chunk this ArrowBuf has access to.
+   * @param index index (0 based relative to this ArrowBuf)
+   *              where the value will be read from
+   * @return 4 byte float value
+   */
+  public float getFloat16(long index) {
+    return Float16.toFloat(getShort(index));
+  }
+
+
+  /**
+   * Set float16 value at a particular index in the
+   * underlying memory chunk this ArrowBuf has access to.
+   * @param index index (0 based relative to this ArrowBuf)
+   *              where the value will be written
+   * @param value value to write
+   */
+  public void setFloat16(long index, float value) {

Review Comment:
   IMO, these methods should work via shorts and not floats, since there is no 
conversion ambiguity with shorts.



##########
java/dataset/src/test/java/org/apache/arrow/dataset/TestAllTypes.java:
##########
@@ -260,4 +266,29 @@ public void testAllTypesParquet() throws Exception {
       }
     }
   }
+
+  /*
+  The purpose of this method is to refresh the data in alltypes-java.parquet 
as required:
+  
https://github.com/apache/arrow-testing/blob/master/data/parquet/alltypes-java.parquet
+   */
+  public static void main(String[] args) throws Exception {
+    TestAllTypes test = new TestAllTypes();
+    try (BufferAllocator allocator = new RootAllocator();
+         VectorSchemaRoot root = test.generateAllTypesVector(allocator)) {
+      byte[] featherData = test.serializeFile(root);
+      try (SeekableByteChannel channel = new 
ByteArrayReadableSeekableByteChannel(featherData);
+           ArrowStreamReader reader = new ArrowStreamReader(channel, 
allocator)) {
+        TMP.create();
+        final File writtenFolder = TMP.newFolder();
+        final String writtenParquet = writtenFolder.toURI().toString();
+        DatasetFileWriter.write(allocator, reader, FileFormat.PARQUET,
+                writtenParquet);
+        Objects.requireNonNull(writtenFolder.listFiles());
+        Files.move(writtenFolder.listFiles()[0].toPath(),
+                Paths.get(writtenFolder.toPath().toString(), 
"alltypes-java.parquet"));
+        System.out.println("The file data/parquet/alltypes-java.parquet should 
be updated with this new data: " +
+                writtenFolder.listFiles()[0].toURI());
+      }
+    }
+  }

Review Comment:
   Can you refactor this so that it doesn't duplicate the test itself? Also, 
technically this isn't really necessary; you can run the test in an IDE (and 
set a breakpoint so you can copy the temporary file)



##########
java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/Float16.java:
##########
@@ -0,0 +1,262 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.memory.util;
+
+
+import org.apache.arrow.util.VisibleForTesting;
+
+/**
+ * The class is a utility class to manipulate half-precision 16-bit
+ * <a 
href="https://en.wikipedia.org/wiki/Half-precision_floating-point_format";>IEEE 
754</a>
+ * floating point data types (also called fp16 or binary16). A half-precision 
float can be
+ * created from or converted to single-precision floats, and is stored in a 
short data type.
+ * The IEEE 754 standard specifies an float16 as having the following format:
+ * <ul>
+ * <li>Sign bit: 1 bit</li>
+ * <li>Exponent width: 5 bits</li>
+ * <li>Significand: 10 bits</li>
+ * </ul>
+ *
+ * <p>The format is laid out as follows:</p>
+ * <pre>
+ * 1   11111   1111111111
+ * ^   --^--   -----^----
+ * sign  |          |_______ significand
+ *       |
+ *      -- exponent
+ * </pre>
+ * Half-precision floating points can be useful to save memory and/or
+ * bandwidth at the expense of range and precision when compared to 
single-precision
+ * floating points (float32).
+ * Ref: 
https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/libcore/util/FP16.java
+ */
+public class Float16 {

Review Comment:
   Document inline that the file is copied from Parquet.



##########
java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestArrowBuf.java:
##########
@@ -180,4 +181,15 @@ public void testEnabledHistoricalLog() {
       ((Logger) LoggerFactory.getLogger("org.apache.arrow")).setLevel(null);
     }
   }
+
+  @Test
+  public void testArrowBufFloat16() {
+    try (BufferAllocator allocator = new RootAllocator();
+         ArrowBuf buf = allocator.buffer(1024)
+    ) {
+      buf.setFloat16(0, +32.875f);
+      assertEquals((short) 0x501c, Float16.toFloat16(+32.875f));
+      assertEquals(Float16.toFloat((short) 0x501c), buf.getFloat16(0), 0);

Review Comment:
   Yeah, we should just use short in the first place.



##########
java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/Float16.java:
##########
@@ -0,0 +1,262 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.memory.util;
+
+
+import org.apache.arrow.util.VisibleForTesting;
+
+/**
+ * The class is a utility class to manipulate half-precision 16-bit
+ * <a 
href="https://en.wikipedia.org/wiki/Half-precision_floating-point_format";>IEEE 
754</a>
+ * floating point data types (also called fp16 or binary16). A half-precision 
float can be
+ * created from or converted to single-precision floats, and is stored in a 
short data type.
+ * The IEEE 754 standard specifies an float16 as having the following format:
+ * <ul>
+ * <li>Sign bit: 1 bit</li>
+ * <li>Exponent width: 5 bits</li>
+ * <li>Significand: 10 bits</li>
+ * </ul>
+ *
+ * <p>The format is laid out as follows:</p>
+ * <pre>
+ * 1   11111   1111111111
+ * ^   --^--   -----^----
+ * sign  |          |_______ significand
+ *       |
+ *      -- exponent
+ * </pre>
+ * Half-precision floating points can be useful to save memory and/or
+ * bandwidth at the expense of range and precision when compared to 
single-precision
+ * floating points (float32).
+ * Ref: 
https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/libcore/util/FP16.java
+ */
+public class Float16 {

Review Comment:
   Also, how much of this class do we actually need?



##########
java/vector/src/test/java/org/apache/arrow/vector/TestValueVector.java:
##########
@@ -332,6 +332,106 @@ public void testSizeOfValueBuffer() {
     }
   }
 
+  @Test /* Float2Vector */

Review Comment:
   redundant comment?



##########
java/dataset/pom.xml:
##########
@@ -192,6 +192,15 @@
                     </execution>
                 </executions>
             </plugin>
+            <plugin>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <configuration>
+                    <enableAssertions>false</enableAssertions>
+                    <systemPropertyVariables>
+                        
<arrow.test.dataRoot>${project.basedir}/../../testing/data</arrow.test.dataRoot>

Review Comment:
   Where is this actually used? I don't see any new tests referencing it.



-- 
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