Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message for invalid charset string #2763

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions enginetest/enginetests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4314,14 +4314,14 @@ func TestPreparedInsert(t *testing.T, harness Harness) {
Bindings: map[string]sqlparser.Expr{
"v1": mustBuildBindVariable([]byte{0x99, 0x98, 0x97}),
},
ExpectedErrStr: "incorrect string value: '[153 152 151]'",
ExpectedErrStr: "invalid string for charset utf8mb4: '[153 152 151]'",
},
{
Query: "INSERT INTO test VALUES (?);",
Bindings: map[string]sqlparser.Expr{
"v1": mustBuildBindVariable(string([]byte{0x99, 0x98, 0x97})),
},
ExpectedErrStr: "incorrect string value: '[153 152 151]'",
ExpectedErrStr: "invalid string for charset utf8mb4: '[153 152 151]'",
},
{
Query: "INSERT INTO test2 VALUES (?);",
Expand Down
6 changes: 3 additions & 3 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -7135,15 +7135,15 @@ where
Assertions: []ScriptTestAssertion{
{
Query: "insert into t(c) values (X'9876543210');",
ExpectedErrStr: "incorrect string value: '[152 118 84 50 16]'",
ExpectedErrStr: "invalid string for charset utf8mb4: '[152 118 84 50 16]'",
},
{
Query: "insert into t(v) values (X'9876543210');",
ExpectedErrStr: "incorrect string value: '[152 118 84 50 16]'",
ExpectedErrStr: "invalid string for charset utf8mb4: '[152 118 84 50 16]'",
},
{
Query: "insert into t(txt) values (X'9876543210');",
ExpectedErrStr: "incorrect string value: '[152 118 84 50 16]'",
ExpectedErrStr: "invalid string for charset utf8mb4: '[152 118 84 50 16]'",
},
{
Query: "insert into t(b) values (X'9876543210');",
Expand Down
12 changes: 6 additions & 6 deletions sql/types/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ const (

var (
// ErrLengthTooLarge is thrown when a string's length is too large given the other parameters.
ErrLengthTooLarge = errors.NewKind("length is %v but max allowed is %v")
ErrLengthBeyondLimit = errors.NewKind("string '%v' is too large for column '%v'")
ErrBinaryCollation = errors.NewKind("binary types must have the binary collation: %v")
ErrIncorrectStringValue = errors.NewKind("incorrect string value: '%v'")
ErrLengthTooLarge = errors.NewKind("length is %v but max allowed is %v")
ErrLengthBeyondLimit = errors.NewKind("string '%v' is too large for column '%v'")
ErrBinaryCollation = errors.NewKind("binary types must have the binary collation: %v")
ErrBadCharsetString = errors.NewKind("invalid string for charset %s: '%v'")

TinyText = MustCreateStringWithDefaults(sqltypes.Text, TinyTextBlobMax)
Text = MustCreateStringWithDefaults(sqltypes.Text, TextBlobMax)
Expand Down Expand Up @@ -428,11 +428,11 @@ func ConvertToString(v interface{}, t sql.StringType) (string, error) {
if !IsBinaryType(t) && !utf8.Valid(bytesVal) {
charset := t.CharacterSet()
if charset == sql.CharacterSet_utf8mb4 {
return "", ErrIncorrectStringValue.New(bytesVal)
return "", ErrBadCharsetString.New(charset.String(), bytesVal)
} else {
var ok bool
if bytesVal, ok = t.CharacterSet().Encoder().Decode(bytesVal); !ok {
return "", ErrIncorrectStringValue.New(bytesVal)
return "", ErrBadCharsetString.New(charset.String(), bytesVal)
}
}
}
Expand Down