HashCode encodes a Datum into a unique byte slice. It is mostly the same as EncodeValue, but it doesn't contain truncation or verification logic in order to make the encoding lossless.
(b []byte, d types.Datum)
| 1604 | // HashCode encodes a Datum into a unique byte slice. |
| 1605 | // It is mostly the same as EncodeValue, but it doesn't contain truncation or verification logic in order to make the encoding lossless. |
| 1606 | func HashCode(b []byte, d types.Datum) []byte { |
| 1607 | switch d.Kind() { |
| 1608 | case types.KindInt64: |
| 1609 | b = encodeSignedInt(b, d.GetInt64(), false) |
| 1610 | case types.KindUint64: |
| 1611 | b = encodeUnsignedInt(b, d.GetUint64(), false) |
| 1612 | case types.KindFloat32, types.KindFloat64: |
| 1613 | b = append(b, floatFlag) |
| 1614 | b = EncodeFloat(b, d.GetFloat64()) |
| 1615 | case types.KindString: |
| 1616 | b = encodeString(b, d, false) |
| 1617 | case types.KindBytes: |
| 1618 | b = encodeBytes(b, d.GetBytes(), false) |
| 1619 | case types.KindMysqlTime: |
| 1620 | b = append(b, uintFlag) |
| 1621 | t := d.GetMysqlTime().CoreTime() |
| 1622 | b = encodeUnsignedInt(b, uint64(t), true) |
| 1623 | case types.KindMysqlDuration: |
| 1624 | // duration may have negative value, so we cannot use String to encode directly. |
| 1625 | b = append(b, durationFlag) |
| 1626 | b = EncodeInt(b, int64(d.GetMysqlDuration().Duration)) |
| 1627 | case types.KindMysqlDecimal: |
| 1628 | b = append(b, decimalFlag) |
| 1629 | decStr := d.GetMysqlDecimal().ToString() |
| 1630 | b = encodeBytes(b, decStr, false) |
| 1631 | case types.KindMysqlEnum: |
| 1632 | b = encodeUnsignedInt(b, d.GetMysqlEnum().Value, false) |
| 1633 | case types.KindMysqlSet: |
| 1634 | b = encodeUnsignedInt(b, d.GetMysqlSet().Value, false) |
| 1635 | case types.KindMysqlBit, types.KindBinaryLiteral: |
| 1636 | val := d.GetBinaryLiteral() |
| 1637 | b = encodeBytes(b, val, false) |
| 1638 | case types.KindMysqlJSON: |
| 1639 | b = append(b, jsonFlag) |
| 1640 | j := d.GetMysqlJSON() |
| 1641 | b = append(b, j.TypeCode) |
| 1642 | b = append(b, j.Value...) |
| 1643 | case types.KindVectorFloat32: |
| 1644 | b = append(b, vectorFloat32Flag) |
| 1645 | v := d.GetVectorFloat32() |
| 1646 | b = v.SerializeTo(b) |
| 1647 | case types.KindNull: |
| 1648 | b = append(b, NilFlag) |
| 1649 | case types.KindMinNotNull: |
| 1650 | b = append(b, bytesFlag) |
| 1651 | case types.KindMaxValue: |
| 1652 | b = append(b, maxFlag) |
| 1653 | default: |
| 1654 | logutil.BgLogger().Warn("trying to calculate HashCode of an unexpected type of Datum", |
| 1655 | zap.Uint8("Datum Kind", d.Kind()), |
| 1656 | zap.Stack("stack")) |
| 1657 | } |
| 1658 | return b |
| 1659 | } |
no test coverage detected