diff --git a/arrow/schema.go b/arrow/schema.go index 806bd0d0..cb99adb5 100644 --- a/arrow/schema.go +++ b/arrow/schema.go @@ -220,7 +220,7 @@ func (sc *Schema) FieldsByName(n string) ([]Field, bool) { return nil, ok } if len(indices) == 1 { - return sc.fields[indices[0] : indices[0]+1], ok + return []Field{sc.fields[indices[0]]}, ok } else if len(indices) > 1 { fields := make([]Field, 0, len(indices)) for _, v := range indices { diff --git a/arrow/schema_test.go b/arrow/schema_test.go index 3cc7b374..0069e69d 100644 --- a/arrow/schema_test.go +++ b/arrow/schema_test.go @@ -336,6 +336,19 @@ func TestSchema(t *testing.T) { } } +func TestSchemaFieldsByNameReturnsCopy(t *testing.T) { + schema := NewSchema([]Field{{Name: "id", Type: PrimitiveTypes.Int64}}, nil) + fields, ok := schema.FieldsByName("id") + if !ok { + t.Fatal("field not found") + } + fields[0].Name = "changed" + + if got, want := schema.Field(0).Name, "id"; got != want { + t.Fatalf("schema field mutated through returned slice: got %q, want %q", got, want) + } +} + func TestSchemaAddField(t *testing.T) { s := NewSchema([]Field{ {Name: "f1", Type: PrimitiveTypes.Int32},