{"record":{"id":"d7d42d242ba5045d","repo":"dgraph-io/dgraph","slug":"invalid-math-expression","errorCode":null,"errorMessage":"Invalid Math expression","messagePattern":"Invalid Math expression","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dql/math.go","lineNumber":105,"sourceCode":"\t\tif !ok {\n\t\t\treturn false\n\t\t}\n\t\tswitch f {\n\t\tcase \"floor\", \"/\", \"%\", \"ceil\", \"sqrt\", \"u-\":\n\t\t\treturn g == 0\n\t\tcase \"ln\":\n\t\t\treturn g == 1\n\t\t}\n\t\treturn false\n\t}\n\n\treturn false\n}\n\nfunc evalMathStack(opStack, valueStack *mathTreeStack) error {\n\ttopOp, err := opStack.pop()\n\tif err != nil {\n\t\treturn errors.Errorf(\"Invalid Math expression\")\n\t}\n\tswitch {\n\tcase isUnary(topOp.Fn):\n\t\t// Since \"not\" is a unary operator, just pop one value.\n\t\ttopVal, err := valueStack.pop()\n\t\tif err != nil {\n\t\t\treturn errors.Errorf(\"Invalid math statement. Expected 1 operands\")\n\t\t}\n\t\tif opStack.size() > 1 {\n\t\t\tpeek := opStack.peek().Fn\n\t\t\tif (peek == \"/\" || peek == \"%\") && isZero(topOp.Fn, topVal.Const) {\n\t\t\t\treturn errors.Errorf(\"Division by zero\")\n\t\t\t}\n\t\t}\n\t\ttopOp.Child = []*MathTree{topVal}\n\n\tcase isTernary(topOp.Fn):\n\t\tif valueStack.size() < 3 {","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/dql/math.go#L87-L123","documentation":"During DQL math expression evaluation (shunting-yard style), evalMathStack tries to pop an operator from the operator stack and finds it empty. This happens when the expression structure is malformed so an evaluation was triggered with no pending operator. The parser reports it as a generic 'Invalid Math expression'.","triggerScenarios":"A math(...) block whose operator and value stacks get out of sync, e.g. math applied with an empty/degenerate expression inside the parentheses such as math() or an expression that ends without any operators to apply when a comma/paren forces evaluation.","commonSituations":"Typos in DQL queries like `math()` or stray parentheses: `math(a ())`; copy-pasted queries with removed operators; programmatically generated math expressions missing operators.","solutions":["Inspect the math() expression for empty parentheses or missing operators.","Test the expression with a simple valid form like math(a+1) and build up incrementally.","Remove stray '(' or ')' characters that don't pair with operators.","Check nested function calls have the right commas and arguments."],"exampleFix":"// before\nroot @filter(...) { u as math() }\n// after\nroot @filter(...) { u as math(a + 1) }","handlingStrategy":"validation","validationCode":"// Validate math expression is non-empty and contains an operator before sending\nconst expr = 'a + 1';\nif (!/[+\\-*/%<>=!]|sqrt|exp|ln|cond|pow|logbase|floor|ceil|min|max|since|dot/.test(expr)) {\n  throw new Error('math() requires at least one operator/function');\n}","typeGuard":null,"tryCatchPattern":"try {\n  return await dgraph.query(query);\n} catch (err) {\n  if (/Invalid Math expression/.test(String(err))) {\n    throw new Error(`Malformed math expression in query: ${query}. Check operators/parens.`);\n  }\n  throw err;\n}","preventionTips":["Always include at least one operator or function inside math(...)","Pair-check parentheses and commas before sending the query","Test new math expressions with a small query first","Build expressions programmatically with a helper that guarantees valid structure"],"tags":["dql","dgraph","math-parser","query-syntax"],"backgroundTag":"invalid-math-expression","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}