-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunked.go
163 lines (137 loc) · 3.96 KB
/
chunked.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package bridge
import (
"errors"
"github.com/DataDog/go-python3"
"github.com/apache/arrow/go/arrow"
"github.com/apache/arrow/go/arrow/array"
"github.com/apache/arrow/go/arrow/memory"
)
func PyChunkedToChunked(pyChunked *python3.PyObject, dtype arrow.DataType) (*array.Chunked, error) {
// Convert pyChunks to []Interface
chunks, err := PyChunkedToChunks(pyChunked, dtype)
if err != nil {
return nil, err
}
chunked := array.NewChunked(dtype, chunks)
return chunked, nil
}
func PyChunkedToChunks(pyChunked *python3.PyObject, dtype arrow.DataType) ([]array.Interface, error) {
pyChunks, err := PyChunkedGetPyChunks(pyChunked)
if err != nil {
return nil, err
}
defer pyChunks.DecRef()
if !python3.PyList_Check(pyChunks) {
return nil, errors.New("pyChunks is not a list")
}
length := python3.PyList_Size(pyChunks)
chunks := make([]array.Interface, 0, length)
for i := 0; i < length; i++ {
chunk, err := PyChunksGetChunk(pyChunks, i, dtype)
if err != nil {
// TODO: Should release any chunks we've already got.
return nil, err
}
chunks = append(chunks, chunk)
}
return chunks, nil
}
func PyChunkedGetPyChunks(pyChunked *python3.PyObject) (*python3.PyObject, error) {
pyChunks := pyChunked.GetAttrString("chunks")
if pyChunks == nil {
return nil, errors.New("could not get pyChunks")
}
return pyChunks, nil
}
func PyChunksGetChunk(pyChunks *python3.PyObject, i int, dtype arrow.DataType) (array.Interface, error) {
pyChunk, err := PyChunksGetPyChunk(pyChunks, i)
if err != nil {
return nil, err
}
defer pyChunk.DecRef()
chunk, err := PyChunkToChunk(pyChunk, dtype)
if err != nil {
return nil, err
}
return chunk, nil
}
func PyChunksGetPyChunk(pyChunks *python3.PyObject, i int) (*python3.PyObject, error) {
pyChunk := python3.PyList_GetItem(pyChunks, i)
if pyChunk == nil {
return nil, errors.New("could not get pyChunk from list")
}
return pyChunk, nil
}
func PyChunkToChunk(pyChunk *python3.PyObject, dtype arrow.DataType) (array.Interface, error) {
data, err := PyChunkToData(pyChunk, dtype)
if err != nil {
return nil, err
}
defer data.Release()
chunk := array.MakeFromData(data)
return chunk, nil
}
func PyChunkToData(pyChunk *python3.PyObject, dtype arrow.DataType) (*array.Data, error) {
buffers, err := PyChunkGetBuffers(pyChunk)
if err != nil {
return nil, err
}
nullCount, err := PyChunkGetNullCount(pyChunk)
if err != nil {
return nil, err
}
offset, err := PyChunkGetOffset(pyChunk)
if err != nil {
return nil, err
}
chunkLen, err := PyChunkGetLength(pyChunk)
if err != nil {
return nil, err
}
var childData []*array.Data // TODO: Implement
data := array.NewData(dtype, chunkLen, buffers, childData, nullCount, offset)
return data, nil
}
func PyChunkGetBuffers(pyChunk *python3.PyObject) ([]*memory.Buffer, error) {
pyBuffers, err := PyChunkGetPyBuffers(pyChunk)
if err != nil {
return nil, err
}
defer pyBuffers.DecRef()
return PyBuffersToBuffers(pyBuffers)
}
func PyChunkGetPyBuffers(pyChunk *python3.PyObject) (*python3.PyObject, error) {
pyBuffersFunc := pyChunk.GetAttrString("buffers")
if pyBuffersFunc == nil {
return nil, errors.New("could not get pyBuffersFunc")
}
defer pyBuffersFunc.DecRef()
pyBuffers := pyBuffersFunc.CallFunctionObjArgs()
if pyBuffers == nil {
return nil, errors.New("could not get pyBuffers")
}
return pyBuffers, nil
}
func PyChunkGetNullCount(pyChunk *python3.PyObject) (int, error) {
v, ok := GetIntAttr(pyChunk, "null_count")
if !ok {
return 0, errors.New("could not get null_count")
}
return v, nil
}
func PyChunkGetOffset(pyChunk *python3.PyObject) (int, error) {
v, ok := GetIntAttr(pyChunk, "offset")
if !ok {
return 0, errors.New("could not get offset")
}
return v, nil
}
func PyChunkGetLength(pyChunk *python3.PyObject) (int, error) {
pyLength := CallPyFunc(pyChunk, "__len__")
if pyLength == nil {
return 0, errors.New("could not get pyChunk.__len__()")
}
defer pyLength.DecRef()
length := python3.PyLong_AsLong(pyLength)
return length, nil
}