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: rewrite builtin function: EXPORT_SET #4434

Merged
merged 14 commits into from Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 45 additions & 46 deletions expression/builtin_string.go
Expand Up @@ -2372,62 +2372,62 @@ func (c *exportSetFunctionClass) getFunction(ctx context.Context, args []Express
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
sig := &builtinExportSetSig{newBaseBuiltinFunc(args, ctx)}
argTps := make([]evalTp, 0, 5)
argTps = append(argTps, tpInt, tpString, tpString)
if len(args) > 3 {
argTps = append(argTps, tpString)
}
if len(args) > 4 {
argTps = append(argTps, tpInt)
}
bf := newBaseBuiltinFuncWithTp(args, ctx, tpString, argTps...)
bf.tp.Flen = mysql.MaxBlobWidth
sig := &builtinExportSetSig{baseStringBuiltinFunc{bf}}
Copy link
Contributor

Choose a reason for hiding this comment

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

split it into 3 sigs.

return sig.setSelf(sig), nil
}

type builtinExportSetSig struct {
baseBuiltinFunc
baseStringBuiltinFunc
}

// eval evals a builtinExportSetSig.
// evalString evals EXPORT_SET(bits,on,off[,separator[,number_of_bits]]).
Copy link
Contributor

Choose a reason for hiding this comment

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

update comments.
so as the others

// See https://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_export-set
func (b *builtinExportSetSig) eval(row []types.Datum) (d types.Datum, err error) {
args, err := b.evalArgs(row)
if err != nil {
return d, errors.Trace(err)
func (b *builtinExportSetSig) evalString(row []types.Datum) (string, bool, error) {
sc := b.ctx.GetSessionVars().StmtCtx
bits, isNull, err := b.args[0].EvalInt(row, sc)
if isNull || err != nil {
return "", true, errors.Trace(err)
}
var (
bits uint64
on string
off string
separator = ","
numberOfBits = 64
)
switch len(args) {
case 5:
var arg int64
arg, err = args[4].ToInt64(b.ctx.GetSessionVars().StmtCtx)
if err != nil {
return d, errors.Trace(err)
}
if arg >= 0 && arg < 64 {
numberOfBits = int(arg)
}
fallthrough
case 4:
separator, err = args[3].ToString()
if err != nil {
return d, errors.Trace(err)
}
fallthrough
case 3:
arg, err := args[0].ToInt64(b.ctx.GetSessionVars().StmtCtx)
if err != nil {
return d, errors.Trace(err)

on, isNull, err := b.args[1].EvalString(row, sc)
if isNull || err != nil {
return "", true, errors.Trace(err)
}

off, isNull, err := b.args[2].EvalString(row, sc)
if isNull || err != nil {
return "", true, errors.Trace(err)
}

separator, numberOfBits := ",", int64(64)
if len(b.args) > 3 {
separator, isNull, err = b.args[3].EvalString(row, sc)
if isNull || err != nil {
return "", true, errors.Trace(err)
}
bits = uint64(arg)
on, err = args[1].ToString()
if err != nil {
return d, errors.Trace(err)
}
if len(b.args) > 4 {
numberOfBits, isNull, err = b.args[4].EvalInt(row, sc)
if isNull || err != nil {
return "", true, errors.Trace(err)
}
off, err = args[2].ToString()
if err != nil {
return d, errors.Trace(err)
if numberOfBits < 0 || numberOfBits > 64 {
numberOfBits = 64
}
}
var result string
for i := 0; i < numberOfBits; i++ {

result := ""
for i := int64(0); i < numberOfBits; i++ {
if bits&1 > 0 {
result += on
} else {
Expand All @@ -2438,8 +2438,7 @@ func (b *builtinExportSetSig) eval(row []types.Datum) (d types.Datum, err error)
result += separator
}
}
d.SetString(result)
return d, nil
return result, false, nil
}

type formatFunctionClass struct {
Expand Down
2 changes: 2 additions & 0 deletions expression/builtin_string_test.go
Expand Up @@ -1678,6 +1678,8 @@ func (s *testEvaluatorSuite) TestExportSet(c *C) {
for _, t := range estd {
f, err := fc.getFunction(s.ctx, datumsToConstants(types.MakeDatums(t.argLst...)))
c.Assert(err, IsNil)
c.Assert(f, NotNil)
c.Assert(f.canBeFolded(), IsTrue)
exportSetRes, err := f.eval(nil)
res, err := exportSetRes.ToString()
c.Assert(err, IsNil)
Expand Down
14 changes: 14 additions & 0 deletions expression/integration_test.go
Expand Up @@ -834,6 +834,20 @@ func (s *testIntegrationSuite) TestStringBuiltin(c *C) {
result.Check(testkit.Rows("'aaaa' '' '\"\"' '\n\n'"))
result = tk.MustQuery(`select quote(0121), quote(0000), quote("中文"), quote(NULL);`)
result.Check(testkit.Rows("'121' '0' '中文' <nil>"))

// for export_set
result = tk.MustQuery(`select export_set(7, "1", "0", ",", 65);`)
result.Check(testkit.Rows("1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"))
result = tk.MustQuery(`select export_set(7, "1", "0", ",", -1);`)
result.Check(testkit.Rows("1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"))
result = tk.MustQuery(`select export_set(7, "1", "0", ",");`)
result.Check(testkit.Rows("1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"))
result = tk.MustQuery(`select export_set(7, "1", "0");`)
result.Check(testkit.Rows("1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"))
result = tk.MustQuery(`select export_set(NULL, "1", "0", ",", 65);`)
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery(`select export_set(7, "1", "0", ",", 1);`)
result.Check(testkit.Rows("1"))
}

func (s *testIntegrationSuite) TestEncryptionBuiltin(c *C) {
Expand Down
4 changes: 4 additions & 0 deletions plan/typeinfer_test.go
Expand Up @@ -424,6 +424,10 @@ func (s *testPlanSuite) createTestCase4StrFuncs() []typeInferTestCase {
{"quote(c_bigint_d )", mysql.TypeVarString, charset.CharsetUTF8, 0, 42, types.UnspecifiedLength},
{"quote(c_float_d )", mysql.TypeVarString, charset.CharsetUTF8, 0, 26, types.UnspecifiedLength},
{"quote(c_double_d )", mysql.TypeVarString, charset.CharsetUTF8, 0, 46, types.UnspecifiedLength},

{"export_set(c_double_d, c_text_d, c_text_d)", mysql.TypeLongBlob, charset.CharsetUTF8, 0, mysql.MaxBlobWidth, types.UnspecifiedLength},
{"export_set(c_double_d, c_text_d, c_text_d, c_text_d)", mysql.TypeLongBlob, charset.CharsetUTF8, 0, mysql.MaxBlobWidth, types.UnspecifiedLength},
{"export_set(c_double_d, c_text_d, c_text_d, c_text_d, c_int_d)", mysql.TypeLongBlob, charset.CharsetUTF8, 0, mysql.MaxBlobWidth, types.UnspecifiedLength},
}
}

Expand Down