From 365e7bf2b1cd671e513a32fe4126536df5adcfaf Mon Sep 17 00:00:00 2001 From: Ye Kuang Date: Mon, 30 Sep 2019 12:06:31 +0900 Subject: [PATCH] expression: implement vectorized evaluation for `builtinUnaryNot` series functions (#12461) --- expression/builtin_op_vec.go | 76 ++++++++++++++++++++++++++++--- expression/builtin_op_vec_test.go | 6 ++- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/expression/builtin_op_vec.go b/expression/builtin_op_vec.go index 24ca5dadec..0544282cbc 100644 --- a/expression/builtin_op_vec.go +++ b/expression/builtin_op_vec.go @@ -135,11 +135,35 @@ func (b *builtinRealIsNullSig) vecEvalInt(input *chunk.Chunk, result *chunk.Colu } func (b *builtinUnaryNotRealSig) vectorized() bool { - return false + return true } func (b *builtinUnaryNotRealSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) error { - return errors.Errorf("not implemented") + n := input.NumRows() + buf, err := b.bufAllocator.get(types.ETReal, n) + if err != nil { + return err + } + defer b.bufAllocator.put(buf) + if err := b.args[0].VecEvalReal(b.ctx, input, buf); err != nil { + return err + } + f64s := buf.Float64s() + + result.ResizeInt64(n, false) + result.MergeNulls(buf) + i64s := result.Int64s() + for i := 0; i < n; i++ { + if result.IsNull(i) { + continue + } + if f64s[i] == 0 { + i64s[i] = 1 + } else { + i64s[i] = 0 + } + } + return nil } func (b *builtinLogicAndSig) vectorized() bool { @@ -262,19 +286,59 @@ func (b *builtinUnaryMinusIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.C } func (b *builtinUnaryNotDecimalSig) vectorized() bool { - return false + return true } func (b *builtinUnaryNotDecimalSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) error { - return errors.Errorf("not implemented") + n := input.NumRows() + buf, err := b.bufAllocator.get(types.ETDecimal, n) + if err != nil { + return err + } + defer b.bufAllocator.put(buf) + if err := b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { + return err + } + decs := buf.Decimals() + + result.ResizeInt64(n, false) + result.MergeNulls(buf) + i64s := result.Int64s() + for i := 0; i < n; i++ { + if result.IsNull(i) { + continue + } + if decs[i].IsZero() { + i64s[i] = 1 + } else { + i64s[i] = 0 + } + } + return nil } func (b *builtinUnaryNotIntSig) vectorized() bool { - return false + return true } func (b *builtinUnaryNotIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) error { - return errors.Errorf("not implemented") + n := input.NumRows() + if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil { + return err + } + + i64s := result.Int64s() + for i := 0; i < n; i++ { + if result.IsNull(i) { + continue + } + if i64s[i] == 0 { + i64s[i] = 1 + } else { + i64s[i] = 0 + } + } + return nil } func (b *builtinDecimalIsNullSig) vectorized() bool { diff --git a/expression/builtin_op_vec_test.go b/expression/builtin_op_vec_test.go index 7b1ae57494..8dc0d869e2 100644 --- a/expression/builtin_op_vec_test.go +++ b/expression/builtin_op_vec_test.go @@ -34,7 +34,11 @@ var vecBuiltinOpCases = map[string][]vecExprBenchCase{ ast.LogicAnd: { {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, geners: makeBinaryLogicOpDataGeners()}, }, - ast.UnaryNot: {}, + ast.UnaryNot: { + {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETReal}}, + {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETDecimal}}, + {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt}}, + }, ast.UnaryMinus: {}, ast.IsNull: {}, }