This repository has been archived by the owner on Jul 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
constructors.go
55 lines (44 loc) · 1.56 KB
/
constructors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package dataframe
import (
"github.com/apache/arrow/go/arrow/array"
"github.com/apache/arrow/go/arrow/memory"
"github.com/go-bullseye/bullseye/internal/cast"
"github.com/go-bullseye/bullseye/internal/constructors"
)
// NewColumnFromMem is a helper for creating a new Column from memory.
func NewColumnFromMem(mem memory.Allocator, name string, values interface{}) (*array.Column, error) {
arr, field, err := constructors.NewInterfaceFromMem(mem, name, values, nil)
if err != nil {
return nil, err
}
defer arr.Release()
// create the chunk from the data
chunk := array.NewChunked(arr.DataType(), []array.Interface{arr})
defer chunk.Release()
// create the column from the schema and chunk
col := array.NewColumn(*field, chunk)
return col, nil
}
// NewColumnFromSparseMem is a helper for creating a new Column from sparse memory.
func NewColumnFromSparseMem(mem memory.Allocator, name string, values []interface{}, valueIndexes []int, size int) (*array.Column, error) {
// build valid mask
valid := make([]bool, size)
for _, idx := range valueIndexes {
valid[idx] = true
}
ifaceDense, err := cast.SparseCollectionToInterface(values, valueIndexes, size)
if err != nil {
return nil, err
}
arr, field, err := constructors.NewInterfaceFromMem(mem, name, ifaceDense, valid)
if err != nil {
return nil, err
}
defer arr.Release()
// create the chunk from the data
chunk := array.NewChunked(arr.DataType(), []array.Interface{arr})
defer chunk.Release()
// create the column from the schema and chunk
col := array.NewColumn(*field, chunk)
return col, nil
}