-
Notifications
You must be signed in to change notification settings - Fork 3
/
parquet.go
281 lines (248 loc) · 7.87 KB
/
parquet.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package data
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path"
"sync"
"time"
"github.com/apache/arrow/go/arrow/memory"
"github.com/apache/arrow/go/v15/arrow"
"github.com/apache/arrow/go/v15/arrow/array"
"github.com/apache/arrow/go/v15/parquet"
"github.com/apache/arrow/go/v15/parquet/pqarrow"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/data"
)
var logger = log.DefaultLogger
func ToParquet(frames []*data.Frame, chunk int) (map[string]string, error) {
dirs := map[string]string{}
frameIndex := framesByRef(frames)
// TODO - appending lables to fields for now
// need to return multiple frames instead
// for _, f := range frames {
// for _, fld := range f.Fields {
// if len(fld.Labels) > 0 {
// lbls := fld.Labels.String()
// fld.Name = fmt.Sprintf("%s %s", fld.Name, lbls)
// }
// }
// }
for _, frameList := range frameIndex {
labelsToFields(frameList)
dir, err := os.MkdirTemp("", "duck")
if err != nil {
logger.Error("failed to create temp dir", "error", err)
return nil, err
}
mergeFrames(frameList)
for i, frame := range frameList {
dirs[frame.RefID] = dir
schema, err := MarshalArrow(frame)
if err != nil {
logger.Error("failed to marshal arrow schema", "error", err)
return nil, err
}
data := frameData(frame)
if chunk > 0 {
var wg sync.WaitGroup
errCh := make(chan error, 10)
// write files in chunks
chunks := makeChunks(data, chunk)
for i, chunk := range chunks {
wg.Add(1)
go func(chunk FrameData, idx int) error {
defer wg.Done()
raw, err := json.Marshal(chunk)
if err != nil {
logger.Error("failed to marshal chunk", "error", err)
return err
}
name := fmt.Sprintf("%s%d", frame.RefID, idx)
_, _, err = write(dir, name, schema, raw)
if err != nil {
logger.Error("failed to write parquet file", "error", err)
}
return err
}(chunk, i)
}
go func() {
wg.Wait()
close(errCh)
}()
for err := range errCh {
logger.Error("failed to write chunk", "error", err)
return nil, err
}
continue
}
raw, err := json.Marshal(data)
if err != nil {
logger.Error("parquet failed to marshal frame data to raw data", "error", err)
return nil, err
}
name := fmt.Sprintf("%s%d", frame.RefID, i)
_, _, err = write(dir, name, schema, raw)
if err != nil {
logger.Error("parquet failed to write parquet file", "error", err)
return nil, err
}
}
}
return dirs, nil
}
func frameData(frame *data.Frame) FrameData {
data := FrameData{}
for rowIdx := 0; rowIdx < frame.Rows(); rowIdx++ {
row := map[string]any{}
for colIdx, f := range frame.Fields {
name := getFieldName(f)
row[name] = frame.At(colIdx, rowIdx)
}
data = append(data, row)
}
return data
}
func write(dir string, name string, schema *arrow.Schema, jsonData []byte) (string, string, error) {
filename := path.Join(dir, name+".parquet")
output, err := os.Create(filename)
if err != nil {
logger.Error("failed to create parquet file", "file", filename, "error", err)
return "", "", err
}
defer output.Close()
writerProps := parquet.NewWriterProperties()
writer, err := pqarrow.NewFileWriter(schema, output, writerProps, pqarrow.DefaultWriterProps())
if err != nil {
logger.Error("failed to create parquet writer", "error", err)
return "", "", err
}
r := bytes.NewReader(jsonData)
record, _, err := array.RecordFromJSON(memory.DefaultAllocator, schema, r)
if err != nil {
logger.Error("failed to create record from json", "error", err)
return "", "", err
}
err = writer.Write(record)
if err != nil {
logger.Error("failed to write record", "error", err)
return "", "", err
}
err = writer.Close()
if err != nil {
logger.Error("failed to close writer", "error", err)
return dir, output.Name(), nil
}
return dir, output.Name(), nil
}
type FrameData []map[string]any
func makeChunks(xs FrameData, chunkSize int) []FrameData {
if len(xs) == 0 {
return nil
}
divided := make([]FrameData, (len(xs)+chunkSize-1)/chunkSize)
prev := 0
i := 0
till := len(xs) - chunkSize
for prev < till {
next := prev + chunkSize
divided[i] = xs[prev:next]
prev = next
i++
}
divided[i] = xs[prev:]
return divided
}
func framesByRef(frames []*data.Frame) map[string][]*data.Frame {
byRef := map[string][]*data.Frame{}
for _, f := range frames {
fr := byRef[f.RefID]
if fr == nil {
refFrames := []*data.Frame{}
byRef[f.RefID] = refFrames
}
byRef[f.RefID] = append(byRef[f.RefID], clone(f))
}
return byRef
}
func clone(f *data.Frame) *data.Frame {
copy := data.NewFrame(f.Name, f.Fields...)
copy.RefID = f.RefID
copy.Meta = f.Meta
return copy
}
func mergeFrames(frames []*data.Frame) {
fields := map[string]*data.Field{}
for _, f := range frames {
for _, fld := range f.Fields {
fields[fld.Name] = fld
}
}
for _, fld := range fields {
for _, f := range frames {
found := false
for _, fld2 := range f.Fields {
if fld2.Name == fld.Name {
found = true
break
}
}
if !found {
makeArray := maker[fld.Type()]
arr := makeArray(f.Rows())
nullField := data.NewField(fld.Name, fld.Labels, arr)
f.Fields = append(f.Fields, nullField)
}
}
}
}
var maker = map[data.FieldType]func(length int) any{
data.FieldTypeFloat64: func(length int) any { return makeArray[float64](length) },
data.FieldTypeFloat32: func(length int) any { return makeArray[float32](length) },
data.FieldTypeInt16: func(length int) any { return makeArray[int16](length) },
data.FieldTypeInt64: func(length int) any { return makeArray[int64](length) },
data.FieldTypeInt8: func(length int) any { return makeArray[int8](length) },
data.FieldTypeUint8: func(length int) any { return makeArray[uint8](length) },
data.FieldTypeUint16: func(length int) any { return makeArray[uint16](length) },
data.FieldTypeUint32: func(length int) any { return makeArray[uint32](length) },
data.FieldTypeUint64: func(length int) any { return makeArray[uint64](length) },
data.FieldTypeNullableFloat64: func(length int) any { return makeArray[*float64](length) },
data.FieldTypeNullableFloat32: func(length int) any { return makeArray[*float32](length) },
data.FieldTypeNullableInt16: func(length int) any { return makeArray[*int16](length) },
data.FieldTypeNullableInt64: func(length int) any { return makeArray[*int64](length) },
data.FieldTypeNullableInt8: func(length int) any { return makeArray[*int8](length) },
data.FieldTypeNullableUint8: func(length int) any { return makeArray[*uint8](length) },
data.FieldTypeNullableUint16: func(length int) any { return makeArray[*uint16](length) },
data.FieldTypeNullableUint32: func(length int) any { return makeArray[*uint32](length) },
data.FieldTypeNullableUint64: func(length int) any { return makeArray[*uint64](length) },
data.FieldTypeString: func(length int) any { return makeArray[string](length) },
data.FieldTypeNullableString: func(length int) any { return makeArray[*string](length) },
data.FieldTypeTime: func(length int) any { return makeArray[time.Time](length) },
data.FieldTypeNullableTime: func(length int) any { return makeArray[*time.Time](length) },
}
func makeArray[T any](length int) []T {
return make([]T, length)
}
func labelsToFields(frames []*data.Frame) {
for _, f := range frames {
fields := []*data.Field{}
for _, fld := range f.Fields {
if fld.Labels != nil {
for lbl, val := range fld.Labels {
newFld := newField(lbl, val, f.Rows())
fields = append(fields, newFld)
}
}
}
f.Fields = append(f.Fields, fields...)
}
}
func newField(name string, val string, size int) *data.Field {
values := make([]string, size)
newField := data.NewField(name, nil, values)
for i := 0; i < size; i++ {
newField.Set(i, val)
}
return newField
}