forked from v3io/frames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.go
423 lines (348 loc) · 9.14 KB
/
frame.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
Copyright 2018 Iguazio Systems Ltd.
Licensed under the Apache License, Version 2.0 (the "License") with
an addition restriction as set forth herein. You may not use this
file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
In addition, you may not use the software for any purposes that are
illegal under applicable law, and the grant of the foregoing license
under the Apache 2.0 license is conditioned upon your compliance with
such restriction.
*/
package frames
import (
"fmt"
"math"
"time"
"github.com/pkg/errors"
"github.com/v3io/frames/pb"
)
// frameImpl is a frame implementation
type frameImpl struct {
labels map[string]interface{}
byName map[string]Column // name -> Column
columns []Column
indices []Column
msg *pb.Frame
names []string // Created on 1st call to Names
}
func (fr *frameImpl) NullValuesMap() []*pb.NullValuesMap {
return fr.msg.NullValues
}
// NewFrame returns a new Frame
func NewFrame(columns []Column, indices []Column, labels map[string]interface{}) (Frame, error) {
return NewFrameWithNullValues(columns, indices, labels, nil)
}
func NewFrameWithNullValues(columns []Column, indices []Column, labels map[string]interface{}, nullValues []*pb.NullValuesMap) (Frame, error) {
if err := checkEqualLen(columns, indices); err != nil {
return nil, err
}
msg := &pb.Frame{}
var err error
msg.Columns, err = cols2PB(columns)
if err != nil {
return nil, err
}
msg.Indices, err = cols2PB(indices)
if err != nil {
return nil, err
}
msg.Labels, err = pb.FromGoMap(labels)
if err != nil {
return nil, err
}
msg.NullValues = nullValues
byName := make(map[string]Column)
for _, col := range columns {
byName[col.Name()] = col
}
frame := &frameImpl{
byName: byName,
msg: msg,
labels: labels,
indices: indices,
columns: columns,
}
return frame, nil
}
// NewFrameFromMap returns a new MapFrame from a map
func NewFrameFromMap(columns map[string]interface{}, indices map[string]interface{}) (Frame, error) {
cols, err := mapToColumns(columns)
if err != nil {
return nil, err
}
idx, err := mapToColumns(indices)
if err != nil {
return nil, err
}
return NewFrame(cols, idx, nil)
}
// NewFrameFromRows creates a new frame from rows
func NewFrameFromRows(rows []map[string]interface{}, indices []string, labels map[string]interface{}) (Frame, error) {
frameCols := make(map[string]Column)
for rowNum, row := range rows {
for name, value := range row {
col, ok := frameCols[name]
if !ok {
var err error
col, err = newColumn(name, value)
if err != nil {
return nil, err
}
frameCols[name] = col
}
_ = extendCol(col, rowNum)
if err := colAppend(col, value); err != nil {
return nil, err
}
}
// Extend columns not in row
for name, col := range frameCols {
if _, ok := row[name]; !ok {
_ = extendCol(col, rowNum+1)
}
}
}
var dataCols, indexCols []Column
for name, col := range frameCols {
if inSlice(name, indices) {
indexCols = append(indexCols, col)
} else {
dataCols = append(dataCols, col)
}
}
return NewFrame(dataCols, indexCols, labels)
}
// Names returns the column names
func (fr *frameImpl) Names() []string {
if fr.names == nil {
names := make([]string, len(fr.msg.Columns))
for i, col := range fr.columns {
names[i] = col.Name()
}
fr.names = names
}
return fr.names
}
// Indices returns the index columns
func (fr *frameImpl) Indices() []Column {
return fr.indices
}
// Labels returns the Label set, nil if there's none
func (fr *frameImpl) Labels() map[string]interface{} {
if fr.labels == nil {
fr.labels = pb.AsGoMap(fr.msg.Labels)
}
return fr.labels
}
// Len is the number of rows
func (fr *frameImpl) Len() int {
if len(fr.columns) > 0 {
return fr.columns[0].Len()
}
return 0
}
// Column gets a column by name
func (fr *frameImpl) Column(name string) (Column, error) {
// TODO: We can speed it up by calculating once, but then we'll use more memory
col, ok := fr.byName[name]
if !ok {
return nil, fmt.Errorf("column %q not found", name)
}
return col, nil
}
// Slice return a new Frame with is slice of the original
func (fr *frameImpl) Slice(start int, end int) (Frame, error) {
if err := validateSlice(start, end, fr.Len()); err != nil {
return nil, err
}
colSlices, err := sliceCols(fr.columns, start, end)
if err != nil {
return nil, err
}
indexSlices, err := sliceCols(fr.indices, start, end)
if err != nil {
return nil, err
}
var nullValuesSlice []*pb.NullValuesMap
if len(fr.msg.NullValues) != 0 {
nullValuesSlice = fr.msg.NullValues[start:end]
}
return NewFrameWithNullValues(colSlices, indexSlices, fr.labels, nullValuesSlice)
}
// FrameRowIterator returns iterator over rows
func (fr *frameImpl) IterRows(includeIndex bool) RowIterator {
return newRowIterator(fr, includeIndex)
}
// Proto returns the underlying protobuf message
func (fr *frameImpl) Proto() *pb.Frame {
return fr.msg
}
func (fr *frameImpl) IsNull(index int, colName string) bool {
if len(fr.msg.NullValues) == 0 {
return false
}
_, exist := fr.msg.NullValues[index].NullColumns[colName]
return exist
}
// NewFrameFromProto return a new frame from protobuf message
func NewFrameFromProto(msg *pb.Frame) Frame {
byName := make(map[string]Column)
columns := make([]Column, len(msg.Columns))
for i, colMsg := range msg.Columns {
col := &colImpl{msg: colMsg}
columns[i] = col
byName[colMsg.Name] = col
}
indices := make([]Column, len(msg.Indices))
for i, colMsg := range msg.Indices {
indices[i] = &colImpl{msg: colMsg}
}
return &frameImpl{
msg: msg,
columns: columns,
indices: indices,
byName: byName,
}
}
func validateSlice(start int, end int, size int) error {
if start < 0 || end < 0 {
return fmt.Errorf("negative indexing not supported")
}
if end < start {
return fmt.Errorf("end < start")
}
if start >= size {
return fmt.Errorf("start out of bounds")
}
if end > size {
return fmt.Errorf("end out of bounds")
}
return nil
}
func checkEqualLen(columns []Column, indices []Column) error {
size := -1
for _, col := range columns {
if size == -1 { // first column
size = col.Len()
continue
}
if colSize := col.Len(); colSize != size {
return fmt.Errorf("%q column size mismatch (%d != %d)", col.Name(), colSize, size)
}
}
for i, col := range indices {
if colSize := col.Len(); colSize != size {
return fmt.Errorf("index column %d size mismatch (%d != %d)", i, colSize, size)
}
}
return nil
}
func sliceCols(columns []Column, start int, end int) ([]Column, error) {
slices := make([]Column, len(columns))
for i, col := range columns {
slice, err := col.Slice(start, end)
if err != nil {
return nil, errors.Wrapf(err, "can't get slice from %q", col.Name())
}
slices[i] = slice
}
return slices, nil
}
func zeroValue(dtype DType) (interface{}, error) {
switch dtype {
case IntType:
return int64(0), nil
case FloatType:
return math.NaN(), nil
case StringType:
return "", nil
case TimeType:
return time.Unix(0, 0), nil
case BoolType:
return false, nil
}
return nil, fmt.Errorf("unsupported data type - %d", dtype)
}
// TODO: Unite with backend/utils.AppendNil
func extendCol(col Column, size int) error {
if col.Len() >= size {
return nil
}
value, err := zeroValue(col.DType())
if err != nil {
return err
}
for col.Len() < size {
if err := colAppend(col, value); err != nil {
return err
}
}
return nil
}
// TODO: Unite with backend/utils
func newColumn(name string, value interface{}) (Column, error) {
var data interface{}
switch value.(type) {
case int64, int32, int16, int8, int:
data = []int64{}
case float64, float32:
data = []float64{}
case string:
data = []string{}
case time.Time:
data = []time.Time{}
case bool:
data = []bool{}
default:
return nil, fmt.Errorf("unsupported type %T", value)
}
return NewSliceColumn(name, data)
}
func inSlice(name string, names []string) bool {
for _, n := range names {
if name == n {
return true
}
}
return false
}
func mapToColumns(data map[string]interface{}) ([]Column, error) {
columns := make([]Column, len(data))
i := 0
for name, values := range data {
column, err := NewSliceColumn(name, values)
if err != nil {
return nil, errors.Wrapf(err, "can't create column %q", name)
}
columns[i] = column
i++
}
return columns, nil
}
type colAppender interface {
Append(value interface{}) error
}
func colAppend(col Column, value interface{}) error {
ca, ok := col.(colAppender)
if !ok {
return fmt.Errorf("column does not support appending")
}
return ca.Append(value)
}
func cols2PB(columns []Column) ([]*pb.Column, error) {
pbCols := make([]*pb.Column, len(columns))
for i, col := range columns {
pbCol, ok := col.(*colImpl)
if !ok {
return nil, fmt.Errorf("%d: column %q is not protobuf", i, col.Name())
}
pbCols[i] = pbCol.msg
}
return pbCols, nil
}