Skip to content

Commit a7fafdd

Browse files
committed
ARROW-5111: [Go] implement reading list arrays from Arrow file
1 parent 79fb767 commit a7fafdd

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

go/arrow/ipc/file_reader.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,9 @@ func (ctx *arrayLoaderContext) loadArray(dt arrow.DataType) array.Interface {
364364
case *arrow.BinaryType, *arrow.StringType:
365365
return ctx.loadBinary(dt)
366366

367+
case *arrow.ListType:
368+
return ctx.loadList(dt)
369+
367370
default:
368371
panic(errors.Errorf("array type %T not handled yet", dt))
369372
}
@@ -385,6 +388,16 @@ func (ctx *arrayLoaderContext) loadCommon(nbufs int) (*flatbuf.FieldNode, []*mem
385388
return field, buffers
386389
}
387390

391+
func (ctx *arrayLoaderContext) loadChild(dt arrow.DataType) array.Interface {
392+
if ctx.max == 0 {
393+
panic("arrow/ipc: nested type limit reached")
394+
}
395+
ctx.max--
396+
sub := ctx.loadArray(dt)
397+
ctx.max++
398+
return sub
399+
}
400+
388401
func (ctx *arrayLoaderContext) loadNull() array.Interface {
389402
field, buffers := ctx.loadCommon(1)
390403
buffers = append(buffers, ctx.buffer())
@@ -422,6 +435,19 @@ func (ctx *arrayLoaderContext) loadBinary(dt arrow.DataType) array.Interface {
422435
return array.MakeFromData(data)
423436
}
424437

438+
func (ctx *arrayLoaderContext) loadList(dt *arrow.ListType) array.Interface {
439+
field, buffers := ctx.loadCommon(2)
440+
buffers = append(buffers, ctx.buffer())
441+
442+
sub := ctx.loadChild(dt.Elem())
443+
defer sub.Release()
444+
445+
data := array.NewData(dt, int(field.Length()), buffers, []*array.Data{sub.Data()}, int(field.NullCount()), 0)
446+
defer data.Release()
447+
448+
return array.NewListData(data)
449+
}
450+
425451
func readDictionary(meta *memory.Buffer, types dictTypeMap, r ReadAtSeeker) (int64, array.Interface, error) {
426452
// msg := flatbuf.GetRootAsMessage(meta.Bytes(), 0)
427453
// var dictBatch flatbuf.DictionaryBatch

go/arrow/ipc/metadata.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ func concreteTypeFromFB(typ flatbuf.Type, data flatbuffers.Table, children []arr
248248
case flatbuf.TypeBool:
249249
return arrow.FixedWidthTypes.Boolean, nil
250250

251+
case flatbuf.TypeList:
252+
if len(children) != 1 {
253+
return nil, errors.Errorf("arrow/ipc: List must have exactly 1 child field (got=%d)", len(children))
254+
}
255+
return arrow.ListOf(children[0].Type), nil
256+
251257
default:
252258
// FIXME(sbinet): implement all the other types.
253259
panic(fmt.Errorf("arrow/ipc: type %v not implemented", flatbuf.EnumNamesType[typ]))

0 commit comments

Comments
 (0)