adityaRaj369 commented on code in PR #39043:
URL: https://github.com/apache/beam/pull/39043#discussion_r3446937257
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/WithKeys.java:
##########
@@ -110,27 +110,32 @@ public WithKeys<K, V> withKeyType(TypeDescriptor<K>
keyType) {
@Override
public PCollection<KV<K, V>> expand(PCollection<V> in) {
+ TypeDescriptor<V> inputType = in.getTypeDescriptor();
+ TypeDescriptor<KV<K, V>> outputType = getOutputTypeDescriptor(inputType);
PCollection<KV<K, V>> result =
in.apply(
"AddKeys",
- MapElements.via(
- new SimpleFunction<V, KV<K, V>>() {
- @Override
- public KV<K, V> apply(V element) {
- return KV.of(fn.apply(element), element);
- }
- }));
+ outputType == null
+ ? MapElements.via(
+ new SimpleFunction<V, KV<K, V>>() {
+ @Override
+ public KV<K, V> apply(V element) {
+ return KV.of(fn.apply(element), element);
+ }
+ })
+ : MapElements.into(outputType)
+ .via(
+ (SerializableFunction<V, KV<K, V>>)
+ element -> KV.of(fn.apply(element), element)));
Review Comment:
Fixed. The mapping function now uses a local copy of fn so it does not
capture the outer WithKeys instance. I also applied it to both mapping branches
and reran WithKeysTest successfully.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]