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

builtin: add oct built-in function #2835

Merged
merged 16 commits into from
Mar 17, 2017
Merged
46 changes: 45 additions & 1 deletion expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/hex"
"fmt"
"math"
"strconv"
"strings"

"github.com/juju/errors"
Expand Down Expand Up @@ -1445,7 +1446,50 @@ type builtinOctSig struct {

// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_oct
func (b *builtinOctSig) eval(row []types.Datum) (d types.Datum, err error) {
return d, errFunctionNotExists.GenByArgs("oct")
args, err := b.evalArgs(row)
if err != nil {
return d, errors.Trace(err)
}
var (
negative bool
overflow bool
)
arg := args[0]
if arg.IsNull() {
return d, nil
}
n, err := arg.ToString()
if err != nil {
return d, errors.Trace(err)
}
n = getValidPrefix(strings.TrimSpace(n), 10)
if len(n) == 0 {
d.SetString("0")
return d, nil
}
if n[0] == '-' {
negative = true
n = n[1:]
}
val, err := strconv.ParseUint(n, 10, 64)
Copy link
Member

Choose a reason for hiding this comment

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

The error is discarded.

if err != nil {
if numError, ok := err.(*strconv.NumError); ok {
if numError.Err == strconv.ErrRange {
overflow = true
} else {
return d, errors.Trace(err)
}
} else {
return d, errors.Trace(err)
}
}

if negative && !overflow {
val = -val
}
str := strconv.FormatUint(val, 8)
d.SetString(str)
return d, nil
}

type ordFunctionClass struct {
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 @@ -1009,3 +1009,48 @@ func (s *testEvaluatorSuite) TestMakeSet(c *C) {
c.Assert(r, testutil.DatumEquals, types.NewDatum(t.ret))
}
}

func (s *testEvaluatorSuite) TestOct(c *C) {
defer testleak.AfterTest(c)()
octCases := []struct {
origin interface{}
ret string
}{
{"-2.7", "1777777777777777777776"},
{-1.5, "1777777777777777777777"},
{-1, "1777777777777777777777"},
{"0", "0"},
{"1", "1"},
{"8", "10"},
{"12", "14"},
{"20", "24"},
{"100", "144"},
{"1024", "2000"},
{"2048", "4000"},
Copy link
Member

Choose a reason for hiding this comment

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

Please add cases for different origin types like integer and float.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, add some negative numbers

Copy link
Member

Choose a reason for hiding this comment

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

Please add a string that is larger than max uint64.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.
should output the maximum value after exceeding unint64.

{1.0, "1"},
{9.5, "11"},
{13, "15"},
{1025, "2001"},
{"8a8", "10"},
{"abc", "0"},
//overflow uint64
{"9999999999999999999999999", "1777777777777777777777"},
{"-9999999999999999999999999", "1777777777777777777777"},
}
fc := funcs[ast.Oct]
for _, test := range octCases {
in := types.NewDatum(test.origin)
f, _ := fc.getFunction(datumsToConstants([]types.Datum{in}), s.ctx)
r, err := f.eval(nil)
c.Assert(err, IsNil)
res, err := r.ToString()
c.Assert(err, IsNil)
c.Assert(res, Equals, test.ret)
}
// test NULL input for sha
var argNull types.Datum
f, _ := fc.getFunction(datumsToConstants([]types.Datum{argNull}), s.ctx)
r, err := f.eval(nil)
c.Assert(err, IsNil)
c.Assert(r.IsNull(), IsTrue)
}
2 changes: 1 addition & 1 deletion plan/typeinferer.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) {
"concat", "concat_ws", "left", "lcase", "lower", "repeat",
"replace", "ucase", "upper", "convert", "substring",
"substring_index", "trim", "ltrim", "rtrim", "reverse", "hex", "unhex",
"date_format", "rpad", "lpad", "char_func", "conv", "make_set":
"date_format", "rpad", "lpad", "char_func", "conv", "make_set", "oct":
tp = types.NewFieldType(mysql.TypeVarString)
chs = v.defaultCharset
case "strcmp", "isnull", "bit_length", "char_length", "character_length", "crc32", "timestampdiff", "sign":
Expand Down
1 change: 1 addition & 0 deletions plan/typeinferer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ func (ts *testTypeInferrerSuite) TestInferType(c *C) {
{`coalesce(1, "1" + 1)`, mysql.TypeDouble, charset.CharsetBin},
{`coalesce(1, "abc")`, mysql.TypeVarString, charset.CharsetUTF8},
{`make_set(1 | 3, "hello", "nice", null, "world")`, mysql.TypeVarString, charset.CharsetUTF8},
{`oct(12)`, mysql.TypeVarString, charset.CharsetUTF8},
{`exp(1)`, mysql.TypeDouble, charset.CharsetBin},
{`exp(1.23)`, mysql.TypeDouble, charset.CharsetBin},
{`exp('1.23')`, mysql.TypeDouble, charset.CharsetBin},
Expand Down