-
Notifications
You must be signed in to change notification settings - Fork 67
/
shaper.go
554 lines (524 loc) · 16 KB
/
shaper.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package expr
import (
"fmt"
"slices"
"sort"
"github.com/brimdata/zed"
"github.com/brimdata/zed/zcode"
"github.com/brimdata/zed/zson"
)
// A ShaperTransform represents one of the different transforms that a
// shaper can apply. The transforms are represented as bit flags that
// can be bitwise-ored together to create a single shaping operator that
// represents the composition of all operators. This composition is efficient
// as it is created once per incoming type and then the resulting
// operator is run for every value of that type.
type ShaperTransform int
const (
Cast ShaperTransform = 1 << iota
Crop
Fill
Order
)
func NewShaperTransform(s string) ShaperTransform {
switch s {
case "cast":
return Cast
case "crop":
return Crop
case "fill":
return Fill
case "fit":
return Crop | Fill
case "order":
return Order
case "shape":
return Cast | Fill | Order
}
return 0
}
// NewShaper returns a shaper that will shape the result of expr
// to the type returned by typeExpr according to tf.
func NewShaper(zctx *zed.Context, expr, typeExpr Evaluator, tf ShaperTransform) (Evaluator, error) {
if l, ok := typeExpr.(*Literal); ok {
typeVal := l.val
switch id := typeVal.Type.ID(); {
case id == zed.IDType:
typ, err := zctx.LookupByValue(typeVal.Bytes())
if err != nil {
return nil, err
}
return NewConstShaper(zctx, expr, typ, tf), nil
case id == zed.IDString && tf == Cast:
name := zed.DecodeString(typeVal.Bytes())
if _, err := zed.NewContext().LookupTypeNamed(name, zed.TypeNull); err != nil {
return nil, err
}
return &casterNamedType{zctx, expr, name}, nil
}
return nil, fmt.Errorf("shaper type argument is not a type: %s", zson.FormatValue(typeVal))
}
return &Shaper{
zctx: zctx,
expr: expr,
typeExpr: typeExpr,
transforms: tf,
shapers: make(map[zed.Type]*ConstShaper),
}, nil
}
type Shaper struct {
zctx *zed.Context
expr Evaluator
typeExpr Evaluator
transforms ShaperTransform
shapers map[zed.Type]*ConstShaper
}
func (s *Shaper) Eval(ectx Context, this *zed.Value) *zed.Value {
typeVal := s.typeExpr.Eval(ectx, this)
switch id := typeVal.Type.ID(); {
case id == zed.IDType:
typ, err := s.zctx.LookupByValue(typeVal.Bytes())
if err != nil {
return ectx.CopyValue(*s.zctx.NewError(err))
}
shaper, ok := s.shapers[typ]
if !ok {
shaper = NewConstShaper(s.zctx, s.expr, typ, s.transforms)
s.shapers[typ] = shaper
}
return shaper.Eval(ectx, this)
case id == zed.IDString && s.transforms == Cast:
name := zed.DecodeString(typeVal.Bytes())
return (&casterNamedType{s.zctx, s.expr, name}).Eval(ectx, this)
}
return ectx.CopyValue(*s.zctx.WrapError("shaper type argument is not a type", typeVal))
}
type ConstShaper struct {
zctx *zed.Context
expr Evaluator
shapeTo zed.Type
transforms ShaperTransform
b zcode.Builder
caster Evaluator // used when shapeTo is a primitive type
shapers map[int]*shaper // map from input type ID to shaper
}
// NewConstShaper returns a shaper that will shape the result of expr
// to the provided shapeTo type.
func NewConstShaper(zctx *zed.Context, expr Evaluator, shapeTo zed.Type, tf ShaperTransform) *ConstShaper {
var caster Evaluator
if tf == Cast {
// Use a caster since it's faster.
caster = LookupPrimitiveCaster(zctx, zed.TypeUnder(shapeTo))
}
return &ConstShaper{
zctx: zctx,
expr: expr,
shapeTo: shapeTo,
transforms: tf,
caster: caster,
shapers: make(map[int]*shaper),
}
}
func (c *ConstShaper) Eval(ectx Context, this *zed.Value) *zed.Value {
val := c.expr.Eval(ectx, this)
if val.IsError() {
return val
}
if val.IsNull() {
// Null values can be shaped to any type.
return ectx.NewValue(c.shapeTo, nil)
}
id, shapeToID := val.Type.ID(), c.shapeTo.ID()
if id == shapeToID {
// Same underlying types but one or both are named.
val = ectx.CopyValue(*val)
val.Type = c.shapeTo
return val
}
if c.caster != nil && !zed.IsUnionType(val.Type) {
val = c.caster.Eval(ectx, val)
if val.Type != c.shapeTo && val.Type.ID() == shapeToID {
// Same underlying types but one or both are named.
val = ectx.CopyValue(*val)
val.Type = c.shapeTo
}
return val
}
s, ok := c.shapers[id]
if !ok {
var err error
s, err = newShaper(c.zctx, c.transforms, val.Type, c.shapeTo)
if err != nil {
return ectx.CopyValue(*c.zctx.NewError(err))
}
c.shapers[id] = s
}
c.b.Reset()
typ := s.step.build(c.zctx, ectx, val.Bytes(), &c.b)
return ectx.NewValue(typ, c.b.Bytes().Body())
}
// A shaper is a per-input type ID "spec" that contains the output
// type and the op to create an output value.
type shaper struct {
typ zed.Type
step step
}
func newShaper(zctx *zed.Context, tf ShaperTransform, in, out zed.Type) (*shaper, error) {
typ, err := shaperType(zctx, tf, in, out)
if err != nil {
return nil, err
}
step, err := newStep(zctx, in, typ)
return &shaper{typ, step}, err
}
func shaperType(zctx *zed.Context, tf ShaperTransform, in, out zed.Type) (zed.Type, error) {
inUnder, outUnder := zed.TypeUnder(in), zed.TypeUnder(out)
if tf&Cast != 0 {
if inUnder == outUnder || inUnder == zed.TypeNull {
return out, nil
}
if isMap(outUnder) {
return nil, fmt.Errorf("cannot yet use maps in shaping functions (issue #2894)")
}
if zed.IsPrimitiveType(inUnder) && zed.IsPrimitiveType(outUnder) {
// Matching field is a primitive: output type is cast type.
if LookupPrimitiveCaster(zctx, outUnder) == nil {
return nil, fmt.Errorf("cast to %s not implemented", zson.FormatType(out))
}
return out, nil
}
if in, ok := inUnder.(*zed.TypeUnion); ok {
for _, t := range in.Types {
if _, err := shaperType(zctx, tf, t, out); err != nil {
return nil, fmt.Errorf("cannot cast union %q to %q due to %q",
zson.FormatType(in), zson.FormatType(out), zson.FormatType(t))
}
}
return out, nil
}
if bestUnionTag(in, outUnder) > -1 {
return out, nil
}
} else if inUnder == outUnder {
return in, nil
}
if inRec, ok := inUnder.(*zed.TypeRecord); ok {
if outRec, ok := outUnder.(*zed.TypeRecord); ok {
fields, err := shaperFields(zctx, tf, inRec, outRec)
if err != nil {
return nil, err
}
if tf&Cast != 0 {
if slices.Equal(fields, outRec.Fields) {
return out, nil
}
} else if slices.Equal(fields, inRec.Fields) {
return in, nil
}
return zctx.LookupTypeRecord(fields)
}
}
inInner, outInner := zed.InnerType(inUnder), zed.InnerType(outUnder)
if inInner != nil && outInner != nil && (tf&Cast != 0 || isArray(inUnder) == isArray(outUnder)) {
t, err := shaperType(zctx, tf, inInner, outInner)
if err != nil {
return nil, err
}
if tf&Cast != 0 {
if t == outInner {
return out, nil
}
} else if t == inInner {
return in, nil
}
if isArray(outUnder) {
return zctx.LookupTypeArray(t), nil
}
return zctx.LookupTypeSet(t), nil
}
return in, nil
}
func shaperFields(zctx *zed.Context, tf ShaperTransform, in, out *zed.TypeRecord) ([]zed.Field, error) {
crop, fill := tf&Crop != 0, tf&Fill != 0
if tf&Order == 0 {
crop, fill = !fill, !crop
out, in = in, out
}
var fields []zed.Field
for _, outField := range out.Fields {
if inFieldType, ok := in.TypeOfField(outField.Name); ok {
outFieldType := outField.Type
if tf&Order == 0 {
// Counteract the swap of in and out above.
outFieldType, inFieldType = inFieldType, outFieldType
}
t, err := shaperType(zctx, tf, inFieldType, outFieldType)
if err != nil {
return nil, err
}
fields = append(fields, zed.NewField(outField.Name, t))
} else if fill {
fields = append(fields, outField)
}
}
if !crop {
inFields := in.Fields
if tf&Order != 0 {
// Order appends unknown fields in lexicographic order.
inFields = slices.Clone(inFields)
sort.Slice(inFields, func(i, j int) bool {
return inFields[i].Name < inFields[j].Name
})
}
for _, f := range inFields {
if !out.HasField(f.Name) {
fields = append(fields, f)
}
}
}
return fields, nil
}
// bestUnionTag tries to return the most specific union tag for in
// within out. It returns -1 if out is not a union or contains no type
// compatible with in. (Types are compatible if they have the same underlying
// type.) If out contains in, bestUnionTag returns its tag.
// Otherwise, if out contains in's underlying type, bestUnionTag returns
// its tag. Finally, bestUnionTag returns the smallest tag in
// out whose type is compatible with in.
func bestUnionTag(in, out zed.Type) int {
outUnion, ok := zed.TypeUnder(out).(*zed.TypeUnion)
if !ok {
return -1
}
typeUnderIn := zed.TypeUnder(in)
underlying := -1
compatible := -1
for i, t := range outUnion.Types {
if t == in {
return i
}
if t == typeUnderIn && underlying == -1 {
underlying = i
}
if zed.TypeUnder(t) == typeUnderIn && compatible == -1 {
compatible = i
}
}
if underlying != -1 {
return underlying
}
return compatible
}
func isArray(t zed.Type) bool {
_, ok := t.(*zed.TypeArray)
return ok
}
func isMap(t zed.Type) bool {
_, ok := t.(*zed.TypeMap)
return ok
}
type op int
const (
copyOp op = iota // copy field fromIndex from input record
castPrimitive // cast field fromIndex from fromType to toType
castFromUnion // cast union value with tag s using children[s]
castToUnion // cast non-union fromType to union toType with tag toTag
null // write null
array // build array
set // build set
record // build record
)
// A step is a recursive data structure encoding a series of
// copy/cast steps to be carried out over an input record.
type step struct {
op op
caster Evaluator // for castPrimitive
fromIndex int // for children of a record step
fromType zed.Type // for castPrimitive and castToUnion
toTag int // for castToUnion
toType zed.Type
// if op == record, contains one op for each field.
// if op == array, contains one op for all array elements.
// if op == castFromUnion, contains one op per union tag.
children []step
types []zed.Type
uniqueTypes []zed.Type
}
func newStep(zctx *zed.Context, in, out zed.Type) (step, error) {
Switch:
switch {
case in.ID() == zed.IDNull:
return step{op: null, toType: out}, nil
case in.ID() == out.ID():
return step{op: copyOp, toType: out}, nil
case zed.IsRecordType(in) && zed.IsRecordType(out):
return newRecordStep(zctx, zed.TypeRecordOf(in), out)
case zed.IsPrimitiveType(in) && zed.IsPrimitiveType(out):
caster := LookupPrimitiveCaster(zctx, zed.TypeUnder(out))
return step{op: castPrimitive, caster: caster, fromType: in, toType: out}, nil
case zed.InnerType(in) != nil:
if k := out.Kind(); k == zed.ArrayKind {
return newArrayOrSetStep(zctx, array, zed.InnerType(in), out)
} else if k == zed.SetKind {
return newArrayOrSetStep(zctx, set, zed.InnerType(in), out)
}
case zed.IsUnionType(in):
var steps []step
for _, t := range zed.TypeUnder(in).(*zed.TypeUnion).Types {
s, err := newStep(zctx, t, out)
if err != nil {
break Switch
}
steps = append(steps, s)
}
return step{op: castFromUnion, toType: out, children: steps}, nil
}
if tag := bestUnionTag(in, out); tag != -1 {
return step{op: castToUnion, fromType: in, toTag: tag, toType: out}, nil
}
return step{}, fmt.Errorf("createStep: incompatible types %s and %s", zson.FormatType(in), zson.FormatType(out))
}
// newRecordStep returns a step that will build a record of type out from a
// record of type in. The two types must be compatible, meaning that
// the input type must be an unordered subset of the input type
// (where 'unordered' means that if the output type has record fields
// [a b] and the input type has fields [b a] that is ok). It is also
// ok for leaf primitive types to be different; if they are a casting
// step is inserted.
func newRecordStep(zctx *zed.Context, in *zed.TypeRecord, out zed.Type) (step, error) {
var children []step
for _, outField := range zed.TypeRecordOf(out).Fields {
ind, ok := in.IndexOfField(outField.Name)
if !ok {
children = append(children, step{op: null, toType: outField.Type})
continue
}
child, err := newStep(zctx, in.Fields[ind].Type, outField.Type)
if err != nil {
return step{}, err
}
child.fromIndex = ind
children = append(children, child)
}
return step{op: record, toType: out, children: children}, nil
}
func newArrayOrSetStep(zctx *zed.Context, op op, inInner, out zed.Type) (step, error) {
innerStep, err := newStep(zctx, inInner, zed.InnerType(out))
if err != nil {
return step{}, err
}
return step{op: op, toType: out, children: []step{innerStep}}, nil
}
// build applies the operation described by s to in, appends the resulting bytes
// to b, and returns the resulting type. The type is usually s.toType but can
// differ if a primitive cast fails.
func (s *step) build(zctx *zed.Context, ectx Context, in zcode.Bytes, b *zcode.Builder) zed.Type {
if in == nil || s.op == copyOp {
b.Append(in)
return s.toType
}
switch s.op {
case castPrimitive:
// For a successful cast, v.Type == zed.TypeUnder(s.toType).
// For a failed cast, v.Type is a zed.TypeError.
v := s.caster.Eval(ectx, ectx.NewValue(s.fromType, in))
b.Append(v.Bytes())
if zed.TypeUnder(v.Type) == zed.TypeUnder(s.toType) {
// Prefer s.toType in case it's a named type.
return s.toType
}
return v.Type
case castFromUnion:
it := in.Iter()
tag := int(zed.DecodeInt(it.Next()))
return s.children[tag].build(zctx, ectx, it.Next(), b)
case castToUnion:
zed.BuildUnion(b, s.toTag, in)
return s.toType
case array, set:
return s.buildArrayOrSet(zctx, ectx, s.op, in, b)
case record:
return s.buildRecord(zctx, ectx, in, b)
default:
panic(fmt.Sprintf("unknown step.op %v", s.op))
}
}
func (s *step) buildArrayOrSet(zctx *zed.Context, ectx Context, op op, in zcode.Bytes, b *zcode.Builder) zed.Type {
b.BeginContainer()
defer b.EndContainer()
s.types = s.types[:0]
for it := in.Iter(); !it.Done(); {
typ := s.children[0].build(zctx, ectx, it.Next(), b)
s.types = append(s.types, typ)
}
s.uniqueTypes = append(s.uniqueTypes[:0], s.types...)
s.uniqueTypes = zed.UniqueTypes(s.uniqueTypes)
var inner zed.Type
switch len(s.uniqueTypes) {
case 0:
return s.toType
case 1:
inner = s.uniqueTypes[0]
default:
union := zctx.LookupTypeUnion(s.uniqueTypes)
// Convert each container element to the union type.
b.TransformContainer(func(bytes zcode.Bytes) zcode.Bytes {
var b2 zcode.Builder
for i, it := 0, bytes.Iter(); !it.Done(); i++ {
zed.BuildUnion(&b2, union.TagOf(s.types[i]), it.Next())
}
return b2.Bytes()
})
inner = union
}
if op == set {
b.TransformContainer(zed.NormalizeSet)
}
if zed.TypeUnder(inner) == zed.TypeUnder(zed.InnerType(s.toType)) {
// Prefer s.toType in case it or its inner type is a named type.
return s.toType
}
if op == set {
return zctx.LookupTypeSet(inner)
}
return zctx.LookupTypeArray(inner)
}
func (s *step) buildRecord(zctx *zed.Context, ectx Context, in zcode.Bytes, b *zcode.Builder) zed.Type {
b.BeginContainer()
defer b.EndContainer()
s.types = s.types[:0]
var needNewRecordType bool
for _, child := range s.children {
if child.op == null {
b.Append(nil)
s.types = append(s.types, child.toType)
continue
}
// Using getNthFromContainer means we iterate from the
// beginning of the record for each field. An
// optimization (for shapes that don't require field
// reordering) would be make direct use of a
// zcode.Iter along with keeping track of our
// position.
bytes := getNthFromContainer(in, child.fromIndex)
typ := child.build(zctx, ectx, bytes, b)
if zed.TypeUnder(typ) == zed.TypeUnder(child.toType) {
// Prefer child.toType in case it's a named type.
typ = child.toType
} else {
// This field's type differs from the corresponding
// field in s.toType, so we'll need to look up a new
// record type below.
needNewRecordType = true
}
s.types = append(s.types, typ)
}
if needNewRecordType {
fields := slices.Clone(zed.TypeUnder(s.toType).(*zed.TypeRecord).Fields)
for i, t := range s.types {
fields[i].Type = t
}
return zctx.MustLookupTypeRecord(fields)
}
return s.toType
}