Skip to content

Commit

Permalink
table: improve error message for incorrect utf8 value
Browse files Browse the repository at this point in the history
This now more closely matches the output from MySQL 8.0
  • Loading branch information
dveeden committed Jun 21, 2021
1 parent bd8d3b1 commit 321842f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
11 changes: 11 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9740,3 +9740,14 @@ OR Variable_name = 'sql_mode' OR Variable_name = 'query_cache_type' OR Variable
OR Variable_name = 'license' OR Variable_name = 'init_connect'`).Rows(), HasLen, 19)

}

func (s *testIntegrationSuite) TestCharsetErr(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table charset_test(id int auto_increment primary key, c1 varchar(255) character set ascii)")
err := tk.ExecToErr("insert into charset_test(c1) values ('aaa\xEF\xBF\xBDabcdef')")
c.Assert(err.Error(), Equals, "[table:1366]Incorrect string value '\\xEF\\xBF\\xBDabc...' for column 'c1'")

err = tk.ExecToErr("insert into charset_test(c1) values ('aaa\xEF\xBF\xBD')")
c.Assert(err.Error(), Equals, "[table:1366]Incorrect string value '\\xEF\\xBF\\xBD' for column 'c1'")
}
35 changes: 21 additions & 14 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,26 @@ func truncateTrailingSpaces(v *types.Datum) {
v.SetString(str, v.Collation())
}

func handleWrongASCIIValue(ctx sessionctx.Context, col *model.ColumnInfo, casted *types.Datum, str string, i int) (types.Datum, error) {
func handleWrongCharsetValue(ctx sessionctx.Context, col *model.ColumnInfo, casted *types.Datum, str string, i int) (types.Datum, error) {
sc := ctx.GetSessionVars().StmtCtx
err := ErrTruncatedWrongValueForField.FastGen("incorrect ascii value %x(%s) for column %s", casted.GetBytes(), str, col.Name)
logutil.BgLogger().Error("incorrect ASCII value", zap.Uint64("conn", ctx.GetSessionVars().ConnectionID), zap.Error(err))
truncateVal := types.NewStringDatum(str[:i])
err = sc.HandleTruncate(err)
return truncateVal, err
}

func handleWrongUtf8Value(ctx sessionctx.Context, col *model.ColumnInfo, casted *types.Datum, str string, i int) (types.Datum, error) {
sc := ctx.GetSessionVars().StmtCtx
err := ErrTruncatedWrongValueForField.FastGen("incorrect utf8 value %x(%s) for column %s", casted.GetBytes(), str, col.Name)
logutil.BgLogger().Error("incorrect UTF-8 value", zap.Uint64("conn", ctx.GetSessionVars().ConnectionID), zap.Error(err))
var strval strings.Builder
for j := 0; j < 6; j++ {
if len(str) > (i + j) {
if str[i+j] > unicode.MaxASCII {
fmt.Fprintf(&strval, "\\x%X", str[i+j])
} else {
strval.WriteRune(rune(str[i+j]))
}
}
}
if len(str) > i+6 {
strval.WriteString(`...`)
}

// TODO: Add 'at row %d'
err := ErrTruncatedWrongValueForField.FastGen("Incorrect string value '%s' for column '%s'", strval.String(), col.Name)
logutil.BgLogger().Error("incorrect string value", zap.Uint64("conn", ctx.GetSessionVars().ConnectionID), zap.Error(err))
// Truncate to valid utf8 string.
truncateVal := types.NewStringDatum(str[:i])
err = sc.HandleTruncate(err)
Expand Down Expand Up @@ -280,7 +287,7 @@ func CastValue(ctx sessionctx.Context, val types.Datum, col *model.ColumnInfo, r
str := casted.GetString()
for i := 0; i < len(str); i++ {
if str[i] > unicode.MaxASCII {
casted, err = handleWrongASCIIValue(ctx, col, &casted, str, i)
casted, err = handleWrongCharsetValue(ctx, col, &casted, str, i)
break
}
}
Expand All @@ -307,11 +314,11 @@ func CastValue(ctx sessionctx.Context, val types.Datum, col *model.ColumnInfo, r
w = width
continue
}
casted, err = handleWrongUtf8Value(ctx, col, &casted, str, i)
casted, err = handleWrongCharsetValue(ctx, col, &casted, str, i)
break
} else if width > 3 && doMB4CharCheck {
// Handle non-BMP characters.
casted, err = handleWrongUtf8Value(ctx, col, &casted, str, i)
casted, err = handleWrongCharsetValue(ctx, col, &casted, str, i)
break
}
w = width
Expand Down

0 comments on commit 321842f

Please sign in to comment.