{"record":{"id":"70918044f72443f7","repo":"dgraph-io/dgraph","slug":"wrong-type-v-encountered-for-func-ln","errorCode":null,"errorMessage":"Wrong type %v encountered for func ln","messagePattern":"Wrong type (.+?) encountered for func ln","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/aggregator.go","lineNumber":444,"sourceCode":"\nfunc applyLn(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 ErrorNegativeLog\n\t\t}\n\t\tres.Value = math.Log(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 ErrorNegativeLog\n\t\t}\n\t\tres.Value = math.Log(a.Value.(float64))\n\n\tcase DEFAULT:\n\t\treturn errors.Errorf(\"Wrong type %v encountered for func ln\", a.Tid)\n\t}\n\treturn nil\n}\n\nfunc applyExp(a, res *types.Val) error {\n\tvBase := getValType(a)\n\tswitch vBase {\n\tcase INT:\n\t\tres.Value = math.Exp(float64(a.Value.(int64)))\n\t\tres.Tid = types.FloatID\n\n\tcase FLOAT:\n\t\tres.Value = math.Exp(a.Value.(float64))\n\n\tcase DEFAULT:\n\t\treturn errors.Errorf(\"Wrong type %v encountered for func exp\", a.Tid)\n\t}\n\treturn nil","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/aggregator.go#L426-L462","documentation":"The `ln` unary math function in the Dgraph query aggregator only accepts INT, FLOAT, and BIGFLOAT typed values. When the argument's type ID (a.Tid) falls through to the DEFAULT case — e.g. a string, bool, datetime, or default-typed value — the aggregator returns this error instead of computing a logarithm. It reports the offending type ID so the caller can see which type was unexpected.","triggerScenarios":"Running a Dgraph query with math ln(x) (or an aggregator applying ln) where x evaluates to a non-numeric type: a string predicate, a bool, a dateTime, or a value that could not be coerced to a numeric type by matchType.","commonSituations":"Developers apply ln() to a string-typed predicate or a facet value; schema of the predicate changed from float to string so values arrive with an unexpected Tid; a variable in the math expression resolves to a non-numeric scalar (e.g. uid or datetime from since()).","solutions":["Ensure the argument to ln() is a numeric predicate (int/float in the schema) or cast via math operations that yield floats","Check the predicate's schema with the /state or schema endpoint to confirm it is numeric","Fix query variables so the value fed to ln is numeric, e.g. use math ln(math.sqrt(x)) only on numeric vars","If the value is optional, filter out nodes where the predicate is absent or non-numeric before aggregating"],"exampleFix":"// before: ln over a string predicate\nq := `{ me(func: eq(name, \"x\")) { math ln(score_str) } }`\n// after: use a numeric predicate\nq := `{ me(func: eq(name, \"x\")) { math ln(score) } }`","handlingStrategy":"type-guard","validationCode":"// ensure value is numeric before running the query\nfunction isNumericPred(schema, pred) {\n  const t = schema.find(p => p.predicate === pred).type;\n  return t === 'int' || t === 'float';\n}","typeGuard":"function isNumeric(v) {\n  return typeof v === 'number' && Number.isFinite(v);\n}","tryCatchPattern":"try {\n  await dgraph.newTxn().query(q);\n} catch (e) {\n  if (String(e).includes('Wrong type') && String(e).includes('func ln')) {\n    // fall back to non-log path or fix operand type\n  }\n  throw e;\n}","preventionTips":["Validate predicate schema types before building math expressions","Keep numeric math functions away from string/bool/date predicates","Add query-level tests for every math expression","Filter null/absent values before aggregation"],"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"}