Skip to content

Commit fb0c72c

Browse files
authored
MINOR: [Go] Add arrow.ListLikeType interface (#35885)
### Rationale for this change Add `arrow.ListLikeType` interface corresponding to `array.ListLike` implementations. ### What changes are included in this PR? 1. Added `arrow.ListLikeType` 2. Some receivers changed from value to pointer (per Go recommendation to have either one of them, but not both) ### Are these changes tested? No (per it mainly being the interface addition) ### Are there any user-facing changes? 1. Added `arrow.ListLikeType` 2. Some receivers changed from value to pointer (per Go recommendation to have either one of them, but not both) Authored-by: candiduslynx <candiduslynx@gmail.com> Signed-off-by: Matt Topol <zotthewizard@gmail.com>
1 parent cd42895 commit fb0c72c

2 files changed

Lines changed: 56 additions & 26 deletions

File tree

go/arrow/array/list.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,4 +611,14 @@ var (
611611
_ arrow.Array = (*LargeList)(nil)
612612
_ Builder = (*ListBuilder)(nil)
613613
_ Builder = (*LargeListBuilder)(nil)
614+
615+
_ ListLike = (*List)(nil)
616+
_ ListLike = (*LargeList)(nil)
617+
_ ListLike = (*FixedSizeList)(nil)
618+
_ ListLike = (*Map)(nil)
619+
620+
_ ListLikeBuilder = (*ListBuilder)(nil)
621+
_ ListLikeBuilder = (*LargeListBuilder)(nil)
622+
_ ListLikeBuilder = (*FixedSizeListBuilder)(nil)
623+
_ ListLikeBuilder = (*MapBuilder)(nil)
614624
)

go/arrow/datatype_nested.go

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@ import (
2525
"github.com/apache/arrow/go/v13/arrow/internal/debug"
2626
)
2727

28-
type NestedType interface {
29-
DataType
28+
type (
29+
NestedType interface {
30+
DataType
3031

31-
// Fields method provides a copy of NestedType fields
32-
// (so it can be safely mutated and will not result in updating the NestedType).
33-
Fields() []Field
34-
}
32+
// Fields method provides a copy of NestedType fields
33+
// (so it can be safely mutated and will not result in updating the NestedType).
34+
Fields() []Field
35+
}
36+
37+
ListLikeType interface {
38+
DataType
39+
Elem() DataType
40+
}
41+
)
3542

3643
// ListType describes a nested type in which each array slot contains
3744
// a variable-size sequence of values, all having the same relative type.
@@ -97,11 +104,11 @@ func (t *ListType) ElemField() Field {
97104

98105
func (t *ListType) Fields() []Field { return []Field{t.ElemField()} }
99106

100-
func (ListType) Layout() DataTypeLayout {
107+
func (*ListType) Layout() DataTypeLayout {
101108
return DataTypeLayout{Buffers: []BufferSpec{SpecBitmap(), SpecFixedWidth(Int32SizeBytes)}}
102109
}
103110

104-
func (ListType) OffsetTypeTraits() OffsetTraits { return Int32Traits }
111+
func (*ListType) OffsetTypeTraits() OffsetTraits { return Int32Traits }
105112

106113
type LargeListType struct {
107114
ListType
@@ -121,11 +128,11 @@ func (t *LargeListType) Fingerprint() string {
121128
return ""
122129
}
123130

124-
func (LargeListType) Layout() DataTypeLayout {
131+
func (*LargeListType) Layout() DataTypeLayout {
125132
return DataTypeLayout{Buffers: []BufferSpec{SpecBitmap(), SpecFixedWidth(Int64SizeBytes)}}
126133
}
127134

128-
func (LargeListType) OffsetTypeTraits() OffsetTraits { return Int64Traits }
135+
func (*LargeListType) OffsetTypeTraits() OffsetTraits { return Int64Traits }
129136

130137
func LargeListOfField(f Field) *LargeListType {
131138
if f.Type == nil {
@@ -134,18 +141,18 @@ func LargeListOfField(f Field) *LargeListType {
134141
return &LargeListType{ListType{elem: f}}
135142
}
136143

137-
// ListOf returns the list type with element type t.
138-
// For example, if t represents int32, ListOf(t) represents []int32.
144+
// LargeListOf returns the list type with element type t.
145+
// For example, if t represents int32, LargeListOf(t) represents []int32.
139146
//
140-
// ListOf panics if t is nil or invalid. NullableElem defaults to true
147+
// LargeListOf panics if t is nil or invalid. NullableElem defaults to true
141148
func LargeListOf(t DataType) *LargeListType {
142149
if t == nil {
143150
panic("arrow: nil DataType")
144151
}
145152
return &LargeListType{ListType{elem: Field{Name: "item", Type: t, Nullable: true}}}
146153
}
147154

148-
// ListOfNonNullable is like ListOf but NullableElem defaults to false, indicating
155+
// LargeListOfNonNullable is like ListOf but NullableElem defaults to false, indicating
149156
// that the child type should be marked as non-nullable.
150157
func LargeListOfNonNullable(t DataType) *LargeListType {
151158
if t == nil {
@@ -230,7 +237,7 @@ func (t *FixedSizeListType) Fingerprint() string {
230237

231238
func (t *FixedSizeListType) Fields() []Field { return []Field{t.ElemField()} }
232239

233-
func (FixedSizeListType) Layout() DataTypeLayout {
240+
func (*FixedSizeListType) Layout() DataTypeLayout {
234241
return DataTypeLayout{Buffers: []BufferSpec{SpecBitmap()}}
235242
}
236243

@@ -330,7 +337,7 @@ func (t *StructType) Fingerprint() string {
330337
return b.String()
331338
}
332339

333-
func (StructType) Layout() DataTypeLayout {
340+
func (*StructType) Layout() DataTypeLayout {
334341
return DataTypeLayout{Buffers: []BufferSpec{SpecBitmap()}}
335342
}
336343

@@ -389,12 +396,10 @@ func (t *MapType) KeyType() DataType { return t.KeyField().Type }
389396
func (t *MapType) ItemField() Field { return t.value.Elem().(*StructType).Field(1) }
390397
func (t *MapType) ItemType() DataType { return t.ItemField().Type }
391398
func (t *MapType) ValueType() *StructType { return t.value.Elem().(*StructType) }
392-
func (t *MapType) ValueField() Field {
393-
return Field{
394-
Name: "entries",
395-
Type: t.ValueType(),
396-
}
397-
}
399+
func (t *MapType) ValueField() Field { return Field{Name: "entries", Type: t.ValueType()} }
400+
401+
// Elem returns the MapType's element type (if treating MapType as ListLikeType)
402+
func (t *MapType) Elem() DataType { return t.ValueType() }
398403

399404
func (t *MapType) SetItemNullable(nullable bool) {
400405
t.value.Elem().(*StructType).fields[1].Nullable = nullable
@@ -420,7 +425,7 @@ func (t *MapType) Layout() DataTypeLayout {
420425
return t.value.Layout()
421426
}
422427

423-
func (MapType) OffsetTypeTraits() OffsetTraits { return Int32Traits }
428+
func (*MapType) OffsetTypeTraits() OffsetTraits { return Int32Traits }
424429

425430
type (
426431
// UnionTypeCode is an alias to int8 which is the type of the ids
@@ -502,14 +507,14 @@ func (t *unionType) init(fields []Field, typeCodes []UnionTypeCode) {
502507

503508
// Fields method provides a copy of union type fields
504509
// (so it can be safely mutated and will not result in updating the union type).
505-
func (t unionType) Fields() []Field {
510+
func (t *unionType) Fields() []Field {
506511
fields := make([]Field, len(t.children))
507512
copy(fields, t.children)
508513
return fields
509514
}
510515

511-
func (t unionType) TypeCodes() []UnionTypeCode { return t.typeCodes }
512-
func (t unionType) ChildIDs() []int { return t.childIDs[:] }
516+
func (t *unionType) TypeCodes() []UnionTypeCode { return t.typeCodes }
517+
func (t *unionType) ChildIDs() []int { return t.childIDs[:] }
513518

514519
func (t *unionType) validate(fields []Field, typeCodes []UnionTypeCode, _ UnionMode) error {
515520
if len(fields) != len(typeCodes) {
@@ -767,7 +772,22 @@ func (f Field) String() string {
767772

768773
var (
769774
_ DataType = (*ListType)(nil)
775+
_ DataType = (*LargeListType)(nil)
770776
_ DataType = (*FixedSizeListType)(nil)
771777
_ DataType = (*StructType)(nil)
772778
_ DataType = (*MapType)(nil)
779+
_ DataType = (*DenseUnionType)(nil)
780+
_ DataType = (*SparseUnionType)(nil)
781+
782+
_ NestedType = (*ListType)(nil)
783+
_ NestedType = (*LargeListType)(nil)
784+
_ NestedType = (*FixedSizeListType)(nil)
785+
_ NestedType = (*MapType)(nil)
786+
_ NestedType = (*DenseUnionType)(nil)
787+
_ NestedType = (*SparseUnionType)(nil)
788+
789+
_ ListLikeType = (*ListType)(nil)
790+
_ ListLikeType = (*LargeListType)(nil)
791+
_ ListLikeType = (*FixedSizeListType)(nil)
792+
_ ListLikeType = (*MapType)(nil)
773793
)

0 commit comments

Comments
 (0)