@@ -71,7 +71,9 @@ public BaseLargeVariableWidthVector(Field field, final BufferAllocator allocator
7171 lastValueCapacity = INITIAL_VALUE_ALLOCATION - 1 ;
7272 valueCount = 0 ;
7373 lastSet = -1 ;
74- offsetBuffer = allocator .getEmpty ();
74+ // Allocate offset buffer with at least OFFSET_WIDTH capacity to ensure
75+ // offset[0] is always available according to Arrow spec.
76+ offsetBuffer = allocateOffsetBuffer (OFFSET_WIDTH );
7577 validityBuffer = allocator .getEmpty ();
7678 valueBuffer = allocator .getEmpty ();
7779 }
@@ -383,7 +385,19 @@ private void setReaderAndWriterIndex() {
383385 // Both are set to 0 means 0 bytes are written to the IPC stream which will crash IPC readers
384386 // in other libraries. According to Arrow spec, we should still output the offset buffer which
385387 // is [0].
386- offsetBuffer .writerIndex ((long ) (valueCount + 1 ) * OFFSET_WIDTH );
388+ final long requiredOffsetBufferSize = (long ) (valueCount + 1 ) * OFFSET_WIDTH ;
389+ if (offsetBuffer .capacity () < requiredOffsetBufferSize ) {
390+ // Allocate a new buffer with sufficient capacity. This can happen when vector
391+ // was loaded via loadFieldBuffers() with an empty offset buffer.
392+ ArrowBuf newOffsetBuffer = allocateOffsetBuffer (requiredOffsetBufferSize );
393+ // Copy existing data if any
394+ if (offsetBuffer .capacity () > 0 ) {
395+ newOffsetBuffer .setBytes (0 , offsetBuffer , 0 , offsetBuffer .capacity ());
396+ }
397+ offsetBuffer .getReferenceManager ().release ();
398+ offsetBuffer = newOffsetBuffer ;
399+ }
400+ offsetBuffer .writerIndex (requiredOffsetBufferSize );
387401 }
388402
389403 /** Same as {@link #allocateNewSafe()}. */
@@ -495,7 +509,9 @@ private void allocateBytes(final long valueBufferSize, final int valueCount) {
495509
496510 /* allocate offset buffer */
497511 private ArrowBuf allocateOffsetBuffer (final long size ) {
498- ArrowBuf offsetBuffer = allocator .buffer (size );
512+ // Ensure at least OFFSET_WIDTH capacity according to Arrow spec
513+ final long curSize = Math .max (size , OFFSET_WIDTH );
514+ ArrowBuf offsetBuffer = allocator .buffer (curSize );
499515 offsetBuffer .readerIndex (0 );
500516 offsetBuffer .setZero (0 , offsetBuffer .capacity ());
501517 return offsetBuffer ;
0 commit comments