Skip to content

Commit 51db52c

Browse files
committed
avoid complaining about the binary collation in prepare
Signed-off-by: Yang Keao <[email protected]>
1 parent 3118aeb commit 51db52c

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

pkg/types/field_type.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func SetTypeFlag(flag *uint, flagItem uint, on bool) {
179179
// InferParamTypeFromDatum is used for plan cache to infer the type of a parameter from its datum.
180180
func InferParamTypeFromDatum(d *Datum, tp *FieldType) {
181181
InferParamTypeFromUnderlyingValue(d.GetValue(), tp)
182-
if IsStringKind(d.k) {
182+
if IsStringKind(d.k) || d.GetValue() == nil {
183183
// consider charset and collation here
184184
c, err := collate.GetCollationByName(d.collation)
185185
if err != nil || c == nil {
@@ -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
@@ -806,3 +806,8 @@ SELECT JSON_KEYS(b2) FROM t1;
806806
Error 3144 (HY000): Cannot create a JSON value from a string with CHARACTER SET 'binary'.
807807
SELECT JSON_SCHEMA_VALID(b2, '{}') FROM t1;
808808
Error 3144 (HY000): Cannot create a JSON value from a string with CHARACTER SET 'binary'.
809+
prepare stmt from 'select json_object(?, ?)';
810+
set @a=1;
811+
execute stmt using @a, @a;
812+
json_object(?, ?)
813+
{"1": 1}

tests/integrationtest/t/expression/json.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,8 @@ SELECT JSON_SEARCH(b2, 'one', '1') FROM t1;
527527
SELECT JSON_KEYS(b2) FROM t1;
528528
-- error 3144
529529
SELECT JSON_SCHEMA_VALID(b2, '{}') FROM t1;
530+
531+
# TestIssue54044
532+
prepare stmt from 'select json_object(?, ?)';
533+
set @a=1;
534+
execute stmt using @a, @a;

0 commit comments

Comments
 (0)