{"record":{"id":"315669d2857575a1","repo":"dgraph-io/dgraph","slug":"root-of-negative-number","errorCode":null,"errorMessage":"Root of negative number","messagePattern":"Root of negative number","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/math.go","lineNumber":31,"sourceCode":"\n\t\"github.com/dgraph-io/dgraph/v25/types\"\n)\n\ntype mathTree struct {\n\tFn    string\n\tVar   string\n\tConst types.Val // If its a const value node.\n\tVal   *types.ShardedMap\n\tChild []*mathTree\n}\n\nvar (\n\tErrorIntOverflow     = errors.New(\"Integer overflow\")\n\tErrorFloat32Overflow = errors.New(\"Float32 overflow\")\n\tErrorDivisionByZero  = errors.New(\"Division by zero\")\n\tErrorFractionalPower = errors.New(\"Fractional power of negative number\")\n\tErrorNegativeLog     = errors.New(\"Log of negative number\")\n\tErrorNegativeRoot    = errors.New(\"Root of negative number\")\n\tErrorVectorsNotMatch = errors.New(\"The length of vectors must match\")\n\tErrorArgsDisagree    = errors.New(\"Left and right arguments must match\")\n\tErrorShouldBeVector  = errors.New(\"Type should be []float, but is not. Cannot determine type.\")\n\tErrorBadVectorMult   = errors.New(\"Cannot multiply vector by vector\")\n)\n\n// processBinary handles the binary operands like\n// +, -, *, /, %, max, min, logbase, dot\nfunc processBinary(mNode *mathTree) error {\n\taggName := mNode.Fn\n\n\tmpl := mNode.Child[0].Val\n\tmpr := mNode.Child[1].Val\n\tcl := mNode.Child[0].Const\n\tcr := mNode.Child[1].Const\n\n\tf := func(k uint64, lshard, rshard, destMapi *map[uint64]types.Val) error {\n\t\tag := aggregator{","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/math.go#L13-L49","documentation":"Error returned by the DQL math evaluator (query/math.go) when computing a root (e.g. sqrt) of a negative number, which is undefined for real-valued results. Fix by ensuring the operand is non-negative or by using a function that supports negative inputs.","triggerScenarios":"math(sqrt(a)) where a < 0; returned by applySqrt (TestProcessUnary covers it).","commonSituations":"Taking sqrt of variance/delta fields that can be negative; sign errors upstream in normalization; sqrt applied to temperature diffs or signed scores.","solutions":["Guard with abs(): math(sqrt(abs(x))) if only magnitude matters","Filter out negative values with @filter(ge(x, 0)) before computing sqrt","Fix upstream data/normalization so the value cannot be negative"],"exampleFix":"// before\nmath(sqrt(delta))\n// after\nmath(sqrt(abs(delta)))","handlingStrategy":"validation","validationCode":"// Filter non-negative values before sqrt:\n// { q(func: type(M)) @filter(ge(x, 0)) { r as math(sqrt(x)) } }\nconst query = `{ q(func: type(M)) @filter(ge(x, 0)) { r as math(sqrt(x)) } }`;","typeGuard":null,"tryCatchPattern":"try {\n  const res = await dgraph.newTxn().query(query);\n} catch (e) {\n  if (String(e).includes('Root of negative')) {\n    // retry with math(sqrt(abs(x)))\n  } else throw e;\n}","preventionTips":["Use math(sqrt(abs(x))) by default for signed fields","Filter ge(x, 0) when the sign is meaningful","Fix upstream normalization that produces unexpected negatives"],"tags":["dgraph","math","sqrt","query"],"backgroundTag":"invalid-math-domain","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}