Skip to content

Commit 9697a20

Browse files
committed
avoid complaining about the binary collation in prepare
Signed-off-by: Yang Keao <[email protected]>
1 parent 1bf1d39 commit 9697a20

File tree

6 files changed

+17
-0
lines changed

6 files changed

+17
-0
lines changed

pkg/parser/mysql/errcode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,7 @@ const (
898898
ErrInvalidJSONText = 3140
899899
ErrInvalidJSONTextInParam = 3141
900900
ErrInvalidJSONPath = 3143
901+
ErrInvalidJSONCharset = 3144
901902
ErrInvalidTypeForJSON = 3146
902903
ErrInvalidJSONPathWildcard = 3149
903904
ErrInvalidJSONContainsPathType = 3150

pkg/parser/mysql/errname.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ var MySQLErrName = map[uint16]*ErrMessage{
907907
ErrInvalidJSONText: Message("Invalid JSON text: %-.192s", nil),
908908
ErrInvalidJSONTextInParam: Message("Invalid JSON text in argument %d to function %s: \"%s\" at position %d.", nil),
909909
ErrInvalidJSONPath: Message("Invalid JSON path expression %s.", nil),
910+
ErrInvalidJSONCharset: Message("Cannot create a JSON value from a string with CHARACTER SET '%s'.", nil),
910911
ErrInvalidTypeForJSON: Message("Invalid data type for JSON data in argument %d to function %s; a JSON string or JSON type is required.", nil),
911912
ErrInvalidJSONPathWildcard: Message("In this situation, path expressions may not contain the * and ** tokens or an array range.", nil),
912913
ErrInvalidJSONContainsPathType: Message("The second argument can only be either 'one' or 'all'.", nil),

pkg/parser/mysql/state.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ var MySQLState = map[uint16]string{
254254
ErrInvalidJSONText: "22032",
255255
ErrInvalidJSONTextInParam: "22032",
256256
ErrInvalidJSONPath: "42000",
257+
ErrInvalidJSONCharset: "22032",
257258
ErrInvalidJSONData: "22032",
258259
ErrInvalidJSONPathWildcard: "42000",
259260
ErrJSONUsedAsKey: "42000",

pkg/types/field_type.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ func InferParamTypeFromUnderlyingValue(value any, tp *FieldType) {
197197
tp.SetType(mysql.TypeVarString)
198198
tp.SetFlen(UnspecifiedLength)
199199
tp.SetDecimal(UnspecifiedLength)
200+
// Also set the `charset` and `collation` for it, because some function (e.g. `json_object`) will return error
201+
// if the argument collation is `binary`.
202+
tp.SetCharset(mysql.DefaultCharset)
203+
tp.SetCollate(mysql.DefaultCollationName)
200204
default:
201205
DefaultTypeForValue(value, tp, mysql.DefaultCharset, mysql.DefaultCollationName)
202206
if hasVariantFieldLength(tp) {

tests/integrationtest/r/expression/json.result

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,3 +812,8 @@ SELECT JSON_KEYS(b2) FROM t1;
812812
Error 3144 (HY000): Cannot create a JSON value from a string with CHARACTER SET 'binary'.
813813
SELECT JSON_SCHEMA_VALID(b2, '{}') FROM t1;
814814
Error 3144 (HY000): Cannot create a JSON value from a string with CHARACTER SET 'binary'.
815+
prepare stmt from 'select json_object(?, ?)';
816+
set @a=1;
817+
execute stmt using @a, @a;
818+
json_object(?, ?)
819+
{"1": 1}

tests/integrationtest/t/expression/json.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,8 @@ SELECT JSON_SEARCH(b2, 'one', '1') FROM t1;
533533
SELECT JSON_KEYS(b2) FROM t1;
534534
-- error 3144
535535
SELECT JSON_SCHEMA_VALID(b2, '{}') FROM t1;
536+
537+
# TestIssue54044
538+
prepare stmt from 'select json_object(?, ?)';
539+
set @a=1;
540+
execute stmt using @a, @a;

0 commit comments

Comments
 (0)