From dae3e8a69b1a4ef82ea9d5f68551bd00cc4e7322 Mon Sep 17 00:00:00 2001 From: ZhaoQian888 <1900022850@pku.edu.cn> Date: Wed, 18 Dec 2019 21:51:36 +0800 Subject: [PATCH] add func --- expression/builtin_math.go | 18 ++++++----- expression/builtin_math_vec.go | 59 +++------------------------------- 2 files changed, 14 insertions(+), 63 deletions(-) diff --git a/expression/builtin_math.go b/expression/builtin_math.go index 59baf23e048c0..9d0e39cc9ac55 100644 --- a/expression/builtin_math.go +++ b/expression/builtin_math.go @@ -1125,7 +1125,7 @@ func (b *builtinConvSig) Clone() builtinFunc { // evalString evals CONV(N,from_base,to_base). // See https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_conv. func (b *builtinConvSig) evalString(row chunk.Row) (res string, isNull bool, err error) { - n, isNull, err := b.args[0].EvalString(b.ctx, row) + str, isNull, err := b.args[0].EvalString(b.ctx, row) if isNull || err != nil { return res, isNull, err } @@ -1139,7 +1139,9 @@ func (b *builtinConvSig) evalString(row chunk.Row) (res string, isNull bool, err if isNull || err != nil { return res, isNull, err } - + return b.conv(str, fromBase, toBase) +} +func (b *builtinConvSig) conv(str string, fromBase, toBase int64) (res string, isNull bool, err error) { var ( signed bool negative bool @@ -1159,19 +1161,19 @@ func (b *builtinConvSig) evalString(row chunk.Row) (res string, isNull bool, err return res, true, nil } - n = getValidPrefix(strings.TrimSpace(n), fromBase) - if len(n) == 0 { + str = getValidPrefix(strings.TrimSpace(str), fromBase) + if len(str) == 0 { return "0", false, nil } - if n[0] == '-' { + if str[0] == '-' { negative = true - n = n[1:] + str = str[1:] } - val, err := strconv.ParseUint(n, int(fromBase), 64) + val, err := strconv.ParseUint(str, int(fromBase), 64) if err != nil { - return res, false, types.ErrOverflow.GenWithStackByArgs("BIGINT UNSINGED", n) + return res, false, types.ErrOverflow.GenWithStackByArgs("BIGINT UNSINGED", str) } if signed { if negative && val > -math.MinInt64 { diff --git a/expression/builtin_math_vec.go b/expression/builtin_math_vec.go index b5e8d7914e1d3..5628b89bde10c 100644 --- a/expression/builtin_math_vec.go +++ b/expression/builtin_math_vec.go @@ -19,7 +19,6 @@ import ( "math" "math/rand" "strconv" - "strings" "time" "github.com/cznic/mathutil" @@ -1081,69 +1080,19 @@ func (b *builtinConvSig) vecEvalString(input *chunk.Chunk, result *chunk.Column) result.ReserveString(n) fromBase := buf2.Int64s() toBase := buf3.Int64s() - var ( - signed bool - negative bool - ignoreSign bool - ) for i := 0; i < n; i++ { if buf1.IsNull(i) || buf2.IsNull(i) || buf3.IsNull(i) { result.AppendNull() continue } - signed = false - negative = false - ignoreSign = false - if fromBase[i] < 0 { - fromBase[i] = -fromBase[i] - signed = true - } - if toBase[i] < 0 { - toBase[i] = -toBase[i] - ignoreSign = true + res, isNull, err := b.conv(buf1.GetString(i), fromBase[i], toBase[i]) + if err != nil { + return err } - if fromBase[i] > 36 || fromBase[i] < 2 || toBase[i] > 36 || toBase[i] < 2 { + if isNull { result.AppendNull() continue } - n := getValidPrefix(strings.TrimSpace(buf1.GetString(i)), fromBase[i]) - if len(n) == 0 { - result.AppendString("0") - continue - } - if n[0] == '-' { - negative = true - n = n[1:] - } - val, err := strconv.ParseUint(n, int(fromBase[i]), 64) - if err != nil { - result.AppendNull() - return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSINGED", n) - } - if signed { - if negative && val > -math.MinInt64 { - val = -math.MinInt64 - } - if !negative && val > math.MaxInt64 { - val = math.MaxInt64 - } - } - if negative { - val = -val - } - if int64(val) < 0 { - negative = true - } else { - negative = false - } - if ignoreSign && negative { - val = 0 - val - } - s := strconv.FormatUint(val, int(toBase[i])) - if negative && ignoreSign { - s = "-" + s - } - res := strings.ToUpper(s) result.AppendString(res) } return nil