From 02388edd5acfbc669bf64b474ebe9a0e28bad8d4 Mon Sep 17 00:00:00 2001 From: Alex Dubov Date: Fri, 8 May 2026 18:56:23 +1000 Subject: [PATCH] RecordBuilder: add Resize method Presently added Resize method addresses two, relatively common needs: 1. All fields within the Builder may need to be resized to the same length. 2. As part of error recovery process, fields may need to be truncated to the length of the shortest one, effectively discarding incomplete "rows". Fixes #796 --- arrow/array/record.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/arrow/array/record.go b/arrow/array/record.go index 0aaf771b5..6853dcca0 100644 --- a/arrow/array/record.go +++ b/arrow/array/record.go @@ -348,6 +348,34 @@ func (b *RecordBuilder) Reserve(size int) { } } +// Resize adjusts the space allocated by all the field builders to n elements. +// If n is greater than an individual builder Cap(), additional memory will be +// allocated. If n is smaller, the allocated memory may reduced. +// +// As a special case, if n equals to -1, all field builders will be resized +// to the size of the shortest one. +func (b *RecordBuilder) Resize(n int) { + if n >= 0 { + for _, f := range b.fields { + f.Resize(n) + } + } else if n == -1 && len(b.fields) > 0 { + minLen := b.fields[0].Len() + var resizeNeeded bool + + for _, f := range b.fields[1 : ] { + if f.Len() < minLen { + resizeNeeded = true + minLen = f.Len() + } + } + + if resizeNeeded { + b.Resize(minLen) + } + } +} + // NewRecordBatch creates a new record batch from the memory buffers and resets the // RecordBuilder so it can be used to build a new record batch. //