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/21 22:52:54 UTC

[GitHub] [beam] amaliujia commented on a change in pull request #12313: [BEAM-10551] Implement Navigation Functions FIRST_VALUE and LAST_VALUE

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



##########
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:
       Ah I see. Yes it's actually called sequentially on the sorted input.
   
   I think you can through an [UnsupportedOperationException](https://docs.oracle.com/javase/7/docs/api/java/lang/UnsupportedOperationException.html) since you will not call mergeAccumulators.
   
   And in the future if there is usage to call `mergeAccumulators`, the exception will be hit and people will know they need to implement it.




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