raminqaf opened a new pull request, #29689:
URL: https://github.com/apache/flink/pull/29689

   ## What is the purpose of the change
   
   `CAST` of a `TIMESTAMP` before 1970 to `DATE` or `TIME` returns a wrong 
value. The cast divides the epoch milliseconds by the length of a day with `/` 
and `%`, which round toward zero, so a negative value moves toward 1970:
   
   | Input                                  | Target   | Before       | After   
     |
   
|----------------------------------------|----------|--------------|--------------|
   | `TIMESTAMP(3) '1969-12-31 23:59:59.999'` | `DATE`   | `1970-01-01` | 
`1969-12-31` |
   | `TIMESTAMP(3) '1969-12-31 23:59:59.999'` | `TIME(0)` | `00:00:00`   | 
`23:59:59`   |
   
   The fix uses `Math.floorDiv` and `Math.floorMod`. `TIMESTAMP_LTZ` to `DATE` 
and `TIME` was already correct, because it converts through `java.time`.
   
   Results change for pre-1970 values, also when a compiled plan is restored, 
since the cast is generated code. There is no flag for the old behavior because 
the old result is wrong.
   
   ## Brief change log
   
     - `TimestampToDateCastRule` generates `Math.floorDiv` instead of `/`
     - `DateTimeUtils.timestampWithoutLocalZoneToTime` uses `Math.floorMod` 
instead of `%`
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
     - `CastRulesTest`: the last millisecond of 1969-12-31 cast to `DATE` and 
to `TIME`, which also truncates the fraction for `TIME(0)`. Both cases fail 
without the fix.
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): no
     - The public API, i.e., is any changed class annotated with 
`@Public(Evolving)`: no
     - The serializers: no
     - The runtime per-record code paths (performance sensitive): yes, the cast 
itself. `Math.floorDiv` and `Math.floorMod` only add a sign check.
     - Anything that affects deployment or recovery: JobManager (and its 
components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
     - The S3 file system connector: no
   
   ## Documentation
   
     - Does this pull request introduce a new feature? no
     - If yes, how is the feature documented? not applicable
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code 2.1.314 (Claude Opus 5.5)
   


-- 
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]

Reply via email to