-
Notifications
You must be signed in to change notification settings - Fork 66
/
complex.go
360 lines (304 loc) · 7.19 KB
/
complex.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
package super
import (
"bytes"
"errors"
"sort"
"github.com/brimdata/super/zcode"
)
type TypeArray struct {
id int
Type Type
}
func NewTypeArray(id int, typ Type) *TypeArray {
return &TypeArray{id, typ}
}
func (t *TypeArray) ID() int {
return t.id
}
func (t *TypeArray) Kind() Kind {
return ArrayKind
}
// ErrMissing is a Go error that implies a missing value in the runtime logic
// whereas Missing is a Zed error value that represents a missing value embedded
// in the dataflow computation.
var ErrMissing = errors.New("missing")
// Missing is value that represents an error condition arising from a referenced
// entity not present, e.g., a reference to a non-existent record field, a map
// lookup for a key not present, an array index that is out of range, etc.
// The Missing error can be propagated through functions and expressions and
// each operator has clearly defined semantics with respect to the Missing value.
// For example, "true AND MISSING" is MISSING.
var Missing = zcode.Bytes("missing")
var Quiet = zcode.Bytes("quiet")
type TypeError struct {
id int
Type Type
}
func NewTypeError(id int, typ Type) *TypeError {
return &TypeError{id, typ}
}
func (t *TypeError) ID() int {
return t.id
}
func (t *TypeError) Kind() Kind {
return ErrorKind
}
func (t *TypeError) IsMissing(zv zcode.Bytes) bool {
return t.Type == TypeString && bytes.Equal(zv, Missing)
}
func (t *TypeError) IsQuiet(zv zcode.Bytes) bool {
return t.Type == TypeString && bytes.Equal(zv, Quiet)
}
type TypeEnum struct {
id int
Symbols []string
}
func NewTypeEnum(id int, symbols []string) *TypeEnum {
return &TypeEnum{id, symbols}
}
func (t *TypeEnum) ID() int {
return t.id
}
func (t *TypeEnum) Symbol(index int) (string, error) {
if index < 0 || index >= len(t.Symbols) {
return "", ErrEnumIndex
}
return t.Symbols[index], nil
}
func (t *TypeEnum) Lookup(symbol string) int {
for k, s := range t.Symbols {
if s == symbol {
return k
}
}
return -1
}
func (t *TypeEnum) Kind() Kind {
return EnumKind
}
type TypeMap struct {
id int
KeyType Type
ValType Type
}
func NewTypeMap(id int, keyType, valType Type) *TypeMap {
return &TypeMap{id, keyType, valType}
}
func (t *TypeMap) ID() int {
return t.id
}
func (t *TypeMap) Kind() Kind {
return MapKind
}
type keyval struct {
key zcode.Bytes
val zcode.Bytes
}
// NormalizeMap interprets zv as a map body and returns an equivalent map body
// that is normalized according to the ZNG specification (i.e., the tag-counted
// value of each entry's key is lexicographically greater than that of the
// preceding entry).
func NormalizeMap(zv zcode.Bytes) zcode.Bytes {
elements := make([]keyval, 0, 8)
for it := zv.Iter(); !it.Done(); {
key := it.NextTagAndBody()
val := it.NextTagAndBody()
elements = append(elements, keyval{key, val})
}
if len(elements) < 2 {
return zv
}
sort.Slice(elements, func(i, j int) bool {
return bytes.Compare(elements[i].key, elements[j].key) == -1
})
norm := make(zcode.Bytes, 0, len(zv))
norm = append(norm, elements[0].key...)
norm = append(norm, elements[0].val...)
for i := 1; i < len(elements); i++ {
// Skip duplicates.
if !bytes.Equal(elements[i].key, elements[i-1].key) {
norm = append(norm, elements[i].key...)
norm = append(norm, elements[i].val...)
}
}
return norm
}
type TypeNamed struct {
id int
Name string
Type Type
}
func NewTypeNamed(id int, name string, typ Type) *TypeNamed {
return &TypeNamed{
id: id,
Name: name,
Type: typ,
}
}
func (t *TypeNamed) ID() int {
return t.Type.ID()
}
func (t *TypeNamed) NamedID() int {
return t.id
}
func (t *TypeNamed) Kind() Kind {
return t.Type.Kind()
}
func TypeUnder(typ Type) Type {
if named, ok := typ.(*TypeNamed); ok {
return TypeUnder(named.Type)
}
return typ
}
// Field defines the name and type of a field for [TypeRecord].
type Field struct {
Name string
Type Type
}
func NewField(name string, typ Type) Field {
return Field{name, typ}
}
type TypeRecord struct {
id int
Fields []Field
LUT map[string]int
}
func NewTypeRecord(id int, fields []Field) *TypeRecord {
if fields == nil {
fields = []Field{}
}
r := &TypeRecord{
id: id,
Fields: fields,
}
r.createLUT()
return r
}
func (t *TypeRecord) ID() int {
return t.id
}
func (t *TypeRecord) IndexOfField(field string) (int, bool) {
v, ok := t.LUT[field]
return v, ok
}
func (t *TypeRecord) TypeOfField(field string) (Type, bool) {
n, ok := t.LUT[field]
if !ok {
return nil, false
}
return t.Fields[n].Type, true
}
func (t *TypeRecord) HasField(field string) bool {
_, ok := t.LUT[field]
return ok
}
func (t *TypeRecord) createLUT() {
t.LUT = make(map[string]int)
for k, f := range t.Fields {
t.LUT[f.Name] = k
}
}
func (t *TypeRecord) Kind() Kind {
return RecordKind
}
type TypeSet struct {
id int
Type Type
}
func NewTypeSet(id int, typ Type) *TypeSet {
return &TypeSet{id, typ}
}
func (t *TypeSet) ID() int {
return t.id
}
func (t *TypeSet) Kind() Kind {
return SetKind
}
// NormalizeSet interprets zv as a set body and returns an equivalent set body
// that is normalized according to the ZNG specification (i.e., each element's
// tag-counted value is lexicographically greater than that of the preceding
// element).
func NormalizeSet(zv zcode.Bytes) zcode.Bytes {
elements := make([]zcode.Bytes, 0, 8)
for it := zv.Iter(); !it.Done(); {
elements = append(elements, it.NextTagAndBody())
}
if len(elements) < 2 {
return zv
}
sort.Slice(elements, func(i, j int) bool {
return bytes.Compare(elements[i], elements[j]) == -1
})
norm := make(zcode.Bytes, 0, len(zv))
norm = append(norm, elements[0]...)
for i := 1; i < len(elements); i++ {
// Skip duplicates.
if !bytes.Equal(elements[i], elements[i-1]) {
norm = append(norm, elements[i]...)
}
}
return norm
}
type TypeUnion struct {
id int
Types []Type
LUT map[Type]int
}
func NewTypeUnion(id int, types []Type) *TypeUnion {
t := &TypeUnion{id: id, Types: types}
t.createLUT()
return t
}
func (t *TypeUnion) createLUT() {
t.LUT = make(map[Type]int)
for i, typ := range t.Types {
t.LUT[typ] = i
}
}
func (t *TypeUnion) ID() int {
return t.id
}
// Type returns the type corresponding to tag.
func (t *TypeUnion) Type(tag int) (Type, error) {
if tag < 0 || tag >= len(t.Types) {
return nil, ErrUnionTag
}
return t.Types[tag], nil
}
// TagOf returns the tag for typ in the union. If no type exists -1 is
// returned.
func (t *TypeUnion) TagOf(typ Type) int {
if s, ok := t.LUT[typ]; ok {
return s
}
return -1
}
// Untag takes bytes of the reciever's type and returns the underlying value
// as its type and bytes by removing the tag and determining that tag's
// type from the union. Untag panics if the tag is invalid.
func (t *TypeUnion) Untag(bytes zcode.Bytes) (Type, zcode.Bytes) {
if bytes == nil {
return t, nil
}
it := bytes.Iter()
tag := DecodeInt(it.Next())
inner, err := t.Type(int(tag))
if err != nil {
panic(err)
}
return inner, it.Next()
}
func (t *TypeUnion) Kind() Kind {
return UnionKind
}
// BuildUnion appends to b a union described by tag and val.
func BuildUnion(b *zcode.Builder, tag int, val zcode.Bytes) {
if val == nil {
b.Append(nil)
return
}
b.BeginContainer()
b.Append(EncodeInt(int64(tag)))
b.Append(val)
b.EndContainer()
}