-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
fromyaml.go
446 lines (407 loc) · 12.3 KB
/
fromyaml.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
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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.
// fromyaml generates a resource file from the standard_coders.yaml
// file for use in these coder regression tests.
//
// It expects to be run in it's test directory, or via it's go test.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"math"
"reflect"
"runtime/debug"
"strconv"
"strings"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/graph/mtime"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/graph/window"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/exec"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/graphx"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/reflectx"
pipepb "github.com/apache/beam/sdks/v2/go/pkg/beam/model/pipeline_v1"
"github.com/google/go-cmp/cmp"
"golang.org/x/text/encoding/charmap"
yaml "gopkg.in/yaml.v2"
)
var unimplementedCoders = map[string]bool{
"beam:coder:param_windowed_value:v1": true,
"beam:coder:timer:v1": true,
"beam:coder:sharded_key:v1": true,
"beam:coder:custom_window:v1": true,
}
var filteredCases = []struct{ filter, reason string }{
{"logical", "BEAM-9615: Support logical types"},
{"30ea5a25-dcd8-4cdb-abeb-5332d15ab4b9", "https://github.com/apache/beam/issues/21206: Support encoding position."},
}
// Coder is a representation a serialized beam coder.
type Coder struct {
Urn string `yaml:"urn,omitempty"`
Payload string `yaml:"payload,omitempty"`
Components []Coder `yaml:"components,omitempty"`
NonDeterministic bool `yaml:"non_deterministic,omitempty"`
}
type logger interface {
Errorf(string, ...interface{})
Logf(string, ...interface{})
}
// Spec is a set of conditions that a coder must pass.
type Spec struct {
Coder Coder `yaml:"coder,omitempty"`
Nested *bool `yaml:"nested,omitempty"`
Examples yaml.MapSlice `yaml:"examples,omitempty"`
Log logger
id int // for generating coder ids.
coderPBs map[string]*pipepb.Coder
}
func (s *Spec) nextID() string {
ret := fmt.Sprintf("%d", s.id)
s.id++
return ret
}
func (s *Spec) testStandardCoder() (err error) {
if unimplementedCoders[s.Coder.Urn] {
log.Printf("skipping unimplemented coder urn: %v", s.Coder.Urn)
return nil
}
if s.Coder.Urn == "beam:coder:state_backed_iterable:v1" {
log.Printf("skipping unimplemented test coverage for beam:coder:state_backed_iterable:v1. https://github.com/apache/beam/issues/21324")
return nil
}
for _, c := range filteredCases {
if strings.Contains(s.Coder.Payload, c.filter) {
log.Printf("skipping coder case. Unsupported in the Go SDK for now: %v Payload: %v", c.reason, s.Coder.Payload)
return nil
}
}
// Construct the coder proto equivalents.
// Only nested tests need to be run, since nestedness is a pre-portability
// concept.
// For legacy Java reasons, the row coder examples are all marked nested: false
// so we need to check that before skipping unnested tests.
if s.Coder.Urn != "beam:coder:row:v1" && s.Nested != nil && !*s.Nested {
log.Printf("skipping unnested coder spec: %v\n", s.Coder)
return nil
}
s.coderPBs = make(map[string]*pipepb.Coder)
id := s.parseCoder(s.Coder)
b := graphx.NewCoderUnmarshaller(s.coderPBs)
underTest, err := b.Coder(id)
if err != nil {
return fmt.Errorf("unable to create coder: %v", err)
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("panicked on coder %v || %v:\n\t%v :\n%s", underTest, s.Coder, e, debug.Stack())
}
}()
var decFails, encFails int
for _, eg := range s.Examples {
// Test Decoding
// Ideally we'd use the beam package coders, but KVs make that complicated.
// This can be cleaned up once a type parametered beam.KV type exists.
dec := exec.MakeElementDecoder(underTest)
encoded := eg.Key.(string)
var elem exec.FullValue
// What I would have expected.
// r := charmap.ISO8859_1.NewDecoder().Reader(strings.NewReader(encoded))
recoded, err := charmap.ISO8859_1.NewEncoder().String(encoded)
if err != nil {
return err
}
r := strings.NewReader(recoded)
if err := dec.DecodeTo(r, &elem); err != nil {
return fmt.Errorf("err decoding %q: %v", encoded, err)
}
if !diff(s.Coder, &elem, eg) {
decFails++
continue
}
// Test Encoding
if s.Coder.NonDeterministic {
// Skip verifying nondeterministic encodings.
continue
}
enc := exec.MakeElementEncoder(underTest)
var out bytes.Buffer
if err := enc.Encode(&elem, &out); err != nil {
return err
}
if d := cmp.Diff(recoded, string(out.Bytes())); d != "" {
log.Printf("Encoding error: diff(-want,+got): %v\n", d)
}
}
if decFails+encFails > 0 {
return fmt.Errorf("failed to decode %v times, and encode %v times", decFails, encFails)
}
return nil
}
var cmpOpts = []cmp.Option{
cmp.Transformer("bytes2string", func(in []byte) (out string) {
return string(in)
}),
}
func diff(c Coder, elem *exec.FullValue, eg yaml.MapItem) bool {
var got, want interface{}
switch c.Urn {
case "beam:coder:bytes:v1":
got = string(elem.Elm.([]byte))
switch egv := eg.Value.(type) {
case string:
want = egv
case []byte:
want = string(egv)
}
case "beam:coder:varint:v1":
got, want = elem.Elm.(int64), int64(eg.Value.(int))
case "beam:coder:double:v1":
got = elem.Elm.(float64)
switch v := eg.Value.(string); v {
case "NaN":
// Do the NaN comparison here since NaN by definition != NaN.
if math.IsNaN(got.(float64)) {
want, got = 1, 1
} else {
want = math.NaN()
}
case "-Infinity":
want = math.Inf(-1)
case "Infinity":
want = math.Inf(1)
default:
want, _ = strconv.ParseFloat(v, 64)
}
case "beam:coder:kv:v1":
v := eg.Value.(yaml.MapSlice)
pass := true
if !diff(c.Components[0], &exec.FullValue{Elm: elem.Elm}, v[0]) {
pass = false
}
if !diff(c.Components[1], &exec.FullValue{Elm: elem.Elm2}, v[1]) {
pass = false
}
return pass
case "beam:coder:nullable:v1":
if elem.Elm == nil || eg.Value == nil {
got, want = elem.Elm, eg.Value
} else {
got = string(elem.Elm.([]byte))
switch egv := eg.Value.(type) {
case string:
want = egv
case []byte:
want = string(egv)
}
}
case "beam:coder:iterable:v1":
pass := true
gotrv := reflect.ValueOf(elem.Elm)
wantrv := reflect.ValueOf(eg.Value)
if gotrv.Len() != wantrv.Len() {
log.Printf("Lengths don't match. got %v, want %v; %v, %v", gotrv.Len(), wantrv.Len(), gotrv, wantrv)
return false
}
for i := 0; i < wantrv.Len(); i++ {
if !diff(c.Components[0],
&exec.FullValue{Elm: gotrv.Index(i).Interface()},
yaml.MapItem{Value: wantrv.Index(i).Interface()}) {
pass = false
}
}
return pass
case "beam:coder:interval_window:v1":
var a, b int
val := eg.Value
if is, ok := eg.Value.([]interface{}); ok {
val = is[0]
}
v := val.(yaml.MapSlice)
a = v[0].Value.(int)
b = v[1].Value.(int)
end := mtime.FromMilliseconds(int64(a))
start := end - mtime.Time(int64(b))
want = window.IntervalWindow{Start: start, End: end}
// If this is nested in an iterable, windows won't be populated.
if len(elem.Windows) == 0 {
got = elem.Elm
} else {
got = elem.Windows[0]
}
case "beam:coder:global_window:v1":
want = window.GlobalWindow{}
// If this is nested in an iterable, windows won't be populated.
if len(elem.Windows) == 0 {
got = window.GlobalWindow(elem.Elm.(struct{}))
} else {
got = elem.Windows[0]
}
case "beam:coder:windowed_value:v1", "beam:coder:param_windowed_value:v1":
// elem contains all the information, but we need to compare the element+timestamp
// separately from the windows, to avoid repeated expected value parsing logic.
pass := true
vs := eg.Value.(yaml.MapSlice)
if !diff(c.Components[0], elem, vs[0]) {
pass = false
}
if d := cmp.Diff(
mtime.FromMilliseconds(int64(vs[1].Value.(int))),
elem.Timestamp, cmpOpts...); d != "" {
pass = false
}
if !diff(c.Components[1], elem, vs[3]) {
pass = false
}
// TODO compare pane information.
return pass
case "beam:coder:row:v1":
fs := eg.Value.(yaml.MapSlice)
var rfs []reflect.StructField
// There are only 2 pointer examples, but they reuse field names,
// so we key off the proto hash to know which example we're handling.
ptrEg := strings.Contains(c.Payload, "51ace21c7393")
for _, rf := range fs {
name := rf.Key.(string)
t := nameToType[name]
if ptrEg {
t = reflect.PtrTo(t)
}
rfs = append(rfs, reflect.StructField{
Name: strings.ToUpper(name[:1]) + name[1:],
Type: t,
Tag: reflect.StructTag(fmt.Sprintf("beam:\"%v\"", name)),
})
}
rv := reflect.New(reflect.StructOf(rfs)).Elem()
for i, rf := range fs {
setField(rv, i, rf.Value)
}
got, want = elem.Elm, rv.Interface()
default:
got, want = elem.Elm, eg.Value
}
if d := cmp.Diff(want, got, cmpOpts...); d != "" {
log.Printf("Decoding error: diff(-want,+got): %v\n", d)
return false
}
return true
}
// standard_coders.yaml uses the name for type indication, except for nullability.
var nameToType = map[string]reflect.Type{
"str": reflectx.String,
"i32": reflectx.Int32,
"f64": reflectx.Float64,
"arr": reflect.SliceOf(reflectx.String),
"f_bool": reflectx.Bool,
"f_bytes": reflect.PtrTo(reflectx.ByteSlice),
"f_map": reflect.MapOf(reflectx.String, reflect.PtrTo(reflectx.Int64)),
}
func setField(rv reflect.Value, i int, v interface{}) {
if v == nil {
return
}
rf := rv.Field(i)
if rf.Kind() == reflect.Ptr {
// Ensure it's initialized.
rf.Set(reflect.New(rf.Type().Elem()))
rf = rf.Elem()
}
switch rf.Kind() {
case reflect.String:
rf.SetString(v.(string))
case reflect.Int32:
rf.SetInt(int64(v.(int)))
case reflect.Float64:
c, err := strconv.ParseFloat(v.(string), 64)
if err != nil {
panic(err)
}
rf.SetFloat(c)
case reflect.Slice:
if rf.Type() == reflectx.ByteSlice {
rf.Set(reflect.ValueOf([]byte(v.(string))))
break
}
// Value is a []interface{} with string values.
var arr []string
for _, a := range v.([]interface{}) {
arr = append(arr, a.(string))
}
rf.Set(reflect.ValueOf(arr))
case reflect.Bool:
rf.SetBool(v.(bool))
case reflect.Map:
// only f_map presently, which is always map[string]*int64
rm := reflect.MakeMap(rf.Type())
for _, a := range v.(yaml.MapSlice) {
rk := reflect.ValueOf(a.Key.(string))
rv := reflect.Zero(rf.Type().Elem())
if a.Value != nil {
rv = reflect.New(reflectx.Int64)
rv.Elem().SetInt(int64(a.Value.(int)))
}
rm.SetMapIndex(rk, rv)
}
rf.Set(rm)
}
}
func (s *Spec) parseCoder(c Coder) string {
id := s.nextID()
var compIDs []string
for _, comp := range c.Components {
compIDs = append(compIDs, s.parseCoder(comp))
}
s.coderPBs[id] = &pipepb.Coder{
Spec: &pipepb.FunctionSpec{
Urn: c.Urn,
Payload: []byte(c.Payload),
},
ComponentCoderIds: compIDs,
}
return id
}
// Simple logger to run as main program.
type logLogger struct{}
func (*logLogger) Errorf(format string, v ...interface{}) {
log.Printf(format, v...)
}
func (*logLogger) Logf(format string, v ...interface{}) {
log.Printf(format, v...)
}
const yamlPath = "../../../../../../model/fn-execution/src/main/resources/org/apache/beam/model/fnexecution/v1/standard_coders.yaml"
func main() {
data, err := ioutil.ReadFile(yamlPath)
if err != nil {
log.Fatalf("Couldn't read %v: %v", yamlPath, err)
}
specs := bytes.Split(data, []byte("\n---\n"))
var failures bool
var l logLogger
for _, data := range specs {
cs := Spec{Log: &l}
if err := yaml.Unmarshal(data, &cs); err != nil {
failures = true
l.Logf("unable to parse yaml: %v %q", err, data)
continue
}
if err := cs.testStandardCoder(); err != nil {
failures = true
l.Logf("Failed \"%v\": %v", cs.Coder, err)
}
}
if !failures {
log.Println("PASS")
}
}