{"record":{"id":"e67b4cf7b1a74d82","repo":"dgraph-io/dgraph","slug":"unknown-math-function-v","errorCode":null,"errorMessage":"Unknown math function: %v","messagePattern":"Unknown math function: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dql/math.go","lineNumber":228,"sourceCode":"\t\t\t\t\tchild, again, err = parseMathFunc(gq, it, again)\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, false, err\n\t\t\t\t\t}\n\t\t\t\t\tvalueStack.push(child)\n\t\t\t\t\tif !again {\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\tcase item.Typ == itemName: // Value.\n\t\t\tpeekIt, err := it.Peek(1)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, false, err\n\t\t\t}\n\t\t\tif peekIt[0].Typ == itemLeftRound {\n\t\t\t\tagain := false\n\t\t\t\tif !isMathFunc(item.Val) {\n\t\t\t\t\treturn nil, false, errors.Errorf(\"Unknown math function: %v\", item.Val)\n\t\t\t\t}\n\t\t\t\tvar child *MathTree\n\t\t\t\tfor {\n\t\t\t\t\tchild, again, err = parseMathFunc(gq, it, again)\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, false, err\n\t\t\t\t\t}\n\t\t\t\t\tvalueStack.push(child)\n\t\t\t\t\tif !again {\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\t// We will try to parse the constant as an Int first, if that fails we move to float\n\t\t\tchild := &MathTree{}\n\t\t\ti, err := strconv.ParseInt(item.Val, 10, 64)\n\t\t\tif err != nil {","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/dql/math.go#L210-L246","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ntotal as math(abs(u))\n// after\nsq as math(sqrt(u * u))","handlingStrategy":"type-guard","validationCode":"const MATH_FUNCS = new Set(['+','-','*','/','%','exp','ln','cond','<','>','>=','<=','==','!=','min','max','sqrt','pow','logbase','floor','ceil','since','dot']);\nfunction assertKnownMathFuncs(expr) {\n  const calls = expr.match(/([a-zA-Z_][\\w.]*)\\s*\\(/g) || [];\n  for (const c of calls) {\n    const name = c.replace(/[\\s(]/g, '');\n    if (!MATH_FUNCS.has(name)) throw new Error(`Unknown math function: ${name}`);\n  }\n}","typeGuard":"const isMathFunc = (name) =>\n  ['+','-','*','/','%','exp','ln','cond','<','>','>=','<=','==','!=',\n   'min','max','sqrt','pow','logbase','floor','ceil','since','dot'].includes(name);","tryCatchPattern":"try {\n  return await dgraph.query(query);\n} catch (err) {\n  if (/Unknown math function/.test(String(err))) {\n    const fn = String(err).match(/Unknown math function: (\\S+)/)?.[1];\n    throw new Error(`${fn} is not a DQL math function; compute aggregates as value variables instead`);\n  }\n  throw err;\n}","preventionTips":["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))"],"tags":["dql","dgraph","unknown-function","math-parser"],"backgroundTag":"unknown-function","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}