diff --git a/sdks/python/apache_beam/io/iobase.py b/sdks/python/apache_beam/io/iobase.py index 34ee781d87a1..fe997b0caa26 100644 --- a/sdks/python/apache_beam/io/iobase.py +++ b/sdks/python/apache_beam/io/iobase.py @@ -1189,9 +1189,12 @@ def _expand_bounded(self, pcoll, min_shards): keyed_pcoll = pcoll | core.Map(lambda x: (None, x)) else: keyed_pcoll = pcoll | core.ParDo(_RoundRobinKeyFn(), count=min_shards) + # pcoll.windowing checks pcoll.producer which may be absent when expand() + # is called directly (e.g. in tests) + if pcoll.producer is None or not keyed_pcoll.windowing.is_default(): + keyed_pcoll = keyed_pcoll | core.WindowInto(window.GlobalWindows()) write_result_coll = ( keyed_pcoll - | core.WindowInto(window.GlobalWindows()) | core.GroupByKey() | 'WriteBundles' >> core.ParDo( _WriteKeyedBundleDoFn(self.sink), AsSingleton(init_result_coll))) @@ -1200,9 +1203,10 @@ def _expand_bounded(self, pcoll, min_shards): "*** WriteImpl min_shards undef so it's 1, and we write per Bundle") min_shards = 1 + if pcoll.producer is None or not pcoll.windowing.is_default(): + pcoll = pcoll | core.WindowInto(window.GlobalWindows()) 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))