Skip to content

Commit

Permalink
expression: wrap to_binary and from_binary for cast function's argume…
Browse files Browse the repository at this point in the history
…nt (#30706)
  • Loading branch information
xiongjiwei authored Dec 20, 2021
1 parent eb6c89d commit 87ab28e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
10 changes: 4 additions & 6 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,7 @@ func (c *castAsStringFunctionClass) getFunction(ctx sessionctx.Context, args []E
return nil, err
}
bf.tp = c.tp
if args[0].GetType().Hybrid() || IsBinaryLiteral(args[0]) {
// When cast from binary to some other charsets, we should check if the binary is valid or not.
// so we build a from_binary function to do this check.
ft := args[0].GetType().Clone()
ft.Charset, ft.Collate = c.tp.Charset, c.tp.Collate
bf.args[0] = BuildFromBinaryFunction(ctx, args[0], ft)
if args[0].GetType().Hybrid() {
sig = &builtinCastStringAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsString)
return sig, nil
Expand Down Expand Up @@ -318,6 +313,9 @@ func (c *castAsStringFunctionClass) getFunction(ctx sessionctx.Context, args []E
sig = &builtinCastJSONAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastJsonAsString)
case types.ETString:
// When cast from binary to some other charsets, we should check if the binary is valid or not.
// so we build a from_binary function to do this check.
bf.args[0] = HandleBinaryLiteral(ctx, args[0], &ExprCollation{Charset: c.tp.Charset, Collation: c.tp.Collate}, c.funcName)
sig = &builtinCastStringAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsString)
default:
Expand Down
3 changes: 3 additions & 0 deletions expression/builtin_cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,9 @@ func TestWrapWithCastAsString(t *testing.T) {
require.Equal(t, c.ret, res)
}
}

expr := BuildCastFunction(ctx, &Constant{RetType: types.NewFieldType(mysql.TypeEnum)}, types.NewFieldType(mysql.TypeVarString))
require.NotContains(t, expr.String(), "to_binary")
}

func TestWrapWithCastAsJSON(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_convert_charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ var convertActionMap = map[funcProp][]string{
ast.Replace, ast.Rpad, ast.SubstringIndex, ast.Trim,
/* operators */
ast.GE, ast.LE, ast.GT, ast.LT, ast.EQ, ast.NE, ast.NullEQ, ast.If, ast.Ifnull, ast.In,
ast.Case,
ast.Case, ast.Cast,
/* string comparing */
ast.Like, ast.Strcmp,
/* regex */
Expand Down

0 comments on commit 87ab28e

Please sign in to comment.