From 1f70627b8bf451f12174338459591f88a397541d Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Sat, 9 Nov 2019 00:43:19 +0300 Subject: [PATCH 1/4] expression: implement vectorized evaluation for builtinMakeDateSig --- expression/builtin_time_vec.go | 60 ++++++++++++++++++++++++++++- expression/builtin_time_vec_test.go | 8 +++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/expression/builtin_time_vec.go b/expression/builtin_time_vec.go index 05a81fac380b5..a03e008510801 100644 --- a/expression/builtin_time_vec.go +++ b/expression/builtin_time_vec.go @@ -818,11 +818,67 @@ func (b *builtinAddDateIntDecimalSig) vecEvalTime(input *chunk.Chunk, result *ch } func (b *builtinMakeDateSig) vectorized() bool { - return false + return true } func (b *builtinMakeDateSig) vecEvalTime(input *chunk.Chunk, result *chunk.Column) error { - return errors.Errorf("not implemented") + n := input.NumRows() + buf1, err := b.bufAllocator.get(types.ETInt, n) + if err != nil { + return err + } + defer b.bufAllocator.put(buf1) + if err := b.args[0].VecEvalInt(b.ctx, input, buf1); err != nil { + return err + } + + buf2, err := b.bufAllocator.get(types.ETInt, n) + if err != nil { + return err + } + defer b.bufAllocator.put(buf2) + if err := b.args[0].VecEvalInt(b.ctx, input, buf2); err != nil { + return err + } + + result.ResizeTime(n, false) + result.MergeNulls(buf1) + result.MergeNulls(buf2) + + times := result.Times() + years := buf1.Int64s() + days := buf2.Int64s() + + for i := 0; i < n; i++ { + if result.IsNull(i) { + continue + } + if days[i] <= 0 || years[i] < 0 || years[i] > 9999 { + result.SetNull(i, true) + continue + } + if years[i] < 70 { + years[i] += 2000 + } else if years[i] < 100 { + years[i] += 1900 + } + startTime := types.Time{ + Time: types.FromDate(int(years[i]), 1, 1, 0, 0, 0, 0), + Type: mysql.TypeDate, + Fsp: 0, + } + retTimestamp := types.TimestampDiff("DAY", types.ZeroDate, startTime) + if retTimestamp == 0 { + return handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(startTime.String())) + } + ret := types.TimeFromDays(retTimestamp + days[i] - 1) + if ret.IsZero() || ret.Time.Year() > 9999 { + result.SetNull(i, true) + continue + } + times[i] = ret + } + return nil } func (b *builtinWeekOfYearSig) vectorized() bool { diff --git a/expression/builtin_time_vec_test.go b/expression/builtin_time_vec_test.go index cdd99cfcae0b2..57acb0b49c4cf 100644 --- a/expression/builtin_time_vec_test.go +++ b/expression/builtin_time_vec_test.go @@ -87,7 +87,13 @@ var vecBuiltinTimeCases = map[string][]vecExprBenchCase{ ast.CurrentDate: { {retEvalType: types.ETDatetime}, }, - ast.MakeDate: {}, + ast.MakeDate: { + {retEvalType: types.ETDatetime, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, + geners: []dataGenerator{ + &rangeInt64Gener{begin: 1900, end: 2200}, + &rangeInt64Gener{begin: 1, end: 365}, + }}, + }, ast.MakeTime: {}, ast.PeriodAdd: { {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, geners: []dataGenerator{new(periodGener), new(periodGener)}}, From 411fb65832e1bc7a5d91cc79f9f1985d849b3cf9 Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Sun, 10 Nov 2019 02:09:00 +0300 Subject: [PATCH 2/4] expression: builtinMakeDateSig, fix err handle --- expression/builtin_time_vec.go | 6 +++++- expression/builtin_time_vec_test.go | 6 +----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/expression/builtin_time_vec.go b/expression/builtin_time_vec.go index 29e0eb9df61c4..07e6e91f4866d 100644 --- a/expression/builtin_time_vec.go +++ b/expression/builtin_time_vec.go @@ -904,7 +904,11 @@ func (b *builtinMakeDateSig) vecEvalTime(input *chunk.Chunk, result *chunk.Colum } retTimestamp := types.TimestampDiff("DAY", types.ZeroDate, startTime) if retTimestamp == 0 { - return handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(startTime.String())) + if err = handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(startTime.String())); err != nil { + return err + } + result.SetNull(i, true) + continue } ret := types.TimeFromDays(retTimestamp + days[i] - 1) if ret.IsZero() || ret.Time.Year() > 9999 { diff --git a/expression/builtin_time_vec_test.go b/expression/builtin_time_vec_test.go index d0d991726bb5c..e8a861e892738 100644 --- a/expression/builtin_time_vec_test.go +++ b/expression/builtin_time_vec_test.go @@ -88,11 +88,7 @@ var vecBuiltinTimeCases = map[string][]vecExprBenchCase{ {retEvalType: types.ETDatetime}, }, ast.MakeDate: { - {retEvalType: types.ETDatetime, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, - geners: []dataGenerator{ - &rangeInt64Gener{begin: 1900, end: 2200}, - &rangeInt64Gener{begin: 1, end: 365}, - }}, + {retEvalType: types.ETDatetime, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}}, }, ast.MakeTime: {}, ast.PeriodAdd: { From 5ff9f544dd6c0e833a3a1cc4bda1b27c13eef692 Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Mon, 11 Nov 2019 12:25:32 +0300 Subject: [PATCH 3/4] expression: builtinMakeDateSig, fixes after review --- expression/builtin_time_vec.go | 5 ++--- expression/builtin_time_vec_test.go | 4 +++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/expression/builtin_time_vec.go b/expression/builtin_time_vec.go index 8703b9d2d4ed5..8284b28e93522 100644 --- a/expression/builtin_time_vec.go +++ b/expression/builtin_time_vec.go @@ -1024,13 +1024,12 @@ func (b *builtinMakeDateSig) vecEvalTime(input *chunk.Chunk, result *chunk.Colum return err } defer b.bufAllocator.put(buf2) - if err := b.args[0].VecEvalInt(b.ctx, input, buf2); err != nil { + if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil { return err } result.ResizeTime(n, false) - result.MergeNulls(buf1) - result.MergeNulls(buf2) + result.MergeNulls(buf1, buf2) times := result.Times() years := buf1.Int64s() diff --git a/expression/builtin_time_vec_test.go b/expression/builtin_time_vec_test.go index 284f2fa8e0429..2633c5d66bb98 100644 --- a/expression/builtin_time_vec_test.go +++ b/expression/builtin_time_vec_test.go @@ -88,7 +88,9 @@ var vecBuiltinTimeCases = map[string][]vecExprBenchCase{ {retEvalType: types.ETDatetime}, }, ast.MakeDate: { - {retEvalType: types.ETDatetime, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}}, + {retEvalType: types.ETDatetime, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, + geners: []dataGenerator{&rangeInt64Gener{1700, 2200}, &rangeInt64Gener{0, 365}}, + }, }, ast.MakeTime: {}, ast.PeriodAdd: { From 6aecb594fb0f0bd9b37c4ae1ad28abf199576676 Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Mon, 11 Nov 2019 12:45:56 +0300 Subject: [PATCH 4/4] expression: builtinMakeDateSig, fixes after review --- expression/builtin_time_vec_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expression/builtin_time_vec_test.go b/expression/builtin_time_vec_test.go index 2633c5d66bb98..a3856afd3709c 100644 --- a/expression/builtin_time_vec_test.go +++ b/expression/builtin_time_vec_test.go @@ -89,7 +89,7 @@ var vecBuiltinTimeCases = map[string][]vecExprBenchCase{ }, ast.MakeDate: { {retEvalType: types.ETDatetime, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, - geners: []dataGenerator{&rangeInt64Gener{1700, 2200}, &rangeInt64Gener{0, 365}}, + geners: []dataGenerator{&rangeInt64Gener{0, 2200}, &rangeInt64Gener{0, 365}}, }, }, ast.MakeTime: {},