{"record":{"id":"a6e2b21fabff98d5","repo":"dgraph-io/dgraph","slug":"wrong-type-v-encountered-for-func-sqrt","errorCode":null,"errorMessage":"Wrong type %v encountered for func sqrt","messagePattern":"Wrong type (.+?) encountered for func sqrt","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/aggregator.go","lineNumber":521,"sourceCode":"\t\tif a.Value.(int64) < 0 {\n\t\t\treturn ErrorNegativeRoot\n\t\t}\n\t\tres.Value = math.Sqrt(float64(a.Value.(int64)))\n\t\tres.Tid = types.FloatID\n\n\tcase FLOAT:\n\t\tif a.Value.(float64) < 0 {\n\t\t\treturn ErrorNegativeRoot\n\t\t}\n\t\tres.Value = math.Sqrt(a.Value.(float64))\n\n\tcase BIGFLOAT:\n\t\tvalue := a.Value.(big.Float)\n\t\trt := big.NewFloat(0).SetPrec(types.BigFloatPrecision).Sqrt(&value)\n\t\tres.Value = *rt\n\n\tcase DEFAULT:\n\t\treturn errors.Errorf(\"Wrong type %v encountered for func sqrt\", a.Tid)\n\t}\n\treturn nil\n}\n\nfunc applyFloor(a, res *types.Val) error {\n\tvBase := getValType(a)\n\tswitch vBase {\n\tcase INT:\n\t\tres.Value = a.Value.(int64)\n\n\tcase FLOAT:\n\t\tres.Value = math.Floor(a.Value.(float64))\n\n\tcase BIGFLOAT:\n\t\tvalue := a.Value.(big.Float)\n\t\tf, _ := value.Float64()\n\t\tres.Value = *big.NewFloat(math.Floor(f)).SetPrec(types.BigFloatPrecision)\n","sourceCodeStart":503,"sourceCodeEnd":539,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/aggregator.go#L503-L539","documentation":"The `sqrt` unary function computes the square root only for INT, FLOAT, and BIGFLOAT values (BIGFLOAT handled with big.Float at configured precision). If the operand type is anything else, the DEFAULT branch of the type switch returns this error with the offending type ID.","triggerScenarios":"A query math sqrt(x) where x is a non-numeric value: string/bool/dateTime predicate or a default-typed variable.","commonSituations":"sqrt over a string predicate; variable shadows a uid; predicate resharded or schema changed so values no longer arrive as float; facet values that are strings.","solutions":["Ensure sqrt's argument is an int/float predicate or numeric variable","Check the schema to confirm the predicate's type","Filter or coerce the value to numeric before sqrt","Validate the expression with a literal argument to confirm the rest of the query works"],"exampleFix":"// before\n\"math sqrt(distance_str)\"\n// after\n\"math sqrt(distance)\"","handlingStrategy":"type-guard","validationCode":"const t = schemaOf('distance');\nif (t !== 'float' && t !== 'int') throw new Error('sqrt needs numeric type');","typeGuard":"function isSqrtSafe(v) { return typeof v === 'number' && v >= 0; }","tryCatchPattern":"try {\n  out = await txn.query(q);\n} catch (e) {\n  if (String(e).includes('func sqrt')) throw new TypeError('sqrt operand must be numeric');\n  throw e;\n}","preventionTips":["Use sqrt only on int/float predicates","Verify operand types in generated queries","Keep schema and query in sync after migrations","Guard against negative inputs at query design time"],"tags":["dgraph","query","math-function","type-mismatch"],"backgroundTag":"math-function-invalid-type","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}