dgraph-io/dgraph · error

Wrong type %v encountered for func log

Error message

Wrong type %v encountered for func log

What it means

applyLog returns this error when a logarithm (log) operand has DEFAULT type. Log requires float-typed numeric values; DEFAULT means the operand is untyped or its bytes cannot be interpreted as a number. Log of base 0/1 or non-positive values raise separate domain errors.

Source

Thrown at query/aggregator.go:378

	case INT:
		if a.Value.(int64) < 0 || b.Value.(int64) < 0 {
			return ErrorNegativeLog
		} else if b.Value.(int64) == 1 {
			return ErrorDivisionByZero
		}
		c.Value = math.Log(float64(a.Value.(int64))) / math.Log(float64(b.Value.(int64)))
		c.Tid = types.FloatID

	case FLOAT:
		if a.Value.(float64) < 0 || b.Value.(float64) < 0 {
			return ErrorNegativeLog
		} else if b.Value.(float64) == 1 {
			return ErrorDivisionByZero
		}
		c.Value = math.Log(a.Value.(float64)) / math.Log(b.Value.(float64))

	case DEFAULT:
		return errors.Errorf("Wrong type %v encountered for func log", a.Tid)
	}
	return nil
}

func applyMin(a, b, c *types.Val) error {
	r, err := types.Less(*a, *b)
	if err != nil {
		return err
	}
	if r {
		*c = *a
		return nil
	}
	*c = *b
	return nil
}

func applyMax(a, b, c *types.Val) error {

View on GitHub (pinned to 759e242be6)

Solutions

  1. Declare the predicate as float in the schema
  2. Ensure operands are positive numerics stored with a float type
  3. Filter or default missing values before applying log

Example fix

// before
math(log(rating)) // rating: DEFAULT
// after
// schema: rating: float .
math(log(rating))
Defensive patterns

Strategy: validation

Validate before calling

func isLogOperand(v types.Val) bool {
    t := getValType(&v)
    if t != INT && t != FLOAT { return false }
    f, ok := v.Value.(float64)
    return ok && f > 0
}

Type guard

func canLog(a, b types.Val) bool {
    return isLogOperand(a) && (isLogOperand(b) || b.Value == nil)
}

Try / catch

if err := applyLog(a, b, c); err != nil {
    if strings.Contains(err.Error(), "func log") {
        return fmt.Errorf("operand type %v not supported for log; declare float schema type", a.Tid)
    }
    return err // includes log(0) / base 1 domain errors
}

Prevention

When it happens

Trigger: math("log", a, b) evaluates where an operand's Tid is DEFAULT — e.g. taking log of an untyped predicate value or a value whose earlier conversion failed.

Common situations: Log-scaled scoring on predicates without a declared float type; computing log over string-stored numerics; empty optional values flowing into math().

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/400815f09761df70. Report an issue: GitHub.