dgraph-io/dgraph · error
Unknown math function: %v
Error message
Unknown math function: %v
What it means
An identifier followed by '(' was found inside a math expression, so the parser treats it as a math function call, but the name is not in the known math function set (+,-,*,/,%,exp,ln,cond,comparisons,min,max,sqrt,pow,logbase,floor,ceil,since,dot). The parser rejects the unknown function name.
Source
Thrown at dql/math.go:228
child, again, err = parseMathFunc(gq, it, again)
if err != nil {
return nil, false, err
}
valueStack.push(child)
if !again {
break
}
}
}
case item.Typ == itemName: // Value.
peekIt, err := it.Peek(1)
if err != nil {
return nil, false, err
}
if peekIt[0].Typ == itemLeftRound {
again := false
if !isMathFunc(item.Val) {
return nil, false, errors.Errorf("Unknown math function: %v", item.Val)
}
var child *MathTree
for {
child, again, err = parseMathFunc(gq, it, again)
if err != nil {
return nil, false, err
}
valueStack.push(child)
if !again {
break
}
}
continue
}
// We will try to parse the constant as an Int first, if that fails we move to float
child := &MathTree{}
i, err := strconv.ParseInt(item.Val, 10, 64)
if err != nil {View on GitHub (pinned to 759e242be6)
Solutions
- Check the function name against the supported list: exp, ln, sqrt, floor, ceil, since, cond, min, max, pow, logbase, dot, and arithmetic/comparison operators.
- Compute aggregates as value variables first (a as sum(val(x))) and use the variable inside math, not the function call.
- Fix misspellings (squrt -> sqrt).
- There is no abs() in DQL math — use math(sqrt(v*v)) as a workaround.
Example fix
// before total as math(abs(u)) // after sq as math(sqrt(u * u))
Defensive patterns
Strategy: type-guard
Validate before calling
const MATH_FUNCS = new Set(['+','-','*','/','%','exp','ln','cond','<','>','>=','<=','==','!=','min','max','sqrt','pow','logbase','floor','ceil','since','dot']);
function assertKnownMathFuncs(expr) {
const calls = expr.match(/([a-zA-Z_][\w.]*)\s*\(/g) || [];
for (const c of calls) {
const name = c.replace(/[\s(]/g, '');
if (!MATH_FUNCS.has(name)) throw new Error(`Unknown math function: ${name}`);
}
} Type guard
const isMathFunc = (name) => ['+','-','*','/','%','exp','ln','cond','<','>','>=','<=','==','!=', 'min','max','sqrt','pow','logbase','floor','ceil','since','dot'].includes(name);
Try / catch
try {
return await dgraph.query(query);
} catch (err) {
if (/Unknown math function/.test(String(err))) {
const fn = String(err).match(/Unknown math function: (\S+)/)?.[1];
throw new Error(`${fn} is not a DQL math function; compute aggregates as value variables instead`);
}
throw err;
} Prevention
- Keep the DQL math function list handy (no abs, count, len inside math)
- Compute aggregates as value variables first, then reference them in math
- Fix misspellings of sqrt, floor, ceil, logbase
- Emulate abs with math(sqrt(v * v))
When it happens
Trigger: Calling non-math functions inside math(), e.g. math(len(a)) or math(count(u)) — len/count are aggregate roots, not math funcs; typos like math(squrt(x)); using GraphQL-style functions in DQL math.
Common situations: Trying to nest aggregate functions inside math instead of using value variables; misspelling sqrt/floor/ceil; expecting SQL functions (ABS, ROUND) to exist — DQL uses abs-free patterns like sqrt(x*x) or floor/ceil only.
Related errors
- Invalid Math expression
- Invalid math statement. Expected 1 operands
- Invalid Math expression. Expected 3 operands
- Invalid Math expression. Expected 2 operands
- Expected ( after math
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/e67b4cf7b1a74d82.
Report an issue: GitHub.