diff --git a/go/arrow/array/concat.go b/go/arrow/array/concat.go index 9d815023c4b76..dadc184839757 100644 --- a/go/arrow/array/concat.go +++ b/go/arrow/array/concat.go @@ -666,7 +666,7 @@ func concat(data []arrow.ArrayData, mem memory.Allocator) (arr arrow.ArrayData, } out.childData = []arrow.ArrayData{children} case *arrow.StructType: - out.childData = make([]arrow.ArrayData, len(dt.Fields())) + out.childData = make([]arrow.ArrayData, dt.NumFields()) for i := range dt.Fields() { children := gatherChildren(data, i) for _, c := range children { diff --git a/go/arrow/array/record.go b/go/arrow/array/record.go index 0b0fe4c3847fc..16ac82c4cb419 100644 --- a/go/arrow/array/record.go +++ b/go/arrow/array/record.go @@ -185,7 +185,7 @@ func (rec *simpleRecord) validate() error { return nil } - if len(rec.arrs) != len(rec.schema.Fields()) { + if len(rec.arrs) != rec.schema.NumFields() { return fmt.Errorf("arrow/array: number of columns/fields mismatch") } @@ -285,11 +285,11 @@ func NewRecordBuilder(mem memory.Allocator, schema *arrow.Schema) *RecordBuilder refCount: 1, mem: mem, schema: schema, - fields: make([]Builder, len(schema.Fields())), + fields: make([]Builder, schema.NumFields()), } - for i, f := range schema.Fields() { - b.fields[i] = NewBuilder(b.mem, f.Type) + for i := 0; i < schema.NumFields(); i++ { + b.fields[i] = NewBuilder(b.mem, schema.Field(i).Type) } return b @@ -397,8 +397,8 @@ func (b *RecordBuilder) UnmarshalJSON(data []byte) error { } } - for i, f := range b.schema.Fields() { - if !keylist[f.Name] { + for i := 0; i < b.schema.NumFields(); i++ { + if !keylist[b.schema.Field(i).Name] { b.fields[i].AppendNull() } } diff --git a/go/arrow/array/struct.go b/go/arrow/array/struct.go index 248a25bf6044d..0b9572e3d8cdc 100644 --- a/go/arrow/array/struct.go +++ b/go/arrow/array/struct.go @@ -224,7 +224,7 @@ func NewStructBuilder(mem memory.Allocator, dtype *arrow.StructType) *StructBuil b := &StructBuilder{ builder: builder{refCount: 1, mem: mem}, dtype: dtype, - fields: make([]Builder, len(dtype.Fields())), + fields: make([]Builder, dtype.NumFields()), } for i, f := range dtype.Fields() { b.fields[i] = NewBuilder(b.mem, f.Type) diff --git a/go/arrow/array/table.go b/go/arrow/array/table.go index 6456992e34973..266d9771e4483 100644 --- a/go/arrow/array/table.go +++ b/go/arrow/array/table.go @@ -137,11 +137,11 @@ func NewTable(schema *arrow.Schema, cols []arrow.Column, rows int64) *simpleTabl // - the total length of each column's array slice (ie: number of rows // in the column) aren't the same for all columns. func NewTableFromSlice(schema *arrow.Schema, data [][]arrow.Array) *simpleTable { - if len(data) != len(schema.Fields()) { + if len(data) != schema.NumFields() { panic("array/table: mismatch in number of columns and data for creating a table") } - cols := make([]arrow.Column, len(schema.Fields())) + cols := make([]arrow.Column, schema.NumFields()) for i, arrs := range data { field := schema.Field(i) chunked := arrow.NewChunked(field.Type, arrs) @@ -177,7 +177,7 @@ func NewTableFromSlice(schema *arrow.Schema, data [][]arrow.Array) *simpleTable // NewTableFromRecords panics if the records and schema are inconsistent. func NewTableFromRecords(schema *arrow.Schema, recs []arrow.Record) *simpleTable { arrs := make([]arrow.Array, len(recs)) - cols := make([]arrow.Column, len(schema.Fields())) + cols := make([]arrow.Column, schema.NumFields()) defer func(cols []arrow.Column) { for i := range cols { @@ -224,7 +224,7 @@ func (tbl *simpleTable) NumCols() int64 { return int64(len(tbl.cols) func (tbl *simpleTable) Column(i int) *arrow.Column { return &tbl.cols[i] } func (tbl *simpleTable) validate() { - if len(tbl.cols) != len(tbl.schema.Fields()) { + if len(tbl.cols) != tbl.schema.NumFields() { panic(errors.New("arrow/array: table schema mismatch")) } for i, col := range tbl.cols { diff --git a/go/arrow/array/union.go b/go/arrow/array/union.go index 869355ac7108a..2f0026d457c4e 100644 --- a/go/arrow/array/union.go +++ b/go/arrow/array/union.go @@ -896,7 +896,7 @@ func NewEmptySparseUnionBuilder(mem memory.Allocator) *SparseUnionBuilder { // children and type codes. Builders will be constructed for each child // using the fields in typ func NewSparseUnionBuilder(mem memory.Allocator, typ *arrow.SparseUnionType) *SparseUnionBuilder { - children := make([]Builder, len(typ.Fields())) + children := make([]Builder, typ.NumFields()) for i, f := range typ.Fields() { children[i] = NewBuilder(mem, f.Type) defer children[i].Release() @@ -1129,7 +1129,7 @@ func NewEmptyDenseUnionBuilder(mem memory.Allocator) *DenseUnionBuilder { // children and type codes. Builders will be constructed for each child // using the fields in typ func NewDenseUnionBuilder(mem memory.Allocator, typ *arrow.DenseUnionType) *DenseUnionBuilder { - children := make([]Builder, 0, len(typ.Fields())) + children := make([]Builder, 0, typ.NumFields()) defer func() { for _, child := range children { child.Release() diff --git a/go/arrow/array/util.go b/go/arrow/array/util.go index 54d15a8095c80..bb5cf1a674993 100644 --- a/go/arrow/array/util.go +++ b/go/arrow/array/util.go @@ -428,7 +428,7 @@ func (n *nullArrayFactory) create() *Data { } if nf, ok := dt.(arrow.NestedType); ok { - childData = make([]arrow.ArrayData, len(nf.Fields())) + childData = make([]arrow.ArrayData, nf.NumFields()) } switch dt := dt.(type) { diff --git a/go/arrow/cdata/cdata_exports.go b/go/arrow/cdata/cdata_exports.go index 187c2deb9755f..b0708b208cb7e 100644 --- a/go/arrow/cdata/cdata_exports.go +++ b/go/arrow/cdata/cdata_exports.go @@ -274,7 +274,7 @@ func (exp *schemaExporter) export(field arrow.Field) { exp.dict = new(schemaExporter) exp.dict.export(arrow.Field{Type: dt.ValueType}) case arrow.NestedType: - exp.children = make([]schemaExporter, len(dt.Fields())) + exp.children = make([]schemaExporter, dt.NumFields()) for i, f := range dt.Fields() { exp.children[i].export(f) } diff --git a/go/arrow/compute/cast.go b/go/arrow/compute/cast.go index 8b720a2bad5d8..e9fb0bb9a4368 100644 --- a/go/arrow/compute/cast.go +++ b/go/arrow/compute/cast.go @@ -266,8 +266,8 @@ func CastStruct(ctx *exec.KernelCtx, batch *exec.ExecSpan, out *exec.ExecResult) opts = ctx.State.(kernels.CastState) inType = batch.Values[0].Array.Type.(*arrow.StructType) outType = out.Type.(*arrow.StructType) - inFieldCount = len(inType.Fields()) - outFieldCount = len(outType.Fields()) + inFieldCount = inType.NumFields() + outFieldCount = outType.NumFields() ) fieldsToSelect := make([]int, outFieldCount) diff --git a/go/arrow/compute/exec/span.go b/go/arrow/compute/exec/span.go index b6d240fa4a0be..b019220d18f2c 100644 --- a/go/arrow/compute/exec/span.go +++ b/go/arrow/compute/exec/span.go @@ -633,7 +633,7 @@ func FillZeroLength(dt arrow.DataType, span *ArraySpan) { return } - span.resizeChildren(len(nt.Fields())) + span.resizeChildren(nt.NumFields()) for i, f := range nt.Fields() { FillZeroLength(f.Type, &span.Children[i]) } diff --git a/go/arrow/compute/exprs/builders.go b/go/arrow/compute/exprs/builders.go index 39606c27e714a..e9fd0af079514 100644 --- a/go/arrow/compute/exprs/builders.go +++ b/go/arrow/compute/exprs/builders.go @@ -162,7 +162,7 @@ func NewFieldRefFromDotPath(dotpath string, rootSchema *arrow.Schema) (expr.Refe idx, _ := strconv.Atoi(dotpath[:subend]) switch ct := curType.(type) { case *arrow.StructType: - if idx > len(ct.Fields()) { + if idx > ct.NumFields() { return nil, fmt.Errorf("%w: field out of bounds in dotpath", arrow.ErrIndex) } curType = ct.Field(idx).Type diff --git a/go/arrow/compute/exprs/exec.go b/go/arrow/compute/exprs/exec.go index d63752696082b..7cd280cada6eb 100644 --- a/go/arrow/compute/exprs/exec.go +++ b/go/arrow/compute/exprs/exec.go @@ -53,7 +53,7 @@ func makeExecBatch(ctx context.Context, schema *arrow.Schema, partial compute.Da partialBatch := partial.(*compute.RecordDatum).Value batchSchema := partialBatch.Schema() - out.Values = make([]compute.Datum, len(schema.Fields())) + out.Values = make([]compute.Datum, schema.NumFields()) out.Len = partialBatch.NumRows() for i, field := range schema.Fields() { @@ -99,7 +99,7 @@ func makeExecBatch(ctx context.Context, schema *arrow.Schema, partial compute.Da return makeExecBatch(ctx, schema, compute.NewDatumWithoutOwning(batch)) case *compute.ScalarDatum: out.Len = 1 - out.Values = make([]compute.Datum, len(schema.Fields())) + out.Values = make([]compute.Datum, schema.NumFields()) s := part.Value.(*scalar.Struct) dt := s.Type.(*arrow.StructType) diff --git a/go/arrow/compute/exprs/types.go b/go/arrow/compute/exprs/types.go index 9f807e1e1ecbc..a841a531ecc58 100644 --- a/go/arrow/compute/exprs/types.go +++ b/go/arrow/compute/exprs/types.go @@ -594,7 +594,7 @@ func ToSubstraitType(dt arrow.DataType, nullable bool, ext ExtensionIDSet) (type Precision: dt.GetPrecision(), Scale: dt.GetScale()}, nil case arrow.STRUCT: dt := dt.(*arrow.StructType) - fields := make([]types.Type, len(dt.Fields())) + fields := make([]types.Type, dt.NumFields()) var err error for i, f := range dt.Fields() { fields[i], err = ToSubstraitType(f.Type, f.Nullable, ext) diff --git a/go/arrow/compute/fieldref_test.go b/go/arrow/compute/fieldref_test.go index c38ad8ac1fe30..3c9fb258352a6 100644 --- a/go/arrow/compute/fieldref_test.go +++ b/go/arrow/compute/fieldref_test.go @@ -44,7 +44,7 @@ func TestFieldPathBasics(t *testing.T) { assert.Nil(t, f) assert.ErrorIs(t, err, compute.ErrEmpty) - f, err = compute.FieldPath{len(s.Fields()) * 2}.Get(s) + f, err = compute.FieldPath{s.NumFields() * 2}.Get(s) assert.Nil(t, f) assert.ErrorIs(t, err, compute.ErrIndexRange) } @@ -63,7 +63,7 @@ func TestFieldRefBasics(t *testing.T) { } // out of range index results in failure to match - assert.Empty(t, compute.FieldRefIndex(len(s.Fields())*2).FindAll(s.Fields())) + assert.Empty(t, compute.FieldRefIndex(s.NumFields()*2).FindAll(s.Fields())) // lookup by name returns the indices of both matching fields assert.Equal(t, []compute.FieldPath{{0}, {2}}, compute.FieldRefName("alpha").FindAll(s.Fields())) diff --git a/go/arrow/datatype_encoded.go b/go/arrow/datatype_encoded.go index c1750a8894f43..749f03a582646 100644 --- a/go/arrow/datatype_encoded.go +++ b/go/arrow/datatype_encoded.go @@ -58,6 +58,8 @@ func (t *RunEndEncodedType) Fields() []Field { } } +func (t *RunEndEncodedType) NumFields() int { return 2 } + func (*RunEndEncodedType) ValidRunEndsType(dt DataType) bool { switch dt.ID() { case INT16, INT32, INT64: diff --git a/go/arrow/datatype_extension.go b/go/arrow/datatype_extension.go index 271c8b0dbc789..50ae107583802 100644 --- a/go/arrow/datatype_extension.go +++ b/go/arrow/datatype_extension.go @@ -161,6 +161,13 @@ func (e *ExtensionBase) Fields() []Field { return nil } +func (e *ExtensionBase) NumFields() int { + if nested, ok := e.Storage.(NestedType); ok { + return nested.NumFields() + } + return 0 +} + func (e *ExtensionBase) Layout() DataTypeLayout { return e.Storage.Layout() } // this no-op exists to ensure that this type must be embedded in any user-defined extension type. diff --git a/go/arrow/datatype_nested.go b/go/arrow/datatype_nested.go index 4ae4880334620..fb9b8a3e30c64 100644 --- a/go/arrow/datatype_nested.go +++ b/go/arrow/datatype_nested.go @@ -32,6 +32,8 @@ type ( // Fields method provides a copy of NestedType fields // (so it can be safely mutated and will not result in updating the NestedType). Fields() []Field + // NumFields provides the number of fields without allocating. + NumFields() int } ListLikeType interface { @@ -109,6 +111,8 @@ func (t *ListType) ElemField() Field { func (t *ListType) Fields() []Field { return []Field{t.ElemField()} } +func (t *ListType) NumFields() int { return 1 } + func (*ListType) Layout() DataTypeLayout { return DataTypeLayout{Buffers: []BufferSpec{SpecBitmap(), SpecFixedWidth(Int32SizeBytes)}} } @@ -242,6 +246,8 @@ func (t *FixedSizeListType) Fingerprint() string { func (t *FixedSizeListType) Fields() []Field { return []Field{t.ElemField()} } +func (t *FixedSizeListType) NumFields() int { return 1 } + func (*FixedSizeListType) Layout() DataTypeLayout { return DataTypeLayout{Buffers: []BufferSpec{SpecBitmap()}} } @@ -308,6 +314,8 @@ func (t *ListViewType) ElemField() Field { func (t *ListViewType) Fields() []Field { return []Field{t.ElemField()} } +func (t *ListViewType) NumFields() int { return 1 } + func (*ListViewType) Layout() DataTypeLayout { return DataTypeLayout{Buffers: []BufferSpec{SpecBitmap(), SpecFixedWidth(Int32SizeBytes), SpecFixedWidth(Int32SizeBytes)}} } @@ -376,6 +384,8 @@ func (t *LargeListViewType) ElemField() Field { func (t *LargeListViewType) Fields() []Field { return []Field{t.ElemField()} } +func (t *LargeListViewType) NumFields() int { return 1 } + func (*LargeListViewType) Layout() DataTypeLayout { return DataTypeLayout{Buffers: []BufferSpec{SpecBitmap(), SpecFixedWidth(Int64SizeBytes), SpecFixedWidth(Int64SizeBytes)}} } @@ -447,6 +457,8 @@ func (t *StructType) Fields() []Field { return fields } +func (t *StructType) NumFields() int { return len(t.fields) } + func (t *StructType) Field(i int) Field { return t.fields[i] } // FieldByName gets the field with the given name. @@ -598,6 +610,8 @@ func (t *MapType) Fingerprint() string { func (t *MapType) Fields() []Field { return []Field{t.ElemField()} } +func (t *MapType) NumFields() int { return 1 } + func (t *MapType) Layout() DataTypeLayout { return t.value.Layout() } @@ -690,6 +704,8 @@ func (t *unionType) Fields() []Field { return fields } +func (t *unionType) NumFields() int { return len(t.children) } + func (t *unionType) TypeCodes() []UnionTypeCode { return t.typeCodes } func (t *unionType) ChildIDs() []int { return t.childIDs[:] } diff --git a/go/arrow/datatype_nested_test.go b/go/arrow/datatype_nested_test.go index 1e09f49da147c..a1daa8e58df31 100644 --- a/go/arrow/datatype_nested_test.go +++ b/go/arrow/datatype_nested_test.go @@ -205,7 +205,7 @@ func TestStructOf(t *testing.T) { t.Fatalf("invalid name. got=%q, want=%q", got, want) } - if got, want := len(got.Fields()), len(tc.fields); got != want { + if got, want := got.NumFields(), len(tc.fields); got != want { t.Fatalf("invalid number of fields. got=%d, want=%d", got, want) } diff --git a/go/arrow/flight/flightsql/driver/driver.go b/go/arrow/flight/flightsql/driver/driver.go index b4cb0d2dbda2e..e29e10dc19e13 100644 --- a/go/arrow/flight/flightsql/driver/driver.go +++ b/go/arrow/flight/flightsql/driver/driver.go @@ -159,7 +159,7 @@ func (s *Stmt) NumInput() int { // If NumInput returns >= 0, the sql package will sanity check argument // counts from callers and return errors to the caller before the // statement's Exec or Query methods are called. - return len(schema.Fields()) + return schema.NumFields() } // Exec executes a query that doesn't return rows, such diff --git a/go/arrow/flight/flightsql/example/sql_batch_reader.go b/go/arrow/flight/flightsql/example/sql_batch_reader.go index 7101b8f830a6f..acf6a2378cc04 100644 --- a/go/arrow/flight/flightsql/example/sql_batch_reader.go +++ b/go/arrow/flight/flightsql/example/sql_batch_reader.go @@ -111,7 +111,7 @@ type SqlBatchReader struct { } func NewSqlBatchReaderWithSchema(mem memory.Allocator, schema *arrow.Schema, rows *sql.Rows) (*SqlBatchReader, error) { - rowdest := make([]interface{}, len(schema.Fields())) + rowdest := make([]interface{}, schema.NumFields()) for i, f := range schema.Fields() { switch f.Type.ID() { case arrow.DENSE_UNION, arrow.SPARSE_UNION: diff --git a/go/arrow/internal/arrjson/arrjson.go b/go/arrow/internal/arrjson/arrjson.go index ad87b73fc4ddb..78e8cbc24bccf 100644 --- a/go/arrow/internal/arrjson/arrjson.go +++ b/go/arrow/internal/arrjson/arrjson.go @@ -1160,7 +1160,7 @@ func arrayFromJSON(mem memory.Allocator, dt arrow.DataType, arr Array) arrow.Arr nulls := arr.Count - bitutil.CountSetBits(bitmap.Bytes(), 0, arr.Count) - fields := make([]arrow.ArrayData, len(dt.Fields())) + fields := make([]arrow.ArrayData, dt.NumFields()) for i := range fields { child := arrayFromJSON(mem, dt.Field(i).Type, arr.Children[i]) defer child.Release() @@ -1307,7 +1307,7 @@ func arrayFromJSON(mem memory.Allocator, dt arrow.DataType, arr Array) arrow.Arr return array.NewData(dt, arr.Count, []*memory.Buffer{nil}, []arrow.ArrayData{runEnds, values}, 0, 0) case arrow.UnionType: - fields := make([]arrow.ArrayData, len(dt.Fields())) + fields := make([]arrow.ArrayData, dt.NumFields()) for i, f := range dt.Fields() { child := arrayFromJSON(mem, f.Type, arr.Children[i]) defer child.Release() @@ -1581,7 +1581,7 @@ func arrayToJSON(field arrow.Field, arr arrow.Array) Array { Name: field.Name, Count: arr.Len(), Valids: validsToJSON(arr), - Children: make([]Array, len(dt.Fields())), + Children: make([]Array, dt.NumFields()), } for i := range o.Children { o.Children[i] = arrayToJSON(dt.Field(i), arr.Field(i)) @@ -1702,7 +1702,7 @@ func arrayToJSON(field arrow.Field, arr arrow.Array) Array { Count: arr.Len(), Valids: validsToJSON(arr), TypeID: arr.RawTypeCodes(), - Children: make([]Array, len(dt.Fields())), + Children: make([]Array, dt.NumFields()), } if dt.Mode() == arrow.DenseMode { o.Offset = arr.(*array.DenseUnion).RawValueOffsets() diff --git a/go/arrow/internal/dictutils/dict.go b/go/arrow/internal/dictutils/dict.go index e09a2f4a0ecd1..f9b2f3c67cb3d 100644 --- a/go/arrow/internal/dictutils/dict.go +++ b/go/arrow/internal/dictutils/dict.go @@ -104,7 +104,7 @@ func (d *Mapper) InsertPath(pos FieldPos) { d.hasher.Reset() } -func (d *Mapper) ImportField(pos FieldPos, field *arrow.Field) { +func (d *Mapper) ImportField(pos FieldPos, field arrow.Field) { dt := field.Type if dt.ID() == arrow.EXTENSION { dt = dt.(arrow.ExtensionType).StorageType() @@ -126,13 +126,18 @@ func (d *Mapper) ImportField(pos FieldPos, field *arrow.Field) { func (d *Mapper) ImportFields(pos FieldPos, fields []arrow.Field) { for i := range fields { - d.ImportField(pos.Child(int32(i)), &fields[i]) + d.ImportField(pos.Child(int32(i)), fields[i]) } } func (d *Mapper) ImportSchema(schema *arrow.Schema) { d.pathToID = make(map[uint64]int64) - d.ImportFields(NewFieldPos(), schema.Fields()) + // This code path intentionally avoids calling ImportFields with + // schema.Fields to avoid allocations. + pos := NewFieldPos() + for i := 0; i < schema.NumFields(); i++ { + d.ImportField(pos.Child(int32(i)), schema.Field(i)) + } } func hasUnresolvedNestedDict(data arrow.ArrayData) bool { diff --git a/go/arrow/ipc/file_reader.go b/go/arrow/ipc/file_reader.go index fee88232ab2dd..4ea654f3e5f15 100644 --- a/go/arrow/ipc/file_reader.go +++ b/go/arrow/ipc/file_reader.go @@ -351,9 +351,9 @@ func newRecord(schema *arrow.Schema, memo *dictutils.Memo, meta *memory.Buffer, } pos := dictutils.NewFieldPos() - cols := make([]arrow.Array, len(schema.Fields())) - for i, field := range schema.Fields() { - data := ctx.loadArray(field.Type) + cols := make([]arrow.Array, schema.NumFields()) + for i := 0; i < schema.NumFields(); i++ { + data := ctx.loadArray(schema.Field(i).Type) defer data.Release() if err := dictutils.ResolveFieldDict(memo, data, pos.Child(int32(i)), mem); err != nil { @@ -637,7 +637,7 @@ func (ctx *arrayLoaderContext) loadStruct(dt *arrow.StructType) arrow.ArrayData field, buffers := ctx.loadCommon(dt.ID(), 1) defer releaseBuffers(buffers) - subs := make([]arrow.ArrayData, len(dt.Fields())) + subs := make([]arrow.ArrayData, dt.NumFields()) for i, f := range dt.Fields() { subs[i] = ctx.loadChild(f.Type) } @@ -679,7 +679,7 @@ func (ctx *arrayLoaderContext) loadUnion(dt arrow.UnionType) arrow.ArrayData { } defer releaseBuffers(buffers) - subs := make([]arrow.ArrayData, len(dt.Fields())) + subs := make([]arrow.ArrayData, dt.NumFields()) for i, f := range dt.Fields() { subs[i] = ctx.loadChild(f.Type) } diff --git a/go/arrow/ipc/metadata.go b/go/arrow/ipc/metadata.go index 9bab47d6fa0cd..b8949f7158f44 100644 --- a/go/arrow/ipc/metadata.go +++ b/go/arrow/ipc/metadata.go @@ -363,7 +363,7 @@ func (fv *fieldVisitor) visit(field arrow.Field) { case *arrow.StructType: fv.dtype = flatbuf.TypeStruct_ - offsets := make([]flatbuffers.UOffsetT, len(dt.Fields())) + offsets := make([]flatbuffers.UOffsetT, dt.NumFields()) for i, field := range dt.Fields() { offsets[i] = fieldToFB(fv.b, fv.pos.Child(int32(i)), field, fv.memo) } @@ -462,7 +462,7 @@ func (fv *fieldVisitor) visit(field arrow.Field) { case arrow.UnionType: fv.dtype = flatbuf.TypeUnion - offsets := make([]flatbuffers.UOffsetT, len(dt.Fields())) + offsets := make([]flatbuffers.UOffsetT, dt.NumFields()) for i, field := range dt.Fields() { offsets[i] = fieldToFB(fv.b, fv.pos.Child(int32(i)), field, fv.memo) } @@ -1084,10 +1084,10 @@ func schemaFromFB(schema *flatbuf.Schema, memo *dictutils.Memo) (*arrow.Schema, } func schemaToFB(b *flatbuffers.Builder, schema *arrow.Schema, memo *dictutils.Mapper) flatbuffers.UOffsetT { - fields := make([]flatbuffers.UOffsetT, len(schema.Fields())) + fields := make([]flatbuffers.UOffsetT, schema.NumFields()) pos := dictutils.NewFieldPos() - for i, field := range schema.Fields() { - fields[i] = fieldToFB(b, pos.Child(int32(i)), field, memo) + for i := 0; i < schema.NumFields(); i++ { + fields[i] = fieldToFB(b, pos.Child(int32(i)), schema.Field(i), memo) } flatbuf.SchemaStartFieldsVector(b, len(fields)) diff --git a/go/arrow/scalar/nested.go b/go/arrow/scalar/nested.go index 87e84210f58a1..57041cf7d5c6e 100644 --- a/go/arrow/scalar/nested.go +++ b/go/arrow/scalar/nested.go @@ -132,7 +132,7 @@ func NewLargeListScalarData(val arrow.ArrayData) *LargeList { } func makeMapType(typ *arrow.StructType) *arrow.MapType { - debug.Assert(len(typ.Fields()) == 2, "must pass struct with only 2 fields for MapScalar") + debug.Assert(typ.NumFields() == 2, "must pass struct with only 2 fields for MapScalar") return arrow.MapOf(typ.Field(0).Type, typ.Field(1).Type) } @@ -265,7 +265,7 @@ func (s *Struct) Validate() (err error) { } st := s.Type.(*arrow.StructType) - num := len(st.Fields()) + num := st.NumFields() if len(s.Value) != num { return fmt.Errorf("non-null %s scalar should have %d child values, got %d", s.Type, num, len(s.Value)) } @@ -303,7 +303,7 @@ func (s *Struct) ValidateFull() (err error) { } st := s.Type.(*arrow.StructType) - num := len(st.Fields()) + num := st.NumFields() if len(s.Value) != num { return fmt.Errorf("non-null %s scalar should have %d child values, got %d", s.Type, num, len(s.Value)) } @@ -571,8 +571,8 @@ func (s *SparseUnion) Release() { func (s *SparseUnion) Validate() (err error) { dt := s.Type.(*arrow.SparseUnionType) - if len(dt.Fields()) != len(s.Value) { - return fmt.Errorf("sparse union scalar value had %d fields but type has %d fields", len(dt.Fields()), len(s.Value)) + if dt.NumFields() != len(s.Value) { + return fmt.Errorf("sparse union scalar value had %d fields but type has %d fields", dt.NumFields(), len(s.Value)) } if s.TypeCode < 0 || int(s.TypeCode) >= len(dt.ChildIDs()) || dt.ChildIDs()[s.TypeCode] == arrow.InvalidUnionChildID { @@ -593,8 +593,8 @@ func (s *SparseUnion) Validate() (err error) { func (s *SparseUnion) ValidateFull() (err error) { dt := s.Type.(*arrow.SparseUnionType) - if len(dt.Fields()) != len(s.Value) { - return fmt.Errorf("sparse union scalar value had %d fields but type has %d fields", len(dt.Fields()), len(s.Value)) + if dt.NumFields() != len(s.Value) { + return fmt.Errorf("sparse union scalar value had %d fields but type has %d fields", dt.NumFields(), len(s.Value)) } if s.TypeCode < 0 || int(s.TypeCode) >= len(dt.ChildIDs()) || dt.ChildIDs()[s.TypeCode] == arrow.InvalidUnionChildID { @@ -643,7 +643,7 @@ func NewSparseUnionScalar(val []Scalar, code arrow.UnionTypeCode, dt *arrow.Spar func NewSparseUnionScalarFromValue(val Scalar, idx int, dt *arrow.SparseUnionType) *SparseUnion { code := dt.TypeCodes()[idx] - values := make([]Scalar, len(dt.Fields())) + values := make([]Scalar, dt.NumFields()) for i, f := range dt.Fields() { if i == idx { values[i] = val diff --git a/go/arrow/scalar/scalar.go b/go/arrow/scalar/scalar.go index 395771fa89c94..8582cee9e27ea 100644 --- a/go/arrow/scalar/scalar.go +++ b/go/arrow/scalar/scalar.go @@ -512,7 +512,7 @@ func init() { arrow.LIST: func(dt arrow.DataType) Scalar { return &List{scalar: scalar{dt, false}} }, arrow.STRUCT: func(dt arrow.DataType) Scalar { typ := dt.(*arrow.StructType) - values := make([]Scalar, len(typ.Fields())) + values := make([]Scalar, typ.NumFields()) for i, f := range typ.Fields() { values[i] = MakeNullScalar(f.Type) } @@ -520,10 +520,10 @@ func init() { }, arrow.SPARSE_UNION: func(dt arrow.DataType) Scalar { typ := dt.(*arrow.SparseUnionType) - if len(typ.Fields()) == 0 { + if typ.NumFields() == 0 { panic("cannot make scalar of empty union type") } - values := make([]Scalar, len(typ.Fields())) + values := make([]Scalar, typ.NumFields()) for i, f := range typ.Fields() { values[i] = MakeNullScalar(f.Type) } @@ -531,7 +531,7 @@ func init() { }, arrow.DENSE_UNION: func(dt arrow.DataType) Scalar { typ := dt.(*arrow.DenseUnionType) - if len(typ.Fields()) == 0 { + if typ.NumFields() == 0 { panic("cannot make scalar of empty union type") } return NewDenseUnionScalar(MakeNullScalar(typ.Fields()[0].Type), typ.TypeCodes()[0], typ) diff --git a/go/arrow/scalar/scalar_test.go b/go/arrow/scalar/scalar_test.go index 405571a14880d..09d2568dc434c 100644 --- a/go/arrow/scalar/scalar_test.go +++ b/go/arrow/scalar/scalar_test.go @@ -1194,7 +1194,7 @@ func makeDenseUnionScalar(ty *arrow.DenseUnionType, val scalar.Scalar, idx int) func makeSpecificNullScalar(dt arrow.UnionType, idx int) scalar.Scalar { switch dt.Mode() { case arrow.SparseMode: - values := make([]scalar.Scalar, len(dt.Fields())) + values := make([]scalar.Scalar, dt.NumFields()) for i, f := range dt.Fields() { values[i] = scalar.MakeNullScalar(f.Type) } diff --git a/go/arrow/schema.go b/go/arrow/schema.go index a7fa434131925..6b1e9af7ef73c 100644 --- a/go/arrow/schema.go +++ b/go/arrow/schema.go @@ -259,8 +259,8 @@ func (s *Schema) AddField(i int, field Field) (*Schema, error) { func (s *Schema) String() string { o := new(strings.Builder) - fmt.Fprintf(o, "schema:\n fields: %d\n", len(s.Fields())) - for i, f := range s.Fields() { + fmt.Fprintf(o, "schema:\n fields: %d\n", s.NumFields()) + for i, f := range s.fields { if i > 0 { o.WriteString("\n") } @@ -282,7 +282,7 @@ func (s *Schema) Fingerprint() string { var b strings.Builder b.WriteString("S{") - for _, f := range s.Fields() { + for _, f := range s.fields { fieldFingerprint := f.Fingerprint() if fieldFingerprint == "" { return "" diff --git a/go/arrow/schema_test.go b/go/arrow/schema_test.go index 5e79723f90352..5b8dcbd246710 100644 --- a/go/arrow/schema_test.go +++ b/go/arrow/schema_test.go @@ -255,7 +255,7 @@ func TestSchema(t *testing.T) { s = s.WithEndianness(endian.NonNativeEndian) } - if got, want := len(s.Fields()), len(tc.fields); got != want { + if got, want := s.NumFields(), len(tc.fields); got != want { t.Fatalf("invalid number of fields. got=%d, want=%d", got, want) } @@ -339,7 +339,7 @@ func TestSchemaAddField(t *testing.T) { if err != nil { t.Fatalf("unexpected error: %v", err) } - if got, want := len(s.Fields()), 3; got != want { + if got, want := s.NumFields(), 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} @@ -476,5 +476,5 @@ func TestSchemaNumFields(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 3, s.NumFields()) - assert.Equal(t, s.NumFields(), len(s.Fields())) + assert.Equal(t, s.NumFields(), s.NumFields()) } diff --git a/go/parquet/pqarrow/file_reader.go b/go/parquet/pqarrow/file_reader.go index d91010c62c19d..d68ae6e2830d9 100755 --- a/go/parquet/pqarrow/file_reader.go +++ b/go/parquet/pqarrow/file_reader.go @@ -363,7 +363,7 @@ func (fr *FileReader) ReadRowGroups(ctx context.Context, indices, rowGroups []in close(ch) // output slice of columns - columns := make([]arrow.Column, len(sc.Fields())) + columns := make([]arrow.Column, sc.NumFields()) defer releaseColumns(columns) for data := range results { if data.err != nil { diff --git a/go/parquet/pqarrow/schema.go b/go/parquet/pqarrow/schema.go index 9ba8554898986..35e1259c8a886 100644 --- a/go/parquet/pqarrow/schema.go +++ b/go/parquet/pqarrow/schema.go @@ -233,11 +233,11 @@ func repFromNullable(isnullable bool) parquet.Repetition { } func structToNode(typ *arrow.StructType, name string, nullable bool, props *parquet.WriterProperties, arrprops ArrowWriterProperties) (schema.Node, error) { - if len(typ.Fields()) == 0 { + if typ.NumFields() == 0 { return nil, fmt.Errorf("cannot write struct type '%s' with no children field to parquet. Consider adding a dummy child", name) } - children := make(schema.FieldList, 0, len(typ.Fields())) + children := make(schema.FieldList, 0, typ.NumFields()) for _, f := range typ.Fields() { n, err := fieldToNode(f.Name, f, props, arrprops) if err != nil { @@ -436,7 +436,7 @@ func ToParquet(sc *arrow.Schema, props *parquet.WriterProperties, arrprops Arrow props = parquet.NewWriterProperties() } - nodes := make(schema.FieldList, 0, len(sc.Fields())) + nodes := make(schema.FieldList, 0, sc.NumFields()) for _, f := range sc.Fields() { n, err := fieldToNode(f.Name, f, props, arrprops) if err != nil { @@ -996,7 +996,7 @@ func applyOriginalStorageMetadata(origin arrow.Field, inferred *SchemaField) (mo err = xerrors.New("unimplemented type") case arrow.STRUCT: typ := origin.Type.(*arrow.StructType) - if nchildren != len(typ.Fields()) { + if nchildren != typ.NumFields() { return }