twalthr commented on code in PR #25077:
URL: https://github.com/apache/flink/pull/25077#discussion_r1674118355
##########
flink-core/src/main/java/org/apache/flink/util/TimeUtils.java:
##########
@@ -39,6 +41,10 @@ public class TimeUtils {
private static final Map<String, ChronoUnit> LABEL_TO_UNIT_MAP =
Collections.unmodifiableMap(initMap());
+ private static final BigInteger NANOS_PER_SECOND =
BigInteger.valueOf(1000_000_000L);
Review Comment:
```suggestion
private static final BigInteger NANOS_PER_SECOND =
BigInteger.valueOf(1_000_000_000L);
```
##########
flink-core/src/main/java/org/apache/flink/util/TimeUtils.java:
##########
@@ -79,30 +85,45 @@ public static Duration parseDuration(String text) {
throw new NumberFormatException("text does not start with a
number");
}
- final long value;
+ final BigInteger value;
try {
- value = Long.parseLong(number); // this throws a
NumberFormatException on overflow
+ value = new BigInteger(number); // this throws a
NumberFormatException
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
- "The value '"
- + number
- + "' cannot be re represented as 64bit number
(numeric overflow).");
+ "The value '" + number + "' cannot be re represented as an
integer number.", e);
Review Comment:
```suggestion
"The value '" + number + "' cannot be represented as an
integer number.", e);
```
##########
flink-core/src/main/java/org/apache/flink/util/TimeUtils.java:
##########
@@ -79,30 +85,45 @@ public static Duration parseDuration(String text) {
throw new NumberFormatException("text does not start with a
number");
}
- final long value;
+ final BigInteger value;
try {
- value = Long.parseLong(number); // this throws a
NumberFormatException on overflow
+ value = new BigInteger(number); // this throws a
NumberFormatException
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
- "The value '"
- + number
- + "' cannot be re represented as 64bit number
(numeric overflow).");
+ "The value '" + number + "' cannot be re represented as an
integer number.", e);
}
+ final ChronoUnit unit;
if (unitLabel.isEmpty()) {
- return Duration.of(value, ChronoUnit.MILLIS);
- }
-
- ChronoUnit unit = LABEL_TO_UNIT_MAP.get(unitLabel);
- if (unit != null) {
- return Duration.of(value, unit);
+ unit = ChronoUnit.MILLIS;
} else {
+ unit = LABEL_TO_UNIT_MAP.get(unitLabel);
+ }
+ if (unit == null) {
throw new IllegalArgumentException(
"Time interval unit label '"
+ unitLabel
+ "' does not match any of the recognized units: "
+ TimeUnit.getAllUnits());
}
+
+ try {
+ return convertBigIntToDuration(value, unit);
+ } catch (ArithmeticException e) {
+ throw new IllegalArgumentException(
+ "The value '"
+ + number
+ + "' cannot be re represented as
java.time.Duration (numeric overflow).",
Review Comment:
```suggestion
+ "' cannot be represented as java.time.Duration
(numeric overflow).",
```
##########
flink-core/src/main/java/org/apache/flink/util/TimeUtils.java:
##########
@@ -136,17 +157,35 @@ public static String getStringInMillis(final Duration
duration) {
* <b>NOTE:</b> It supports only durations that fit into long.
*/
public static String formatWithHighestUnit(Duration duration) {
- long nanos = duration.toNanos();
+ BigInteger nanos = toNanos(duration);
TimeUnit highestIntegerUnit = getHighestIntegerUnit(nanos);
return String.format(
- "%d %s",
- nanos / highestIntegerUnit.unit.getDuration().toNanos(),
+ "%s %s",
+ nanos.divide(highestIntegerUnit.getUnitAsNanos()),
highestIntegerUnit.getLabels().get(0));
}
- private static TimeUnit getHighestIntegerUnit(long nanos) {
- if (nanos == 0) {
+ /**
+ * Converted from {@link Duration#toNanos()}, but produces {@link
BigDecimal} and does not throw
Review Comment:
```suggestion
* Converted from {@link Duration#toNanos()}, but produces {@link
BigInteger} and does not throw
```
--
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]