Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull;

import java.io.Serializable;
import javax.annotation.CheckForNull;
import org.apache.beam.sdk.coders.CannotProvideCoderException;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.coders.CoderRegistry;
import org.apache.beam.sdk.coders.KvCoder;
import org.apache.beam.sdk.schemas.NoSuchSchemaException;
import org.apache.beam.sdk.schemas.SchemaCoder;
import org.apache.beam.sdk.schemas.SchemaRegistry;
import org.apache.beam.sdk.coders.SerializableCoder;
import org.apache.beam.sdk.coders.VarIntCoder;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.TypeDescriptor;
Expand Down Expand Up @@ -124,31 +124,27 @@ public KV<K, V> apply(V element) {
try {
Coder<K> keyCoder;
CoderRegistry coderRegistry = in.getPipeline().getCoderRegistry();

if (keyType == null) {
keyCoder = coderRegistry.getOutputCoder(fn, in.getCoder());
try {
keyCoder = coderRegistry.getOutputCoder(fn, in.getCoder());
} catch (CannotProvideCoderException e) {
// fallback for lambda (Integer output)
keyCoder = (Coder<K>) VarIntCoder.of();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we assume an int here?

}
} else {
keyCoder = coderRegistry.getCoder(keyType);
}
// TODO: Remove when we can set the coder inference context.
result.setCoder(KvCoder.of(keyCoder, in.getCoder()));

result.setCoder(
KvCoder.of((Coder<K>) (Object) SerializableCoder.of(Serializable.class), in.getCoder()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we change this?


} catch (CannotProvideCoderException exc) {
if (keyType != null) {
try {
SchemaRegistry schemaRegistry = SchemaRegistry.createDefault();
SchemaCoder<K> schemaCoder =
SchemaCoder.of(
schemaRegistry.getSchema(keyType),
keyType,
schemaRegistry.getToRowFunction(keyType),
schemaRegistry.getFromRowFunction(keyType));
result.setCoder(KvCoder.of(schemaCoder, in.getCoder()));
} catch (NoSuchSchemaException exception) {
// No Schema.
}
}
// let lazy coder inference have a try
// Fallback: use SerializableCoder. We use a wildcard cast to avoid
// raw type warnings while still allowing the fallback to function.
Coder<K> fallbackCoder = (Coder<K>) (Coder<?>) SerializableCoder.of(Serializable.class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make sense - why would we do this instead of the existing schema inference

result.setCoder(KvCoder.of(fallbackCoder, in.getCoder()));
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
package org.apache.beam.sdk.transforms;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.coders.KvCoder;
import org.apache.beam.sdk.coders.StringUtf8Coder;
Expand Down Expand Up @@ -209,6 +211,22 @@ public void testKeySchemaCoderSet() throws NoSuchSchemaException {
p.run();
}

@Test
public void testKeyCoderInferenceSafe() {
Pipeline p = Pipeline.create();

PCollection<String> input = p.apply(Create.of("a", "bb", "ccc"));

PCollection<KV<Integer, String>> result =
input.apply(
"AddKeys",
WithKeys.of((String s) -> s.length()).withKeyType(TypeDescriptors.integers()));

assertNotNull(result.getCoder());

p.run().waitUntilFinish();
}

@DefaultSchema(JavaBeanSchema.class)
private static class Pojo {
private final long num;
Expand Down
Loading