{"record":{"id":"261b6659ae29ffd2","repo":"dgraph-io/dgraph","slug":"invalid-math-expression-expected-2-operands","errorCode":null,"errorMessage":"Invalid Math expression. Expected 2 operands","messagePattern":"Invalid Math expression\\. Expected 2 operands","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dql/math.go","lineNumber":133,"sourceCode":"\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\n\tdefault:\n\t\tif valueStack.size() < 2 {\n\t\t\treturn errors.Errorf(\"Invalid Math expression. Expected 2 operands\")\n\t\t}\n\t\tif isZero(topOp.Fn, valueStack.peek().Const) {\n\t\t\treturn errors.Errorf(\"Division by zero.\")\n\t\t}\n\t\ttopVal1 := valueStack.popAssert()\n\t\ttopVal2 := valueStack.popAssert()\n\t\ttopOp.Child = []*MathTree{topVal2, topVal1}\n\n\t}\n\t// Push the new value (tree) into the valueStack.\n\tvalueStack.push(topOp)\n\treturn nil\n}\n\nfunc isMathFunc(f string) bool {\n\t// While adding an op, also add it to the corresponding function type.\n\treturn f == \"*\" || f == \"%\" || f == \"+\" || f == \"-\" || f == \"/\" ||\n\t\tf == \"exp\" || f == \"ln\" || f == \"cond\" ||","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/dql/math.go#L115-L151","documentation":"A binary math operator (+, -, *, /, %, comparisons, pow, logbase, etc.) was evaluated but fewer than 2 operands were on the value stack. The operator is missing one or both of its operands, indicating an incomplete math expression.","triggerScenarios":"math(a +) with trailing operator, math(+ a), or binary function missing an argument like math(pow(a)) — pow needs two arguments (math(pow(a, 2))).","commonSituations":"Deleting an operand during query editing; writing two-arg functions (pow, logbase, min/max usage patterns) with one argument; an extra closing paren swallowing operands: math((a + b)) typo variants like math((a + )).","solutions":["Ensure every binary operator has two operands: math(a + b).","Supply both arguments for binary functions: math(pow(base, exp)), math(logbase(v, b)).","Remove trailing/dangling operators at the end of the expression.","Add parentheses around sub-expressions so operator grouping is unambiguous."],"exampleFix":"// before\nu as math(pow(v))\n// after\nu as math(pow(v, 2))","handlingStrategy":"validation","validationCode":"// Every binary operator needs an operand on both sides\nconst expr = 'pow(a, 2)';\nif (/(^|[\\s(])[+\\-*/%<>=!]+\\s*([),]|$)/.test(expr)) {\n  throw new Error('Binary operator missing an operand');\n}\nif (/\\bpow\\s*\\([^,)]+\\)/.test(expr)) throw new Error('pow needs 2 arguments');","typeGuard":null,"tryCatchPattern":"try {\n  return await dgraph.query(query);\n} catch (err) {\n  if (/Expected 2 operands/.test(String(err))) {\n    throw new Error('A binary math operator/function lacks operands in: ' + query);\n  }\n  throw err;\n}","preventionTips":["Give two-argument functions (pow, logbase) both arguments","Remove dangling operators from edited expressions","Wrap operand sub-expressions in parentheses","Validate generated expressions with a regex/sentence check before sending"],"tags":["dql","dgraph","math-parser","binary-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"}