You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by tg...@apache.org on 2017/03/25 00:47:04 UTC
[1/4] beam git commit: This closes #2325
Repository: beam
Updated Branches:
refs/heads/master 2723d38a6 -> e5f1a6479
This closes #2325
Project: http://git-wip-us.apache.org/repos/asf/beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/e5f1a647
Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/e5f1a647
Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/e5f1a647
Branch: refs/heads/master
Commit: e5f1a6479c0a7c6f531c340fe371e4b66e7ea2a5
Parents: 2723d38 6bfde5d
Author: Thomas Groh <tg...@google.com>
Authored: Fri Mar 24 17:46:53 2017 -0700
Committer: Thomas Groh <tg...@google.com>
Committed: Fri Mar 24 17:46:53 2017 -0700
----------------------------------------------------------------------
.../DeduplicatedFlattenFactory.java | 109 ++++++++++++++++++
.../EmptyFlattenAsCreateFactory.java | 15 ++-
.../core/construction/PTransformMatchers.java | 26 +++++
.../DeduplicatedFlattenFactoryTest.java | 112 +++++++++++++++++++
.../EmptyFlattenAsCreateFactoryTest.java | 96 ++++++++++++++++
.../construction/PTransformMatchersTest.java | 67 +++++++++++
runners/google-cloud-dataflow-java/pom.xml | 3 -
.../beam/runners/dataflow/DataflowRunner.java | 5 +-
8 files changed, 427 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
[2/4] beam git commit: Explicitly duplicate input PCollections in
Dataflow
Posted by tg...@apache.org.
Explicitly duplicate input PCollections in Dataflow
Project: http://git-wip-us.apache.org/repos/asf/beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/54a7de13
Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/54a7de13
Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/54a7de13
Branch: refs/heads/master
Commit: 54a7de13dc36cf3894cb1d34f4e3869a550e801a
Parents: 0f91068
Author: Thomas Groh <tg...@google.com>
Authored: Fri Mar 24 15:14:27 2017 -0700
Committer: Thomas Groh <tg...@google.com>
Committed: Fri Mar 24 17:46:53 2017 -0700
----------------------------------------------------------------------
.../DeduplicatedFlattenFactory.java | 109 ++++++++++++++++++
.../core/construction/PTransformMatchers.java | 26 +++++
.../DeduplicatedFlattenFactoryTest.java | 112 +++++++++++++++++++
.../construction/PTransformMatchersTest.java | 67 +++++++++++
.../beam/runners/dataflow/DataflowRunner.java | 5 +-
5 files changed, 318 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/beam/blob/54a7de13/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/DeduplicatedFlattenFactory.java
----------------------------------------------------------------------
diff --git a/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/DeduplicatedFlattenFactory.java b/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/DeduplicatedFlattenFactory.java
new file mode 100644
index 0000000..093385e
--- /dev/null
+++ b/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/DeduplicatedFlattenFactory.java
@@ -0,0 +1,109 @@
+/*
+ * 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 org.apache.beam.runners.core.construction;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.runners.PTransformOverrideFactory;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.Flatten;
+import org.apache.beam.sdk.transforms.Flatten.PCollections;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PCollectionList;
+import org.apache.beam.sdk.values.PValue;
+import org.apache.beam.sdk.values.TaggedPValue;
+
+/**
+ * A {@link PTransformOverrideFactory} that will apply a flatten where no element appears in the
+ * input {@link PCollectionList} more than once.
+ */
+public class DeduplicatedFlattenFactory<T>
+ implements PTransformOverrideFactory<
+ PCollectionList<T>, PCollection<T>, Flatten.PCollections<T>> {
+
+ public static <T> DeduplicatedFlattenFactory<T> create() {
+ return new DeduplicatedFlattenFactory<>();
+ }
+
+ private DeduplicatedFlattenFactory() {}
+ @Override
+ public PTransform<PCollectionList<T>, PCollection<T>> getReplacementTransform(
+ PCollections<T> transform) {
+ return new PTransform<PCollectionList<T>, PCollection<T>>() {
+ @Override
+ public PCollection<T> expand(PCollectionList<T> input) {
+ Map<PCollection<T>, Integer> instances = new HashMap<>();
+ for (PCollection<T> pCollection : input.getAll()) {
+ int existing = instances.get(pCollection) == null ? 0 : instances.get(pCollection);
+ instances.put(pCollection, existing + 1);
+ }
+ PCollectionList<T> output = PCollectionList.empty(input.getPipeline());
+ for (Map.Entry<PCollection<T>, Integer> instanceEntry : instances.entrySet()) {
+ if (instanceEntry.getValue().equals(1)) {
+ output = output.and(instanceEntry.getKey());
+ } else {
+ String duplicationName = String.format("Multiply %s", instanceEntry.getKey().getName());
+ PCollection<T> duplicated =
+ instanceEntry
+ .getKey()
+ .apply(duplicationName, ParDo.of(new DuplicateFn<T>(instanceEntry.getValue())));
+ output = output.and(duplicated);
+ }
+ }
+ return output.apply(Flatten.<T>pCollections());
+ }
+ };
+ }
+
+ @Override
+ public PCollectionList<T> getInput(
+ List<TaggedPValue> inputs, Pipeline p) {
+ PCollectionList<T> pCollections = PCollectionList.empty(p);
+ for (TaggedPValue input : inputs) {
+ PCollection<T> pcollection = (PCollection<T>) input.getValue();
+ pCollections = pCollections.and(pcollection);
+ }
+ return pCollections;
+ }
+
+ @Override
+ public Map<PValue, ReplacementOutput> mapOutputs(
+ List<TaggedPValue> outputs, PCollection<T> newOutput) {
+ return ReplacementOutputs.singleton(outputs, newOutput);
+ }
+
+ private static class DuplicateFn<T> extends DoFn<T, T> {
+ private final int numTimes;
+
+ private DuplicateFn(int numTimes) {
+ this.numTimes = numTimes;
+ }
+
+ @ProcessElement
+ public void emitCopies(ProcessContext context) {
+ for (int i = 0; i < numTimes; i++) {
+ context.output(context.element());
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/beam/blob/54a7de13/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/PTransformMatchers.java
----------------------------------------------------------------------
diff --git a/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/PTransformMatchers.java b/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/PTransformMatchers.java
index efcc455..38cf76f 100644
--- a/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/PTransformMatchers.java
+++ b/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/PTransformMatchers.java
@@ -17,6 +17,8 @@
*/
package org.apache.beam.runners.core.construction;
+import java.util.HashSet;
+import java.util.Set;
import org.apache.beam.sdk.annotations.Experimental;
import org.apache.beam.sdk.annotations.Experimental.Kind;
import org.apache.beam.sdk.io.Write;
@@ -30,6 +32,8 @@ import org.apache.beam.sdk.transforms.reflect.DoFnSignature;
import org.apache.beam.sdk.transforms.reflect.DoFnSignature.ProcessElementMethod;
import org.apache.beam.sdk.transforms.reflect.DoFnSignatures;
import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PValue;
+import org.apache.beam.sdk.values.TaggedPValue;
/**
* A {@link PTransformMatcher} that matches {@link PTransform PTransforms} based on the class of the
@@ -178,6 +182,28 @@ public class PTransformMatchers {
};
}
+ /**
+ * A {@link PTransformMatcher} which matches a {@link Flatten.PCollections} which
+ * consumes a single input {@link PCollection} multiple times.
+ */
+ public static PTransformMatcher flattenWithDuplicateInputs() {
+ return new PTransformMatcher() {
+ @Override
+ public boolean matches(AppliedPTransform<?, ?, ?> application) {
+ if (application.getTransform() instanceof Flatten.PCollections) {
+ Set<PValue> observed = new HashSet<>();
+ for (TaggedPValue pvalue : application.getInputs()) {
+ boolean firstInstance = observed.add(pvalue.getValue());
+ if (!firstInstance) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+ };
+ }
+
public static PTransformMatcher writeWithRunnerDeterminedSharding() {
return new PTransformMatcher() {
@Override
http://git-wip-us.apache.org/repos/asf/beam/blob/54a7de13/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/DeduplicatedFlattenFactoryTest.java
----------------------------------------------------------------------
diff --git a/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/DeduplicatedFlattenFactoryTest.java b/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/DeduplicatedFlattenFactoryTest.java
new file mode 100644
index 0000000..a251f5a
--- /dev/null
+++ b/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/DeduplicatedFlattenFactoryTest.java
@@ -0,0 +1,112 @@
+/*
+ * 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 org.apache.beam.runners.core.construction;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertThat;
+
+import com.google.common.collect.Iterables;
+import org.apache.beam.sdk.Pipeline.PipelineVisitor.Defaults;
+import org.apache.beam.sdk.runners.PTransformOverrideFactory.ReplacementOutput;
+import org.apache.beam.sdk.runners.TransformHierarchy;
+import org.apache.beam.sdk.testing.NeedsRunner;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.transforms.Flatten;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PCollectionList;
+import org.apache.beam.sdk.values.PValue;
+import org.hamcrest.Matchers;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Tests for {@link DeduplicatedFlattenFactory}.
+ */
+@RunWith(JUnit4.class)
+public class DeduplicatedFlattenFactoryTest {
+ @Rule public TestPipeline pipeline = TestPipeline.create();
+
+ private PCollection<String> first = pipeline.apply("FirstCreate", Create.of("one"));
+ private PCollection<String> second = pipeline.apply("SecondCreate", Create.of("two"));
+ private DeduplicatedFlattenFactory<String> factory = DeduplicatedFlattenFactory.create();
+
+ @Test
+ public void duplicatesInsertsMultipliers() {
+ PTransform<PCollectionList<String>, PCollection<String>> replacement =
+ factory.getReplacementTransform(Flatten.<String>pCollections());
+ final PCollectionList<String> inputList =
+ PCollectionList.of(first).and(second).and(first).and(first);
+ inputList.apply(replacement);
+ pipeline.traverseTopologically(
+ new Defaults() {
+ @Override
+ public void visitPrimitiveTransform(TransformHierarchy.Node node) {
+ if (node.getTransform() instanceof Flatten.PCollections) {
+ assertThat(node.getInputs(), not(equalTo(inputList.expand())));
+ }
+ }
+ });
+ }
+
+ @Test
+ @Category(NeedsRunner.class)
+ public void testOverride() {
+ PTransform<PCollectionList<String>, PCollection<String>> replacement =
+ factory.getReplacementTransform(Flatten.<String>pCollections());
+ final PCollectionList<String> inputList =
+ PCollectionList.of(first).and(second).and(first).and(first);
+ PCollection<String> flattened = inputList.apply(replacement);
+
+ PAssert.that(flattened).containsInAnyOrder("one", "two", "one", "one");
+ pipeline.run();
+ }
+
+ @Test
+ public void inputReconstruction() {
+ final PCollectionList<String> inputList =
+ PCollectionList.of(first).and(second).and(first).and(first);
+
+ assertThat(factory.getInput(inputList.expand(), pipeline), equalTo(inputList));
+ }
+
+ @Test
+ public void outputMapping() {
+ final PCollectionList<String> inputList =
+ PCollectionList.of(first).and(second).and(first).and(first);
+ PCollection<String> original =
+ inputList.apply(Flatten.<String>pCollections());
+ PCollection<String> replacement =
+ inputList.apply(factory.getReplacementTransform(Flatten.<String>pCollections()));
+
+ assertThat(
+ factory.mapOutputs(original.expand(), replacement),
+ Matchers.<PValue, ReplacementOutput>hasEntry(
+ replacement,
+ ReplacementOutput.of(
+ Iterables.getOnlyElement(original.expand()),
+ Iterables.getOnlyElement(replacement.expand()))));
+ }
+}
http://git-wip-us.apache.org/repos/asf/beam/blob/54a7de13/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/PTransformMatchersTest.java
----------------------------------------------------------------------
diff --git a/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/PTransformMatchersTest.java b/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/PTransformMatchersTest.java
index 491c14f..0fead17 100644
--- a/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/PTransformMatchersTest.java
+++ b/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/PTransformMatchersTest.java
@@ -24,6 +24,7 @@ import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
import java.io.Serializable;
import java.util.Collections;
import org.apache.beam.sdk.coders.VarIntCoder;
@@ -386,6 +387,72 @@ public class PTransformMatchersTest implements Serializable {
}
@Test
+ public void flattenWithDuplicateInputsWithoutDuplicates() {
+ AppliedPTransform application =
+ AppliedPTransform
+ .<PCollectionList<Object>, PCollection<Object>, Flatten.PCollections<Object>>
+ of(
+ "Flatten",
+ Collections.singletonList(
+ TaggedPValue.of(
+ new TupleTag<Object>(),
+ PCollection.createPrimitiveOutputInternal(
+ p, WindowingStrategy.globalDefault(), IsBounded.BOUNDED))),
+ Collections.singletonList(
+ TaggedPValue.of(
+ new TupleTag<Object>(),
+ PCollection.createPrimitiveOutputInternal(
+ p, WindowingStrategy.globalDefault(), IsBounded.BOUNDED))),
+ Flatten.pCollections(),
+ p);
+
+ assertThat(PTransformMatchers.flattenWithDuplicateInputs().matches(application), is(false));
+ }
+
+ @Test
+ public void flattenWithDuplicateInputsWithDuplicates() {
+ PCollection<Object> duplicate = PCollection.createPrimitiveOutputInternal(p,
+ WindowingStrategy.globalDefault(),
+ IsBounded.BOUNDED);
+ AppliedPTransform application =
+ AppliedPTransform
+ .<PCollectionList<Object>, PCollection<Object>, Flatten.PCollections<Object>>of(
+ "Flatten",
+ ImmutableList.of(
+ TaggedPValue.of(new TupleTag<Object>(), duplicate),
+ TaggedPValue.of(new TupleTag<Object>(), duplicate)),
+ Collections.singletonList(
+ TaggedPValue.of(
+ new TupleTag<Object>(),
+ PCollection.createPrimitiveOutputInternal(
+ p, WindowingStrategy.globalDefault(), IsBounded.BOUNDED))),
+ Flatten.pCollections(),
+ p);
+
+ assertThat(PTransformMatchers.flattenWithDuplicateInputs().matches(application), is(true));
+ }
+
+ @Test
+ public void flattenWithDuplicateInputsNonFlatten() {
+ AppliedPTransform application =
+ AppliedPTransform
+ .<PCollection<Iterable<Object>>, PCollection<Object>, Flatten.Iterables<Object>>
+ of(
+ "EmptyFlatten",
+ Collections.<TaggedPValue>emptyList(),
+ Collections.singletonList(
+ TaggedPValue.of(
+ new TupleTag<Object>(),
+ PCollection.createPrimitiveOutputInternal(
+ p, WindowingStrategy.globalDefault(), IsBounded.BOUNDED))),
+ Flatten.iterables() /* This isn't actually possible to construct,
+ * but for the sake of example */,
+ p);
+
+ assertThat(PTransformMatchers.flattenWithDuplicateInputs().matches(application), is(false));
+ }
+
+ @Test
public void writeWithRunnerDeterminedSharding() {
Write<Integer> write =
Write.to(
http://git-wip-us.apache.org/repos/asf/beam/blob/54a7de13/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
----------------------------------------------------------------------
diff --git a/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java b/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
index e2ab5c2..24e23da 100644
--- a/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
+++ b/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
@@ -58,6 +58,7 @@ import java.util.Random;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
+import org.apache.beam.runners.core.construction.DeduplicatedFlattenFactory;
import org.apache.beam.runners.core.construction.EmptyFlattenAsCreateFactory;
import org.apache.beam.runners.core.construction.PTransformMatchers;
import org.apache.beam.runners.core.construction.ReplacementOutputs;
@@ -294,7 +295,9 @@ public class DataflowRunner extends PipelineRunner<DataflowPipelineJob> {
ImmutableMap.builder();
// Create is implemented in terms of a Read, so it must precede the override to Read in
// streaming
- ptoverrides.put(PTransformMatchers.emptyFlatten(), EmptyFlattenAsCreateFactory.instance());
+ ptoverrides
+ .put(PTransformMatchers.flattenWithDuplicateInputs(), DeduplicatedFlattenFactory.create())
+ .put(PTransformMatchers.emptyFlatten(), EmptyFlattenAsCreateFactory.instance());
if (streaming) {
// In streaming mode must use either the custom Pubsub unbounded source/sink or
// defer to Windmill's built-in implementation.
[4/4] beam git commit: Fix EmptyFlattenAsCreateFactory
Posted by tg...@apache.org.
Fix EmptyFlattenAsCreateFactory
The input types of Flatten and Create don't match up, so a composite
must be provided instead of Create directly.
Project: http://git-wip-us.apache.org/repos/asf/beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/0f910688
Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/0f910688
Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/0f910688
Branch: refs/heads/master
Commit: 0f910688cb1d6dcf1bc701fbc4e4124cef190f10
Parents: 2723d38
Author: Thomas Groh <tg...@google.com>
Authored: Fri Mar 24 15:38:20 2017 -0700
Committer: Thomas Groh <tg...@google.com>
Committed: Fri Mar 24 17:46:53 2017 -0700
----------------------------------------------------------------------
.../EmptyFlattenAsCreateFactory.java | 15 ++-
.../EmptyFlattenAsCreateFactoryTest.java | 96 ++++++++++++++++++++
2 files changed, 109 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/beam/blob/0f910688/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/EmptyFlattenAsCreateFactory.java
----------------------------------------------------------------------
diff --git a/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/EmptyFlattenAsCreateFactory.java b/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/EmptyFlattenAsCreateFactory.java
index 0168039..4328cf3 100644
--- a/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/EmptyFlattenAsCreateFactory.java
+++ b/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/EmptyFlattenAsCreateFactory.java
@@ -52,14 +52,17 @@ public class EmptyFlattenAsCreateFactory<T>
@Override
public PTransform<PCollectionList<T>, PCollection<T>> getReplacementTransform(
Flatten.PCollections<T> transform) {
- return (PTransform) Create.empty(VoidCoder.of());
+ return new CreateEmptyFromList<>();
}
@Override
public PCollectionList<T> getInput(
List<TaggedPValue> inputs, Pipeline p) {
checkArgument(
- inputs.isEmpty(), "Must have an empty input to use %s", getClass().getSimpleName());
+ inputs.isEmpty(),
+ "Unexpected nonempty input %s for %s",
+ inputs,
+ getClass().getSimpleName());
return PCollectionList.empty(p);
}
@@ -68,4 +71,12 @@ public class EmptyFlattenAsCreateFactory<T>
List<TaggedPValue> outputs, PCollection<T> newOutput) {
return ReplacementOutputs.singleton(outputs, newOutput);
}
+
+ private static class CreateEmptyFromList<T>
+ extends PTransform<PCollectionList<T>, PCollection<T>> {
+ @Override
+ public PCollection<T> expand(PCollectionList<T> input) {
+ return (PCollection) input.getPipeline().apply(Create.empty(VoidCoder.of()));
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/beam/blob/0f910688/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/EmptyFlattenAsCreateFactoryTest.java
----------------------------------------------------------------------
diff --git a/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/EmptyFlattenAsCreateFactoryTest.java b/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/EmptyFlattenAsCreateFactoryTest.java
new file mode 100644
index 0000000..ad9d908
--- /dev/null
+++ b/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/EmptyFlattenAsCreateFactoryTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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 org.apache.beam.runners.core.construction;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import com.google.common.collect.Iterables;
+import java.util.Collections;
+import java.util.Map;
+import org.apache.beam.sdk.io.CountingInput;
+import org.apache.beam.sdk.runners.PTransformOverrideFactory.ReplacementOutput;
+import org.apache.beam.sdk.testing.NeedsRunner;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Flatten;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PCollectionList;
+import org.apache.beam.sdk.values.PValue;
+import org.apache.beam.sdk.values.TaggedPValue;
+import org.hamcrest.Matchers;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Tests for {@link EmptyFlattenAsCreateFactory}.
+ */
+@RunWith(JUnit4.class)
+public class EmptyFlattenAsCreateFactoryTest {
+ @Rule public TestPipeline pipeline = TestPipeline.create();
+ @Rule public ExpectedException thrown = ExpectedException.none();
+
+ private EmptyFlattenAsCreateFactory<Long> factory = EmptyFlattenAsCreateFactory.instance();
+
+ @Test
+ public void getInputEmptySucceeds() {
+ assertThat(
+ factory.getInput(Collections.<TaggedPValue>emptyList(), pipeline).size(), equalTo(0));
+ }
+
+ @Test
+ public void getInputNonEmptyThrows() {
+ PCollectionList<Long> nonEmpty =
+ PCollectionList.of(pipeline.apply(CountingInput.unbounded()))
+ .and(pipeline.apply(CountingInput.upTo(100L)));
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage(nonEmpty.expand().toString());
+ thrown.expectMessage(EmptyFlattenAsCreateFactory.class.getSimpleName());
+ factory.getInput(nonEmpty.expand(), pipeline);
+ }
+
+ @Test
+ public void mapOutputsSucceeds() {
+ PCollection<Long> original = pipeline.apply("Original", CountingInput.unbounded());
+ PCollection<Long> replacement = pipeline.apply("Replacement", CountingInput.unbounded());
+ Map<PValue, ReplacementOutput> mapping = factory.mapOutputs(original.expand(), replacement);
+
+ assertThat(
+ mapping,
+ Matchers.<PValue, ReplacementOutput>hasEntry(
+ replacement,
+ ReplacementOutput.of(
+ Iterables.getOnlyElement(original.expand()),
+ Iterables.getOnlyElement(replacement.expand()))));
+ }
+
+ @Test
+ @Category(NeedsRunner.class)
+ public void testOverride() {
+ PCollectionList<Long> empty = PCollectionList.empty(pipeline);
+ PCollection<Long> emptyFlattened =
+ empty.apply(factory.getReplacementTransform(Flatten.<Long>pCollections()));
+ PAssert.that(emptyFlattened).empty();
+ pipeline.run();
+ }
+}
[3/4] beam git commit: Reenable Flatten Tests in the Dataflow Runner
Posted by tg...@apache.org.
Reenable Flatten Tests in the Dataflow Runner
Project: http://git-wip-us.apache.org/repos/asf/beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/6bfde5dc
Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/6bfde5dc
Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/6bfde5dc
Branch: refs/heads/master
Commit: 6bfde5dce82ef6ae7bda6c33832bb5e4a55dfafb
Parents: 54a7de1
Author: Thomas Groh <tg...@google.com>
Authored: Fri Mar 24 14:45:08 2017 -0700
Committer: Thomas Groh <tg...@google.com>
Committed: Fri Mar 24 17:46:53 2017 -0700
----------------------------------------------------------------------
runners/google-cloud-dataflow-java/pom.xml | 3 ---
1 file changed, 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/beam/blob/6bfde5dc/runners/google-cloud-dataflow-java/pom.xml
----------------------------------------------------------------------
diff --git a/runners/google-cloud-dataflow-java/pom.xml b/runners/google-cloud-dataflow-java/pom.xml
index f72cc05..caa000f 100644
--- a/runners/google-cloud-dataflow-java/pom.xml
+++ b/runners/google-cloud-dataflow-java/pom.xml
@@ -87,9 +87,6 @@
org.apache.beam.sdk.testing.UsesSplittableParDo,
org.apache.beam.sdk.testing.UsesUnboundedPCollections,
</excludedGroups>
- <excludes>
- <exclude>org.apache.beam.sdk.transforms.FlattenTest</exclude>
- </excludes>
</configuration>
</execution>
</executions>