Skip to content

Commit

Permalink
expression: implement vectorized evaluation for builtinIfJSONSig (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Reminiscent authored and qw4990 committed Sep 9, 2019
1 parent b5bb641 commit 15557df
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
9 changes: 7 additions & 2 deletions expression/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ var vecExprBenchCases = map[string][]vecExprBenchCase{
ast.Log10: {
{types.ETReal, []types.EvalType{types.ETReal}, nil},
},
ast.If: {
{types.ETJson, []types.EvalType{types.ETInt, types.ETJson, types.ETJson}, nil},
},
}

func fillColumn(eType types.EvalType, chk *chunk.Chunk, colIdx int, testCase vecExprBenchCase) {
Expand Down Expand Up @@ -585,11 +588,13 @@ func (s *testEvaluatorSuite) TestVectorizedBuiltinFunc(c *C) {
c.Assert(err, IsNil)
vecWarnCnt = ctx.GetSessionVars().StmtCtx.WarningCount()
for row := it.Begin(); row != it.End(); row = it.Next() {
val, isNull, err := baseFunc.evalDuration(row)
val, isNull, err := baseFunc.evalJSON(row)
c.Assert(err, IsNil)
c.Assert(isNull, Equals, output.IsNull(i))
if !isNull {
c.Assert(val, Equals, output.GetJSON(i))
var cmp int
cmp = json.CompareBinary(val, output.GetJSON(i))
c.Assert(cmp, Equals, 0)
}
i++
}
Expand Down
75 changes: 75 additions & 0 deletions expression/builtin_control_vec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package expression

import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
)

func (b *builtinIfJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk.Column) error {
n := input.NumRows()
buf0, err := b.bufAllocator.get(types.ETInt, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf0)
if err := b.args[0].VecEvalInt(b.ctx, input, buf0); err != nil {
return err
}

buf1, err := b.bufAllocator.get(types.ETJson, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf1)
if err := b.args[1].VecEvalJSON(b.ctx, input, buf1); err != nil {
return err
}

buf2, err := b.bufAllocator.get(types.ETJson, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf2)
if err := b.args[2].VecEvalJSON(b.ctx, input, buf2); err != nil {
return err
}

result.ReserveJSON(n)
arg0 := buf0.Int64s()
for i := 0; i < n; i++ {
arg := arg0[i]
isNull0 := buf0.IsNull(i)
switch {
case isNull0 || arg == 0:
if buf2.IsNull(i) {
result.AppendNull()
} else {
result.AppendJSON(buf2.GetJSON(i))
}
case arg != 0:
if buf1.IsNull(i) {
result.AppendNull()
} else {
result.AppendJSON(buf1.GetJSON(i))
}
}
}
return nil
}

func (b *builtinIfJSONSig) vectorized() bool {
return true
}

0 comments on commit 15557df

Please sign in to comment.