[FLINK-26334][datastream] for 1.14.0 . Modified getWindowStartWithOff… #19158
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Co-authored-by: Lin WanNi linwanniderz@foxmail.com
Co-authored-by: Guo YuanFang 1650213825@qq.com
What is the purpose of the change
https://issues.apache.org/jira/browse/FLINK-26334
about: #18982
The goal of this PR is to fix the bug that: the element couldn't be assigned to the correct window-start, if it's timestamp - offset + windowSize < 0.
This bug located at org.apache.flink.streaming.api.windowing.windows.TimeWindow .
This problem will be triggered by the negative timestamp, and is caused by the calculation method of remainder in the JAVA compiler.
Specifically, when we try to calculate the window-start of an incoming element, if timestamp - offset + windowSize < 0, based on the current calculation formula for window-start, the element will be right shifted to the next window, which has a start time larger than the timestamp of current element, seems violated the assignment principle for elements on window.
This problem can be fixed by modifying the calculation formula inside the getWindowStartWithOffset() method as below:
After this modify, for the element who has negative timestamp, we can still get the correct window-start. Like the below graph showing:

The getWindowStartWithOffset() method in other package
Considered the common usage of this method, we checked out the other getWindowStartWithOffset() method in the project, found one in the org.apache.flink.table.runtime.operators.window.grouping.WindowsGrouping
Turn out this method correctly handled the negative timestamp situation. Below is the source code.
further
When we wrote the test case, we found that the algorithm we wrote would violate the convention that the window is closed on the left and open on the right. In addition, considering the readability of the code, we decided to use the same code as in
org.apache.flink.table.runtime.operators.window.grouping.WindowsGrouping.In addition, in the process of modification, we found that the algorithm of
getWindowStartWithOffsetinorg.apache.flink.table.runtime.operators.window.TimeWindowis the same as that inorg.apache.flink.streaming.api.windowing.windows.TimeWindow. So it should cause the same problem. I think it should also be modified to support negative timestampsBrief change log
Verifying this change
This change is already covered by existing tests, such as the tests in the flink-streaming-java [mvn clean verify]
Does this pull request potentially affect one of the following parts:
@Public(Evolving): noDocumentation