EncodeUniqueIndexValuesForKey encodes unique index values for a key.
(ctx sessionctx.Context, tblInfo *model.TableInfo, idxInfo *model.IndexInfo, idxVals []types.Datum)
| 367 | |
| 368 | // EncodeUniqueIndexValuesForKey encodes unique index values for a key. |
| 369 | func EncodeUniqueIndexValuesForKey(ctx sessionctx.Context, tblInfo *model.TableInfo, idxInfo *model.IndexInfo, idxVals []types.Datum) (_ []byte, err error) { |
| 370 | sc := ctx.GetSessionVars().StmtCtx |
| 371 | for i := range idxVals { |
| 372 | colInfo := tblInfo.Columns[idxInfo.Columns[i].Offset] |
| 373 | // table.CastValue will append 0x0 if the string value's length is smaller than the BINARY column's length. |
| 374 | // So we don't use CastValue for string value for now. |
| 375 | // TODO: The first if branch should have been removed, because the functionality of set the collation of the datum |
| 376 | // have been moved to util/ranger (normal path) and getNameValuePairs/getPointGetValue (fast path). But this change |
| 377 | // will be cherry-picked to a hotfix, so we choose to be a bit conservative and keep this for now. |
| 378 | if colInfo.GetType() == mysql.TypeString || colInfo.GetType() == mysql.TypeVarString || colInfo.GetType() == mysql.TypeVarchar { |
| 379 | var str string |
| 380 | str, err = idxVals[i].ToString() |
| 381 | idxVals[i].SetString(str, idxVals[i].Collation()) |
| 382 | } else if colInfo.GetType() == mysql.TypeEnum && (idxVals[i].Kind() == types.KindString || idxVals[i].Kind() == types.KindBytes || idxVals[i].Kind() == types.KindBinaryLiteral) { |
| 383 | var str string |
| 384 | var e types.Enum |
| 385 | str, err = idxVals[i].ToString() |
| 386 | if err != nil { |
| 387 | return nil, kv.ErrNotExist |
| 388 | } |
| 389 | e, err = types.ParseEnumName(colInfo.FieldType.GetElems(), str, colInfo.FieldType.GetCollate()) |
| 390 | if err != nil { |
| 391 | return nil, kv.ErrNotExist |
| 392 | } |
| 393 | idxVals[i].SetMysqlEnum(e, colInfo.FieldType.GetCollate()) |
| 394 | } else { |
| 395 | // If a truncated error or an overflow error is thrown when converting the type of `idxVal[i]` to |
| 396 | // the type of `colInfo`, the `idxVal` does not exist in the `idxInfo` for sure. |
| 397 | idxVals[i], err = table.CastValue(ctx, idxVals[i], colInfo, true, false) |
| 398 | if types.ErrOverflow.Equal(err) || types.ErrDataTooLong.Equal(err) || |
| 399 | types.ErrTruncated.Equal(err) || types.ErrTruncatedWrongVal.Equal(err) { |
| 400 | return nil, kv.ErrNotExist |
| 401 | } |
| 402 | } |
| 403 | if err != nil { |
| 404 | return nil, err |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | encodedIdxVals, err := codec.EncodeKey(sc.TimeZone(), nil, idxVals...) |
| 409 | err = sc.HandleError(err) |
| 410 | if err != nil { |
| 411 | return nil, err |
| 412 | } |
| 413 | return encodedIdxVals, nil |
| 414 | } |
| 415 | |
| 416 | func getLowerDB(dbName pmodel.CIStr, vars *variable.SessionVars) string { |
| 417 | dbNameL := dbName.L |
no test coverage detected