Skip to content

Commit

Permalink
expression: forbid binary string to be json key (#37541)
Browse files Browse the repository at this point in the history
close #37509
  • Loading branch information
xiongjiwei authored Sep 2, 2022
1 parent b0e84fc commit 080a784
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions executor/aggfuncs/func_json_objectagg.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (e *jsonObjectAgg) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup
return 0, json.ErrJSONDocumentNULLKey
}

if e.args[0].GetType().GetCharset() == charset.CharsetBin {
return 0, json.ErrInvalidJSONCharset.GenWithStackByArgs(e.args[0].GetType().GetCharset())
}

value, err := e.args[1].Eval(row)
if err != nil {
return 0, errors.Trace(err)
Expand Down
8 changes: 8 additions & 0 deletions executor/aggfuncs/func_json_objectagg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func TestMergePartialResult4JsonObjectagg(t *testing.T) {
}
var argCombines [][]*types.FieldType
for i := 0; i < len(typeList); i++ {
if typeList[i].GetCharset() == charset.CharsetBin {
// skip because binary charset cannot be used as key.
continue
}
for j := 0; j < len(typeList); j++ {
argTypes := []*types.FieldType{typeList[i], typeList[j]}
argCombines = append(argCombines, argTypes)
Expand Down Expand Up @@ -112,6 +116,10 @@ func TestJsonObjectagg(t *testing.T) {
}
var argCombines [][]*types.FieldType
for i := 0; i < len(typeList); i++ {
if typeList[i].GetCharset() == charset.CharsetBin {
// skip because binary charset cannot be used as key.
continue
}
for j := 0; j < len(typeList); j++ {
argTypes := []*types.FieldType{typeList[i], typeList[j]}
argCombines = append(argCombines, argTypes)
Expand Down
5 changes: 3 additions & 2 deletions expression/aggregation/base_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (a *baseFuncDesc) TypeInfer(ctx sessionctx.Context) error {
case ast.AggFuncJsonArrayagg:
a.typeInfer4JsonArrayAgg(ctx)
case ast.AggFuncJsonObjectAgg:
a.typeInfer4JsonObjectAgg(ctx)
return a.typeInfer4JsonObjectAgg(ctx)
default:
return errors.Errorf("unsupported agg function: %s", a.Name)
}
Expand Down Expand Up @@ -294,10 +294,11 @@ func (a *baseFuncDesc) typeInfer4JsonArrayAgg(ctx sessionctx.Context) {
types.SetBinChsClnFlag(a.RetTp)
}

func (a *baseFuncDesc) typeInfer4JsonObjectAgg(ctx sessionctx.Context) {
func (a *baseFuncDesc) typeInfer4JsonObjectAgg(ctx sessionctx.Context) error {
a.RetTp = types.NewFieldType(mysql.TypeJSON)
types.SetBinChsClnFlag(a.RetTp)
a.Args[0] = expression.WrapWithCastAsString(ctx, a.Args[0])
return nil
}

func (a *baseFuncDesc) typeInfer4NumberFuncs() {
Expand Down
4 changes: 4 additions & 0 deletions expression/builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -496,6 +497,9 @@ func (c *jsonObjectFunctionClass) getFunction(ctx sessionctx.Context, args []Exp
}
argTps := make([]types.EvalType, 0, len(args))
for i := 0; i < len(args)-1; i += 2 {
if args[i].GetType().EvalType() == types.ETString && args[i].GetType().GetCharset() == charset.CharsetBin {
return nil, json.ErrInvalidJSONCharset.GenWithStackByArgs(args[i].GetType().GetCharset())
}
argTps = append(argTps, types.ETString, types.ETJson)
}
bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETJson, argTps...)
Expand Down
14 changes: 14 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7449,6 +7449,20 @@ func TestIssue36358(t *testing.T) {
tk.MustQuery("select extract(day_microsecond from c) from t").Check(testkit.Rows("1020304050607"))
}

func TestJSONObjectWithBinaryCharset(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(a char(20), b blob);")
tk.MustExec("insert into t values ('a string', 'a binary string');")
tk.MustExec("select json_object(a, b) from t;")
tk.MustExec("select json_objectagg(a, b) from t;")
tk.MustGetErrCode("select json_object(b, a) from t;", errno.ErrInvalidJSONCharset)
err := tk.QueryToErr("select json_objectagg(b, a) from t;")
require.Error(t, err)
require.Equal(t, "[json:3144]Cannot create a JSON value from a string with CHARACTER SET 'binary'.", err.Error())
}

func TestCastJSONOpaqueValueToNumeric(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down

0 comments on commit 080a784

Please sign in to comment.