From d8751e9fa6d65acb09bc27c41f810908aac75774 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 5 Sep 2019 14:59:07 +0800 Subject: [PATCH 1/6] implement vectorized evaluation for builtinCastIntAsDurationSig --- expression/builtin_cast_vec.go | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 expression/builtin_cast_vec.go diff --git a/expression/builtin_cast_vec.go b/expression/builtin_cast_vec.go new file mode 100644 index 0000000000000..51030a1075355 --- /dev/null +++ b/expression/builtin_cast_vec.go @@ -0,0 +1,58 @@ +// 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 *builtinCastIntAsDurationSig) vecEvalDuration(input *chunk.Chunk, result *chunk.Column) error { + n := input.NumRows() + buf, err := b.bufAllocator.get(types.ETInt, n) + if err != nil { + return err + } + defer b.bufAllocator.put(buf) + if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { + return err + } + + result.ResizeDuration(n, false) + i64s := buf.Int64s() + ds := result.GoDurations() + for i := 0; i < n; i++ { + if buf.IsNull(i) { + result.SetNull(i, true) + continue + } + dur, err := types.NumberToDuration(i64s[i], int8(b.tp.Decimal)) + if err != nil { + if types.ErrOverflow.Equal(err) { + err = b.ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err) + } + if err != nil { + return err + } + result.SetNull(i, true) + continue + } + ds[i] = dur.Duration + } + return nil +} + +func (b *builtinCastIntAsDurationSig) vectorized() bool { + return true +} From 5caca3c5d4e65dfd8a60f719d8910f1a36fafb48 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 5 Sep 2019 15:26:44 +0800 Subject: [PATCH 2/6] bugfix --- expression/bench_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/expression/bench_test.go b/expression/bench_test.go index 27feb6fc3ab46..012a2e13ccf93 100644 --- a/expression/bench_test.go +++ b/expression/bench_test.go @@ -232,6 +232,12 @@ func (g *randLenStrGener) gen() interface{} { return string(buf) } +type randDurInt struct{} + +func (g *randDurInt) gen() interface{} { + return int64(rand.Intn(types.TimeMaxHour)*10000 + rand.Intn(60)*100 + rand.Intn(60)) +} + type vecExprBenchCase struct { retEvalType types.EvalType childrenTypes []types.EvalType @@ -245,6 +251,7 @@ type vecExprBenchCase struct { var vecExprBenchCases = map[string][]vecExprBenchCase{ ast.Cast: { {types.ETInt, []types.EvalType{types.ETInt}, nil}, + {types.ETDuration, []types.EvalType{types.ETInt}, []dataGenerator{new(randDurInt)}}, }, ast.Repeat: { {types.ETString, []types.EvalType{types.ETString, types.ETInt}, []dataGenerator{&randLenStrGener{10, 20}, &rangeInt64Gener{-10, 10}}}, @@ -574,13 +581,12 @@ func (s *testEvaluatorSuite) TestVectorizedBuiltinFunc(c *C) { case types.ETDuration: err := baseFunc.vecEvalDuration(input, output) c.Assert(err, IsNil) - d64s := output.GoDurations() for row := it.Begin(); row != it.End(); row = it.Next() { val, isNull, err := baseFunc.evalDuration(row) c.Assert(err, IsNil) c.Assert(isNull, Equals, output.IsNull(i)) if !isNull { - c.Assert(val, Equals, d64s[i]) + c.Assert(val, Equals, output.GetDuration(i, types.UnspecifiedLength)) } i++ } From 87505e03f32b43ab4e4006ec8ca1dc2f436c2371 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Tue, 10 Sep 2019 19:14:52 +0800 Subject: [PATCH 3/6] fixup fixup fixup --- expression/bench_test.go | 4 +++- expression/builtin_cast_vec.go | 1 - expression/builtin_cast_vec_test.go | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/expression/bench_test.go b/expression/bench_test.go index 2eb187eaf6d97..6c5f130db7088 100644 --- a/expression/bench_test.go +++ b/expression/bench_test.go @@ -571,12 +571,14 @@ func testVectorizedBuiltinFunc(c *C, vecExprCases vecExprBenchCases) { case types.ETDuration: err := baseFunc.vecEvalDuration(input, output) c.Assert(err, IsNil) + vecWarnCnt = ctx.GetSessionVars().StmtCtx.WarningCount() + d64s := output.GoDurations() for row := it.Begin(); row != it.End(); row = it.Next() { val, isNull, err := baseFunc.evalDuration(row) c.Assert(err, IsNil) c.Assert(isNull, Equals, output.IsNull(i)) if !isNull { - c.Assert(val, Equals, output.GetDuration(i, types.UnspecifiedLength)) + c.Assert(val, Equals, d64s[i]) } i++ } diff --git a/expression/builtin_cast_vec.go b/expression/builtin_cast_vec.go index 8ebd0d0484308..a011b84764e0b 100644 --- a/expression/builtin_cast_vec.go +++ b/expression/builtin_cast_vec.go @@ -18,7 +18,6 @@ import ( "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/chunk" ) -) func (b *builtinCastIntAsDurationSig) vecEvalDuration(input *chunk.Chunk, result *chunk.Column) error { n := input.NumRows() diff --git a/expression/builtin_cast_vec_test.go b/expression/builtin_cast_vec_test.go index b19bb14f9811d..b3dc77316d1ee 100644 --- a/expression/builtin_cast_vec_test.go +++ b/expression/builtin_cast_vec_test.go @@ -24,6 +24,7 @@ import ( var vecBuiltinCastCases = map[string][]vecExprBenchCase{ ast.Cast: { {types.ETInt, []types.EvalType{types.ETInt}, nil}, + {types.ETDuration, []types.EvalType{types.ETInt}, []dataGenerator{new(randDurInt)}}, }, } From 42851baa7f3dbd391c0235aa39dbbf8aa662a2ad Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Tue, 10 Sep 2019 19:20:03 +0800 Subject: [PATCH 4/6] fixup --- expression/bench_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expression/bench_test.go b/expression/bench_test.go index 6c5f130db7088..fc598466cf68a 100644 --- a/expression/bench_test.go +++ b/expression/bench_test.go @@ -578,7 +578,7 @@ func testVectorizedBuiltinFunc(c *C, vecExprCases vecExprBenchCases) { c.Assert(err, IsNil) c.Assert(isNull, Equals, output.IsNull(i)) if !isNull { - c.Assert(val, Equals, d64s[i]) + c.Assert(val.Duration, Equals, d64s[i]) } i++ } From fd04613e7bab0d03a68f73152b9459ee4d3e02c5 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Tue, 17 Sep 2019 10:32:12 +0800 Subject: [PATCH 5/6] fixup --- expression/builtin_cast_vec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expression/builtin_cast_vec.go b/expression/builtin_cast_vec.go index a011b84764e0b..53eebc840dc38 100644 --- a/expression/builtin_cast_vec.go +++ b/expression/builtin_cast_vec.go @@ -30,7 +30,7 @@ func (b *builtinCastIntAsDurationSig) vecEvalDuration(input *chunk.Chunk, result return err } - result.ResizeDuration(n, false) + result.ResizeGoDuration(n, false) i64s := buf.Int64s() ds := result.GoDurations() for i := 0; i < n; i++ { From b377cc70a7d43505f0e5aca6589d09369712246a Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Tue, 17 Sep 2019 13:42:21 +0800 Subject: [PATCH 6/6] address comments --- expression/builtin_cast_vec.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/expression/builtin_cast_vec.go b/expression/builtin_cast_vec.go index 53eebc840dc38..f4871c26d2910 100644 --- a/expression/builtin_cast_vec.go +++ b/expression/builtin_cast_vec.go @@ -31,11 +31,11 @@ func (b *builtinCastIntAsDurationSig) vecEvalDuration(input *chunk.Chunk, result } result.ResizeGoDuration(n, false) + result.MergeNulls(buf) i64s := buf.Int64s() ds := result.GoDurations() for i := 0; i < n; i++ { - if buf.IsNull(i) { - result.SetNull(i, true) + if result.IsNull(i) { continue } dur, err := types.NumberToDuration(i64s[i], int8(b.tp.Decimal))