snuyanzin commented on code in PR #27132:
URL: https://github.com/apache/flink/pull/27132#discussion_r2444173045


##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/catalog/IntervalFreshness.java:
##########
@@ -31,42 +33,109 @@
 @PublicEvolving
 public class IntervalFreshness {
 
-    private final String interval;
+    private final int interval;
     private final TimeUnit timeUnit;
 
-    private IntervalFreshness(String interval, TimeUnit timeUnit) {
+    private IntervalFreshness(int interval, TimeUnit timeUnit) {
         this.interval = interval;
         this.timeUnit = timeUnit;
     }
 
     public static IntervalFreshness of(String interval, TimeUnit timeUnit) {
-        return new IntervalFreshness(interval, timeUnit);
+        final int validateIntervalInput = validateIntervalInput(interval);
+        return new IntervalFreshness(validateIntervalInput, timeUnit);
+    }
+
+    private static int validateIntervalInput(final String interval) {
+        final String errorMessage =
+                String.format(
+                        "The freshness interval currently only supports 
positive integer type values. But was: %s",
+                        interval);
+        final int parsedInt;
+        try {
+            parsedInt = Integer.parseInt(interval);
+        } catch (Exception e) {
+            throw new ValidationException(errorMessage, e);
+        }
+
+        if (parsedInt <= 0) {
+            throw new ValidationException(errorMessage);
+        }
+        return parsedInt;
     }
 
     public static IntervalFreshness ofSecond(String interval) {
-        return new IntervalFreshness(interval, TimeUnit.SECOND);
+        return IntervalFreshness.of(interval, TimeUnit.SECOND);
     }
 
     public static IntervalFreshness ofMinute(String interval) {
-        return new IntervalFreshness(interval, TimeUnit.MINUTE);
+        return IntervalFreshness.of(interval, TimeUnit.MINUTE);
     }
 
     public static IntervalFreshness ofHour(String interval) {
-        return new IntervalFreshness(interval, TimeUnit.HOUR);
+        return IntervalFreshness.of(interval, TimeUnit.HOUR);
     }
 
     public static IntervalFreshness ofDay(String interval) {
-        return new IntervalFreshness(interval, TimeUnit.DAY);
+        return IntervalFreshness.of(interval, TimeUnit.DAY);
     }
 
+    /**
+     * @deprecated Use {@link #getIntervalInt()} instead.
+     */
+    @Deprecated
     public String getInterval() {
+        return String.valueOf(interval);
+    }
+
+    public int getIntervalInt() {
         return interval;
     }
 
     public TimeUnit getTimeUnit() {
         return timeUnit;
     }
 
+    public Duration toDuration() {
+        switch (timeUnit) {
+            case SECOND:
+                return Duration.ofSeconds(interval);
+            case MINUTE:
+                return Duration.ofMinutes(interval);
+            case HOUR:
+                return Duration.ofHours(interval);
+            case DAY:
+                return Duration.ofDays(interval);
+            default:
+                throw new IllegalStateException("Unexpected value: " + 
timeUnit);
+        }
+    }
+
+    /**
+     * Creates an IntervalFreshness from a Duration, choosing the most 
appropriate time unit.
+     * Prefers larger units when possible (e.g., 60 seconds → 1 minute).
+     */
+    public static IntervalFreshness fromDuration(Duration duration) {
+        long totalSeconds = duration.getSeconds();
+
+        long days = duration.toDays();
+        if (days * 24 * 60 * 60 == totalSeconds) {
+            return IntervalFreshness.ofDay(String.valueOf(days));
+        }
+
+        long hours = duration.toHours();
+        if (hours * 60 * 60 == totalSeconds) {
+            return IntervalFreshness.ofHour(String.valueOf(hours));
+        }
+
+        long minutes = duration.toMinutes();
+        if (minutes * 60 == totalSeconds) {
+            return IntervalFreshness.ofMinute(String.valueOf(minutes));
+        }

Review Comment:
   ```suggestion
          if (duration.equals(duration.truncatedTo(Duration.DAYS))) {
             return IntervalFreshness.ofDay(duration.getDays());
          }
          if (duration.equals(duration.truncatedTo(Duration.HOURS)) {
             return IntervalFreshness.ofHour(duration.getHours());
          }   
          if (duration.equals(duration.truncatedTo(Duration.MINUTES)) {
             return IntervalFreshness.ofMinute(duration.getMinutes());
          }   
   ```



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