Skip to content

[Go SDK] Fix fixed and sliding windows for timestamps before the epoch - #41726

Open
bingqin2 wants to merge 1 commit into
apache:masterfrom
bingqin2:go-window-assignment-before-epoch
Open

bingqin2 wants to merge 1 commit into
apache:masterfrom
bingqin2:go-window-assignment-before-epoch

Conversation

@bingqin2

Copy link
Copy Markdown

Fixes #41725

In the Go SDK, window.NewFixedWindows and window.NewSlidingWindows assigned a timestamp before the epoch to a window that does not contain it: for fixed windows, timestamps more than one window before the epoch; for sliding windows, any timestamp before the epoch that is not a multiple of the period. The issue has examples. This is the Go side of #41601 (Java, PR #41602).

assignWindows in exec/window.go subtracted (ts + size) % size for fixed windows and ts % period for sliding windows. Go's % rounds toward zero, so both were negative there. A new floorMod helper keeps the remainder in [0, n). windowMapper.MapWindow uses assignWindows too, so side input windows are mapped correctly as well.

A timestamp whose current windows contain it keeps the same windows. I checked this outside the tests by comparing the old and new results for 2,000,000 random timestamps, sizes and periods: the windows changed only where an old window did not contain the timestamp, every new window contains it and starts at a multiple of the size or period, and sliding windows have the expected count.

Testing:

  • New cases in TestAssignWindow (fixed and sliding, before the epoch and on the boundary) and TestMapWindow (fixed, before the epoch). They fail without the change.
  • go test passes for core/runtime/exec/..., core/graph/window/..., runners/prism/internal and test/integration/primitives. gofmt is clean.
  • CHANGES.md has a Bugfixes entry.

Fixed windows subtracted (ts + size) % size from the timestamp and sliding
windows subtracted ts % period. Go's % is negative for a negative left
operand, so a timestamp more than one window before the epoch got the next
fixed window, and a timestamp before the epoch that is not a multiple of the
period got an extra sliding window that does not contain it.

Use a floor modulo for both. Timestamps whose window already contained them
keep the same windows.

Fixes apache#41725
@github-actions github-actions Bot added go core size/s S: 10-49 lines changed labels Nov 27, 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 fixes window assignment in the Go SDK for FixedWindows and SlidingWindows when handling timestamps prior to the Unix epoch. By introducing a floorMod helper to ensure non-negative remainders instead of relying on Go's truncation-toward-zero % operator, negative timestamps are now consistently placed into windows that actually contain them.

🔍 General Feedback

  • The mathematical fix in assignWindows (which also benefits windowMapper.MapWindow for side inputs) is clean, accurate, and aligns with the corresponding Java SDK fix in #41602.
  • Unit test coverage in window_test.go thoroughly exercises both boundary and non-boundary negative timestamps for fixed windows, sliding windows, and window mapping.

💡 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 go.
Backup reviewer: lostluck (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.

[Bug]: Go SDK fixed and sliding windows assign timestamps before the epoch to a window that does not contain them

1 participant