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

  1. Set a non-zero modulus literal in the filter
  2. Default the modulus config value to something positive (e.g. 60) in the generator
  3. 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

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


AI-assisted analysis of usememos/memos@14d757ce1f (2026-08-15). Data as JSON: /api/errors/6c50fccae837501e. Report an issue: GitHub.