prestodb/presto · error · PrestoException

DIVISION_BY_ZERO

DIVISION_BY_ZERO

Error message

DIVISION_BY_ZERO

What it means

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.)

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/type/IntegerOperators.java:108

    public static long multiply(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right)
    {
        try {
            return Math.multiplyExact((int) left, (int) right);
        }
        catch (ArithmeticException e) {
            throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("integer multiplication overflow: %s * %s", left, right), e);
        }
    }

    @ScalarOperator(DIVIDE)
    @SqlType(StandardTypes.INTEGER)
    public static long divide(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right)
    {
        try {
            return left / right;
        }
        catch (ArithmeticException e) {
            throw new PrestoException(DIVISION_BY_ZERO, e);
        }
    }

    @ScalarOperator(MODULUS)
    @SqlType(StandardTypes.INTEGER)
    public static long modulus(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right)
    {
        try {
            return left % right;
        }
        catch (ArithmeticException e) {
            throw new PrestoException(DIVISION_BY_ZERO, e);
        }
    }

    @ScalarOperator(NEGATION)
    @SqlType(StandardTypes.INTEGER)
    public static long negate(@SqlType(StandardTypes.INTEGER) long value)

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Guard the denominator: use NULLIF(b, 0) so the result becomes NULL instead of an error.
  2. Filter out zero denominators with a WHERE or CASE expression before dividing.
  3. Use a CASE WHEN b = 0 THEN default ELSE a / b END to supply a fallback value.

Example fix

// before
SELECT a / b FROM t; -- fails when b = 0
// after
SELECT a / NULLIF(b, 0) FROM t;
Defensive patterns

Strategy: validation

Validate before calling

-- Guard denominator before division
SELECT a / NULLIF(b, 0) FROM t;
-- or
SELECT CASE WHEN b = 0 THEN 0 ELSE a / b END FROM t;

Try / catch

try { run("SELECT a / b FROM t"); }
catch (PrestoException e) {
  if (StandardErrorCode.DIVISION_BY_ZERO.toErrorCode().equals(e.getErrorCode())) {
    run("SELECT COALESCE(a / NULLIF(b, 0), 0) FROM t");
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: Computing ratios/percentages where the denominator can be zero (counts per group, averages over empty sets), and queries without NULLIF/COALESCE guards on denominators.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/77fb9cb81beec482. Report an issue: GitHub.