Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions arrow/datatype_nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package arrow
import (
"errors"
"fmt"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -501,9 +502,9 @@ func (t *StructType) FieldsByName(n string) ([]Field, bool) {
return fields, ok
}

// FieldIndices returns indices of all fields with the given name, or nil.
// FieldIndices returns a copy of the indices of all fields with the given name, or nil.
func (t *StructType) FieldIndices(name string) []int {
return t.index[name]
return slices.Clone(t.index[name])
}

func (t *StructType) Fingerprint() string {
Expand Down
11 changes: 11 additions & 0 deletions arrow/datatype_nested_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,17 @@ func TestStructField(t *testing.T) {
assert.Equal(t, ty.FieldIndices("f3"), []int(nil))
}

func TestStructTypeFieldIndicesReturnsCopy(t *testing.T) {
typeWithDuplicates := StructOf(
Field{Name: "id", Type: PrimitiveTypes.Int32},
Field{Name: "id", Type: PrimitiveTypes.Int64},
)
indices := typeWithDuplicates.FieldIndices("id")
indices[0] = 99

assert.Equal(t, []int{0, 1}, typeWithDuplicates.FieldIndices("id"))
}

func TestFieldEqual(t *testing.T) {
for _, tc := range []struct {
a, b Field
Expand Down
6 changes: 3 additions & 3 deletions arrow/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@ func (sc *Schema) FieldsByName(n string) ([]Field, bool) {
return nil, false
}

// FieldIndices returns the indices of the named field or nil.
// FieldIndices returns a copy of the indices of the named field or nil.
func (sc *Schema) FieldIndices(n string) []int {
return sc.index[n]
return slices.Clone(sc.index[n])
}

func (sc *Schema) HasField(n string) bool { return len(sc.FieldIndices(n)) > 0 }
func (sc *Schema) HasField(n string) bool { return len(sc.index[n]) > 0 }
func (sc *Schema) HasMetadata() bool { return len(sc.meta.keys) > 0 }

// Equal returns whether two schema are equal.
Expand Down
13 changes: 13 additions & 0 deletions arrow/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,19 @@ func TestSchemaFieldsByNameReturnsCopy(t *testing.T) {
}
}

func TestSchemaFieldIndicesReturnsCopy(t *testing.T) {
schema := NewSchema([]Field{
{Name: "id", Type: PrimitiveTypes.Int32},
{Name: "id", Type: PrimitiveTypes.Int64},
}, nil)
indices := schema.FieldIndices("id")
indices[0] = 99

if got, want := schema.FieldIndices("id"), []int{0, 1}; !reflect.DeepEqual(got, want) {
t.Fatalf("schema indices mutated through returned slice: got %v, want %v", got, want)
}
}

func TestSchemaAddField(t *testing.T) {
s := NewSchema([]Field{
{Name: "f1", Type: PrimitiveTypes.Int32},
Expand Down