/
create.go
130 lines (111 loc) · 4.14 KB
/
create.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
// 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.
package beam
import (
"bytes"
"reflect"
"github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors"
"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
)
func init() {
register.DoFn2x1[[]byte, func(T), error]((*createFn)(nil))
register.Emitter1[T]()
}
// Create inserts a fixed non-empty set of values into the pipeline. The values must
// be of the same type 'A' and the returned PCollection is of type A.
//
// The returned PCollections can be used as any other PCollections. The values
// are JSON-coded. Each runner may place limits on the sizes of the values and
// Create should generally only be used for small collections.
func Create(s Scope, values ...any) PCollection {
return Must(TryCreate(s, values...))
}
// CreateList inserts a fixed set of values into the pipeline from a slice or
// array. Unlike Create this supports the creation of an empty PCollection.
func CreateList(s Scope, list any) PCollection {
return Must(TryCreateList(s, list))
}
// TryCreate inserts a fixed non-empty set of values into the pipeline. The
// values must be of the same type.
func TryCreate(s Scope, values ...any) (PCollection, error) {
if len(values) == 0 {
err := errors.New("create has no values")
return PCollection{}, addCreateCtx(err, s)
}
t := reflect.ValueOf(values[0]).Type()
return createList(s, values, t)
}
// TryCreateList inserts a fixed set of values into the pipeline from a slice or
// array. The values must be of the same type. Unlike TryCreate this supports
// the creation of an empty PCollection.
func TryCreateList(s Scope, list any) (PCollection, error) {
val := reflect.ValueOf(list)
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
err := errors.Errorf("input %v must be a slice or array", list)
return PCollection{}, addCreateCtx(err, s)
}
var ret []any
for i := 0; i < val.Len(); i++ {
ret = append(ret, val.Index(i).Interface())
}
var t reflect.Type
if len(ret) == 0 {
t = reflect.TypeOf(list).Elem()
} else {
t = reflect.ValueOf(ret[0]).Type()
}
return createList(s, ret, t)
}
func addCreateCtx(err error, s Scope) error {
return errors.WithContextf(err, "inserting Create in scope %s", s)
}
func createList(s Scope, values []any, t reflect.Type) (PCollection, error) {
fn := &createFn{Type: EncodedType{T: t}}
enc := NewElementEncoder(t)
for i, value := range values {
if other := reflect.ValueOf(value).Type(); other != t {
err := errors.Errorf("value %v at index %v has type %v, want %v", value, i, other, t)
return PCollection{}, addCreateCtx(err, s)
}
var buf bytes.Buffer
if err := enc.Encode(value, &buf); err != nil {
err = errors.Wrapf(err, "marshalling of %v failed", value)
return PCollection{}, addCreateCtx(err, s)
}
fn.Values = append(fn.Values, buf.Bytes())
}
imp := Impulse(s)
ret, err := TryParDo(s, fn, imp, TypeDefinition{Var: TType, T: t})
if err != nil || len(ret) != 1 {
panic(addCreateCtx(errors.WithContext(err, "internal error"), s))
}
return ret[0], nil
}
// TODO(herohde) 6/26/2017: make 'create' a SDF once supported. See BEAM-2421.
type createFn struct {
Values [][]byte `json:"values"`
Type EncodedType `json:"type"`
}
func (c *createFn) ProcessElement(_ []byte, emit func(T)) error {
dec := NewElementDecoder(c.Type.T)
for _, val := range c.Values {
element, err := dec.Decode(bytes.NewBuffer(val))
if err != nil {
return err
}
emit(element)
}
return nil
}