From 5534ce078f9ff3c3d05e8d7ba8d84c392a02c0a5 Mon Sep 17 00:00:00 2001 From: jackyan Date: Mon, 9 Sep 2019 11:01:05 +0800 Subject: [PATCH] Add builtinIfJSON --- expression/bench_test.go | 9 +++- expression/builtin_control_vec.go | 75 +++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 expression/builtin_control_vec.go diff --git a/expression/bench_test.go b/expression/bench_test.go index c1a626ff934aa..3dbd2e14b9be3 100644 --- a/expression/bench_test.go +++ b/expression/bench_test.go @@ -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) { @@ -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++ } diff --git a/expression/builtin_control_vec.go b/expression/builtin_control_vec.go new file mode 100644 index 0000000000000..fb03b06a2427c --- /dev/null +++ b/expression/builtin_control_vec.go @@ -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 +}