You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "lostluck (via GitHub)" <gi...@apache.org> on 2023/04/01 19:38:54 UTC

[GitHub] [beam] lostluck opened a new issue, #26066: [Bug][Go SDK]: Generic functions alias to same registered name in Go 1.20

lostluck opened a new issue, #26066:
URL: https://github.com/apache/beam/issues/26066

   ### 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


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org