[ 
https://issues.apache.org/jira/browse/BEAM-6695?focusedWorklogId=224758&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-224758
 ]

ASF GitHub Bot logged work on BEAM-6695:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 09/Apr/19 03:05
            Start Date: 09/Apr/19 03:05
    Worklog Time Spent: 10m 
      Work Description: ttanay commented on pull request #8206: [BEAM-6695] 
Latest PTransform for Python SDK
URL: https://github.com/apache/beam/pull/8206#discussion_r273310080
 
 

 ##########
 File path: sdks/python/apache_beam/transforms/combiners.py
 ##########
 @@ -858,3 +860,60 @@ def merge_only(self, accumulators):
 
   def extract_only(self, accumulator):
     return self.combine_fn.extract_output(accumulator)
+
+
+class Latest(object):
+  """Combiners for computing the latest element"""
+
+  class Globally(ptransform.PTransform):
+    """Compute the element with the latest timestamp from a
+    PCollection."""
+
+    @staticmethod
+    def add_timestamp(element, timestamp=core.DoFn.TimestampParam):
+      return [(element, timestamp)]
+
+    def expand(self, pcoll):
+      return (pcoll
+              | core.ParDo(self.add_timestamp)
+              | core.CombineGlobally(LatestCombineFn()))
+
+  class PerKey(ptransform.PTransform):
+    """Compute elements with the latest timestamp for each key
+    from a keyed PCollection"""
+
+    @staticmethod
+    def add_timestamp(element, timestamp=core.DoFn.TimestampParam):
+      _check_instance_type(KV[T, T], element)
+      (K, V) = element
+      return [(K, (V, timestamp))]
+
+    def expand(self, pcoll):
+      return (pcoll
+              | core.ParDo(self.add_timestamp)
+              | core.CombinePerKey(LatestCombineFn()))
+
+
+@with_input_types(KV[T, T])
 
 Review comment:
   I agree. It should be a Tuple rather than a KV.
   
   Also, only types int, float, long, Timestamp and Duration can be interpreted 
as timestamps[1] so they can be compared to `window.MIN_TIMESTAMP` in the 
accumulator. So, having the timestamp's type as T would be less restrictive. 
   Does it make sense to create a new type - `TimestampT/TimestampType = 
Union[int, float, long, Timestamp, Duration]` in typehints.py or the combiners 
module? This would make the input type `Tuple[T, TimestampType]`.
   
   [1] 
https://github.com/apache/beam/blob/master/sdks/python/apache_beam/utils/timestamp.py#L71
 
----------------------------------------------------------------
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:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 224758)
    Time Spent: 4h 40m  (was: 4.5h)

> Latest transform for Python SDK
> -------------------------------
>
>                 Key: BEAM-6695
>                 URL: https://issues.apache.org/jira/browse/BEAM-6695
>             Project: Beam
>          Issue Type: New Feature
>          Components: sdk-py-core
>            Reporter: Ahmet Altay
>            Assignee: Tanay Tummalapalli
>            Priority: Minor
>          Time Spent: 4h 40m
>  Remaining Estimate: 0h
>
> Add a PTransform} and Combine.CombineFn for computing the latest element in a 
> PCollection.
> It should offer the same API as its Java counterpart: 
> https://github.com/apache/beam/blob/11a977b8b26eff2274d706541127c19dc93131a2/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Latest.java



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to