From 3d2daca4002e610d6a1e307995d7bd0b781d89c6 Mon Sep 17 00:00:00 2001 From: laidahe Date: Mon, 23 Jul 2018 18:31:34 +0800 Subject: [PATCH] add sum float64 and delete sum int/uint --- executor/aggfuncs/aggfuncs.go | 3 +- executor/aggfuncs/builder.go | 28 +++++++++++- executor/aggfuncs/func_sum.go | 86 ++++++----------------------------- 3 files changed, 41 insertions(+), 76 deletions(-) diff --git a/executor/aggfuncs/aggfuncs.go b/executor/aggfuncs/aggfuncs.go index efa13c9c4f082..587d34bc8bff4 100644 --- a/executor/aggfuncs/aggfuncs.go +++ b/executor/aggfuncs/aggfuncs.go @@ -32,8 +32,7 @@ var ( _ AggFunc = (*avgOriginal4Float64)(nil) _ AggFunc = (*avgPartial4Float64)(nil) - _ AggFunc = (*sumAggFunc4Int)(nil) - _ AggFunc = (*sumAggFunc4Uint)(nil) + _ AggFunc = (*sumAggFunc4Float64)(nil) _ AggFunc = (*sumAggFunc4Decimal)(nil) // All the AggFunc implementations for "FIRSTROW" are listed here. diff --git a/executor/aggfuncs/builder.go b/executor/aggfuncs/builder.go index b4a3a5e525c42..8a91bc9efd316 100644 --- a/executor/aggfuncs/builder.go +++ b/executor/aggfuncs/builder.go @@ -54,7 +54,33 @@ func buildCount(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc { } // buildCount builds the AggFunc implementation for function "SUM". -func buildSum(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc { +func buildSum(aggFuncDesc *aggregation.AggFuncDesc, ordinal int, isNull bool) AggFunc { + base := baseSumAggFunc{ + baseAggFunc: baseAggFunc{ + args: aggFuncDesc.Args, + ordinal: ordinal, + }, + } + evalType, fieldType := aggFuncDesc.RetTp.EvalType(), aggFuncDesc.RetTp + switch aggFuncDesc.Mode { + case aggregation.DedupMode: + default: + switch evalType { + case types.ETInt: + return nil + case types.ETReal: + switch fieldType.Tp { + case mysql.TypeFloat: + return &sumAggFunc4Float64{base} + case mysql.TypeDouble: + return &sumAggFunc4Float64{base} + } + case types.ETDecimal: + return &sumAggFunc4Decimal{base} + default: + return nil + } + } return nil } diff --git a/executor/aggfuncs/func_sum.go b/executor/aggfuncs/func_sum.go index 3759ed9651eaf..7fb38a9a321f7 100644 --- a/executor/aggfuncs/func_sum.go +++ b/executor/aggfuncs/func_sum.go @@ -20,19 +20,8 @@ import ( "github.com/pingcap/tidb/util/chunk" ) -// All the following sum function implementations return the decimal result, -// which store the partial results in "partialResult4SumDecimal". - -type partialResult4SumInt struct { - val int64 - // isNull is used to indicates: - // 1. whether the partial result is the initialization value which should not be sum during evaluation; - // 2. whether all the values of arg are all null, if so, we should return null as the default value for SUM. - isNull bool -} - -type partialResult4SumUint struct { - val uint64 +type partialResult4SumFloat64 struct { + val float64 isNull bool } @@ -45,36 +34,36 @@ type baseSumAggFunc struct { baseAggFunc } -type sumAggFunc4Int struct { +type sumAggFunc4Float64 struct { baseSumAggFunc } -func (e *sumAggFunc4Int) AllocPartialResult() PartialResult { - p := new(partialResult4SumInt) +func (e *sumAggFunc4Float64) AllocPartialResult() PartialResult { + p := new(partialResult4SumFloat64) p.isNull = true return PartialResult(p) } -func (e *sumAggFunc4Int) ResetPartialResult(pr PartialResult) { - p := (*partialResult4SumInt)(pr) +func (e *sumAggFunc4Float64) ResetPartialResult(pr PartialResult) { + p := (*partialResult4SumFloat64)(pr) p.val = 0 p.isNull = true } -func (e *sumAggFunc4Int) AppendFinalResult2Chunk(sctx sessionctx.Context, pr PartialResult, chk *chunk.Chunk) error { - p := (*partialResult4SumInt)(pr) +func (e *sumAggFunc4Float64) AppendFinalResult2Chunk(sctx sessionctx.Context, pr PartialResult, chk *chunk.Chunk) error { + p := (*partialResult4SumFloat64)(pr) if p.isNull { chk.AppendNull(e.ordinal) return nil } - chk.AppendInt64(e.ordinal, p.val) + chk.AppendFloat64(e.ordinal, p.val) return nil } -func (e *sumAggFunc4Int) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error { - p := (*partialResult4SumInt)(pr) +func (e *sumAggFunc4Float64) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error { + p := (*partialResult4SumFloat64)(pr) for _, row := range rowsInGroup { - input, isNull, err := e.args[0].EvalInt(sctx, row) + input, isNull, err := e.args[0].EvalReal(sctx, row) if err != nil { return errors.Trace(err) } @@ -91,67 +80,18 @@ func (e *sumAggFunc4Int) UpdatePartialResult(sctx sessionctx.Context, rowsInGrou return nil } -type sumAggFunc4Uint struct { - baseSumAggFunc -} - -func (e *sumAggFunc4Uint) AllocPartialResult() PartialResult { - p := new(partialResult4SumUint) - p.isNull = true - return PartialResult(p) -} - -func (e *sumAggFunc4Uint) ResetPartialResult(pr PartialResult) { - p := (*partialResult4SumUint)(pr) - p.val = 0 - p.isNull = true -} - -func (e *sumAggFunc4Uint) AppendFinalResult2Chunk(sctx sessionctx.Context, pr PartialResult, chk *chunk.Chunk) error { - p := (*partialResult4SumUint)(pr) - if p.isNull { - chk.AppendNull(e.ordinal) - return nil - } - chk.AppendUint64(e.ordinal, p.val) - return nil -} - -func (e *sumAggFunc4Uint) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error { - p := (*partialResult4SumUint)(pr) - for _, row := range rowsInGroup { - input, isNull, err := e.args[0].EvalInt(sctx, row) - if err != nil { - return errors.Trace(err) - } - if isNull { - continue - } - uintVal := uint64(input) - if p.isNull { - p.val = uintVal - p.isNull = false - continue - } - p.val += uintVal - } - return nil -} - type sumAggFunc4Decimal struct { baseSumAggFunc } func (e *sumAggFunc4Decimal) AllocPartialResult() PartialResult { p := new(partialResult4SumDecimal) - //p.val = *types.NewDecFromInt(0) should the line un-commented? p.isNull = true return PartialResult(p) } func (e *sumAggFunc4Decimal) ResetPartialResult(pr PartialResult) { p := (*partialResult4SumDecimal)(pr) - //p.val = *types.NewDecFromInt(0) should the line un-commented? p.isNull = true }