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 2020/07/20 06:30:14 UTC

[GitHub] [beam] amaliujia commented on a change in pull request #12313: Implement navigation functions

amaliujia commented on a change in pull request #12313:
URL: https://github.com/apache/beam/pull/12313#discussion_r457094323



##########
File path: sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/transform/BeamBuiltinAnalyticFunctions.java
##########
@@ -42,4 +44,77 @@
     throw new UnsupportedOperationException(
         String.format("Analytics Function [%s] is not supported", functionName));
   }
+
+  public static <T> Combine.CombineFn<T, ?, T> navigationFirstValue() {
+    return new FirstValueCombineFn();
+  }
+
+  public static <T> Combine.CombineFn<T, ?, T> navigationLastValue() {
+    return new LastValueCombineFn();
+  }
+
+  private static class FirstValueCombineFn<T> extends Combine.CombineFn<T, Optional<T>, T> {
+    private FirstValueCombineFn() {}
+
+    @Override
+    public Optional<T> createAccumulator() {
+      return Optional.empty();
+    }
+
+    @Override
+    public Optional<T> addInput(Optional<T> accumulator, T input) {
+      Optional<T> r = accumulator;
+      if (!accumulator.isPresent()) {
+        r = Optional.of(input);
+      }
+      return r;
+    }
+
+    @Override
+    public Optional<T> mergeAccumulators(Iterable<Optional<T>> accumulators) {
+      Optional<T> r = Optional.empty();
+      for (Optional<T> ac : accumulators) {
+        if (!r.isPresent() && ac.isPresent()) {
+          r = ac;

Review comment:
       you can optimize this for loop by adding a break, because `r.isPresent()` will be always true after first `r=ac`.




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

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