amalsaidov opened a new pull request, #20246:
URL: https://github.com/apache/pinot/pull/20246

   ## Problem
   
   `dateBin(binWidth, source, origin)` aligns a timestamp to the bin boundary 
at or before it, with bins anchored at `origin`. It computed the bin index with 
plain integer division:
   
   ```java
   long offsetFromOrigin = sourceMillisEpoch - originMillisEpoch;
   long binCount = offsetFromOrigin / binWidthMillis;
   ```
   
   Integer division truncates toward zero. When the source is before the 
origin, `offsetFromOrigin` is negative, so the truncation rounds *up* toward 
the origin and picks the bin boundary **after** the source. The source then 
falls outside the bin it was assigned to.
   
   Example:
   
   ```
   dateBin('15m', 2023-12-31 23:45:30, 2024-01-01 00:00:00)
     returns 2024-01-01 00:00:00   (14m30s after the source)
     expected 2023-12-31 23:45:00
   ```
   
   This shows up with pre-epoch timestamps and with any query whose `origin` is 
later than the data being binned. The existing tests only exercised 
source-after-origin, so the case was never caught.
   
   ## Fix
   
   Use `Math.floorDiv`, which rounds toward negative infinity, so the boundary 
is always at or before the source regardless of sign:
   
   ```java
   long binCount = Math.floorDiv(offsetFromOrigin, binWidthMillis);
   ```
   
   For non-negative offsets the result is unchanged, so this is backward 
compatible for the already-covered cases.
   
   ## Tests
   
   Added assertions to `DateTimeFunctionsTest#testDateBin` for sources before 
the origin, including a source that lands exactly on a bin boundary. Verified 
they fail on the old code (`expected [2023-12-31 23:45:00.0] but found 
[2024-01-01 00:00:00.0]`) and pass with the fix.
   
   ```
   ./mvnw -pl pinot-core -am -Dtest=DateTimeFunctionsTest#testDateBin test
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to