Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CombineGlobally with GlobalWindows #26922

Merged
merged 15 commits into from
Jul 7, 2023
55 changes: 55 additions & 0 deletions sdks/python/apache_beam/transforms/combine_globally_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
liferoad marked this conversation as resolved.
Show resolved Hide resolved
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Unit tests for our libraries of combine PTransforms."""
# pytype: skip-file

import time
import unittest

import apache_beam as beam
from apache_beam.testing.test_pipeline import TestPipeline
from apache_beam.transforms import trigger
from apache_beam.transforms import window
from apache_beam.transforms.periodicsequence import PeriodicImpulse


class CombineGloballyTest(unittest.TestCase):
liferoad marked this conversation as resolved.
Show resolved Hide resolved
def test_with_periodic_impulse(self):
liferoad marked this conversation as resolved.
Show resolved Hide resolved
# this error is expected since the below combination is ill-defined.
with self.assertRaises(ValueError):
with TestPipeline() as p:
_ = (
p
| PeriodicImpulse(
start_timestamp=time.time(),
stop_timestamp=time.time() + 4,
fire_interval=1,
apply_windowing=False,
)
| beam.Map(lambda x: ('c', 1))
| beam.WindowInto(
window.GlobalWindows(),
trigger=trigger.Repeatedly(trigger.AfterCount(2)),
accumulation_mode=trigger.AccumulationMode.DISCARDING,
)
| beam.combiners.Count.Globally()
liferoad marked this conversation as resolved.
Show resolved Hide resolved
| "Print Windows" >> beam.Map(print))
liferoad marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == '__main__':
unittest.main()
14 changes: 14 additions & 0 deletions sdks/python/apache_beam/transforms/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,18 @@ def add_input_types(transform):
"or CombineGlobally().as_singleton_view() to get the default "
"output of the CombineFn if the input PCollection is empty.")

# return the error for this ill-defined streaming case
liferoad marked this conversation as resolved.
Show resolved Hide resolved
if not pcoll.is_bounded and not pcoll.windowing.is_default():
tvalentyn marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(
liferoad marked this conversation as resolved.
Show resolved Hide resolved
"For unbounded data sources, "
"default values are not yet supported in CombineGlobally() if the "
"output PCollection is not windowed by GlobalWindows"
" with DefaultTrigger. "
liferoad marked this conversation as resolved.
Show resolved Hide resolved
"Instead, use CombineGlobally().without_defaults() to output "
"an empty PCollection if the input PCollection is empty, "
"or CombineGlobally().as_singleton_view() to get the default "
"output of the CombineFn if the input PCollection is empty.")

def typed(transform):
# TODO(robertwb): We should infer this.
if combined.element_type:
Expand All @@ -2598,6 +2610,8 @@ def typed(transform):

def inject_default(_, combined):
if combined:
if len(combined) > 1:
_LOGGER.error('Cannot inject default values with: %s', combined)
liferoad marked this conversation as resolved.
Show resolved Hide resolved
assert len(combined) == 1
return combined[0]
else:
Expand Down