nictownsend commented on code in PR #25763:
URL: https://github.com/apache/flink/pull/25763#discussion_r1890537094


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/ToTimestampLtzFunction.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+import org.apache.flink.table.utils.DateTimeUtils;
+
+import javax.annotation.Nullable;
+
+/**
+ * Implementation of {@link BuiltInFunctionDefinitions#TO_TIMESTAMP_LTZ}.
+ *
+ * <p>A function that converts various time formats to TIMESTAMP_LTZ type.
+ *
+ * <p>Supported function signatures:
+ *
+ * <ul>
+ *   <li>{@code TO_TIMESTAMP_LTZ(numeric)} -> TIMESTAMP_LTZ(3) <br>
+ *       Converts numeric epoch time in milliseconds to timestamp with local 
timezone
+ *   <li>{@code TO_TIMESTAMP_LTZ(numeric, precision)} -> 
TIMESTAMP_LTZ(precision) <br>
+ *       Converts numeric epoch time to timestamp with specified precision (0 
as seconds, 3 as
+ *       milliseconds)
+ *   <li>{@code TO_TIMESTAMP_LTZ(timestamp)} -> TIMESTAMP_LTZ(3) <br>
+ *       Parses string timestamp using default format 'yyyy-MM-dd HH:mm:ss'
+ *   <li>{@code TO_TIMESTAMP_LTZ(timestamp, format)} -> TIMESTAMP_LTZ(3) <br>
+ *       Parses string timestamp using input string of format
+ *   <li>{@code TO_TIMESTAMP_LTZ(timestamp, format, timezone)} -> 
TIMESTAMP_LTZ(3) <br>
+ *       Parses string timestamp using input strings of format and timezone
+ * </ul>
+ *
+ * <p>Example:
+ *
+ * <pre>{@code
+ * TO_TIMESTAMP_LTZ('2023-01-01 10:00:00')}  // Parses string using default 
format
+ * TO_TIMESTAMP_LTZ(1234567890123)}  // Converts epoch milliseconds
+ * TO_TIMESTAMP_LTZ(1234567890, 0)     // Converts epoch seconds
+ * TO_TIMESTAMP_LTZ(1234567890123, 3)  // Converts epoch milliseconds
+ * TO_TIMESTAMP_LTZ('2023-01-01 10:00:00')  // Parses string using default 
format
+ * TO_TIMESTAMP_LTZ('2023-01-01T10:00:00', 'yyyy-MM-dd\'T\'HH:mm:ss')} // 
Parses string using input format
+ * TO_TIMESTAMP_LTZ('2023-01-01 10:00:00', 'yyyy-MM-dd HH:mm:ss', 'UTC')} // 
Parses string using input format and timezone
+ * }</pre>
+ */
+@Internal
+public class ToTimestampLtzFunction extends BuiltInScalarFunction {
+
+    private static final int DEFAULT_PRECISION = 3;
+
+    public ToTimestampLtzFunction(SpecializedFunction.SpecializedContext 
context) {
+        super(BuiltInFunctionDefinitions.TO_TIMESTAMP_LTZ, context);
+    }
+
+    public @Nullable TimestampData eval(Number epoch, Integer precision) {
+        if (epoch == null || precision == null) {
+            return null;
+        }
+        if (epoch instanceof Float || epoch instanceof Double) {
+            return DateTimeUtils.toTimestampData(epoch.doubleValue(), 
precision);
+        }
+        return DateTimeUtils.toTimestampData(epoch.longValue(), precision);
+    }
+
+    public @Nullable TimestampData eval(DecimalData epoch, Integer precision) {
+        if (epoch == null || precision == null) {
+            return null;
+        }
+
+        return DateTimeUtils.toTimestampData(epoch, precision);
+    }
+
+    public @Nullable TimestampData eval(Number epoch) {
+        return eval(epoch, DEFAULT_PRECISION);
+    }
+
+    public TimestampData eval(DecimalData epoch) {
+        if (epoch == null) {
+            return null;
+        }
+
+        return DateTimeUtils.toTimestampData(epoch);
+    }
+
+    public @Nullable TimestampData eval(StringData timestamp) {

Review Comment:
   Possibly too much for this particular PR - but is it worth considering 
whether expanding the behaviour to support multiple default format?
   
   For example, using 
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatterBuilder.html
 to support multiple formats? E.g ISO + SQL as default matchers?
   
   I have previously used something like 
https://www.baeldung.com/java-parsing-dates-many-formats#using-datetimeformatterbuilder
 when writing UDFs around parsing time to allow for multiple formats within a 
data source, or to allow for more flexible SQL re-use because I don't have to 
remember to swap between ISO and SQL or write date formats to cover different 
zone indcators.



##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/utils/DateTimeUtils.java:
##########
@@ -421,6 +436,21 @@ public static TimestampData parseTimestampData(String 
dateStr, int precision, Ti
                         .toInstant());
     }
 
+    public static TimestampData parseTimestampData(String dateStr, String 
format, String timezone) {
+        if (dateStr == null || format == null || timezone == null) {
+            return null;
+        }
+
+        TimestampData ts = parseTimestampData(dateStr, format);
+        if (ts == null) {
+            return null;
+        }
+
+        ZonedDateTime utcZoned = 
ts.toLocalDateTime().atZone(UTC_ZONE.toZoneId());
+        ZonedDateTime targetTime = 
utcZoned.withZoneSameInstant(ZoneId.of(timezone));

Review Comment:
   If I call this with `dateStr = 01-01-2000 00:00:00` and `timezone = 
UTC+01:00` - then I am asking to parse a zoned timestamp `01-01-2000 00:00:00 
UTC+1` (which is `31-12-1999 23:00:00` in UTC).
   
   This code creates a `ZonedDateTime` of  `01-01-2000 00:00:00 UTC`. It then 
creates a new `ZonedDateTime` with `UTC+1` using `withZoneSameInstant` - this 
means both objects contain the same instant - so `targetTime.toInstant()` is 
returning the instant of `01-01-2000 00:00:00 UTC`
   
   I think you instead need to use `withZoneSameLocal` - that converts the 
timestamp into the same local time but in the new timezone.
   
   
   ```
   LocalDateTime in = LocalDateTime.of(2000,01,01,0,0);
   ZonedDateTime atZone = in.atZone(ZoneId.of("UTC"));
   ZonedDateTime otherZoneInstant = 
atZone.withZoneSameInstant(ZoneId.of("UTC+1"));
   ZonedDateTime otherZoneLocal = atZone.withZoneSameLocal(ZoneId.of("UTC+1"));
   
   System.out.println(otherZoneInstant); //2000-01-01T01:00+01:00[UTC+01:00]
   System.out.println(otherZoneLocal); //2000-01-01T00:00+01:00[UTC+01:00] - 
correct value
   ```



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to