Skip to content

Commit b0d39cd

Browse files
author
Yicong Huang
committed
fix: change write index explicitly
1 parent 878c8d5 commit b0d39cd

File tree

2 files changed

+12
-40
lines changed

2 files changed

+12
-40
lines changed

vector/src/main/java/org/apache/arrow/vector/complex/LargeListVector.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -275,21 +275,9 @@ public void loadFieldBuffers(ArrowFieldNode fieldNode, List<ArrowBuf> ownBuffers
275275
@Override
276276
public List<ArrowBuf> getFieldBuffers() {
277277
List<ArrowBuf> result = new ArrayList<>(2);
278-
279-
// Ensure offset buffer has at least one entry for offset[0].
280-
// According to Arrow specification, offset buffer must have N+1 entries,
281-
// even when N=0, it should contain [0].
282-
if (offsetBuffer.capacity() == 0) {
283-
// Save and restore offsetAllocationSizeInBytes to avoid affecting subsequent allocateNew()
284-
long savedOffsetAllocationSize = offsetAllocationSizeInBytes;
285-
offsetBuffer = allocateOffsetBuffer(OFFSET_WIDTH);
286-
offsetAllocationSizeInBytes = savedOffsetAllocationSize;
287-
}
288-
289278
setReaderAndWriterIndex();
290279
result.add(validityBuffer);
291280
result.add(offsetBuffer);
292-
293281
return result;
294282
}
295283

@@ -318,14 +306,12 @@ public void exportCDataBuffers(List<ArrowBuf> buffers, ArrowBuf buffersPtr, long
318306
private void setReaderAndWriterIndex() {
319307
validityBuffer.readerIndex(0);
320308
offsetBuffer.readerIndex(0);
321-
if (valueCount == 0) {
322-
validityBuffer.writerIndex(0);
323-
// Even when valueCount is 0, offset buffer should have offset[0] per Arrow spec
324-
offsetBuffer.writerIndex(Math.min(OFFSET_WIDTH, offsetBuffer.capacity()));
325-
} else {
326-
validityBuffer.writerIndex(BitVectorHelper.getValidityBufferSizeFromCount(valueCount));
327-
offsetBuffer.writerIndex((valueCount + 1) * OFFSET_WIDTH);
328-
}
309+
validityBuffer.writerIndex(BitVectorHelper.getValidityBufferSizeFromCount(valueCount));
310+
// IPC serializer will determine readable bytes based on `readerIndex` and `writerIndex`.
311+
// Both are set to 0 means 0 bytes are written to the IPC stream which will crash IPC readers
312+
// in other libraries. According to Arrow spec, we should still output the offset buffer which
313+
// is [0].
314+
offsetBuffer.writerIndex((long) (valueCount + 1) * OFFSET_WIDTH);
329315
}
330316

331317
/**

vector/src/main/java/org/apache/arrow/vector/complex/ListVector.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,9 @@ public void loadFieldBuffers(ArrowFieldNode fieldNode, List<ArrowBuf> ownBuffers
233233
@Override
234234
public List<ArrowBuf> getFieldBuffers() {
235235
List<ArrowBuf> result = new ArrayList<>(2);
236-
237-
// Ensure offset buffer has at least one entry for offset[0].
238-
// According to Arrow specification, offset buffer must have N+1 entries,
239-
// even when N=0, it should contain [0].
240-
if (offsetBuffer.capacity() == 0) {
241-
// Save and restore offsetAllocationSizeInBytes to avoid affecting subsequent allocateNew()
242-
long savedOffsetAllocationSize = offsetAllocationSizeInBytes;
243-
offsetBuffer = allocateOffsetBuffer(OFFSET_WIDTH);
244-
offsetAllocationSizeInBytes = savedOffsetAllocationSize;
245-
}
246-
247236
setReaderAndWriterIndex();
248237
result.add(validityBuffer);
249238
result.add(offsetBuffer);
250-
251239
return result;
252240
}
253241

@@ -276,14 +264,12 @@ public void exportCDataBuffers(List<ArrowBuf> buffers, ArrowBuf buffersPtr, long
276264
private void setReaderAndWriterIndex() {
277265
validityBuffer.readerIndex(0);
278266
offsetBuffer.readerIndex(0);
279-
if (valueCount == 0) {
280-
validityBuffer.writerIndex(0);
281-
// Even when valueCount is 0, offset buffer should have offset[0] per Arrow spec
282-
offsetBuffer.writerIndex(Math.min(OFFSET_WIDTH, offsetBuffer.capacity()));
283-
} else {
284-
validityBuffer.writerIndex(BitVectorHelper.getValidityBufferSizeFromCount(valueCount));
285-
offsetBuffer.writerIndex((valueCount + 1) * OFFSET_WIDTH);
286-
}
267+
validityBuffer.writerIndex(BitVectorHelper.getValidityBufferSizeFromCount(valueCount));
268+
// IPC serializer will determine readable bytes based on `readerIndex` and `writerIndex`.
269+
// Both are set to 0 means 0 bytes are written to the IPC stream which will crash IPC readers
270+
// in other libraries. According to Arrow spec, we should still output the offset buffer which
271+
// is [0].
272+
offsetBuffer.writerIndex((long) (valueCount + 1) * OFFSET_WIDTH);
287273
}
288274

289275
/**

0 commit comments

Comments
 (0)