sjvanrossum commented on code in PR #34777: URL: https://github.com/apache/beam/pull/34777#discussion_r2066671305
########## sdks/python/apache_beam/io/iobase.py: ########## @@ -1127,47 +1130,155 @@ def __init__(self, sink: Sink) -> None: self.sink = sink def expand(self, pcoll): - do_once = pcoll.pipeline | 'DoOnce' >> core.Create([None]) - init_result_coll = do_once | 'InitializeWrite' >> core.Map( - lambda _, sink: sink.initialize_write(), self.sink) + if (pcoll.is_bounded): + do_once = pcoll.pipeline | 'DoOnce' >> core.Create([None]) + init_result_coll = do_once | 'InitializeWrite' >> core.Map( + lambda _, sink: sink.initialize_write(), self.sink) if getattr(self.sink, 'num_shards', 0): min_shards = self.sink.num_shards - if min_shards == 1: - keyed_pcoll = pcoll | core.Map(lambda x: (None, x)) - else: - keyed_pcoll = pcoll | core.ParDo(_RoundRobinKeyFn(), count=min_shards) - write_result_coll = ( + + if (pcoll.is_bounded): + if min_shards == 1: + keyed_pcoll = pcoll | core.Map(lambda x: (None, x)) + else: + keyed_pcoll = pcoll | core.ParDo(_RoundRobinKeyFn(), count=min_shards) + write_result_coll = ( + keyed_pcoll + | core.WindowInto(window.GlobalWindows()) + | core.GroupByKey() + | 'WriteBundles' >> core.ParDo( + _WriteKeyedBundleDoFn(self.sink), AsSingleton(init_result_coll))) + else: #unbounded PCollection needes to be written per window + if isinstance(pcoll.windowing.windowfn, window.GlobalWindows): + widowed_pcoll = ( + pcoll + | core.WindowInto(window.FixedWindows(self.sink.triggering_frequency), + trigger=beam.transforms.trigger.AfterWatermark(), + accumulation_mode=beam.transforms.trigger.AccumulationMode.DISCARDING, + allowed_lateness=beam.utils.timestamp.Duration(seconds=0)) + ) + else: #keep user windowing + widowed_pcoll = pcoll + if self.sink.convert_fn is not None: + widowed_pcoll = widowed_pcoll | core.ParDo(self.sink.convert_fn) + if min_shards == 1: + keyed_pcoll = widowed_pcoll | core.Map(lambda x: (None, x)) + else: + keyed_pcoll = widowed_pcoll | core.ParDo(_RoundRobinKeyFn(), count=min_shards) + init_result_window_coll = ( keyed_pcoll - | core.WindowInto(window.GlobalWindows()) - | core.GroupByKey() - | 'WriteBundles' >> core.ParDo( - _WriteKeyedBundleDoFn(self.sink), AsSingleton(init_result_coll))) + | 'Pair init' >> core.Map(lambda x: (None, x)) + | 'Pair init gbk' >> core.GroupByKey() + | 'InitializeWindowedWrite' >> core.Map( + lambda _, sink: sink.initialize_write(), self.sink) + #| 'LogElements init_result_window_coll' >> LogElements(prefix="init_result_window_coll :",with_window=True,with_timestamp=True,level=logging.INFO) + ) + + write_result_coll = ( + keyed_pcoll + | 'Group by random key' >> core.GroupByKey() + #| 'LogElements before WriteWindowedBundles' >> LogElements(prefix="before WriteWindowedBundles :",with_window=True,with_timestamp=True,level=logging.INFO) + | 'WriteWindowedBundles' >> core.ParDo( + _WriteWindowedBundleDoFn(sink=self.sink,per_key=True), + AsSingleton(init_result_window_coll)) + #| 'LogElements' >> LogElements(prefix="after WriteWindowedBundles :",with_window=True,with_timestamp=True,level=logging.INFO) + | 'Pair' >> core.Map(lambda x: (None, x)) + | core.GroupByKey() + | 'Extract' >> core.Map(lambda x: x[1]) + ) + pre_finalized_write_result_coll = ( + write_result_coll + | 'PreFinalize' >> core.ParDo( + _PreFinalizeWindowedBundleDoFn(self.sink), AsSingleton(init_result_window_coll)) + ) + finalized_write_result_coll = ( + pre_finalized_write_result_coll + #| 'LogElements pre_finalized_write_result_coll' >> LogElements(prefix="pre_finalized_write_result_coll :",with_window=True,with_timestamp=True,level=logging.INFO) + | 'FinalizeWrite' >> core.FlatMap( + _finalize_write, + self.sink, + AsSingleton(init_result_window_coll), + AsSingleton(write_result_coll), + min_shards, + AsIter(pre_finalized_write_result_coll) + ).with_output_types(str) + ) + return finalized_write_result_coll else: + _LOGGER.info("*** WriteImpl min_shards undef so it's 1, and we write per Bundle") min_shards = 1 - write_result_coll = ( + if (pcoll.is_bounded): + write_result_coll = ( + pcoll + | core.WindowInto(window.GlobalWindows()) + | 'WriteBundles' >> core.ParDo( + _WriteBundleDoFn(self.sink), AsSingleton(init_result_coll)) + | 'Pair' >> core.Map(lambda x: (None, x)) + | core.GroupByKey() + | 'Extract' >> core.FlatMap(lambda x: x[1])) + else: #unbounded PCollection needes to be written per window + widowed_pcoll = ( pcoll - | core.WindowInto(window.GlobalWindows()) - | 'WriteBundles' >> core.ParDo( - _WriteBundleDoFn(self.sink), AsSingleton(init_result_coll)) - | 'Pair' >> core.Map(lambda x: (None, x)) - | core.GroupByKey() - | 'Extract' >> core.FlatMap(lambda x: x[1])) + | core.WindowInto(window.FixedWindows(self.sink.triggering_frequency), + trigger=beam.transforms.trigger.AfterWatermark(), + accumulation_mode=beam.transforms.trigger.AccumulationMode.DISCARDING, + allowed_lateness=beam.utils.timestamp.Duration(seconds=0)) + ) + init_result_window_coll = ( + widowed_pcoll + | 'Pair init' >> core.Map(lambda x: (None, x)) + | 'Pair init gbk' >> core.GroupByKey() + | 'InitializeWindowedWrite' >> core.Map( + lambda _, sink: sink.initialize_write(), self.sink) + ) + if self.sink.convert_fn is not None: + widowed_pcoll = widowed_pcoll | core.ParDo(self.sink.convert_fn) + write_result_coll = ( + widowed_pcoll + | 'WriteWindowedBundles' >> core.ParDo( + _WriteWindowedBundleDoFn(self.sink), AsSingleton(init_result_window_coll)) + | 'LogElements' >> LogElements(prefix="after WriteWindowedBundles :",with_window=True,with_timestamp=True,level=logging.INFO) + | 'Pair' >> core.Map(lambda x: (None, x)) + | core.GroupByKey() + | 'Extract' >> core.Map(lambda x: x[1]) + ) + pre_finalized_write_result_coll = ( + write_result_coll + | 'PreFinalize' >> core.ParDo( + _PreFinalizeWindowedBundleDoFn(self.sink), AsSingleton(init_result_window_coll)) + ) + finalized_write_result_coll = ( + pre_finalized_write_result_coll + | 'LogElements 2' >> LogElements(prefix="before finalize :",with_window=True,with_timestamp=True,level=logging.INFO) + | 'FinalizeWrite' >> core.FlatMap( + _finalize_write, + self.sink, + AsSingleton(init_result_window_coll), + AsSingleton(write_result_coll), + min_shards, + AsIter(pre_finalized_write_result_coll) + ).with_output_types(str) + ) + return finalized_write_result_coll # PreFinalize should run before FinalizeWrite, and the two should not be # fused. - pre_finalize_coll = ( - do_once - | 'PreFinalize' >> core.FlatMap( - _pre_finalize, - self.sink, - AsSingleton(init_result_coll), - AsIter(write_result_coll))) - return do_once | 'FinalizeWrite' >> core.FlatMap( - _finalize_write, - self.sink, - AsSingleton(init_result_coll), - AsIter(write_result_coll), - min_shards, - AsSingleton(pre_finalize_coll)).with_output_types(str) + if (pcoll.is_bounded): + pre_finalize_coll = ( + do_once + | 'PreFinalize' >> core.FlatMap( + _pre_finalize, + self.sink, + AsSingleton(init_result_coll), + AsIter(write_result_coll))) + return (do_once | 'FinalizeWrite' >> core.FlatMap( + _finalize_write, + self.sink, + AsSingleton(init_result_coll), + AsIter(write_result_coll), + min_shards, + AsSingleton(pre_finalize_coll)).with_output_types(str) + | 'LogElements after FinalizeWrite' >> LogElements(prefix='after FinalizeWrite ', with_window=False,level=logging.INFO) + ) Review Comment: Remove ```suggestion ``` -- 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-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org