Skip to content

Commit

Permalink
This is an automated cherry-pick of #57484
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
lcwangchao authored and ti-chi-bot committed Feb 7, 2025
1 parent 116a7f9 commit 4ae4df5
Show file tree
Hide file tree
Showing 9 changed files with 2,766 additions and 0 deletions.
17 changes: 17 additions & 0 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,27 @@ func (c *castAsStringFunctionClass) getFunction(ctx sessionctx.Context, args []E
if err != nil {
return nil, err
}
<<<<<<< HEAD:expression/builtin_cast.go
if args[0].GetType().Hybrid() {
sig = &builtinCastStringAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsString)
return sig, nil
=======
if ft := args[0].GetType(ctx.GetEvalCtx()); ft.Hybrid() {
castBitAsUnBinary := ft.GetType() == mysql.TypeBit && c.tp.GetCharset() != charset.CharsetBin
if !castBitAsUnBinary {
sig = &builtinCastStringAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsString)
return sig, nil
}
// for type BIT, it maybe an invalid value for the specified charset, we need to convert it to binary first,
// and then convert it to the specified charset with `HandleBinaryLiteral` in the following code.
tp := types.NewFieldType(mysql.TypeString)
tp.SetCharset(charset.CharsetBin)
tp.SetCollate(charset.CollationBin)
tp.AddFlag(mysql.BinaryFlag)
args[0] = BuildCastFunction(ctx, args[0], tp)
>>>>>>> 38104f4f328 (expression: fix tikv crash when `bool like cast(bit as char)` (#57484)):pkg/expression/builtin_cast.go
}
argTp := args[0].GetType().EvalType()
switch argTp {
Expand Down
1 change: 1 addition & 0 deletions expression/builtin_convert_charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ type builtinInternalFromBinarySig struct {
func (b *builtinInternalFromBinarySig) Clone() builtinFunc {
newSig := &builtinInternalFromBinarySig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
newSig.cannotConvertStringAsWarning = b.cannotConvertStringAsWarning
return newSig
}

Expand Down
30 changes: 30 additions & 0 deletions expression/expr_to_pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,37 @@ func TestExprPushDownToTiKV(t *testing.T) {
require.NoError(t, err)
exprs = append(exprs, function)

<<<<<<< HEAD:expression/expr_to_pb_test.go
pushed, remained := PushDownExprs(sc, exprs, client, kv.TiKV)
=======
ctx := mock.NewContext()
pushDownCtx := NewPushDownContextFromSessionVars(ctx, ctx.GetSessionVars(), client)
pushed, remained := PushDownExprs(pushDownCtx, exprs, kv.TiKV)
require.Len(t, pushed, 0)
require.Len(t, remained, len(exprs))

// Test Conv function, `conv` function for a BIT column should not be pushed down for its special behavior which
// is only handled in TiDB currently.
// see issue: https://github.com/pingcap/tidb/issues/51877
exprs = exprs[:0]
function, err = NewFunction(mock.NewContext(), ast.Conv, types.NewFieldType(mysql.TypeString), stringColumn, intColumn, intColumn)
require.NoError(t, err)
exprs = append(exprs, function)
pushed, remained = PushDownExprs(pushDownCtx, exprs, kv.TiKV)
require.Len(t, pushed, len(exprs))
require.Len(t, remained, 0)
exprs = exprs[:0]
// when conv a column with type BIT, a cast function will be used to cast bit to a binary string
castTp := types.NewFieldType(mysql.TypeString)
castTp.SetCharset(charset.CharsetBin)
castTp.SetCollate(charset.CollationBin)
castByteAsStringFunc, err := NewFunction(mock.NewContext(), ast.Cast, castTp, byteColumn)
require.NoError(t, err)
function, err = NewFunction(mock.NewContext(), ast.Conv, types.NewFieldType(mysql.TypeString), castByteAsStringFunc, intColumn, intColumn)
require.NoError(t, err)
exprs = append(exprs, function)
pushed, remained = PushDownExprs(pushDownCtx, exprs, kv.TiKV)
>>>>>>> 38104f4f328 (expression: fix tikv crash when `bool like cast(bit as char)` (#57484)):pkg/expression/expr_to_pb_test.go
require.Len(t, pushed, 0)
require.Len(t, remained, len(exprs))

Expand Down
Loading

0 comments on commit 4ae4df5

Please sign in to comment.