Skip to content

Validate the SlidingWindows period in the Python SDK - #40925

Open
jayyyyqwq wants to merge 1 commit into
apache:masterfrom
jayyyyqwq:fix/sliding-windows-period
Open

jayyyyqwq wants to merge 1 commit into
apache:masterfrom
jayyyyqwq:fix/sliding-windows-period

Conversation

@jayyyyqwq

@jayyyyqwq jayyyyqwq commented Oct 31, 2026 •

Copy link
Copy Markdown

SlidingWindows.__init__ in the Python SDK checks that size is positive but never checks period.

With period=0 the constructor fails on Timestamp.of(offset) % period with ZeroDivisionError: integer modulo by zero (window.py:479 before this change). With a negative period, for example SlidingWindows(size=10, period=-5), it constructs fine and assign() returns [], because the range(...) in assign steps the wrong way. A pipeline using it drops every element without any error.

FixedWindows(size=0) and Sessions(gap_size=0) already raise ValueError('The size parameter must be strictly positive.'), and the Java SlidingWindows rejects this through its 0 <= offset < period check. I added the matching check for period right after the size check:

if period <= 0:
  raise ValueError('The period parameter must be strictly positive.')

Testing: I added test_sliding_windows_non_positive_period to window_test.py. Before the change it fails with ZeroDivisionError: integer modulo by zero at window.py:479. It also asserts that FixedWindows(size=0) raises ValueError, which passes before and after. After the change window_test.py passes (20 passed), and the sliding or window tests in sideinputs_test.py and trigger_manager_test.py pass (12 passed). In the repo, yaml_provider.py and the Nexmark queries pass period from user or config values. A zero period already failed there, and a negative one produced no windows, so it now fails with the clear error. I did not run combiners_test.py, util_test.py or pipeline_test.py locally, since my venv lacks pyhamcrest and mock, and I did not run the full CI.

SlidingWindows checked that size was positive but not period. A zero period failed with a bare ZeroDivisionError, and a negative period built a window function whose assign() returned no windows, silently dropping every element. Reject a non-positive period with a ValueError, as FixedWindows and Sessions already do for their size.
Copilot AI balanced review requested due to automatic review settings October 31, 2026 11:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions github-actions Bot added python core size/s S: 10-49 lines changed labels Oct 31, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR adds validation to SlidingWindows.__init__ to ensure the period parameter is strictly positive, preventing ZeroDivisionError when period == 0 and silent element dropping when period < 0. The implementation and accompanying unit test are clean, accurate, and consistent with existing parameter validation in FixedWindows and Sessions.

🔍 General Feedback

  • The validation logic and error message match existing conventions across apache_beam/transforms/window.py.
  • Unit tests in window_test.py appropriately cover both zero and negative period values.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @shunping for label python.
Backup reviewer: damccorm (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

This pull request likely touches a core component ("core" label). Please review with scrutiny.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants