diff --git a/expression/builtin_info.go b/expression/builtin_info.go index 1ea390336a2e6..e001bb549611c 100644 --- a/expression/builtin_info.go +++ b/expression/builtin_info.go @@ -626,12 +626,13 @@ func (b *builtinTiDBDecodeKeySig) evalString(row chunk.Row) (string, bool, error if isNull || err != nil { return "", isNull, err } - return decodeKey(s), false, nil + return decodeKey(b.ctx, s), false, nil } -func decodeKey(s string) string { +func decodeKey(ctx sessionctx.Context, s string) string { key, err := hex.DecodeString(s) if err != nil { + ctx.GetSessionVars().StmtCtx.AppendWarning(errors.New("the key is a invalid record/index key")) return s } // Auto decode byte if needs. @@ -652,5 +653,6 @@ func decodeKey(s string) string { } // TODO: try to decode other type key. + ctx.GetSessionVars().StmtCtx.AppendWarning(errors.New("the key is a invalid record/index key")) return s } diff --git a/expression/integration_test.go b/expression/integration_test.go index c990e95b03eea..006e1b31a7015 100755 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4131,6 +4131,13 @@ func (s *testIntegrationSuite) TestTiDBInternalFunc(c *C) { result = tk.MustQuery("select tidb_decode_key( '74800000000000019B5F698000000000000001015257303100000000FB013736383232313130FF3900000000000000F8010000000000000000F7' )") result.Check(testkit.Rows("tableID=411, indexID=1, indexValues=015257303100000000FB013736383232313130FF3900000000000000F8010000000000000000F7")) + + // Test invalid record/index key. + result = tk.MustQuery("select tidb_decode_key( '7480000000000000FF2E5F728000000011FFE1A3000000000000' )") + result.Check(testkit.Rows("7480000000000000FF2E5F728000000011FFE1A3000000000000")) + warns := tk.Se.GetSessionVars().StmtCtx.GetWarnings() + c.Assert(warns, HasLen, 1) + c.Assert(warns[0].Err.Error(), Equals, "the key is a invalid record/index key") } func newStoreWithBootstrap() (kv.Storage, *domain.Domain, error) {