Skip to content

Commit

Permalink
GH-36384: [Go] Schema: NumFields (#36365)
Browse files Browse the repository at this point in the history
### Rationale for this change

Previously if one wanted to iterate over the fields in the schema you would call the `Fields()` function and just iterate over the slice. However, due to [this commit](rtpsw@802674b) there is now an allocation and copy that happens when that's called. So to iterate over the fields without allocations one now must use the `Field(i int)` method; however that means a user must already know exactly how many fields are in the schema which isn't possible today.

This adds a simple `NumFields() int` method that returns the number of fields in a schema to allow a user to iterate over all the fields without having to copy them. 

### What changes are included in this PR?

Expose the number of fields in a schema for iteration over fields.

Single function added `NumFields() int` to schema

### Are these changes tested?

N/A

### Are there any user-facing changes?

Yes this is a new API

* Closes: #36384

Authored-by: thorfour <me@thor-hansen.com>
Signed-off-by: Matt Topol <zotthewizard@gmail.com>
  • Loading branch information
thorfour committed Jul 2, 2023
1 parent 0061d79 commit 575b095
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 3 additions & 2 deletions go/arrow/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
18 changes: 17 additions & 1 deletion go/arrow/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()))
}

0 comments on commit 575b095

Please sign in to comment.