{"record":{"id":"243b302bb28728c8","repo":"prestodb/presto","slug":"numeric-value-out-of-range-243b30","errorCode":"NUMERIC_VALUE_OUT_OF_RANGE","errorMessage":"bigint addition overflow: %s + %s","messagePattern":"bigint addition overflow: (.+?) \\+ (.+?)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/BigintOperators.java","lineNumber":76,"sourceCode":"import static java.lang.Float.floatToRawIntBits;\nimport static java.lang.Math.toIntExact;\nimport static java.lang.String.format;\n\npublic final class BigintOperators\n{\n    private BigintOperators()\n    {\n    }\n\n    @ScalarOperator(ADD)\n    @SqlType(StandardTypes.BIGINT)\n    public static long add(@SqlType(StandardTypes.BIGINT) long left, @SqlType(StandardTypes.BIGINT) long right)\n    {\n        try {\n            return Math.addExact(left, right);\n        }\n        catch (ArithmeticException e) {\n            throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format(\"bigint addition overflow: %s + %s\", left, right), e);\n        }\n    }\n\n    @ScalarOperator(SUBTRACT)\n    @SqlType(StandardTypes.BIGINT)\n    public static long subtract(@SqlType(StandardTypes.BIGINT) long left, @SqlType(StandardTypes.BIGINT) long right)\n    {\n        try {\n            return Math.subtractExact(left, right);\n        }\n        catch (ArithmeticException e) {\n            throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format(\"bigint subtraction overflow: %s - %s\", left, right), e);\n        }\n    }\n\n    @ScalarOperator(MULTIPLY)\n    @SqlType(StandardTypes.BIGINT)\n    public static long multiply(@SqlType(StandardTypes.BIGINT) long left, @SqlType(StandardTypes.BIGINT) long right)","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/BigintOperators.java#L58-L94","documentation":"BigintOperators.add computes bigint addition with Math.addExact and wraps ArithmeticException in a PrestoException with NUMERIC_VALUE_OUT_OF_RANGE when the result exceeds the 64-bit bigint range [Long.MIN_VALUE, Long.MAX_VALUE]. Presto never silently wraps bigint arithmetic; overflow is an explicit error.","triggerScenarios":"Executing SQL 'left + right' on two BIGINT operands whose mathematical sum is outside the signed 64-bit range, e.g. 9223372036854775807 + 1. Called from generated query input paths (aggregations, arithmetic in projections/filters).","commonSituations":"Summing large values in aggregations where the total exceeds bigint range; adding timestamps-as-bigint (epoch millis) to large offsets; counters or ids near Long.MAX_VALUE; porting code from languages with silent overflow.","solutions":["Cast operands to DOUBLE or DECIMAL(p,s) with sufficient precision before adding: CAST(a AS DOUBLE) + CAST(b AS DOUBLE).","Reorder/reduce the computation so intermediate sums stay in range.","Use try_cast or a CASE guard to detect overflow-prone values before adding.","If many values are summed, consider approx_percentile/approxDistinct style alternatives or store data as DECIMAL."],"exampleFix":"// before\nSELECT balance + reward FROM accounts; -- overflow when near Long.MAX_VALUE\n\n// after\nSELECT CAST(balance AS DECIMAL(38,0)) + CAST(reward AS DECIMAL(38,0)) FROM accounts;","handlingStrategy":"validation","validationCode":"-- Guard against overflow before adding bigints\nSELECT a + b\nFROM t\nWHERE CAST(a AS DECIMAL(38,0)) + CAST(b AS DECIMAL(38,0))\n      BETWEEN -9223372036854775808 AND 9223372036854775807;","typeGuard":"boolean safeAdd(long a, long b) {\n    return b > 0 ? a <= Long.MAX_VALUE - b : a >= Long.MIN_VALUE - b;\n}","tryCatchPattern":"try {\n    return BigintOperators.add(left, right);\n} catch (PrestoException e) {\n    if (e.getErrorCode().getCode() == StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode().getCode()) {\n        // fall back to DECIMAL/DOUBLE arithmetic or surface a user-friendly overflow message\n    } else {\n        throw e;\n    }\n}","preventionTips":["Cast to DECIMAL/DOUBLE when values may approach Long.MAX_VALUE.","Range-check operand columns before arithmetic with CASE or WHERE filters.","Remember Presto bigint arithmetic errors instead of wrapping around.","Store extreme counters/ids as DECIMAL from the start."],"tags":["sql","bigint","overflow","arithmetic","presto"],"backgroundTag":"numeric-value-out-of-range","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"}