{"record":{"id":"46872d334e67fa5a","repo":"dgraph-io/dgraph","slug":"invalid-math-statement-expected-1-operands","errorCode":null,"errorMessage":"Invalid math statement. Expected 1 operands","messagePattern":"Invalid math statement\\. Expected 1 operands","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dql/math.go","lineNumber":112,"sourceCode":"\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 {\n\t\t\treturn errors.Errorf(\"Invalid Math expression. Expected 3 operands\")\n\t\t}\n\t\ttopVal1 := valueStack.popAssert()\n\t\ttopVal2 := valueStack.popAssert()\n\t\ttopVal3 := valueStack.popAssert()\n\t\ttopOp.Child = []*MathTree{topVal3, topVal2, topVal1}\n","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/dql/math.go#L94-L130","documentation":"A unary math operator (exp, ln, u-, sqrt, floor, ceil, since) was being evaluated but the value stack had no operand to pop. The unary operator is starved of its single operand, indicating a malformed math expression.","triggerScenarios":"Writing math(a -) or math(sqrt) with no operand following/preceding the unary operator, or an expression where a unary function ends up with an empty sub-expression, e.g. math(a * - ).","commonSituations":"Hand-edited DQL where an operand was deleted; negation with missing value: math(u- ) or trailing '-'; generated queries with empty variable values interpolated.","solutions":["Ensure every unary operator/function has exactly one operand: math(sqrt(a)), math(-a).","Fix dangling operators at the end of the expression like math(a -).","Check unary minus is written directly before a value: math(-a + b).","Validate the query in a REPL/ small query first to isolate the failing expression."],"exampleFix":"// before\nu as math(a * )\n// after\nu as math(a * sqrt(b))","handlingStrategy":"validation","validationCode":"// Ensure unary ops/functions have an operand\nconst expr = 'sqrt(a)';\nconst unary = ['sqrt','exp','ln','floor','ceil','since'];\nfor (const f of unary) {\n  const re = new RegExp(f + '\\\\s*\\\\(\\\\s*\\\\)');\n  if (re.test(expr)) throw new Error(`${f} is missing its operand`);\n}\nif (/[+\\-*/%]\\s*([),]|$)/.test(expr)) throw new Error('Dangling operator without operand');","typeGuard":null,"tryCatchPattern":"try {\n  return await dgraph.query(query);\n} catch (err) {\n  if (/Expected 1 operands/.test(String(err))) {\n    throw new Error('A unary math operator is missing its operand in: ' + query);\n  }\n  throw err;\n}","preventionTips":["Write unary minus directly attached to a value (-a, not a -)","Never leave a trailing operator at the end of a math expression","Always parenthesize function arguments: sqrt(a), not sqrt a","Lint generated DQL for dangling operators before submission"],"tags":["dql","dgraph","math-parser","unary-operator"],"backgroundTag":"invalid-math-expression","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}