{"record":{"id":"77fb9cb81beec482","repo":"prestodb/presto","slug":"division-by-zero-77fb9c","errorCode":"DIVISION_BY_ZERO","errorMessage":"DIVISION_BY_ZERO","messagePattern":"DIVISION_BY_ZERO","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/IntegerOperators.java","lineNumber":108,"sourceCode":"    public static long multiply(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right)\n    {\n        try {\n            return Math.multiplyExact((int) left, (int) right);\n        }\n        catch (ArithmeticException e) {\n            throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format(\"integer multiplication overflow: %s * %s\", left, right), e);\n        }\n    }\n\n    @ScalarOperator(DIVIDE)\n    @SqlType(StandardTypes.INTEGER)\n    public static long divide(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right)\n    {\n        try {\n            return left / right;\n        }\n        catch (ArithmeticException e) {\n            throw new PrestoException(DIVISION_BY_ZERO, e);\n        }\n    }\n\n    @ScalarOperator(MODULUS)\n    @SqlType(StandardTypes.INTEGER)\n    public static long modulus(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right)\n    {\n        try {\n            return left % right;\n        }\n        catch (ArithmeticException e) {\n            throw new PrestoException(DIVISION_BY_ZERO, e);\n        }\n    }\n\n    @ScalarOperator(NEGATION)\n    @SqlType(StandardTypes.INTEGER)\n    public static long negate(@SqlType(StandardTypes.INTEGER) long value)","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/IntegerOperators.java#L90-L126","documentation":"Thrown by the INTEGER division operator when the right operand is 0, causing Java's int division to raise ArithmeticException. Presto rethrows it as a DIVISION_BY_ZERO PrestoException instead of returning NULL. (Note the overflow case MIN_INT / -1 is not caught here as division-by-zero.)","triggerScenarios":"Executing the SQL '/' operator on INTEGER values where the divisor evaluates to 0, e.g. SELECT a / b with b = 0, often from an aggregate producing 0 or an empty bucket.","commonSituations":"Computing ratios/percentages where the denominator can be zero (counts per group, averages over empty sets), and queries without NULLIF/COALESCE guards on denominators.","solutions":["Guard the denominator: use NULLIF(b, 0) so the result becomes NULL instead of an error.","Filter out zero denominators with a WHERE or CASE expression before dividing.","Use a CASE WHEN b = 0 THEN default ELSE a / b END to supply a fallback value."],"exampleFix":"// before\nSELECT a / b FROM t; -- fails when b = 0\n// after\nSELECT a / NULLIF(b, 0) FROM t;","handlingStrategy":"validation","validationCode":"-- Guard denominator before division\nSELECT a / NULLIF(b, 0) FROM t;\n-- or\nSELECT CASE WHEN b = 0 THEN 0 ELSE a / b END FROM t;","typeGuard":null,"tryCatchPattern":"try { run(\"SELECT a / b FROM t\"); }\ncatch (PrestoException e) {\n  if (StandardErrorCode.DIVISION_BY_ZERO.toErrorCode().equals(e.getErrorCode())) {\n    run(\"SELECT COALESCE(a / NULLIF(b, 0), 0) FROM t\");\n  } else throw e;\n}","preventionTips":["Make NULLIF(denominator, 0) a house style for every ratio expression.","Pre-aggregate group counts and filter empty groups before computing averages.","Test queries with edge data containing zero denominators."],"tags":["presto","sql","division-by-zero","numeric"],"backgroundTag":"division-by-zero","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"}