Skip to content

Commit

Permalink
add sum float64 and delete sum int/uint
Browse files Browse the repository at this point in the history
  • Loading branch information
laidahe committed Jul 23, 2018
1 parent cbfdba4 commit 3d2daca
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 76 deletions.
3 changes: 1 addition & 2 deletions executor/aggfuncs/aggfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 27 additions & 1 deletion executor/aggfuncs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
86 changes: 13 additions & 73 deletions executor/aggfuncs/func_sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
Expand All @@ -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
}

Expand Down

0 comments on commit 3d2daca

Please sign in to comment.