apache / apache/beam

[Bug][Go SDK]: Generic functions alias to same registered name in Go 1.20

Open
#26,066 0 comments 0 reactions 0 assignees View on GitHub
bug go P3
Dominant language
Java
Stars
8.7k
Forks
4.7k
Avg merge
1d 20h
Merged PRs (30d)
196

Description

### What happened?

Changes in Go 1.20 lead to the "function name" of generic functions to have type substitutions registered as "..." instead of a concrete type. This can lead to aliasing of registered generic functions, even with different types.

This affects the generic BSON coders in the mongodbio.
https://github.com/apache/beam/blob/master/sdks/go/pkg/beam/io/mongodbio/coder.go#L30

```

func init() {
beam.RegisterCoder(
reflect.TypeOf((*idRangeRestriction)(nil)).Elem(),
encodeBSON[idRangeRestriction],
decodeBSON[idRangeRestriction],
)
beam.RegisterCoder(
reflect.TypeOf((*idRange)(nil)).Elem(),
encodeBSON[idRange],
decodeBSON[idRange],
)
}

func encodeBSON[T any](in T) ([]byte, error) {
out, err := bson.Marshal(in)
if err != nil {
return nil, fmt.Errorf("error encoding BSON: %w", err)
}

return out, nil
}

func decodeBSON[T any](in []byte) (T, error) {
var out T
if err := bson.Unmarshal(in, &out); err != nil {
return out, fmt.Errorf("error decoding BSON: %w", err)
}

return out, nil
}
```

Fortunately a work around exists, by just wrapping the generic functions with a concrete function for use in the registrations instead.

```
func encodeRestriction(in idRangeRestriction) ([]byte, error) {
return encodeBSON(in)
}
func decodeRestriction(in []byte) (idRangeRestriction, error) {
return decodeBSON[idRangeRestriction](in)
}

func encodeRange(in idRange) ([]byte, error) {
return encodeBSON(in)
}
func decodeRange(in []byte) (idRange, error) {
return decodeBSON[idRange](in)
}
```

This issue is to track and add a more permanent resilient fix into Beam itself.

Likely in the reflectx and funcx packages and methods, but in particular for when wrapping a CustomCoder here. https://github.com/apache/beam/blob/master/sdks/go/pkg/beam/core/graph/coder/coder.go#L137

### Issue Priority

Priority: 3 (minor)

### Issue Components

- [ ] Component: Python SDK
- [ ] Component: Java SDK
- [X] Component: Go SDK
- [ ] Component: Typescript SDK
- [ ] Component: IO connector
- [ ] Component: Beam examples
- [ ] Component: Beam playground
- [ ] Component: Beam katas
- [ ] Component: Website
- [ ] Component: Spark Runner
- [ ] Component: Flink Runner
- [ ] Component: Samza Runner
- [ ] Component: Twister2 Runner
- [ ] Component: Hazelcast Jet Runner
- [ ] Component: Google Cloud Dataflow Runner

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.