Skip to content

Commit

Permalink
expression: implement vectorized evaluation for builtinInetAtonSig (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyCpp authored and qw4990 committed Nov 11, 2019
1 parent 88e96eb commit 99720f1
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
24 changes: 24 additions & 0 deletions expression/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,18 @@ func (g *defaultGener) gen() interface{} {
return nil
}

// selectStringGener select one string randomly from the candidates array
type selectStringGener struct {
candidates []string
}

func (g *selectStringGener) gen() interface{} {
if len(g.candidates) == 0 {
return nil
}
return g.candidates[rand.Intn(len(g.candidates))]
}

type constJSONGener struct {
jsonStr string
}
Expand Down Expand Up @@ -392,6 +404,18 @@ func (g *ipv6StrGener) gen() interface{} {
return ip.String()
}

// ipv4StrGener is used to generate ipv4 strings. For example 111.111.111.111
type ipv4StrGener struct {
}

func (g *ipv4StrGener) gen() interface{} {
var ip net.IP = make([]byte, net.IPv4len)
for i := range ip {
ip[i] = uint8(rand.Intn(256))
}
return ip.String()
}

// ipv6ByteGener is used to generate ipv6 address in 16 bytes string.
type ipv6ByteGener struct {
}
Expand Down
69 changes: 67 additions & 2 deletions expression/builtin_miscellaneous_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,76 @@ func (b *builtinTimeAnyValueSig) vecEvalTime(input *chunk.Chunk, result *chunk.C
}

func (b *builtinInetAtonSig) vectorized() bool {
return false
return true
}

func (b *builtinInetAtonSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("not implemented")
n := input.NumRows()
buf, err := b.bufAllocator.get(types.ETString, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf)
if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil {
return err
}
var (
byteResult, res uint64
dotCount int
)
result.ResizeInt64(n, false)
i64s := result.Int64s()
result.MergeNulls(buf)
for i := 0; i < n; i++ {
if result.IsNull(i) {
continue
}
ipAddr := buf.GetString(i)
if len(ipAddr) == 0 || ipAddr[len(ipAddr)-1] == '.' {
// ip address should not end with '.'.
result.SetNull(i, true)
continue
}
//reset
byteResult = 0
res = 0
dotCount = 0
for _, c := range ipAddr {
if c >= '0' && c <= '9' {
digit := uint64(c - '0')
byteResult = byteResult*10 + digit
if byteResult > 255 {
result.SetNull(i, true)
break
}
} else if c == '.' {
dotCount++
if dotCount > 3 {
result.SetNull(i, true)
break
}
res = (res << 8) + byteResult
byteResult = 0
} else {
result.SetNull(i, true)
break // illegal char (not number or .)
}
}
// 127 -> 0.0.0.127
// 127.255 -> 127.0.0.255
// 127.256 -> NULL
// 127.2.1 -> 127.2.0.1
if !result.IsNull(i) {
if dotCount == 1 {
res <<= 16
}
if dotCount == 2 {
res <<= 8
}
i64s[i] = int64((res << 8) + byteResult)
}
}
return nil
}

func (b *builtinInet6NtoaSig) vectorized() bool {
Expand Down
17 changes: 16 additions & 1 deletion expression/builtin_miscellaneous_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,22 @@ var vecBuiltinMiscellaneousCases = map[string][]vecExprBenchCase{
ast.Sleep: {},
ast.UUID: {},
ast.Inet6Ntoa: {},
ast.InetAton: {},
ast.InetAton: {
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETString}, geners: []dataGenerator{&ipv4StrGener{}}},
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETString}},
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETString}, geners: []dataGenerator{
&selectStringGener{
candidates: []string{
"11.11.11.11.", // last char is .
"266.266.266.266", // int in string exceed 255
"127",
".122",
".123.123",
"127.255",
"127.2.1",
},
}}},
},
ast.IsIPv4Mapped: {
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETString}, geners: []dataGenerator{&ipv4MappedByteGener{}}},
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETString}, geners: []dataGenerator{&ipv6ByteGener{}}},
Expand Down

0 comments on commit 99720f1

Please sign in to comment.