Skip to content

Commit

Permalink
add func
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiaoTeo committed Dec 18, 2019
1 parent 2d25bdc commit dae3e8a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 63 deletions.
18 changes: 10 additions & 8 deletions expression/builtin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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 {
Expand Down
59 changes: 4 additions & 55 deletions expression/builtin_math_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"math"
"math/rand"
"strconv"
"strings"
"time"

"github.com/cznic/mathutil"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dae3e8a

Please sign in to comment.