binary-signal opened a new issue, #2157:
URL: https://github.com/apache/fluss/issues/2157

   ### Search before asking
   
   - [x] I searched in the [issues](https://github.com/apache/fluss/issues) and 
found nothing similar.
   
   
   ### Fluss version
   
   main (development)
   
   ### Please describe the bug 🐞
   
   ## Describe the bug
   Calling `toDoubleArray()` (or other `to*Array()` methods) on a `BinaryArray` 
throws `ArrayIndexOutOfBoundsException` when the array has more than a few 
elements. (20 elements).
   
   
   Example Code
   ```java
       public static Point toPoint(InternalRow row) {
   
           String   timeseriesId = row.getString(0).toString();
           double[] values       = row.getArray(1).toDoubleArray(); // BOOM
   
   
           // Create Point
           return new Point(timeseriesId, values);
       }
   
   ```
   
   Will throw out of bound exception when `toDoubleArray()` is called
   
   ```text
   **Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 20 out of 
bounds for length 20
        at 
org.apache.fluss.row.BinarySegmentUtils.copyToUnsafe(BinarySegmentUtils.java:1158)
        at org.apache.fluss.row.BinaryArray.toDoubleArray(BinaryArray.java:530)
        at gr.tuc.flink.cdstream.row.RowMapper.toPoint(RowMapper.java:40)
        at 
gr.tuc.flink.cdstream.row.AsyncRowBatchLoader.lambda$loadAsync$0(AsyncRowBatchLoader.java:50)
        at 
java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:646)
        ... 41 more
   ```
   
   ## How to reproduce
   
   ```java
   // Create a BinaryArray from a double array with 20 elements
   double[] values = new double[20];
   for (int i = 0; i < 20; i++) {
       values[i] = i * 1.1;
   }
   
   // This works fine
   BinaryArray binaryArray = BinaryArray.fromPrimitiveArray(values);
   
   // This throws ArrayIndexOutOfBoundsException
   double[] result = binaryArray.toDoubleArray();
   ```
   
   ## Error stacktrace
   
   ```
   java.lang.ArrayIndexOutOfBoundsException: Index 20 out of bounds for length 
20
       at 
org.apache.fluss.row.BinarySegmentUtils.copyToUnsafe(BinarySegmentUtils.java:1158)
       at org.apache.fluss.row.BinaryArray.toDoubleArray(BinaryArray.java:530)
   ```
   
   ## Expected behavior
   `toDoubleArray()` should return a `double[]` with the same values as the 
original array.
   
   ## Actual behavior
   `ArrayIndexOutOfBoundsException` is thrown.
   
   ## Root cause
   The `to*Array()` methods in `BinaryArray` pass `*_ARRAY_OFFSET` constants 
(e.g., `DOUBLE_ARRAY_OFFSET = UNSAFE.arrayBaseOffset(double[].class)`) as the 
`targetOffset` parameter to `BinarySegmentUtils.copyToUnsafe()`. 
   
   These constants are Unsafe memory offsets (typically 16 bytes on 64-bit 
JVMs), but `copyToUnsafe()` treats `targetOffset` as a regular Java array index 
(`target[targetOffset + i]`), not an Unsafe memory offset.
   
   ## Affected methods
   - `BinaryArray.toBooleanArray()`
   - `BinaryArray.toByteArray()`
   - `BinaryArray.toShortArray()`
   - `BinaryArray.toIntArray()`
   - `BinaryArray.toLongArray()`
   - `BinaryArray.toFloatArray()`
   - `BinaryArray.toDoubleArray()`
   
   ## Environment
   - Fluss version: 0.9-SNAPSHOT (after commit `05a5db96`)
   - Java version: 11+
   
   
   ### Solution
   
   _No response_
   
   ### Are you willing to submit a PR?
   
   - [x] I'm willing to submit a PR!


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