diff --git a/sdks/python/apache_beam/transforms/window.py b/sdks/python/apache_beam/transforms/window.py index 1a0c3007b4f1..e405f8b7db23 100644 --- a/sdks/python/apache_beam/transforms/window.py +++ b/sdks/python/apache_beam/transforms/window.py @@ -474,6 +474,8 @@ def __init__( ): if size <= 0: raise ValueError('The size parameter must be strictly positive.') + if period <= 0: + raise ValueError('The period parameter must be strictly positive.') self.size = Duration.of(size) self.period = Duration.of(period) self.offset = Timestamp.of(offset) % period diff --git a/sdks/python/apache_beam/transforms/window_test.py b/sdks/python/apache_beam/transforms/window_test.py index 9c3ee11e4a2e..d13a9a78d765 100644 --- a/sdks/python/apache_beam/transforms/window_test.py +++ b/sdks/python/apache_beam/transforms/window_test.py @@ -137,6 +137,14 @@ def test_sliding_windows_assignment_fraction_large_offset(self): self.assertEqual([IntervalWindow(4.0, 7.5), IntervalWindow(1.5, 5.0)], windowfn.assign(context('v', 4.5))) + def test_sliding_windows_non_positive_period(self): + # Control: the sibling window function already rejects a non-positive size. + with self.assertRaises(ValueError): + FixedWindows(size=0) + for period in (0, -5): + with self.assertRaisesRegex(ValueError, 'period'): + SlidingWindows(size=10, period=period) + def test_sessions_merging(self): windowfn = Sessions(10)