{"record":{"id":"5dad0edec79f93c0","repo":"prestodb/presto","slug":"timestampwithtimezone-overflow-s-ms","errorCode":null,"errorMessage":"TimestampWithTimeZone overflow: %s ms","messagePattern":"TimestampWithTimeZone overflow: (.+?) ms","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/type/DateTimeEncoding.java","lineNumber":35,"sourceCode":"import static com.facebook.presto.common.type.TimeZoneKey.getTimeZoneKeyForOffset;\nimport static java.lang.String.format;\nimport static java.util.Objects.requireNonNull;\n\npublic final class DateTimeEncoding\n{\n    private DateTimeEncoding()\n    {\n    }\n\n    private static final int TIME_ZONE_MASK = 0xFFF;\n    private static final int MILLIS_SHIFT = 12;\n    private static final long MAX_MILLIS = 0x7FFFFFFFFFFFFL;\n    private static final long MIN_MILLIS = (MAX_MILLIS + 1) * -1;\n\n    private static long pack(long millisUtc, short timeZoneKey)\n    {\n        if (millisUtc > MAX_MILLIS || millisUtc < MIN_MILLIS) {\n            throw new ArithmeticException(format(\"TimestampWithTimeZone overflow: %s ms\", millisUtc));\n        }\n        return (millisUtc << MILLIS_SHIFT) | (timeZoneKey & TIME_ZONE_MASK);\n    }\n\n    public static long packDateTimeWithZone(long millisUtc, String zoneId)\n    {\n        return packDateTimeWithZone(millisUtc, getTimeZoneKey(zoneId));\n    }\n\n    public static long packDateTimeWithZone(long millisUtc, int offsetMinutes)\n    {\n        return packDateTimeWithZone(millisUtc, getTimeZoneKeyForOffset(offsetMinutes));\n    }\n\n    public static long packDateTimeWithZone(long millisUtc, TimeZoneKey timeZoneKey)\n    {\n        requireNonNull(timeZoneKey, \"timeZoneKey is null\");\n        return pack(millisUtc, timeZoneKey.getKey());","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/type/DateTimeEncoding.java#L17-L53","documentation":"DateTimeEncoding.pack encodes a timestamp as (millisUtc << shift) | timeZoneKey inside a single long, reserving only 51 bits for millis (MAX_MILLIS = 0x7FFFFFFFFFFFF ≈ ±1.4e16 ms ≈ ±450k years). If millisUtc is outside [MIN_MILLIS, MAX_MILLIS] an ArithmeticException is thrown because the value cannot be bit-packed without overflow corrupting the timezone bits.","triggerScenarios":"Calling packDateTimeWithZone(millisUtc, zoneId) or updateMillisUtc with a millisecond epoch beyond ±0x7FFFFFFFFFFFF; year values far beyond the representable range (astronomical dates), or a corrupted/erroneous epoch such as microseconds fed as millis multiplied further.","commonSituations":"Unit confusion: passing microseconds or nanoseconds where millis are expected, blowing past the range; corrupted data producing absurd epoch values; computations like epoch*1000*1000 by accident.","solutions":["Verify the input unit is milliseconds since epoch; divide by 1000 if microseconds","Clamp/validate millisUtc against MIN_MILLIS/MAX_MILLIS before packing","Sanitize source data; treat absurd epochs as NULL or recompute","If legitimately needing a bigger range, use a different timestamp representation (e.g. long nanos type or BigDecimal)"],"exampleFix":"// before\nlong packed = DateTimeEncoding.packDateTimeWithZone(microsSinceEpoch, zoneId); // wrong unit\n// after\nlong millisUtc = microsSinceEpoch / 1000;\nif (millisUtc > DateTimeEncoding.MAX_MILLIS || millisUtc < -DateTimeEncoding.MAX_MILLIS - 1) {\n    throw new IllegalArgumentException(\"timestamp out of representable range\");\n}\nlong packed = DateTimeEncoding.packDateTimeWithZone(millisUtc, zoneId);","handlingStrategy":"validation","validationCode":"public static boolean isPackableMillis(long millisUtc) {\n    return millisUtc <= 0x7FFFFFFFFFFFFL && millisUtc >= -0x8000000000000L;\n}\n// also confirm the input unit is milliseconds, not micros/nanos","typeGuard":"public static boolean isPlausibleEpochMillis(Long millis) {\n    return millis != null && millis > -62135596800000L && millis < 253402300799999L; // year 1..9999\n}","tryCatchPattern":"try {\n    long packed = DateTimeEncoding.packDateTimeWithZone(millisUtc, zoneId);\n} catch (ArithmeticException e) {\n    // value out of packing range: sanitize source or use alternate representation\n}","preventionTips":["Double-check time unit (ms vs µs vs ns) before packing","Validate year is in a sane calendar range before encoding","Treat absurd epochs from corrupted data as NULL"],"tags":["presto","timestamp","overflow","bit-packing"],"backgroundTag":"timestamp-encoding-overflow","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}