From 89eae9dd043531d087d003ad5f192b7ca886ac8a Mon Sep 17 00:00:00 2001 From: Noah Treuhaft Date: Tue, 29 Nov 2022 11:56:25 -0500 Subject: [PATCH] GH-14775: [Go] Fix UnionBuilder.Len implementations (*DenseUnionBuilder).Len and (*SparseUnionBuilder).Len always return 0. They should instead return the number of values appended. This bug exists because these methods are promoted (via unionBuilder) from (*builder).Len, which reads builder.length, but DenseUnionBuilder and SparseUnionBuilder don't update that field. Fix by adding a (*unionBuilder).Len implementation that calls unionBuilder.typesBuilder.Len. --- go/arrow/array/union.go | 3 +++ go/arrow/array/union_test.go | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/go/arrow/array/union.go b/go/arrow/array/union.go index f5bd4396c39..a1ac4f7e7a5 100644 --- a/go/arrow/array/union.go +++ b/go/arrow/array/union.go @@ -745,6 +745,9 @@ func (b *unionBuilder) Child(idx int) Builder { return b.children[idx] } +// Len returns the current number of elements in the builder. +func (b *unionBuilder) Len() int { return b.typesBuilder.Len() } + func (b *unionBuilder) Mode() arrow.UnionMode { return b.mode } func (b *unionBuilder) reserve(elements int, resize func(int)) { diff --git a/go/arrow/array/union_test.go b/go/arrow/array/union_test.go index 56cec197751..fff73b35c54 100644 --- a/go/arrow/array/union_test.go +++ b/go/arrow/array/union_test.go @@ -625,6 +625,8 @@ func (s *UnionBuilderSuite) appendBasics() { s.appendInt(-10) s.appendDbl(0.5) + s.Equal(9, s.unionBldr.Len()) + s.actual = s.unionBldr.NewArray().(array.Union) s.NoError(s.actual.ValidateFull()) s.createExpectedTypesArr() @@ -640,6 +642,8 @@ func (s *UnionBuilderSuite) appendNullsAndEmptyValues() { s.unionBldr.AppendEmptyValues(2) s.expectedTypes = append(s.expectedTypes, s.I8, s.I8, s.I8) + s.Equal(8, s.unionBldr.Len()) + s.actual = s.unionBldr.NewArray().(array.Union) s.NoError(s.actual.ValidateFull()) s.createExpectedTypesArr() @@ -664,6 +668,8 @@ func (s *UnionBuilderSuite) appendInferred() { s.appendDbl(-1.0) s.appendDbl(0.5) + s.Equal(9, s.unionBldr.Len()) + s.actual = s.unionBldr.NewArray().(array.Union) s.NoError(s.actual.ValidateFull()) s.createExpectedTypesArr() @@ -695,6 +701,8 @@ func (s *UnionBuilderSuite) appendListOfInferred(utyp arrow.UnionType) *array.Li s.EqualValues(2, s.DBL) s.appendDbl(0.5) + s.Equal(4, s.unionBldr.Len()) + s.createExpectedTypesArr() return listBldr.NewListArray() }