{"record":{"id":"ad3ce0d580e20855","repo":"dgraph-io/dgraph","slug":"wrong-type-v-encountered-for-func-u","errorCode":null,"errorMessage":"Wrong type %v encountered for func u-","messagePattern":"Wrong type (.+?) encountered for func u-","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/aggregator.go","lineNumber":494,"sourceCode":"\tcase FLOAT:\n\t\tres.Value = -a.Value.(float64)\n\t\tres.Tid = types.FloatID\n\tcase VFLOAT:\n\t\taVal := a.Value.([]float32)\n\t\tresVal := make([]float32, len(aVal))\n\t\tfor i, v := range aVal {\n\t\t\tresVal[i] = -v\n\t\t}\n\t\tres.Value = resVal\n\t\tres.Tid = types.VFloatID\n\n\tcase BIGFLOAT:\n\t\tvalue := a.Value.(big.Float)\n\t\tneg := big.NewFloat(0).SetPrec(types.BigFloatPrecision).Neg(&value)\n\t\tres.Value = *neg\n\n\tcase DEFAULT:\n\t\treturn errors.Errorf(\"Wrong type %v encountered for func u-\", a.Tid)\n\t}\n\treturn nil\n}\n\nfunc applySqrt(a, res *types.Val) error {\n\tvBase := getValType(a)\n\tswitch vBase {\n\tcase INT:\n\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}","sourceCodeStart":476,"sourceCodeEnd":512,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/aggregator.go#L476-L512","documentation":"The unary negation function `u-` (used for math expressions like -x) supports INT, FLOAT, BIGFLOAT, and related numeric types. applyNeg returns this error when the operand's type ID is not one of the supported numeric types, reporting the actual Tid in the message. It is a defensive fallthrough in the type switch after all numeric cases are handled.","triggerScenarios":"Evaluating a unary minus in a math expression where the operand is a string, bool, dateTime, or DEFAULT-typed value, e.g. math -(someStringPredicate).","commonSituations":"Negating a boolean facet or string field by mistake; a variable expected to hold a number actually holds a datetime from since() mis-use; schema drift changed a predicate's type.","solutions":["Apply the unary minus only to numeric predicates or numeric variables","Confirm predicate types in the schema before using arithmetic/negation","If the value is stored as a string number, migrate the data/schema to a float type","Rewrite the expression so negation happens after a numeric conversion step"],"exampleFix":"// before: negating a string\n\"math -(label)\"\n// after: negate a numeric field\n\"math -(balance)\"","handlingStrategy":"type-guard","validationCode":"if (!Number.isFinite(value)) throw new Error('u- requires a number');","typeGuard":"function canNegate(v) { return typeof v === 'number' || typeof v === 'bigint'; }","tryCatchPattern":"try {\n  res = await txn.query(q);\n} catch (e) {\n  if (String(e).includes('func u-')) { /* operand not numeric; handle */ }\n  throw e;\n}","preventionTips":["Only negate numeric predicates/variables","Check schema before arithmetic on predicates","Avoid negating facet values that may be strings","Write integration tests covering unary minus paths"],"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"}