dgraph-io/dgraph · error

Invalid data type

Error message

Invalid data type

What it means

Error returned by x/keys.go when a key's byte prefix maps to an unknown or invalid data type during key parsing. Indicates a corrupt key or unsupported key format; fix by writing keys only through the supported key-construction functions.

Source

Thrown at x/keys.go:617

	case ByteCount, ByteCountRev:
		if len(k) < 4 {
			return p, errors.Errorf("count length < 4 for key: %q, parsed key: %+v", key, p)
		}
		p.Count = binary.BigEndian.Uint32(k)

		if !p.HasStartUid {
			break
		}

		if len(k) != 12 {
			return p, errors.Errorf("StartUid length != 8 for key: %q, parsed key: %+v", key, p)
		}

		k = k[4:]
		p.StartUid = binary.BigEndian.Uint64(k)
	default:
		// Some other data type.
		return p, errors.Errorf("Invalid data type")
	}
	return p, nil
}

func IsDropOpKey(key []byte) (bool, error) {
	pk, err := Parse(key)
	if err != nil {
		return false, errors.Wrapf(err, "could not parse key %s", hex.Dump(key))
	}

	if pk.IsData() && ParseAttr(pk.Attr) == "dgraph.drop.op" {
		return true, nil
	}
	return false, nil
}

// These predicates appear for queries that have * as predicate in them.
var starAllPredicateMap = map[string]struct{}{

View on GitHub (pinned to 759e242be6)

Solutions

  1. Confirm the key is a posting/data-family key before calling Parse
  2. Filter out non-posting keys (schema, drop-op markers) by prefix/type before parsing
  3. Check for version skew where newer keys are read by older code
  4. Dump the first byte and compare against the known key-type constants

Example fix

// before: parsing any badger key
pk, err := x.Parse(it.Item().Key())
// after: skip unknown types first
typeID := it.Item().Key()[0]
if typeID == byte(x.ByteSchema) { continue }
pk, err := x.Parse(it.Item().Key())
Defensive patterns

Strategy: type-guard

Validate before calling

known := map[byte]bool{byte(x.ByteData): true, byte(x.ByteReverse): true, byte(x.ByteIndex): true, byte(x.ByteCount): true, byte(x.ByteCountRev): true}
if !known[key[0]] { return nil } // skip non-posting keys
ok, err := x.IsDropOpKey(key)

Type guard

func isPostingKey(key []byte) bool {
  if len(key) == 0 { return false }
  switch key[0] {
  case byte(x.ByteData), byte(x.ByteReverse), byte(x.ByteIndex), byte(x.ByteCount), byte(x.ByteCountRev):
    return true
  }
  return false
}

Prevention

When it happens

Trigger: Calling Parse/IsDropOpKey on a key whose first byte is not a known key-type constant, e.g. schema, mutation or other non-posting keys being fed to Parse.

Common situations: Passing non-posting badger keys (schema keys, ACL keys) to Parse, version mismatch introducing new key types, or corrupted type byte.

Related errors


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