You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/12/07 20:40:32 UTC

[GitHub] [beam] ibzib commented on a change in pull request #16113: [BEAM-12976] Implement pipeline visitor to get global field access in…

ibzib commented on a change in pull request #16113:
URL: https://github.com/apache/beam/pull/16113#discussion_r764345354



##########
File path: runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/graph/FieldAccessVisitor.java
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.graph;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.sdk.Pipeline.PipelineVisitor;
+import org.apache.beam.sdk.runners.TransformHierarchy.Node;
+import org.apache.beam.sdk.schemas.FieldAccessDescriptor;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.ParDo.MultiOutput;
+import org.apache.beam.sdk.util.Preconditions;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TupleTag;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables;
+
+/** Computes which Schema fields are (or conversely, are not) accessed in a pipeline. */
+class FieldAccessVisitor extends PipelineVisitor.Defaults {
+  private final Map<String, FieldAccessDescriptor> pCollectionFieldAccess = new HashMap<>();
+
+  /**
+   * Returns a map keyed by the {@link TupleTag} ID referencing a PCollection. Values are the set of
+   * fields accessed on that PCollection.
+   */
+  ImmutableMap<String, FieldAccessDescriptor> getPCollectionFieldAccess() {
+    return ImmutableMap.copyOf(pCollectionFieldAccess);
+  }
+
+  @Override
+  public void visitPrimitiveTransform(Node node) {
+    Map<String, FieldAccessDescriptor> currentFieldAccess = getFieldAccess(node);
+    for (Entry<String, FieldAccessDescriptor> entry : currentFieldAccess.entrySet()) {
+      FieldAccessDescriptor previousFieldAccess = pCollectionFieldAccess.get(entry.getKey());
+      FieldAccessDescriptor newFieldAccess =
+          previousFieldAccess == null
+              ? entry.getValue()
+              : FieldAccessDescriptor.union(
+                  ImmutableList.of(previousFieldAccess, entry.getValue()));
+      pCollectionFieldAccess.put(entry.getKey(), newFieldAccess);
+    }
+  }
+
+  private static Map<String, FieldAccessDescriptor> getFieldAccess(Node node) {
+    PTransform<?, ?> transform = node.getTransform();
+    HashMap<String, FieldAccessDescriptor> access = new HashMap<>();
+
+    if (transform instanceof MultiOutput) {
+      DoFn<?, ?> fn = ((MultiOutput<?, ?>) transform).getFn();
+      Pair<TupleTag<?>, PCollection<?>> mainInput = getMainInputTagId(node);
+      FieldAccessDescriptor fields =
+          ParDo.getDoFnSchemaInformation(fn, mainInput.getRight()).getFieldAccessDescriptor();
+      access.put(mainInput.getLeft().getId(), fields);
+    }
+
+    // For every input without field access info, we must assume all fields need to be accessed.
+    for (TupleTag<?> tag : node.getInputs().keySet()) {
+      if (!access.containsKey(tag.getId())) {
+        access.put(tag.getId(), FieldAccessDescriptor.withAllFields());
+      }
+    }
+
+    return ImmutableMap.copyOf(access);
+  }
+
+  private static Pair<TupleTag<?>, PCollection<?>> getMainInputTagId(Node node) {
+    HashSet<TupleTag<?>> mainInputTags = new HashSet<>(node.getInputs().keySet());

Review comment:
       It was only mainInputTags after subtracting additional input tags. I refactored this a bit.

##########
File path: runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/graph/FieldAccessVisitor.java
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.graph;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.sdk.Pipeline.PipelineVisitor;
+import org.apache.beam.sdk.runners.TransformHierarchy.Node;
+import org.apache.beam.sdk.schemas.FieldAccessDescriptor;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.ParDo.MultiOutput;
+import org.apache.beam.sdk.util.Preconditions;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TupleTag;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables;
+
+/** Computes which Schema fields are (or conversely, are not) accessed in a pipeline. */
+class FieldAccessVisitor extends PipelineVisitor.Defaults {
+  private final Map<String, FieldAccessDescriptor> pCollectionFieldAccess = new HashMap<>();
+
+  /**
+   * Returns a map keyed by the {@link TupleTag} ID referencing a PCollection. Values are the set of
+   * fields accessed on that PCollection.
+   */
+  ImmutableMap<String, FieldAccessDescriptor> getPCollectionFieldAccess() {
+    return ImmutableMap.copyOf(pCollectionFieldAccess);
+  }
+
+  @Override
+  public void visitPrimitiveTransform(Node node) {
+    Map<String, FieldAccessDescriptor> currentFieldAccess = getFieldAccess(node);
+    for (Entry<String, FieldAccessDescriptor> entry : currentFieldAccess.entrySet()) {
+      FieldAccessDescriptor previousFieldAccess = pCollectionFieldAccess.get(entry.getKey());
+      FieldAccessDescriptor newFieldAccess =
+          previousFieldAccess == null
+              ? entry.getValue()
+              : FieldAccessDescriptor.union(
+                  ImmutableList.of(previousFieldAccess, entry.getValue()));
+      pCollectionFieldAccess.put(entry.getKey(), newFieldAccess);
+    }
+  }
+
+  private static Map<String, FieldAccessDescriptor> getFieldAccess(Node node) {
+    PTransform<?, ?> transform = node.getTransform();
+    HashMap<String, FieldAccessDescriptor> access = new HashMap<>();
+
+    if (transform instanceof MultiOutput) {
+      DoFn<?, ?> fn = ((MultiOutput<?, ?>) transform).getFn();
+      Pair<TupleTag<?>, PCollection<?>> mainInput = getMainInputTagId(node);
+      FieldAccessDescriptor fields =
+          ParDo.getDoFnSchemaInformation(fn, mainInput.getRight()).getFieldAccessDescriptor();
+      access.put(mainInput.getLeft().getId(), fields);
+    }
+
+    // For every input without field access info, we must assume all fields need to be accessed.
+    for (TupleTag<?> tag : node.getInputs().keySet()) {
+      if (!access.containsKey(tag.getId())) {
+        access.put(tag.getId(), FieldAccessDescriptor.withAllFields());
+      }
+    }
+
+    return ImmutableMap.copyOf(access);
+  }
+
+  private static Pair<TupleTag<?>, PCollection<?>> getMainInputTagId(Node node) {

Review comment:
       Removed per your other comment.

##########
File path: runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/graph/FieldAccessVisitor.java
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.graph;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.sdk.Pipeline.PipelineVisitor;
+import org.apache.beam.sdk.runners.TransformHierarchy.Node;
+import org.apache.beam.sdk.schemas.FieldAccessDescriptor;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.ParDo.MultiOutput;
+import org.apache.beam.sdk.util.Preconditions;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TupleTag;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables;
+
+/** Computes which Schema fields are (or conversely, are not) accessed in a pipeline. */
+class FieldAccessVisitor extends PipelineVisitor.Defaults {
+  private final Map<String, FieldAccessDescriptor> pCollectionFieldAccess = new HashMap<>();
+
+  /**
+   * Returns a map keyed by the {@link TupleTag} ID referencing a PCollection. Values are the set of
+   * fields accessed on that PCollection.
+   */
+  ImmutableMap<String, FieldAccessDescriptor> getPCollectionFieldAccess() {
+    return ImmutableMap.copyOf(pCollectionFieldAccess);
+  }
+
+  @Override
+  public void visitPrimitiveTransform(Node node) {
+    Map<String, FieldAccessDescriptor> currentFieldAccess = getFieldAccess(node);
+    for (Entry<String, FieldAccessDescriptor> entry : currentFieldAccess.entrySet()) {
+      FieldAccessDescriptor previousFieldAccess = pCollectionFieldAccess.get(entry.getKey());
+      FieldAccessDescriptor newFieldAccess =
+          previousFieldAccess == null
+              ? entry.getValue()
+              : FieldAccessDescriptor.union(
+                  ImmutableList.of(previousFieldAccess, entry.getValue()));
+      pCollectionFieldAccess.put(entry.getKey(), newFieldAccess);
+    }
+  }
+
+  private static Map<String, FieldAccessDescriptor> getFieldAccess(Node node) {
+    PTransform<?, ?> transform = node.getTransform();
+    HashMap<String, FieldAccessDescriptor> access = new HashMap<>();
+
+    if (transform instanceof MultiOutput) {
+      DoFn<?, ?> fn = ((MultiOutput<?, ?>) transform).getFn();
+      Pair<TupleTag<?>, PCollection<?>> mainInput = getMainInputTagId(node);

Review comment:
       Done.

##########
File path: runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/graph/FieldAccessVisitor.java
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.graph;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.sdk.Pipeline.PipelineVisitor;
+import org.apache.beam.sdk.runners.TransformHierarchy.Node;
+import org.apache.beam.sdk.schemas.FieldAccessDescriptor;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.ParDo.MultiOutput;
+import org.apache.beam.sdk.util.Preconditions;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TupleTag;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables;
+
+/** Computes which Schema fields are (or conversely, are not) accessed in a pipeline. */
+class FieldAccessVisitor extends PipelineVisitor.Defaults {
+  private final Map<String, FieldAccessDescriptor> pCollectionFieldAccess = new HashMap<>();
+
+  /**
+   * Returns a map keyed by the {@link TupleTag} ID referencing a PCollection. Values are the set of
+   * fields accessed on that PCollection.
+   */
+  ImmutableMap<String, FieldAccessDescriptor> getPCollectionFieldAccess() {
+    return ImmutableMap.copyOf(pCollectionFieldAccess);
+  }
+
+  @Override
+  public void visitPrimitiveTransform(Node node) {
+    Map<String, FieldAccessDescriptor> currentFieldAccess = getFieldAccess(node);
+    for (Entry<String, FieldAccessDescriptor> entry : currentFieldAccess.entrySet()) {
+      FieldAccessDescriptor previousFieldAccess = pCollectionFieldAccess.get(entry.getKey());
+      FieldAccessDescriptor newFieldAccess =
+          previousFieldAccess == null
+              ? entry.getValue()
+              : FieldAccessDescriptor.union(
+                  ImmutableList.of(previousFieldAccess, entry.getValue()));
+      pCollectionFieldAccess.put(entry.getKey(), newFieldAccess);
+    }
+  }
+
+  private static Map<String, FieldAccessDescriptor> getFieldAccess(Node node) {
+    PTransform<?, ?> transform = node.getTransform();
+    HashMap<String, FieldAccessDescriptor> access = new HashMap<>();
+
+    if (transform instanceof MultiOutput) {
+      DoFn<?, ?> fn = ((MultiOutput<?, ?>) transform).getFn();
+      Pair<TupleTag<?>, PCollection<?>> mainInput = getMainInputTagId(node);
+      FieldAccessDescriptor fields =
+          ParDo.getDoFnSchemaInformation(fn, mainInput.getRight()).getFieldAccessDescriptor();

Review comment:
       getFieldAccessDescriptor returns a vacuous "all fields" for DoFns without actual schema information, so I copied that convention. I added a test case with no schemas.

##########
File path: runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/graph/FieldAccessVisitor.java
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.graph;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.sdk.Pipeline.PipelineVisitor;
+import org.apache.beam.sdk.runners.TransformHierarchy.Node;
+import org.apache.beam.sdk.schemas.FieldAccessDescriptor;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.ParDo.MultiOutput;
+import org.apache.beam.sdk.util.Preconditions;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TupleTag;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables;
+
+/** Computes which Schema fields are (or conversely, are not) accessed in a pipeline. */
+class FieldAccessVisitor extends PipelineVisitor.Defaults {
+  private final Map<String, FieldAccessDescriptor> pCollectionFieldAccess = new HashMap<>();

Review comment:
       Done.




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

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