{"record":{"id":"e1c1881dcfadcb21","repo":"prestodb/presto","slug":"numeric-value-out-of-range-e1c188","errorCode":"NUMERIC_VALUE_OUT_OF_RANGE","errorMessage":"integer addition overflow: %s + %s","messagePattern":"integer addition overflow: (.+?) \\+ (.+?)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/IntegerOperators.java","lineNumber":72,"sourceCode":"import static io.airlift.slice.Slices.utf8Slice;\nimport static java.lang.Float.floatToRawIntBits;\nimport static java.lang.String.format;\n\npublic final class IntegerOperators\n{\n    private IntegerOperators()\n    {\n    }\n\n    @ScalarOperator(ADD)\n    @SqlType(StandardTypes.INTEGER)\n    public static long add(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right)\n    {\n        try {\n            return Math.addExact((int) left, (int) right);\n        }\n        catch (ArithmeticException e) {\n            throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format(\"integer addition overflow: %s + %s\", left, right), e);\n        }\n    }\n\n    @ScalarOperator(SUBTRACT)\n    @SqlType(StandardTypes.INTEGER)\n    public static long subtract(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right)\n    {\n        try {\n            return Math.subtractExact((int) left, (int) right);\n        }\n        catch (ArithmeticException e) {\n            throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format(\"integer subtraction overflow: %s - %s\", left, right), e);\n        }\n    }\n\n    @ScalarOperator(MULTIPLY)\n    @SqlType(StandardTypes.INTEGER)\n    public static long multiply(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right)","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/IntegerOperators.java#L54-L90","documentation":"Overflow guard in the INTEGER + INTEGER operator: Math.addExact detected that adding the two 32-bit operands would exceed INT range, so the engine raises NUMERIC_VALUE_OUT_OF_RANGE instead of silently wrapping. Both operands are formatted into the message; the fix is to cast operands to BIGINT or DOUBLE before adding.","triggerScenarios":"`left + right` on INTEGER-typed values where the exact 32-bit sum overflows (beyond 2147483647 or below -2147483648) — add() in IntegerOperators.java:72.","commonSituations":"Summing large counters stored as INTEGER, accumulating user counts or byte sizes that exceed 2^31, arithmetic on legacy int columns that silently wrapped in other engines.","solutions":["Cast operands to BIGINT before adding: CAST(a AS BIGINT) + CAST(b AS BIGINT)","Use try(a + b) to get NULL instead of failing the query","Change the column type to BIGINT if values legitimately exceed int range","Clamp inputs or validate ranges before summing"],"exampleFix":"// before (SQL)\nSELECT visits_a + visits_b FROM stats -- INTEGER overflow\n// after\nSELECT CAST(visits_a AS BIGINT) + CAST(visits_b AS BIGINT) FROM stats","handlingStrategy":"validation","validationCode":"SELECT * FROM t WHERE CAST(a AS BIGINT) + CAST(b AS BIGINT) > 2147483647 OR CAST(a AS BIGINT) + CAST(b AS BIGINT) < -2147483648; -- find overflow rows","typeGuard":null,"tryCatchPattern":"SELECT try(a + b) FROM t -- NULL instead of NUMERIC_VALUE_OUT_OF_RANGE","preventionTips":["Promote to BIGINT before adding INTEGER columns likely to overflow","Use try() for arithmetic on untrusted/large data","Watch for INTEGER columns holding accumulating counters and migrate them to BIGINT"],"tags":["overflow","integer","arithmetic","presto"],"backgroundTag":"integer-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"}