Skip to content

[Bug] ArrayIndexOutOfBoundsException when calling toDoubleArray() with arrays larger than a few elements #2157

@binary-signal

Description

@binary-signal

Search before asking

  • I searched in the 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

    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

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

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

  • I'm willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions