apache/cassandra · error · ArithmeticException

Natural log of number zero or less

Error message

Natural log of number zero or less

What it means

IntegerType.log(Number) computes the natural log of a varint by first converting the input to a BigInteger; if the value is zero or negative it throws ArithmeticException with this message, mirroring Math.log's domain restriction. This is a mathematical domain error: log is only defined for strictly positive inputs.

Solutions

  1. Guard the input and only call log(x) when x > 0 (e.g. log(x + 1) or filter WHERE x > 0 first)
  2. Handle negative/zero domain needs explicitly in the query (CASE/IF expressions)
  3. Fix upstream data producing non-positive values
  4. Catch ArithmeticException around the function invocation if input cannot be pre-validated

Example fix

// before
SELECT log(delta) FROM events; // delta can be 0 or negative
// after
SELECT CASE WHEN delta > 0 THEN log(delta) ELSE NULL END FROM events;
Defensive patterns

Strategy: try-catch

Validate before calling

BigInteger bi = toBigInteger(input);
if (bi.signum() <= 0) throw new IllegalArgumentException("log() requires a positive value, got: " + bi);

Type guard

static boolean isLogSafe(Number n) { return toBigInteger(n).signum() > 0; }

Try / catch

try { ByteBuffer r = IntegerType.instance.log(input); } catch (ArithmeticException e) { /* non-positive input; return NULL or default */ }

Prevention

When it happens

Trigger: Calling IntegerType.instance.log(number) (e.g. via the log() CQL native function) with 0 or a negative varint argument; toBigInteger(input).compareTo(BigInteger.ZERO) <= 0 triggers the throw.

Common situations: User-supplied function arguments of 0 or negative values in SELECT log(x); data columns that unexpectedly contain zero/negative values (sensor dropouts, accounting deltas); sign errors in computed expressions.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/10af7452f9386fca. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/db/marshal/IntegerType.java:573

    {
        return decompose(toBigInteger(input).abs());
    }

    @Override
    public ByteBuffer exp(Number input)
    {
        BigInteger bi = toBigInteger(input);
        BigDecimal bd = new BigDecimal(bi);
        BigDecimal result = DecimalType.instance.exp(bd);
        BigInteger out = result.toBigInteger();
        return IntegerType.instance.decompose(out);
    }

    @Override
    public ByteBuffer log(Number input)
    {
        BigInteger bi = toBigInteger(input);
        if (bi.compareTo(BigInteger.ZERO) <= 0) throw new ArithmeticException("Natural log of number zero or less");
        BigDecimal bd = new BigDecimal(bi);
        BigDecimal result = DecimalType.instance.log(bd);
        BigInteger out = result.toBigInteger();
        return IntegerType.instance.decompose(out);
    }

    @Override
    public ByteBuffer log10(Number input)
    {
        BigInteger bi = toBigInteger(input);
        if (bi.compareTo(BigInteger.ZERO) <= 0) throw new ArithmeticException("Log10 of number zero or less");
        BigDecimal bd = new BigDecimal(bi);
        BigDecimal result = DecimalType.instance.log10(bd);
        BigInteger out = result.toBigInteger();
        return IntegerType.instance.decompose(out);
    }

    @Override

View on GitHub (pinned to 88fd0f6a0e)