apache/cassandra · error · ArithmeticException
Natural log of number zero or less
Error message
Natural log of number zero or less
What it means
DecimalType.log computes the natural logarithm of a decimal operand; since ln is undefined for zero and negative numbers, it raises ArithmeticException('Natural log of number zero or less') when the input compares <= 0.
Solutions
- Filter rows in the query: WHERE col > 0 before applying ln()
- Use a CASE/UDF to map non-positive values to null instead of ln(col)
- Clean the data (remove/fix non-positive values) if they are invalid
Example fix
// before SELECT ln(delta) FROM t; // after SELECT ln(delta) FROM t WHERE delta > 0;
Defensive patterns
Strategy: validation
Validate before calling
if (input == null || input.signum() <= 0) throw new IllegalArgumentException("ln requires a strictly positive value"); Type guard
boolean isPositive(BigDecimal v) { return v != null && v.signum() > 0; } Try / catch
try { result = DecimalType.instance.log(value); } catch (ArithmeticException e) { /* map non-positive input to null/default */ } Prevention
- Filter col > 0 in queries using ln()
- Coerce non-positive values to null at ingestion if domain requires positivity
- Add unit tests covering zero/negative inputs
When it happens
Trigger: Executing a CQL expression like ln(col) or the function-call path that reaches log(Number) with a value of 0 or a negative decimal.
Common situations: Queries over columns that legitimately hold zeros/negatives (deltas, balances) without filtering; bad data ingested into measured quantities expected positive.
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
- Log10 of number zero or less
- A NaN cannot be converted into a decimal
- An infinite number cannot be converted into a decimal
- Cannot parse decimal value from
- Expected 0 or at least 4 bytes
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/538c281b62a88d9a.
Report an issue: GitHub.
Appendix: source
Thrown at src/java/org/apache/cassandra/db/marshal/DecimalType.java:438
}
protected BigDecimal exp(BigDecimal input)
{
int precision = input.precision();
precision = Math.max(MIN_SIGNIFICANT_DIGITS, precision);
precision = Math.min(MAX_PRECISION.getPrecision(), precision);
return BigDecimalMath.exp(input, new MathContext(precision, RoundingMode.HALF_EVEN));
}
@Override
public ByteBuffer log(Number input)
{
return decompose(log(toBigDecimal(input)));
}
protected BigDecimal log(BigDecimal input)
{
if (input.compareTo(BigDecimal.ZERO) <= 0) throw new ArithmeticException("Natural log of number zero or less");
int precision = input.precision();
precision = Math.max(MIN_SIGNIFICANT_DIGITS, precision);
precision = Math.min(MAX_PRECISION.getPrecision(), precision);
return BigDecimalMath.log(input, new MathContext(precision, RoundingMode.HALF_EVEN));
}
@Override
public ByteBuffer log10(Number input)
{
return decompose(log10(toBigDecimal(input)));
}
protected BigDecimal log10(BigDecimal input)
{
if (input.compareTo(BigDecimal.ZERO) <= 0) throw new ArithmeticException("Log10 of number zero or less");
int precision = input.precision();
precision = Math.max(MIN_SIGNIFICANT_DIGITS, precision);
precision = Math.min(MAX_PRECISION.getPrecision(), precision);View on GitHub (pinned to 88fd0f6a0e)