featzhang commented on code in PR #28146:
URL: https://github.com/apache/flink/pull/28146#discussion_r3228490257


##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/expressions/ValueLiteralExpression.java:
##########
@@ -462,11 +462,18 @@ private static String serializeTimestampLtz(Instant 
instant, int precision) {
 
     /**
      * Checks whether an {@link Instant} can be represented as a {@code long} 
epoch value at the
-     * given precision without overflow.
+     * given precision without overflow. Accounts for the sub-second nanos 
term that {@link
+     * DateTimeUtils#toEpochValue} adds on top of {@code epochSeconds * 
10^precision}.
      */
     private static boolean canRepresentAsLong(Instant instant, int precision) {
-        long factor = (long) Math.pow(10, precision);
-        long epochSeconds = instant.getEpochSecond();
-        return factor == 0 || Math.abs(epochSeconds) <= Long.MAX_VALUE / 
factor;
+        final long factor = (long) Math.pow(10, precision);
+        final long nanoDivisor = (long) Math.pow(10, 9 - precision);
+        final long epochSeconds = instant.getEpochSecond();
+        final long nanoPart = instant.getNano() / nanoDivisor;
+        if (epochSeconds > Long.MAX_VALUE / factor || epochSeconds < 
Long.MIN_VALUE / factor) {
+            return false;
+        }
+        final long base = epochSeconds * factor;
+        return base < 0 || nanoPart <= Long.MAX_VALUE - base;

Review Comment:
   The short-circuit for negative base is correct but might benefit from a 
comment for clarity:
   
   ```java
   // Negative base + non-negative nanoPart moves toward zero, can't overflow 
upward
   return base < 0 || nanoPart <= Long.MAX_VALUE - base;
   ```
   
   Since `nanoPart >= 0` always (from `instant.getNano() / divisor`), adding it 
to a negative base can't exceed `Long.MAX_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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to