Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: handle max_allowed_packet warnings for repeat function. #7181

Merged
merged 9 commits into from
Aug 17, 2018
17 changes: 15 additions & 2 deletions expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,17 +555,24 @@ func (c *repeatFunctionClass) getFunction(ctx sessionctx.Context, args []Express
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETString, types.ETString, types.ETInt)
bf.tp.Flen = mysql.MaxBlobWidth
SetBinFlagOrBinStr(args[0].GetType(), bf.tp)
sig := &builtinRepeatSig{bf}
valStr, _ := ctx.GetSessionVars().GetSystemVar(variable.MaxAllowedPacket)
maxAllowedPacket, err := strconv.ParseUint(valStr, 10, 64)
if err != nil {
return nil, errors.Trace(err)
}
sig := &builtinRepeatSig{bf, maxAllowedPacket}
return sig, nil
}

type builtinRepeatSig struct {
baseBuiltinFunc
maxAllowedPacket uint64
}

func (b *builtinRepeatSig) Clone() builtinFunc {
newSig := &builtinRepeatSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
newSig.maxAllowedPacket = b.maxAllowedPacket
return newSig
}

Expand All @@ -576,6 +583,7 @@ func (b *builtinRepeatSig) evalString(row chunk.Row) (d string, isNull bool, err
if isNull || err != nil {
return "", isNull, errors.Trace(err)
}
byteLength := len(str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this is rune length, not byte length?
I think we need to add some test cased for multi-byte characters, like Chinese character.

Copy link
Contributor Author

@hhu-cc hhu-cc Aug 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Len of String will return the byte length, you can check it on https://golang.org/pkg/builtin/#len .
I have added some test of Chinese character.


num, isNull, err := b.args[1].EvalInt(b.ctx, row)
if isNull || err != nil {
Expand All @@ -588,7 +596,12 @@ func (b *builtinRepeatSig) evalString(row chunk.Row) (d string, isNull bool, err
num = math.MaxInt32
}

if int64(len(str)) > int64(b.tp.Flen)/num {
if uint64(byteLength)*uint64(num) > b.maxAllowedPacket {
b.ctx.GetSessionVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenByArgs("repeat", b.maxAllowedPacket))
return "", true, nil
}

if int64(byteLength) > int64(b.tp.Flen)/num {
return "", true, nil
}
return strings.Repeat(str, int(num)), false, nil
Expand Down
45 changes: 45 additions & 0 deletions expression/builtin_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,51 @@ func (s *testEvaluatorSuite) TestRepeat(c *C) {
c.Assert(v.GetString(), Equals, "")
}

func (s *testEvaluatorSuite) TestRepeatSig(c *C) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you refine the test?
Maybe you can keep style with another tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

colTypes := []*types.FieldType{
{Tp: mysql.TypeVarchar},
{Tp: mysql.TypeLonglong},
}
resultType := &types.FieldType{Tp: mysql.TypeVarchar, Flen: 1000}
args := []Expression{
&Column{Index: 0, RetType: colTypes[0]},
&Column{Index: 1, RetType: colTypes[1]},
}
base := baseBuiltinFunc{args: args, ctx: s.ctx, tp: resultType}
repeat := &builtinRepeatSig{base, 1000}

cases := []struct {
args []interface{}
warning int
res string
}{
{[]interface{}{"a", int64(6)}, 0, "aaaaaa"},
{[]interface{}{"a", int64(10001)}, 1, ""},
{[]interface{}{"毅", int64(6)}, 0, "毅毅毅毅毅毅"},
{[]interface{}{"毅", int64(334)}, 2, ""},
}

for _, t := range cases {
input := chunk.NewChunkWithCapacity(colTypes, 10)
input.AppendString(0, t.args[0].(string))
input.AppendInt64(1, t.args[1].(int64))

res, isNull, err := repeat.evalString(input.GetRow(0))
c.Assert(res, Equals, t.res)
c.Assert(err, IsNil)
if t.warning == 0 {
c.Assert(isNull, IsFalse)
} else {
c.Assert(isNull, IsTrue)
c.Assert(err, IsNil)
warnings := s.ctx.GetSessionVars().StmtCtx.GetWarnings()
c.Assert(len(warnings), Equals, t.warning)
lastWarn := warnings[len(warnings)-1]
c.Assert(terror.ErrorEqual(errWarnAllowedPacketOverflowed, lastWarn.Err), IsTrue)
}
}
}

func (s *testEvaluatorSuite) TestLower(c *C) {
defer testleak.AfterTest(c)()
cases := []struct {
Expand Down