From d9e95c5776e0bfb293c10e9441db2b95983f10b2 Mon Sep 17 00:00:00 2001 From: Yevgeny Pats <16490766+yevgenypats@users.noreply.github.com> Date: Fri, 7 Apr 2023 16:50:44 +0300 Subject: [PATCH 1/2] GH-34966: Add InsertField and FieldIndex to Schema object --- go/arrow/schema.go | 20 ++++++++++++++++++++ go/arrow/schema_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/go/arrow/schema.go b/go/arrow/schema.go index 1936cea989f..a3c9cc13508 100644 --- a/go/arrow/schema.go +++ b/go/arrow/schema.go @@ -197,6 +197,26 @@ func (sc *Schema) FieldIndices(n string) []int { return sc.index[n] } +// FieldIndex return index of the unique field with the given name. +// Returns -1 if the name isn’t found or there are several fields with the given name. +func (sc *Schema) FieldIndex(name string) int { + indices := sc.index[name] + if len(indices) != 1 { + return -1 + } + return indices[0] +} + +func (sc *Schema) InsertField(i int, f Field) *Schema { + if f.Type == nil { + panic("arrow: field with nil DataType") + } + fields := make([]Field, len(sc.fields)+1) + copy(fields[:i], sc.fields[:i]) + copy(fields[i+1:], sc.fields[i:]) + fields[i] = f + return NewSchemaWithEndian(fields, &sc.meta, sc.endianness) +} func (sc *Schema) HasField(n string) bool { return len(sc.FieldIndices(n)) > 0 } func (sc *Schema) HasMetadata() bool { return len(sc.meta.keys) > 0 } diff --git a/go/arrow/schema_test.go b/go/arrow/schema_test.go index af9576003e1..ecf5b986664 100644 --- a/go/arrow/schema_test.go +++ b/go/arrow/schema_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/apache/arrow/go/v12/arrow/endian" + "github.com/stretchr/testify/assert" ) func TestMetadata(t *testing.T) { @@ -292,6 +293,15 @@ func TestSchema(t *testing.T) { } } + if s.FieldIndex("dup") != -1 { + t.Fatalf("invalid FieldIndex(dup): got=%d, want=-1", s.FieldIndex("dup")) + } + if s.HasField("f1") { + if s.FieldIndex("f1") != 0 { + t.Fatalf("invalid FieldIndex(f1): got=%d, want=0", s.FieldIndex("f1")) + } + } + if got, want := s.String(), tc.serialize; got != want { t.Fatalf("invalid stringer: got=%q, want=%q", got, want) } @@ -413,3 +423,25 @@ func TestSchemaEqual(t *testing.T) { }) } } + +func TestSchemaInsertField(t *testing.T) { + fields := []Field{ + {Name: "f1", Type: PrimitiveTypes.Int32}, + {Name: "f2", Type: PrimitiveTypes.Int64}, + } + md := func() *Metadata { + md := MetadataFrom(map[string]string{"k1": "v1", "k2": "v2"}) + return &md + }() + sc := NewSchema(fields, md) + sc1 := sc.InsertField(0, Field{Name: "f0", Type: PrimitiveTypes.Int32}) + assert.False(t, sc.Equal(sc1)) + + fields1 := []Field{ + {Name: "f0", Type: PrimitiveTypes.Int32}, + {Name: "f1", Type: PrimitiveTypes.Int32}, + {Name: "f2", Type: PrimitiveTypes.Int64}, + } + sc2 := NewSchema(fields1, md) + assert.True(t, sc1.Equal(sc2)) +} From 2a9db68ee2e45f08edfeb92b29390148d57ee136 Mon Sep 17 00:00:00 2001 From: Yevgeny Pats <16490766+yevgenypats@users.noreply.github.com> Date: Fri, 7 Apr 2023 17:47:39 +0300 Subject: [PATCH 2/2] remove duplicate --- go/arrow/schema.go | 10 ---------- go/arrow/schema_test.go | 23 ----------------------- 2 files changed, 33 deletions(-) diff --git a/go/arrow/schema.go b/go/arrow/schema.go index a3c9cc13508..f95c3336522 100644 --- a/go/arrow/schema.go +++ b/go/arrow/schema.go @@ -207,16 +207,6 @@ func (sc *Schema) FieldIndex(name string) int { return indices[0] } -func (sc *Schema) InsertField(i int, f Field) *Schema { - if f.Type == nil { - panic("arrow: field with nil DataType") - } - fields := make([]Field, len(sc.fields)+1) - copy(fields[:i], sc.fields[:i]) - copy(fields[i+1:], sc.fields[i:]) - fields[i] = f - return NewSchemaWithEndian(fields, &sc.meta, sc.endianness) -} func (sc *Schema) HasField(n string) bool { return len(sc.FieldIndices(n)) > 0 } func (sc *Schema) HasMetadata() bool { return len(sc.meta.keys) > 0 } diff --git a/go/arrow/schema_test.go b/go/arrow/schema_test.go index ecf5b986664..63929570ccd 100644 --- a/go/arrow/schema_test.go +++ b/go/arrow/schema_test.go @@ -22,7 +22,6 @@ import ( "testing" "github.com/apache/arrow/go/v12/arrow/endian" - "github.com/stretchr/testify/assert" ) func TestMetadata(t *testing.T) { @@ -423,25 +422,3 @@ func TestSchemaEqual(t *testing.T) { }) } } - -func TestSchemaInsertField(t *testing.T) { - fields := []Field{ - {Name: "f1", Type: PrimitiveTypes.Int32}, - {Name: "f2", Type: PrimitiveTypes.Int64}, - } - md := func() *Metadata { - md := MetadataFrom(map[string]string{"k1": "v1", "k2": "v2"}) - return &md - }() - sc := NewSchema(fields, md) - sc1 := sc.InsertField(0, Field{Name: "f0", Type: PrimitiveTypes.Int32}) - assert.False(t, sc.Equal(sc1)) - - fields1 := []Field{ - {Name: "f0", Type: PrimitiveTypes.Int32}, - {Name: "f1", Type: PrimitiveTypes.Int32}, - {Name: "f2", Type: PrimitiveTypes.Int64}, - } - sc2 := NewSchema(fields1, md) - assert.True(t, sc1.Equal(sc2)) -}