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

*: add buildin function CONV #2390

Merged
merged 9 commits into from Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
83 changes: 81 additions & 2 deletions expression/builtin_math.go
Expand Up @@ -21,6 +21,8 @@ import (
"hash/crc32"
"math"
"math/rand"
"strconv"
"strings"

"github.com/juju/errors"
"github.com/pingcap/tidb/context"
Expand Down Expand Up @@ -190,8 +192,85 @@ func builtinRound(args []types.Datum, ctx context.Context) (d types.Datum, err e

// See http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_conv
func builtinConv(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
//TODO implement
return d, errors.New("Function unimplement")
var (
n string
toBase int64
fromBase int64
signed bool
negative bool
touval bool
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the meaning of touval?

Copy link
Member Author

Choose a reason for hiding this comment

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

it means when tobase < 0 , we let n to unsigned ignore the sign. example -15 to 15, 15 to 15. if tobase > 0, n will be forced conversion.

)
for _, arg := range args {
if arg.IsNull() {
return d, nil
}
}
n, err = args[0].ToString()
Copy link
Contributor

Choose a reason for hiding this comment

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

Use := instead of =, and remove line 196.
The same as line 213, line 217 and line 233.

if err != nil {
return d, errors.Trace(err)
}
sc := ctx.GetSessionVars().StmtCtx
fromBase, err = args[1].ToInt64(sc)
if err != nil {
return d, errors.Trace(err)
}
toBase, err = args[2].ToInt64(sc)
if err != nil {
return d, errors.Trace(err)
}

if fromBase < 0 {
fromBase = -fromBase
signed = true
}
if toBase < 0 {
touval = true
toBase = -toBase
}
if fromBase > 36 || fromBase < 2 || toBase > 36 || toBase < 2 {
return d, nil
}
n = getValidPrefix(n, fromBase)
Copy link
Member

Choose a reason for hiding this comment

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

We should trim 'n' before calling getValidPrefix.

if len(n) == 0 {
return d, nil
}
if n[0] == '-' {
negative = true
n = n[1:]
}

val, err := strconv.ParseUint(n, int(fromBase), 64)
if err != nil {
return d, errors.Trace(types.ErrOverflow)
}
//Ref https://github.com/mysql/mysql-server/blob/5.7/strings/ctype-simple.c#L598
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a space after the slash. And using See instead of Ref is better.
The same as line258.

if signed {
if negative && val > -math.MinInt64 {
val = -math.MinInt64
}
if !negative && val > math.MaxInt64 {
val = math.MaxInt64
}
}
if negative {
val = -val
}
// Ref https://github.com/mysql/mysql-server/blob/5.7/strings/longlong2str.c#L58
if int64(val) < 0 {
negative = true
} else {
negative = false
}
if touval && negative {
val = 0 - val
}

s := strconv.FormatUint(val, int(toBase))
if negative && touval {
s = "-" + s
}
d.SetString(strings.ToUpper(s))
return d, nil
}

// See http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_crc32
Expand Down
37 changes: 37 additions & 0 deletions expression/builtin_math_test.go
Expand Up @@ -191,3 +191,40 @@ func (s *testEvaluatorSuite) TestCRC32(c *C) {
c.Assert(v, testutil.DatumEquals, t["Ret"][0])
}
}

func (s *testEvaluatorSuite) TestConv(c *C) {
defer testleak.AfterTest(c)()
tbl := []struct {
Arg []interface{}
Ret interface{}
}{
{[]interface{}{"a", 16, 2}, "1010"},
{[]interface{}{"6E", 18, 8}, "172"},
{[]interface{}{"-17", 10, -18}, "-H"},
{[]interface{}{"-17", 10, 18}, "2D3FGB0B9CG4BD1H"},
{[]interface{}{nil, 10, 10}, nil},
{[]interface{}{"+18aZ", 7, 36}, 1},
Copy link
Member

Choose a reason for hiding this comment

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

Can we handle this case: "SELECT CONV(10+'10'+'10'+X'0a',10,10);" ?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes we can, constant folding will handle it.

Copy link
Member

Choose a reason for hiding this comment

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

ok, please add this test case.

Copy link
Member Author

Choose a reason for hiding this comment

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

10+'10'+'10'+X'0a' will handle firstly by opfunction, add in here will no work.

Copy link
Contributor

Choose a reason for hiding this comment

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

Add a test with a negative arg[1].

}

Dtbl := tblToDtbl(tbl)

for _, t := range Dtbl {
v, err := builtinConv(t["Arg"], s.ctx)
c.Assert(err, IsNil)
c.Assert(v, testutil.DatumEquals, t["Ret"][0])
}

v := []struct {
s string
base int64
ret string
}{
{"-123456D1f", 5, "-1234"},
{"+12azD", 16, "12a"},
{"+", 12, ""},
}
for _, t := range v {
r := getValidPrefix(t.s, t.base)
c.Assert(r, Equals, t.ret)
}
}
41 changes: 41 additions & 0 deletions expression/util.go
Expand Up @@ -14,6 +14,8 @@
package expression

import (
"unicode"

"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/sessionctx/variable"
Expand Down Expand Up @@ -98,3 +100,42 @@ func calculateSum(sc *variable.StatementContext, sum, v types.Datum) (data types
return data, errors.Errorf("invalid value %v for aggregate", sum.Kind())
}
}

// getValidPrefix gets a prefix of string which can parsed to a number with base. the minimun base is 2 and the maximum is 36.
func getValidPrefix(s string, base int64) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function can be write more elegant, Rest LGTM

Copy link
Member

Choose a reason for hiding this comment

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

Can you add some unit test cases for this function?

Copy link
Member Author

Choose a reason for hiding this comment

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

ok

var (
validLen int
upper rune
)
switch {
case base >= 2 && base <= 9:
upper = rune('0' + base)
case base <= 36:
upper = rune('A' + base - 10)
default:
return ""
}
Loop:
for i := 0; i < len(s); i++ {
c := rune(s[i])
switch {
case unicode.IsDigit(c) || unicode.IsLower(c) || unicode.IsUpper(c):
c = unicode.ToUpper(c)
if c < upper {
validLen = i + 1
} else {
break Loop
}
case c == '+' || c == '-':
if i != 0 {
break Loop
}
default:
break Loop
}
}
if validLen > 1 && s[0] == '+' {
return s[1:validLen]
}
return s[:validLen]
}
2 changes: 1 addition & 1 deletion plan/typeinferer.go
Expand Up @@ -311,7 +311,7 @@ func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) {
case "dayname", "version", "database", "user", "current_user", "schema",
"concat", "concat_ws", "left", "lcase", "lower", "repeat",
"replace", "ucase", "upper", "convert", "substring",
"substring_index", "trim", "ltrim", "rtrim", "reverse", "hex", "unhex", "date_format", "rpad", "char_func":
"substring_index", "trim", "ltrim", "rtrim", "reverse", "hex", "unhex", "date_format", "rpad", "char_func", "conv":
tp = types.NewFieldType(mysql.TypeVarString)
chs = v.defaultCharset
case "strcmp", "isnull", "bit_length", "char_length", "character_length", "crc32":
Expand Down
1 change: 1 addition & 0 deletions plan/typeinferer_test.go
Expand Up @@ -163,6 +163,7 @@ func (ts *testTypeInferrerSuite) TestInferType(c *C) {
{"char_length('TiDB')", mysql.TypeLonglong, charset.CharsetBin},
{"character_length('TiDB')", mysql.TypeLonglong, charset.CharsetBin},
{"crc32('TiDB')", mysql.TypeLonglong, charset.CharsetBin},
{"conv('TiDB',36,10)", mysql.TypeVarString, charset.CharsetUTF8},
}
for _, ca := range cases {
ctx := testKit.Se.(context.Context)
Expand Down