-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathany_value.go
200 lines (170 loc) · 5.25 KB
/
any_value.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
package arrow
import (
"fmt"
"github.com/apache/arrow/go/v11/arrow"
"github.com/apache/arrow/go/v11/arrow/array"
"go.opentelemetry.io/collector/pdata/pcommon"
)
// Constants used to identify the type of value in the union.
const (
StrCode int8 = 0
I64Code int8 = 1
F64Code int8 = 2
BoolCode int8 = 3
BinaryCode int8 = 4
// Future extension CborCode int8 = 5
)
var (
// AnyValueDT is an Arrow Data Type representing an OTLP Any Value.
// Any values are represented as a sparse union of the following variants: str, i64, f64, bool, binary.
AnyValueDT = arrow.SparseUnionOf([]arrow.Field{
// TODO manage case where the cardinality of the dictionary is too high (> 2^16).
{Name: "str", Type: DefaultDictString},
{Name: "i64", Type: arrow.PrimitiveTypes.Int64},
{Name: "f64", Type: arrow.PrimitiveTypes.Float64},
{Name: "bool", Type: arrow.FixedWidthTypes.Boolean},
// TODO manage case where the cardinality of the dictionary is too high (> 2^16).
{Name: "binary", Type: DefaultDictBinary},
// Future extension {Name: "cbor", Type: DefaultDictBinary},
}, []int8{
StrCode,
I64Code,
F64Code,
BoolCode,
BinaryCode,
// Future extension CborCode,
})
)
// AnyValueBuilder is a helper to build an Arrow array containing a collection of OTLP Any Value.
type AnyValueBuilder struct {
released bool
builder *array.SparseUnionBuilder // any value builder
strBuilder *AdaptiveDictionaryBuilder // string builder
i64Builder *array.Int64Builder // int64 builder
f64Builder *array.Float64Builder // float64 builder
boolBuilder *array.BooleanBuilder // bool builder
binaryBuilder *AdaptiveDictionaryBuilder // binary builder
}
// AnyValueBuilderFrom creates a new AnyValueBuilder from an existing SparseUnionBuilder.
func AnyValueBuilderFrom(av *array.SparseUnionBuilder) *AnyValueBuilder {
return &AnyValueBuilder{
released: false,
builder: av,
strBuilder: AdaptiveDictionaryBuilderFrom(av.Child(0)),
i64Builder: av.Child(1).(*array.Int64Builder),
f64Builder: av.Child(2).(*array.Float64Builder),
boolBuilder: av.Child(3).(*array.BooleanBuilder),
binaryBuilder: AdaptiveDictionaryBuilderFrom(av.Child(4)),
}
}
// Build builds the "any value" Arrow array.
//
// Once the returned array is no longer needed, Release() must be called to free the
// memory allocated by the array.
func (b *AnyValueBuilder) Build() (*array.SparseUnion, error) {
if b.released {
return nil, fmt.Errorf("any value builder already released")
}
defer b.Release()
return b.builder.NewSparseUnionArray(), nil
}
// Append appends a new any value to the builder.
func (b *AnyValueBuilder) Append(av pcommon.Value) error {
if b.released {
return fmt.Errorf("any value builder already released")
}
var err error
switch av.Type() {
case pcommon.ValueTypeEmpty:
b.builder.AppendNull()
case pcommon.ValueTypeStr:
err = b.appendStr(av.Str())
case pcommon.ValueTypeInt:
err = b.appendI64(av.Int())
case pcommon.ValueTypeDouble:
err = b.appendF64(av.Double())
case pcommon.ValueTypeBool:
err = b.appendBool(av.Bool())
case pcommon.ValueTypeBytes:
err = b.appendBinary(av.Bytes().AsRaw())
case pcommon.ValueTypeSlice:
// TODO implement this with CBOR encoding
err = fmt.Errorf("slice value type not yet supported")
case pcommon.ValueTypeMap:
// TODO implement this with CBOR encoding
err = fmt.Errorf("map value type not yet supported")
}
return err
}
func (b *AnyValueBuilder) AppendNull() {
b.builder.AppendNull()
}
// Release releases the memory allocated by the builder.
func (b *AnyValueBuilder) Release() {
if !b.released {
b.builder.Release()
b.released = true
}
}
// appendStr appends a new string value to the builder.
func (b *AnyValueBuilder) appendStr(v string) error {
b.builder.Append(StrCode)
if v == "" {
b.strBuilder.AppendNull()
} else {
if err := b.strBuilder.AppendString(v); err != nil {
return err
}
}
b.i64Builder.AppendNull()
b.f64Builder.AppendNull()
b.boolBuilder.AppendNull()
b.binaryBuilder.AppendNull()
return nil
}
// appendI64 appends a new int64 value to the builder.
func (b *AnyValueBuilder) appendI64(v int64) error {
b.builder.Append(I64Code)
b.i64Builder.Append(v)
b.strBuilder.AppendNull()
b.f64Builder.AppendNull()
b.boolBuilder.AppendNull()
b.binaryBuilder.AppendNull()
return nil
}
// appendF64 appends a new double value to the builder.
func (b *AnyValueBuilder) appendF64(v float64) error {
b.builder.Append(F64Code)
b.f64Builder.Append(v)
b.strBuilder.AppendNull()
b.i64Builder.AppendNull()
b.boolBuilder.AppendNull()
b.binaryBuilder.AppendNull()
return nil
}
// appendBool appends a new bool value to the builder.
func (b *AnyValueBuilder) appendBool(v bool) error {
b.builder.Append(BoolCode)
b.boolBuilder.Append(v)
b.strBuilder.AppendNull()
b.i64Builder.AppendNull()
b.f64Builder.AppendNull()
b.binaryBuilder.AppendNull()
return nil
}
// appendBinary appends a new binary value to the builder.
func (b *AnyValueBuilder) appendBinary(v []byte) error {
b.builder.Append(BinaryCode)
if v == nil {
b.binaryBuilder.AppendNull()
} else {
if err := b.binaryBuilder.AppendBinary(v); err != nil {
return err
}
}
b.strBuilder.AppendNull()
b.i64Builder.AppendNull()
b.f64Builder.AppendNull()
b.boolBuilder.AppendNull()
return nil
}