matriv commented on a change in pull request #18632:
URL: https://github.com/apache/flink/pull/18632#discussion_r801497584



##########
File path: 
flink-table/flink-table-common/src/main/java/org/apache/flink/table/utils/DateTimeUtils.java
##########
@@ -477,28 +448,32 @@ public static TimestampData parseTimestampData(String 
dateStr, String format) {
     }
 
     /**
-     * Parse date time string to timestamp based on the given time zone and 
"yyyy-MM-dd HH:mm:ss"
-     * format. Returns null if parsing failed.
-     *
-     * @param dateStr the date time string
-     * @param tz the time zone
+     * This is similar to {@link LocalDateTime#from(TemporalAccessor)}, but 
it's less strict and
+     * introduces default values.
      */
-    public static long parseTimestampMillis(String dateStr, TimeZone tz) 
throws ParseException {
-        int length = dateStr.length();
-        String format;
-        if (length == 10) {
-            format = DATE_FORMAT_STRING;
-        } else if (length == 21) {
-            format = DEFAULT_DATETIME_FORMATS[1];
-        } else if (length == 22) {
-            format = DEFAULT_DATETIME_FORMATS[2];
-        } else if (length == 23) {
-            format = DEFAULT_DATETIME_FORMATS[3];
-        } else {
-            // otherwise fall back to the default
-            format = DEFAULT_DATETIME_FORMATS[0];
-        }
-        return parseTimestampMillis(dateStr, format, tz);
+    private static LocalDateTime fromTemporalAccessor(TemporalAccessor 
accessor, int precision) {
+        // complement year with 1970
+        int year = accessor.isSupported(YEAR) ? accessor.get(YEAR) : 1970;
+        // complement month with 1
+        int month = accessor.isSupported(MONTH_OF_YEAR) ? 
accessor.get(MONTH_OF_YEAR) : 1;
+        // complement day with 1
+        int day = accessor.isSupported(DAY_OF_MONTH) ? 
accessor.get(DAY_OF_MONTH) : 1;
+        // complement hour with 0
+        int hour = accessor.isSupported(HOUR_OF_DAY) ? 
accessor.get(HOUR_OF_DAY) : 0;
+        // complement minute with 0
+        int minute = accessor.isSupported(MINUTE_OF_HOUR) ? 
accessor.get(MINUTE_OF_HOUR) : 0;
+        // complement second with 0
+        int second = accessor.isSupported(SECOND_OF_MINUTE) ? 
accessor.get(SECOND_OF_MINUTE) : 0;
+        // complement nano_of_second with 0
+        int nanoOfSecond = accessor.isSupported(NANO_OF_SECOND) ? 
accessor.get(NANO_OF_SECOND) : 0;
+
+        if (precision == 0) {
+            nanoOfSecond = 0;
+        } else if (precision != 9) {
+            nanoOfSecond = (int) floor(nanoOfSecond, powerX(10, 9 - 
precision));

Review comment:
       Never mind, it's much slower than the math approach:
   ```
   Benchmark                              Mode  Cnt      Score     Error   Units
   TruncatePrecisionBenchmark.useMath    thrpt   30  73815.660 ± 584.328  ops/ms
   TruncatePrecisionBenchmark.useString  thrpt   30  12294.243 ± 751.089  ops/ms
   ```




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