-
Notifications
You must be signed in to change notification settings - Fork 64
/
type.go
534 lines (497 loc) · 11.8 KB
/
type.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// Package zng implements a data typing system based on the zeek type system.
// All zeek types are defined here and implement the Type interface while instances
// of values implement the Value interface. All values conform to exactly one type.
// The package provides a fast-path for comparing a value to a byte slice
// without having to create a zeek value from the byte slice. To exploit this,
// all values include a Comparison method that returns a Predicate function that
// takes a byte slice and a Type and returns a boolean indicating whether the
// the byte slice with the indicated Type matches the value. The package also
// provides mechanism for coercing values in well-defined and natural ways.
package zed
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/brimdata/zed/zcode"
)
var (
ErrNotArray = errors.New("cannot index a non-array")
ErrIndex = errors.New("array index out of bounds")
ErrUnionSelector = errors.New("union selector out of bounds")
ErrEnumIndex = errors.New("enum index out of bounds")
)
// A Type is an interface presented by a zeek type.
// Types can be used to infer type compatibility and create new values
// of the underlying type.
type Type interface {
// ID returns a unique (per Context) identifier that
// represents this type. For a named type, this identifier
// represents the underlying type and not the named type itself.
// Callers that care about the underlying type of a Value for
// example should prefer to use this instead of using a Go
// type assertion on a Type instance.
ID() int
Kind() Kind
}
type Kind int
const (
PrimitiveKind Kind = iota
RecordKind
ArrayKind
SetKind
MapKind
UnionKind
EnumKind
ErrorKind
)
func (k Kind) String() string {
switch k {
case PrimitiveKind:
return "primitive"
case RecordKind:
return "record"
case ArrayKind:
return "array"
case SetKind:
return "set"
case MapKind:
return "map"
case UnionKind:
return "union"
case EnumKind:
return "enum"
case ErrorKind:
return "error"
default:
return fmt.Sprintf("<unknown kind: %d>", k)
}
}
var (
TypeUint8 = &TypeOfUint8{}
TypeUint16 = &TypeOfUint16{}
TypeUint32 = &TypeOfUint32{}
TypeUint64 = &TypeOfUint64{}
TypeInt8 = &TypeOfInt8{}
TypeInt16 = &TypeOfInt16{}
TypeInt32 = &TypeOfInt32{}
TypeInt64 = &TypeOfInt64{}
TypeDuration = &TypeOfDuration{}
TypeTime = &TypeOfTime{}
// XXX add TypeFloat16
TypeFloat32 = &TypeOfFloat32{}
TypeFloat64 = &TypeOfFloat64{}
// XXX add TypeDecimal
TypeBool = &TypeOfBool{}
TypeBytes = &TypeOfBytes{}
TypeString = &TypeOfString{}
TypeIP = &TypeOfIP{}
TypeNet = &TypeOfNet{}
TypeType = &TypeOfType{}
TypeNull = &TypeOfNull{}
)
// Primary Type IDs
const (
IDUint8 = 0
IDUint16 = 1
IDUint32 = 2
IDUint64 = 3
IDUint128 = 4
IDUint256 = 5
IDInt8 = 6
IDInt16 = 7
IDInt32 = 8
IDInt64 = 9
IDInt128 = 10
IDInt256 = 11
IDDuration = 12
IDTime = 13
IDFloat16 = 14
IDFloat32 = 15
IDFloat64 = 16
IDFloat128 = 17
IDFloat256 = 18
IDDecimal32 = 19
IDDecimal64 = 20
IDDecimal128 = 21
IDDecimal256 = 22
IDBool = 23
IDBytes = 24
IDString = 25
IDIP = 26
IDNet = 27
IDType = 28
IDNull = 29
IDTypeComplex = 30
)
// Encodings for complex type values.
const (
TypeValueRecord = 30
TypeValueArray = 31
TypeValueSet = 32
TypeValueMap = 33
TypeValueUnion = 34
TypeValueEnum = 35
TypeValueError = 36
TypeValueNameDef = 37
TypeValueNameRef = 38
TypeValueMax = TypeValueNameRef
)
// True iff the type id is encoded as a zng signed or unsigened integer zcode.Bytes.
func IsInteger(id int) bool {
return id <= IDInt256
}
// True iff the type id is encoded as a zng signed or unsigned integer zcode.Bytes,
// float32 zcode.Bytes, or float64 zcode.Bytes.
func IsNumber(id int) bool {
return id <= IDDecimal256
}
// True iff the type id is encoded as a float encoding.
// XXX add IDDecimal here when we implement coercible math with it.
func IsFloat(id int) bool {
return id >= IDFloat16 && id <= IDFloat256
}
// True iff the type id is encoded as a number encoding and is signed.
func IsSigned(id int) bool {
return id >= IDInt8 && id <= IDTime
}
func LookupPrimitive(name string) Type {
switch name {
case "uint8":
return TypeUint8
case "uint16":
return TypeUint16
case "uint32":
return TypeUint32
case "uint64":
return TypeUint64
case "int8":
return TypeInt8
case "int16":
return TypeInt16
case "int32":
return TypeInt32
case "int64":
return TypeInt64
case "duration":
return TypeDuration
case "time":
return TypeTime
case "float32":
return TypeFloat32
case "float64":
return TypeFloat64
case "bool":
return TypeBool
case "bytes":
return TypeBytes
case "string":
return TypeString
case "ip":
return TypeIP
case "net":
return TypeNet
case "type":
return TypeType
case "null":
return TypeNull
}
return nil
}
func PrimitiveName(typ Type) string {
switch typ.(type) {
case *TypeOfUint8:
return "uint8"
case *TypeOfUint16:
return "uint16"
case *TypeOfUint32:
return "uint32"
case *TypeOfUint64:
return "uint64"
case *TypeOfInt8:
return "int8"
case *TypeOfInt16:
return "int16"
case *TypeOfInt32:
return "int32"
case *TypeOfInt64:
return "int64"
case *TypeOfDuration:
return "duration"
case *TypeOfTime:
return "time"
case *TypeOfFloat32:
return "float32"
case *TypeOfFloat64:
return "float64"
case *TypeOfBool:
return "bool"
case *TypeOfBytes:
return "bytes"
case *TypeOfString:
return "string"
case *TypeOfIP:
return "ip"
case *TypeOfNet:
return "net"
case *TypeOfType:
return "type"
case *TypeOfNull:
return "null"
default:
return fmt.Sprintf("unknown primitive type: %T", typ)
}
}
func LookupPrimitiveByID(id int) Type {
switch id {
case IDBool:
return TypeBool
case IDInt8:
return TypeInt8
case IDUint8:
return TypeUint8
case IDInt16:
return TypeInt16
case IDUint16:
return TypeUint16
case IDInt32:
return TypeInt32
case IDUint32:
return TypeUint32
case IDInt64:
return TypeInt64
case IDUint64:
return TypeUint64
case IDFloat32:
return TypeFloat32
case IDFloat64:
return TypeFloat64
case IDBytes:
return TypeBytes
case IDString:
return TypeString
case IDIP:
return TypeIP
case IDNet:
return TypeNet
case IDTime:
return TypeTime
case IDDuration:
return TypeDuration
case IDType:
return TypeType
case IDNull:
return TypeNull
}
return nil
}
// Utilities shared by complex types (ie, set and array)
// InnerType returns the element type for the underlying set or array type or
// nil if the underlying type is not a set or array.
func InnerType(typ Type) Type {
switch typ := TypeUnder(typ).(type) {
case *TypeSet:
return typ.Type
case *TypeArray:
return typ.Type
default:
return nil
}
}
func IsUnionType(typ Type) bool {
_, ok := typ.(*TypeUnion)
return ok
}
func IsRecordType(typ Type) bool {
_, ok := TypeUnder(typ).(*TypeRecord)
return ok
}
func TypeRecordOf(typ Type) *TypeRecord {
t, _ := TypeUnder(typ).(*TypeRecord)
return t
}
func IsContainerType(typ Type) bool {
switch typ := typ.(type) {
case *TypeNamed:
return IsContainerType(typ.Type)
case *TypeSet, *TypeArray, *TypeRecord, *TypeUnion, *TypeMap:
return true
default:
return false
}
}
func IsPrimitiveType(typ Type) bool {
return !IsContainerType(typ)
}
func TypeID(typ Type) int {
if named, ok := typ.(*TypeNamed); ok {
return named.id
}
return typ.ID()
}
// UniqueTypes returns the set of unique Types in types in sorted
// order. types will be sorted and deduplicated in place.
func UniqueTypes(types []Type) []Type {
sort.SliceStable(types, func(i, j int) bool {
return CompareTypes(types[i], types[j]) < 0
})
out := types[:0]
var prev Type
for _, typ := range types {
if typ != prev {
out = append(out, typ)
prev = typ
}
}
return out
}
func CompareTypes(a, b Type) int {
a, b = TypeUnder(a), TypeUnder(b)
if cmp := compareInts(int(a.Kind()), int(b.Kind())); cmp != 0 {
return cmp
}
switch a.Kind() {
case PrimitiveKind:
return compareInts(a.ID(), b.ID())
case RecordKind:
ra, rb := TypeRecordOf(a), TypeRecordOf(b)
// First compare column lengths.
if len(ra.Columns) != len(rb.Columns) {
return compareInts(len(ra.Columns), len(rb.Columns))
}
// Second compare column names.
for i := 0; i < len(ra.Columns); i++ {
if cmp := strings.Compare(ra.Columns[i].Name, rb.Columns[i].Name); cmp != 0 {
return cmp
}
}
// Lastly compare column types.
for i := 0; i < len(ra.Columns); i++ {
if cmp := CompareTypes(ra.Columns[i].Type, rb.Columns[i].Type); cmp != 0 {
return cmp
}
}
return 0
case ArrayKind, SetKind:
a, b = InnerType(a), InnerType(b)
return CompareTypes(a, b)
case MapKind:
ma, mb := a.(*TypeMap), b.(*TypeMap)
if cmp := CompareTypes(ma.KeyType, mb.KeyType); cmp != 0 {
return cmp
}
return CompareTypes(ma.ValType, mb.ValType)
case UnionKind:
ua, ub := a.(*TypeUnion), b.(*TypeUnion)
if cmp := compareInts(len(ua.Types), len(ub.Types)); cmp != 0 {
return cmp
}
for i := 0; i < len(ua.Types); i++ {
if cmp := CompareTypes(ua.Types[i], ub.Types[i]); cmp != 0 {
return cmp
}
}
return 0
case EnumKind:
ea, eb := a.(*TypeEnum), b.(*TypeEnum)
if cmp := compareInts(len(ea.Symbols), len(eb.Symbols)); cmp != 0 {
return cmp
}
for i := 0; i < len(ea.Symbols); i++ {
if cmp := strings.Compare(ea.Symbols[i], eb.Symbols[i]); cmp != 0 {
return cmp
}
}
return 0
case ErrorKind:
ea, eb := a.(*TypeError), b.(*TypeError)
return CompareTypes(ea.Type, eb.Type)
}
return 0
}
func compareInts(a, b int) int {
if a < b {
return -1
} else if a > b {
return 1
}
return 0
}
type TypeOfType struct{}
func (t *TypeOfType) ID() int {
return IDType
}
func (t *TypeOfType) Kind() Kind {
return PrimitiveKind
}
func NewTypeValue(t Type) *Value {
return &Value{TypeType, AppendTypeValue(nil, t)}
}
func EncodeTypeValue(t Type) zcode.Bytes {
return AppendTypeValue(nil, t)
}
func AppendTypeValue(b zcode.Bytes, t Type) zcode.Bytes {
var typedefs map[string]Type
return appendTypeValue(b, t, &typedefs)
}
func appendTypeValue(b zcode.Bytes, t Type, typedefs *map[string]Type) zcode.Bytes {
switch t := t.(type) {
case *TypeNamed:
if *typedefs == nil {
*typedefs = make(map[string]Type)
}
id := byte(TypeValueNameDef)
if previous := (*typedefs)[t.Name]; previous == t.Type {
id = TypeValueNameRef
} else {
(*typedefs)[t.Name] = t.Type
}
b = append(b, id)
b = zcode.AppendUvarint(b, uint64(len(t.Name)))
b = append(b, zcode.Bytes(t.Name)...)
if id == TypeValueNameRef {
return b
}
return appendTypeValue(b, t.Type, typedefs)
case *TypeRecord:
b = append(b, TypeValueRecord)
b = zcode.AppendUvarint(b, uint64(len(t.Columns)))
for _, col := range t.Columns {
b = zcode.AppendUvarint(b, uint64(len(col.Name)))
b = append(b, col.Name...)
b = appendTypeValue(b, col.Type, typedefs)
}
return b
case *TypeUnion:
b = append(b, TypeValueUnion)
b = zcode.AppendUvarint(b, uint64(len(t.Types)))
for _, t := range t.Types {
b = appendTypeValue(b, t, typedefs)
}
return b
case *TypeSet:
b = append(b, TypeValueSet)
return appendTypeValue(b, t.Type, typedefs)
case *TypeArray:
b = append(b, TypeValueArray)
return appendTypeValue(b, t.Type, typedefs)
case *TypeEnum:
b = append(b, TypeValueEnum)
b = zcode.AppendUvarint(b, uint64(len(t.Symbols)))
for _, s := range t.Symbols {
b = zcode.AppendUvarint(b, uint64(len(s)))
b = append(b, s...)
}
return b
case *TypeMap:
b = append(b, TypeValueMap)
b = appendTypeValue(b, t.KeyType, typedefs)
return appendTypeValue(b, t.ValType, typedefs)
case *TypeError:
b = append(b, TypeValueError)
return appendTypeValue(b, t.Type, typedefs)
default:
// Primitive type
return append(b, byte(t.ID()))
}
}