This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new 575b0957a6 GH-36384: [Go] Schema: NumFields (#36365)
575b0957a6 is described below
commit 575b0957a601ccf208695c336daae142bdb17559
Author: Thor <[email protected]>
AuthorDate: Sun Jul 2 11:59:29 2023 -0500
GH-36384: [Go] Schema: NumFields (#36365)
### 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](https://github.com/rtpsw/arrow/commit/802674b73c94c84388a6775b424ebe4f6e04274e)
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 [...]
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 <[email protected]>
Signed-off-by: Matt Topol <[email protected]>
---
go/arrow/schema.go | 5 +++--
go/arrow/schema_test.go | 18 +++++++++++++++++-
2 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/go/arrow/schema.go b/go/arrow/schema.go
index 03fa79c24c..4aef961a0d 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 a12c119e4c..481bba387a 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()))
+}