-
Notifications
You must be signed in to change notification settings - Fork 3
/
metric.go
406 lines (367 loc) · 11.6 KB
/
metric.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
// Copyright 2024 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// 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.
package greptime
import (
"errors"
"fmt"
"math"
"time"
greptimepb "github.com/GreptimeTeam/greptime-proto/go/greptime/v1"
"github.com/apache/arrow/go/v13/arrow"
"github.com/apache/arrow/go/v13/arrow/array"
"github.com/apache/arrow/go/v13/arrow/flight"
)
// Metric represents multiple rows of data, and also Metric can specify
// the timestamp column name and precision
type Metric struct {
timestampAlias string
timestampPrecision time.Duration
// orders and columns SHOULD NOT contain timestampAlias key
orders []string
columns map[string]column
series []Series
}
// GetTagsAndFields get all column names from metric, except timestamp column
func (m *Metric) GetTagsAndFields() []string {
dst := make([]string, len(m.orders))
copy(dst, m.orders)
return dst
}
// GetSeries gets all data from metric
func (m *Metric) GetSeries() []Series {
return m.series
}
func buildMetricFromReader(r *flight.Reader) (*Metric, error) {
metric := Metric{}
if r == nil {
return nil, errors.New("Internal Error, empty reader pointer")
}
fields := r.Schema().Fields()
for r.Next() {
record := r.Record()
for i := 0; i < int(record.NumRows()); i++ {
series := Series{}
for j := 0; j < int(record.NumCols()); j++ {
column := record.Column(j)
colVal, err := fromColumn(column, i)
if err != nil {
return nil, err
}
series.AddField(fields[j].Name, colVal)
}
if err := metric.AddSeries(series); err != nil {
return nil, err
}
}
}
return &metric, nil
}
func extractPrecision(field *arrow.Field) (time.Duration, error) {
if field == nil {
return 0, errors.New("field should not be empty")
}
dataType, ok := field.Type.(*arrow.TimestampType)
if !ok {
return 0, fmt.Errorf("unsupported arrow field type '%s'", field.Type.Name())
}
switch dataType.Unit {
case arrow.Microsecond:
return time.Microsecond, nil
case arrow.Millisecond:
return time.Millisecond, nil
case arrow.Second:
return time.Second, nil
case arrow.Nanosecond:
return time.Nanosecond, nil
default:
return 0, fmt.Errorf("unsupported arrow type '%s'", field.Type.Name())
}
}
// fromColumn retrieves arrow value from the column at idx position
func fromColumn(column arrow.Array, idx int) (any, error) {
if column.IsNull(idx) {
return nil, nil
}
switch typedColumn := column.(type) {
case *array.Int64:
return typedColumn.Value(idx), nil
case *array.Int32:
return typedColumn.Value(idx), nil
case *array.Int16:
return typedColumn.Value(idx), nil
case *array.Int8:
return typedColumn.Value(idx), nil
case *array.Uint64:
return typedColumn.Value(idx), nil
case *array.Uint32:
return typedColumn.Value(idx), nil
case *array.Uint16:
return typedColumn.Value(idx), nil
case *array.Uint8:
return typedColumn.Value(idx), nil
case *array.Float64:
return typedColumn.Value(idx), nil
case *array.Float32:
return typedColumn.Value(idx), nil
case *array.String:
return typedColumn.Value(idx), nil
case *array.Boolean:
return typedColumn.Value(idx), nil
case *array.Binary:
return typedColumn.Value(idx), nil
case *array.LargeBinary:
return typedColumn.Value(idx), nil
case *array.FixedSizeBinary:
return typedColumn.Value(idx), nil
case *array.Time32:
return typedColumn.Value(idx), nil
case *array.Time64:
return typedColumn.Value(idx), nil
case *array.Date32:
return typedColumn.Value(idx), nil
case *array.Date64:
return typedColumn.Value(idx), nil
case *array.Timestamp:
dataType, ok := column.DataType().(*arrow.TimestampType)
if !ok {
return nil, fmt.Errorf("unsupported arrow timestamp type '%T' for '%s'", typedColumn, column.DataType().Name())
}
value := int64(typedColumn.Value(idx))
switch dataType.Unit {
case arrow.Microsecond:
return time.UnixMicro(value), nil
case arrow.Millisecond:
return time.UnixMilli(value), nil
case arrow.Second:
return time.Unix(value, 0), nil
case arrow.Nanosecond:
return time.Unix(0, value), nil
default:
return nil, fmt.Errorf("unsupported arrow timestamp type '%T' for '%s'", typedColumn, column.DataType().Name())
}
default:
return nil, fmt.Errorf("unsupported arrow type '%T' for '%s'", typedColumn, column.DataType().Name())
}
}
// SetTimePrecision set precision for Metric. Valid durations include:
// - time.Nanosecond
// - time.Microsecond
// - time.Millisecond
// - time.Second.
//
// # Pay attention
//
// - once the precision has been set, it can not be changed
// - insert will fail if precision does not match with the existing precision of the schema in greptimedb
func (m *Metric) SetTimePrecision(precision time.Duration) error {
if !isValidPrecision(precision) {
return ErrInvalidTimePrecision
}
m.timestampPrecision = precision
return nil
}
// SetTimestampAlias helps to specify the timestamp column name, default is ts.
func (m *Metric) SetTimestampAlias(alias string) error {
alias, err := toColumnName(alias)
if err != nil {
return err
}
m.timestampAlias = alias
return nil
}
// GetTimestampAlias get the timestamp column name, default is ts.
func (m *Metric) GetTimestampAlias() string {
if len(m.timestampAlias) == 0 {
return "ts"
}
return m.timestampAlias
}
// AddSeries add one row to Metric.
//
// # Pay Attention
//
// - different row can have different fields, Metric will union all the columns,
// leave empty value of one row if the column is not specified in this row
// - same column name MUST have same schema, which means Tag,Field,Timestamp and
// data type MUST BE the same of the same column name in different rows
func (m *Metric) AddSeries(s Series) error {
if m.columns == nil {
m.columns = map[string]column{}
}
if m.orders == nil {
m.orders = []string{}
}
if m.series == nil {
m.series = []Series{}
}
for _, key := range s.orders {
sCol := s.columns[key]
if mCol, seen := m.columns[key]; seen {
if err := checkColumnEquality(key, mCol, sCol); err != nil {
return err
}
} else {
m.orders = append(m.orders, key)
m.columns[key] = sCol
}
}
m.series = append(m.series, s)
return nil
}
func (m *Metric) intoGreptimeColumn() ([]*greptimepb.Column, error) {
if len(m.series) == 0 {
return nil, ErrNoSeriesInMetric
}
result, err := m.intoDataColumns()
if err != nil {
return nil, err
}
tsColumn, err := m.intoTimestampColumn()
if err != nil {
return nil, err
}
return append(result, tsColumn), nil
}
// nullMaskByteSize helps to calculate how many bytes needed in Mask.shrink
func (m *Metric) nullMaskByteSize() int {
return int(math.Ceil(float64(len(m.series)) / 8.0))
}
// intoDataColumns does not contain timestamp semantic column
func (m *Metric) intoDataColumns() ([]*greptimepb.Column, error) {
nullMasks := map[string]*mask{}
mappedCols := map[string]*greptimepb.Column{}
for name, col := range m.columns {
column := greptimepb.Column{
ColumnName: name,
SemanticType: col.semantic,
Datatype: col.typ,
Values: &greptimepb.Column_Values{},
NullMask: nil,
}
mappedCols[name] = &column
}
for rowIdx, s := range m.series {
for name, col := range mappedCols {
if val, exist := s.vals[name]; exist {
if err := setColumn(col, val); err != nil {
return nil, err
}
} else {
nullMask, exist := nullMasks[name]
if !exist {
nullMask = &mask{}
nullMasks[name] = nullMask
}
nullMask.set(uint(rowIdx))
}
}
}
if len(nullMasks) > 0 {
if err := setNullMask(mappedCols, nullMasks, m.nullMaskByteSize()); err != nil {
return nil, err
}
}
result := make([]*greptimepb.Column, 0, len(mappedCols))
for _, key := range m.orders {
result = append(result, mappedCols[key])
}
return result, nil
}
func (m *Metric) intoTimestampColumn() (*greptimepb.Column, error) {
datatype, err := precisionToDataType(m.timestampPrecision)
if err != nil {
return nil, err
}
tsColumn := &greptimepb.Column{
ColumnName: m.GetTimestampAlias(),
SemanticType: greptimepb.SemanticType_TIMESTAMP,
Datatype: datatype,
Values: &greptimepb.Column_Values{},
NullMask: nil,
}
nullMask := mask{}
for _, s := range m.series {
switch datatype {
case greptimepb.ColumnDataType_TIMESTAMP_SECOND:
setColumn(tsColumn, s.timestamp.Unix())
case greptimepb.ColumnDataType_TIMESTAMP_MICROSECOND:
setColumn(tsColumn, s.timestamp.UnixMicro())
case greptimepb.ColumnDataType_TIMESTAMP_NANOSECOND:
setColumn(tsColumn, s.timestamp.UnixNano())
default: // greptimepb.ColumnDataType_TIMESTAMP_MILLISECOND
setColumn(tsColumn, s.timestamp.UnixMilli())
}
}
if b, err := nullMask.shrink(m.nullMaskByteSize()); err != nil {
return nil, err
} else {
tsColumn.NullMask = b
}
return tsColumn, nil
}
func setColumn(col *greptimepb.Column, val any) error {
switch col.Datatype {
case greptimepb.ColumnDataType_INT8:
col.Values.I8Values = append(col.Values.I8Values, int32(val.(int8)))
case greptimepb.ColumnDataType_INT16:
col.Values.I16Values = append(col.Values.I16Values, int32(val.(int16)))
case greptimepb.ColumnDataType_INT32:
col.Values.I32Values = append(col.Values.I32Values, val.(int32))
case greptimepb.ColumnDataType_INT64:
col.Values.I64Values = append(col.Values.I64Values, val.(int64))
case greptimepb.ColumnDataType_UINT8:
col.Values.U8Values = append(col.Values.U8Values, uint32(val.(uint8)))
case greptimepb.ColumnDataType_UINT16:
col.Values.U16Values = append(col.Values.U16Values, uint32(val.(uint16)))
case greptimepb.ColumnDataType_UINT32:
col.Values.U32Values = append(col.Values.U32Values, val.(uint32))
case greptimepb.ColumnDataType_UINT64:
col.Values.U64Values = append(col.Values.U64Values, val.(uint64))
case greptimepb.ColumnDataType_FLOAT32:
col.Values.F32Values = append(col.Values.F32Values, val.(float32))
case greptimepb.ColumnDataType_FLOAT64:
col.Values.F64Values = append(col.Values.F64Values, val.(float64))
case greptimepb.ColumnDataType_BOOLEAN:
col.Values.BoolValues = append(col.Values.BoolValues, val.(bool))
case greptimepb.ColumnDataType_STRING:
col.Values.StringValues = append(col.Values.StringValues, val.(string))
case greptimepb.ColumnDataType_BINARY:
col.Values.BinaryValues = append(col.Values.BinaryValues, val.([]byte))
case greptimepb.ColumnDataType_TIMESTAMP_SECOND:
col.Values.TimestampSecondValues = append(col.Values.TimestampSecondValues, val.(int64))
case greptimepb.ColumnDataType_TIMESTAMP_MILLISECOND:
col.Values.TimestampMillisecondValues = append(col.Values.TimestampMillisecondValues, val.(int64))
case greptimepb.ColumnDataType_TIMESTAMP_MICROSECOND:
col.Values.TimestampMicrosecondValues = append(col.Values.TimestampMicrosecondValues, val.(int64))
case greptimepb.ColumnDataType_TIMESTAMP_NANOSECOND:
col.Values.TimestampNanosecondValues = append(col.Values.TimestampNanosecondValues, val.(int64))
default:
return fmt.Errorf("unknown column data type: %v", col.Datatype)
}
return nil
}
func setNullMask(cols map[string]*greptimepb.Column, masks map[string]*mask, size int) error {
for name, mask := range masks {
b, err := mask.shrink(size)
if err != nil {
return err
}
col, exist := cols[name]
if !exist {
return fmt.Errorf("'%s' column not found when set null mask", name)
}
col.NullMask = b
}
return nil
}