diff --git a/go/arrow/schema.go b/go/arrow/schema.go index 03fa79c24c364..4aef961a0d04a 100644 --- a/go/arrow/schema.go +++ b/go/arrow/schema.go @@ -194,12 +194,13 @@ func (sc *Schema) WithEndianness(e endian.Endianness) *Schema { func (sc *Schema) Endianness() endian.Endianness { return sc.endianness } func (sc *Schema) IsNativeEndian() bool { return sc.endianness == endian.NativeEndian } func (sc *Schema) Metadata() Metadata { return sc.meta } -func (sc *Schema) Fields() []Field { +func (sc *Schema) Fields() []Field { fields := make([]Field, len(sc.fields)) copy(fields, sc.fields) return fields } -func (sc *Schema) Field(i int) Field { return sc.fields[i] } +func (sc *Schema) Field(i int) Field { return sc.fields[i] } +func (sc *Schema) NumFields() int { return len(sc.fields) } func (sc *Schema) FieldsByName(n string) ([]Field, bool) { indices, ok := sc.index[n] diff --git a/go/arrow/schema_test.go b/go/arrow/schema_test.go index a12c119e4c682..481bba387a308 100644 --- a/go/arrow/schema_test.go +++ b/go/arrow/schema_test.go @@ -342,7 +342,7 @@ func TestSchemaAddField(t *testing.T) { if got, want := len(s.Fields()), 3; got != want { t.Fatalf("invalid number of fields. got=%d, want=%d", got, want) } - got, want := s.Field(2), Field{Name: "f3", Type: PrimitiveTypes.Int32}; + got, want := s.Field(2), Field{Name: "f3", Type: PrimitiveTypes.Int32} if !got.Equal(want) { t.Fatalf("invalid field: got=%#v, want=%#v", got, want) } @@ -462,3 +462,19 @@ func TestSchemaEqual(t *testing.T) { }) } } + +func TestSchemaNumFields(t *testing.T) { + s := NewSchema([]Field{ + {Name: "f1", Type: PrimitiveTypes.Int32}, + {Name: "f2", Type: PrimitiveTypes.Int64}, + }, nil) + + assert.Equal(t, 2, s.NumFields()) + + var err error + s, err = s.AddField(2, Field{Name: "f3", Type: PrimitiveTypes.Int32}) + assert.NoError(t, err) + + assert.Equal(t, 3, s.NumFields()) + assert.Equal(t, s.NumFields(), len(s.Fields())) +}