apache / apache/arrow-java

Empty ListVector and LargeListVector can expose offset buffers with writerIndex greater than capacity

Ouverte
#1,194 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
Java
Étoiles
94
Forks
152
Merge moyen
3 j 16 h
PR mergées (30 j)
11

Description

### Describe the bug, including details regarding any error messages, version, and platform.

`ListVector` and `LargeListVector` can expose an invalid offset buffer state when `valueCount == 0`.

For an empty list vector, the logical offset buffer should still contain the leading offset entry:

- `ListVector`: `(valueCount + 1) * 4 == 4` bytes
- `LargeListVector`: `(valueCount + 1) * 8 == 8` bytes

However, in the empty-vector path, the offset buffer can have:

```text
readerIndex: 0
writerIndex: 4
capacity: 0
```

or the equivalent `writerIndex: 8, capacity: 0` for `LargeListVector`.

This violates the normal buffer invariant:

```text
0 <= readerIndex <= writerIndex <= capacity
```

Downstream consumers that unwrap or serialize the Arrow buffer through Netty can then fail with:

```text
IndexOutOfBoundsException: readerIndex: 0, writerIndex: 4
(expected: 0 <= readerIndex <= writerIndex <= capacity(0))
```

The issue is that `setReaderAndWriterIndex()` sets the offset buffer writer index based on `valueCount * OFFSET_WIDTH`, which is `0` for empty vectors. But list vectors still require one offset slot even when there are no values.

The same issue applies to both:

- `org.apache.arrow.vector.complex.ListVector`
- `org.apache.arrow.vector.complex.LargeListVector`

### Expected behavior

For `valueCount == 0`, the offset buffer should still have enough capacity and readable bytes for the leading zero offset:

```text
(valueCount + 1) * OFFSET_WIDTH
```

So:

- empty `ListVector` should expose at least 4 bytes for offset `[0]`
- empty `LargeListVector` should expose at least 8 bytes for offset `[0]`

The first offset value should be zero.

### Actual behavior

An empty list vector can expose an offset buffer with a non-zero writer index but zero capacity, causing Netty buffer validation to fail when the buffer is unwrapped or consumed.

### Suggested fix

Update `ListVector.setReaderAndWriterIndex()` and `LargeListVector.setReaderAndWriterIndex()` so the offset buffer writer index is based on:

```java
(valueCount + 1) * OFFSET_WIDTH
```

For the `valueCount == 0` case, ensure the offset buffer has enough capacity for the leading zero offset before setting the writer index.

Care should be taken not to shrink the vector's future offset allocation size when allocating this empty sentinel offset buffer.

### Additional context

This was observed downstream in Dremio after upgrading Arrow Java. The failure occurred while sending a record batch containing an empty list vector, where the send path unwraps Arrow buffers through Netty.

The downstream error was:

```text
SYSTEM ERROR: IndexOutOfBoundsException: readerIndex: 0, writerIndex: 4
(expected: 0 <= readerIndex <= writerIndex <= capacity(0))
```

This issue is distinct from #1125. That issue involves `UnionListReader.setPosition` on a post-IPC empty list. This issue is about the offset buffer exported by empty `ListVector` / `LargeListVector` instances having an invalid writer-index/capacity relationship.

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez par inspecter setReaderAndWriterIndex() dans org.apache.arrow.vector.complex.ListVector et LargeListVector, puis suivez la manière dont leurs buffers d’offsets sont alloués pour valueCount == 0. Vérifiez que le buffer exporté conserve l’offset zéro initial et respecte writerIndex <= capacity pour les deux types de vecteurs sans réduire l’allocation future d’offsets.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
java
Domaine
data
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
Calme
Clarté
Clairement spécifiée
Accessibilité débutants
72/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.