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
31 changes: 30 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 @@ -1412,7 +1413,35 @@ 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
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 negative {
val = -val
}
str := strconv.FormatUint(val, 8)
d.SetString(str)
return d, nil
}

type ordFunctionClass struct {
Expand Down
42 changes: 42 additions & 0 deletions expression/builtin_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -983,3 +983,45 @@ func (s *testEvaluatorSuite) TestLpad(c *C) {
}
}
}

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"},
}
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)
}
3 changes: 3 additions & 0 deletions plan/typeinferer.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) {
tp = types.NewFieldType(mysql.TypeVarString)
chs = v.defaultCharset
tp.Flen = 40
case ast.Oct:
Copy link
Contributor

Choose a reason for hiding this comment

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

You can put this case to line 374.

tp = types.NewFieldType(mysql.TypeVarString)
Copy link
Member

Choose a reason for hiding this comment

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

Please set charset.

chs = v.defaultCharset
case ast.Coalesce:
tp = aggArgsType(x.Args)
default:
Expand Down
1 change: 1 addition & 0 deletions plan/typeinferer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func (ts *testTypeInferrerSuite) TestInferType(c *C) {
{`coalesce(null, 0.1)`, mysql.TypeNewDecimal, charset.CharsetBin},
{`coalesce(1, "1" + 1)`, mysql.TypeDouble, charset.CharsetBin},
{`coalesce(1, "abc")`, mysql.TypeVarString, charset.CharsetUTF8},
{`oct(12)`, mysql.TypeVarString, charset.CharsetUTF8},
}
for _, ca := range cases {
ctx := testKit.Se.(context.Context)
Expand Down