diff --git a/arrow/array/table_test.go b/arrow/array/table_test.go index 08fea1a8..4720f606 100644 --- a/arrow/array/table_test.go +++ b/arrow/array/table_test.go @@ -203,8 +203,10 @@ func TestChunkedInvalid(t *testing.T) { } }() + // Keep a valid chunk before the mismatch so failed construction must not + // leave an extra reference to it. c1 := arrow.NewChunked(arrow.PrimitiveTypes.Int32, []arrow.Array{ - f1, f2, + f2, f1, }) defer c1.Release() } diff --git a/arrow/table.go b/arrow/table.go index bdbf85bf..4f873350 100644 --- a/arrow/table.go +++ b/arrow/table.go @@ -146,6 +146,12 @@ type Chunked struct { // // NewChunked panics if the chunks do not have the same data type. func NewChunked(dtype DataType, chunks []Array) *Chunked { + for _, chunk := range chunks { + if chunk != nil && !TypeEqual(chunk.DataType(), dtype) { + panic(fmt.Errorf("%w: arrow/array: mismatch data type %s vs %s", ErrInvalid, chunk.DataType().String(), dtype.String())) + } + } + arr := &Chunked{ chunks: make([]Array, 0, len(chunks)), dtype: dtype, @@ -157,9 +163,6 @@ func NewChunked(dtype DataType, chunks []Array) *Chunked { continue } - if !TypeEqual(chunk.DataType(), dtype) { - panic(fmt.Errorf("%w: arrow/array: mismatch data type %s vs %s", ErrInvalid, chunk.DataType().String(), dtype.String())) - } chunk.Retain() arr.chunks = append(arr.chunks, chunk) arr.length += chunk.Len()