usememos/memos · error
modulo by zero
Error message
modulo by zero
What it means
Same compile-time constant folding path as division, but for the modulo operator '_%_': the right operand folded to zero and integer modulo by zero has no value, so the filter fails to compile.
Source
Thrown at internal/filter/parser.go:578
}
if !ok {
return 0, false, nil
}
switch call.Function {
case "_+_":
return left + right, true, nil
case "_-_":
return left - right, true, nil
case "_*_":
return left * right, true, nil
case "_/_":
if right == 0 {
return 0, false, errors.New("division by zero")
}
return left / right, true, nil
case "_%_":
if right == 0 {
return 0, false, errors.New("modulo by zero")
}
return left % right, true, nil
default:
return 0, false, errors.Errorf("unsupported arithmetic operator %q", call.Function)
}
default:
return 0, false, nil
}
}
// evaluateTimestamp folds timestamp("RFC3339") and timestamp(<epoch int>) into
// Unix epoch seconds.
func evaluateTimestamp(call *exprv1.Expr_Call) (int64, bool, error) {
if len(call.Args) != 1 {
return 0, false, errors.New("timestamp() expects one argument")
}
value, err := getConstValue(call.Args[0])
if err != nil {View on GitHub (pinned to 14d757ce1f)
Solutions
- Set a non-zero modulus literal in the filter
- Default the modulus config value to something positive (e.g. 60) in the generator
- Replace modulo bucketing with range comparisons if the modulus is dynamic
Example fix
// before created_ts % 0 == 0 // after created_ts % 60 == 0
Defensive patterns
Strategy: validation
Validate before calling
// TS: reject zero moduli in generated filters
const modulus = Number(cfg.modulus);
if (!(Number.isInteger(modulus) && modulus > 0)) {
throw new Error('modulus must be a positive integer');
} Type guard
const isPositiveInt = (v: unknown): v is number => Number.isInteger(v) && (v as number) > 0;
Prevention
- Default bucketing moduli to positive constants (e.g. 60)
- Validate modulus parameters at the config layer
- Prefer range comparisons over modulo for time bucketing when possible
When it happens
Trigger: Filters such as created_ts % 0 == 0 or (now - duration("1h")) % 0 > 0 where the modulus folds to the constant 0.
Common situations: Template-generated modulo expressions with a configurable modulus that defaults to 0; leftovers from bucketing logic (bucket by N seconds) where N was never filled in.
Related errors
- division by zero
- filter expression is empty
- filter must evaluate to a boolean value
- unsupported top-level expression
- logical AND expects two arguments
AI-assisted analysis of usememos/memos@14d757ce1f (2026-08-15).
Data as JSON: /api/errors/6c50fccae837501e.
Report an issue: GitHub.