[ 
https://issues.apache.org/jira/browse/CALCITE-6896?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18069667#comment-18069667
 ] 

hongyu guo commented on CALCITE-6896:
-------------------------------------

I don't think is a bug, I wrote a test in UdfTest.java, Timestamp can be 
correctly converted to each other.
{code:java}
@Test void testTimestampRoundTripIsStable() {
      final CalciteAssert.AssertThat with = withUdf();
      with.query("values \"adhoc\".\"toTimestampFun\"(1741363199000)")
          .returnsValue("2025-03-07 15:59:59");
      with.query("values \"adhoc\".\"timestampFun\"(TIMESTAMP '2025-03-07 
15:59:59')")
          .returnsValue("1741363199000");
      with.query("values 
\"adhoc\".\"timestampFun\"(\"adhoc\".\"toTimestampFun\"(1741363199000))")
          .returnsValue("1741363199000");
    } {code}

>  Improper Timezone Handling in SqlFunctions#internalToTimestamp
> ---------------------------------------------------------------
>
>                 Key: CALCITE-6896
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6896
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.39.0
>            Reporter: Wei.Zhou
>            Priority: Major
>
> {*}Error Cause{*}: Improper Timezone Handling in method of 
> SqlFunctions.internalToTimestamp(long v)
>  # {{LocalDateTime.ofEpochSecond()}} generates a UTC-based timestamp but 
> produces a *timezone-less* {{LocalDateTime}} object.
>  # {{Timestamp.valueOf(dateTime)}} implicitly uses the *JVM's default 
> timezone* to interpret the {{{}LocalDateTime{}}}, rather than preserving the 
> UTC context. This creates a hidden timezone conversion.
> {*}Example{*}:
> If {{v = 1741363199000}} (UTC epoch: 2025-03-07 15:59:59) and the system 
> timezone is UTC+8:
>  * {{LocalDateTime}} correctly represents 2025-03-07 15:59:59 (UTC)
>  * {{Timestamp.valueOf()}} treats it as 2025-03-07 15:59:59 {*}in UTC+8{*}, 
> resulting in an actual UTC time of 2025-03-07 7:59:59.000 (8-hour offset 
> error).
> {*}Correct Approach{*}:
> {code:java}
> // Direct conversion without timezone ambiguity
> public static java.sql.Timestamp internalToTimestamp(long v) {
>     return new java.sql.Timestamp(v); // Milliseconds directly map to SQL 
> Timestamp
> }
> // Or using Instant
> public static java.sql.Timestamp internalToTimestamp(long v) {
>     return Timestamp.from(Instant.ofEpochMilli(v));
> } {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to