Skip to content

Commit

Permalink
cherry pick pingcap#19680 to release-3.0 (pingcap#19919)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Sep 11, 2020
1 parent 40a147c commit d23d8bd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
16 changes: 6 additions & 10 deletions executor/aggfuncs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,24 +387,20 @@ func buildVarPop(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {

// buildStdDevPop builds the AggFunc implementation for function "STD()/STDDEV()/STDDEV_POP()"
func buildStdDevPop(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
base := baseStdDevPopAggFunc{
varPop4Float64{
baseVarPopAggFunc{
baseAggFunc{
args: aggFuncDesc.Args,
ordinal: ordinal,
},
},
base := baseVarPopAggFunc{
baseAggFunc{
args: aggFuncDesc.Args,
ordinal: ordinal,
},
}
switch aggFuncDesc.Mode {
case aggregation.DedupMode:
return nil
default:
if aggFuncDesc.HasDistinct {
return &stdDevPop4DistinctFloat64{base}
return &stdDevPop4DistinctFloat64{varPop4DistinctFloat64{base}}
}
return &stdDevPop4Float64{base}
return &stdDevPop4Float64{varPop4Float64{base}}
}
}

Expand Down
8 changes: 2 additions & 6 deletions executor/aggfuncs/func_stddevpop.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ import (
"github.com/pingcap/tidb/util/chunk"
)

type baseStdDevPopAggFunc struct {
varPop4Float64
}

type stdDevPop4Float64 struct {
baseStdDevPopAggFunc
varPop4Float64
}

func (e *stdDevPop4Float64) AppendFinalResult2Chunk(sctx sessionctx.Context, pr PartialResult, chk *chunk.Chunk) error {
Expand All @@ -40,7 +36,7 @@ func (e *stdDevPop4Float64) AppendFinalResult2Chunk(sctx sessionctx.Context, pr
}

type stdDevPop4DistinctFloat64 struct {
baseStdDevPopAggFunc
varPop4DistinctFloat64
}

func (e *stdDevPop4DistinctFloat64) AppendFinalResult2Chunk(sctx sessionctx.Context, pr PartialResult, chk *chunk.Chunk) error {
Expand Down
7 changes: 7 additions & 0 deletions executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,13 @@ func (s *testSuite1) TestAggregation(c *C) {
tk.MustQuery("select stddev_pop(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "0", "0"))
tk.MustQuery("select std(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "0", "0"))
tk.MustQuery("select stddev(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "0", "0"))
// For issue #19676 The result of stddev_pop(distinct xxx) is wrong
tk.MustExec("drop table if exists t1;")
tk.MustExec("CREATE TABLE t1 (id int);")
tk.MustExec("insert into t1 values (1),(2);")
tk.MustQuery("select stddev_pop(id) from t1;").Check(testkit.Rows("0.5"))
tk.MustExec("insert into t1 values (1);")
tk.MustQuery("select stddev_pop(distinct id) from t1;").Check(testkit.Rows("0.5"))
}

func (s *testSuite1) TestStreamAggPushDown(c *C) {
Expand Down

0 comments on commit d23d8bd

Please sign in to comment.