TruncateIndexValue truncate one value in the index.
(v *types.Datum, idxCol *model.IndexColumn, tblCol *model.ColumnInfo)
| 1775 | |
| 1776 | // TruncateIndexValue truncate one value in the index. |
| 1777 | func TruncateIndexValue(v *types.Datum, idxCol *model.IndexColumn, tblCol *model.ColumnInfo) { |
| 1778 | noPrefixIndex := idxCol.Length == types.UnspecifiedLength |
| 1779 | if noPrefixIndex { |
| 1780 | return |
| 1781 | } |
| 1782 | notStringType := v.Kind() != types.KindString && v.Kind() != types.KindBytes |
| 1783 | if notStringType { |
| 1784 | return |
| 1785 | } |
| 1786 | colValue := v.GetBytes() |
| 1787 | if tblCol.GetCharset() == charset.CharsetBin || tblCol.GetCharset() == charset.CharsetASCII { |
| 1788 | // Count character length by bytes if charset is binary or ascii. |
| 1789 | if len(colValue) > idxCol.Length { |
| 1790 | // truncate value and limit its length |
| 1791 | if v.Kind() == types.KindBytes { |
| 1792 | v.SetBytes(colValue[:idxCol.Length]) |
| 1793 | } else { |
| 1794 | v.SetString(v.GetString()[:idxCol.Length], tblCol.GetCollate()) |
| 1795 | } |
| 1796 | } |
| 1797 | } else if utf8.RuneCount(colValue) > idxCol.Length { |
| 1798 | // Count character length by characters for other rune-based charsets, they are all internally encoded as UTF-8. |
| 1799 | rs := bytes.Runes(colValue) |
| 1800 | truncateStr := string(rs[:idxCol.Length]) |
| 1801 | // truncate value and limit its length |
| 1802 | v.SetString(truncateStr, tblCol.GetCollate()) |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | // EncodeHandleInUniqueIndexValue encodes handle in data. |
| 1807 | func EncodeHandleInUniqueIndexValue(h kv.Handle, isUntouched bool) []byte { |
no test coverage detected