diff --git a/executor/aggfuncs/builder.go b/executor/aggfuncs/builder.go index 7afde1def0d50..b2720a721274d 100644 --- a/executor/aggfuncs/builder.go +++ b/executor/aggfuncs/builder.go @@ -387,14 +387,10 @@ 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 { @@ -402,9 +398,9 @@ func buildStdDevPop(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc { return nil default: if aggFuncDesc.HasDistinct { - return &stdDevPop4DistinctFloat64{base} + return &stdDevPop4DistinctFloat64{varPop4DistinctFloat64{base}} } - return &stdDevPop4Float64{base} + return &stdDevPop4Float64{varPop4Float64{base}} } } diff --git a/executor/aggfuncs/func_stddevpop.go b/executor/aggfuncs/func_stddevpop.go index 2db8c06941240..9098020cbd146 100644 --- a/executor/aggfuncs/func_stddevpop.go +++ b/executor/aggfuncs/func_stddevpop.go @@ -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 { @@ -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 { diff --git a/executor/aggregate_test.go b/executor/aggregate_test.go index 1314ba1e38572..ded7d3fd49b48 100644 --- a/executor/aggregate_test.go +++ b/executor/aggregate_test.go @@ -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("", "0", "0")) tk.MustQuery("select std(b) from t1 group by a order by a;").Check(testkit.Rows("", "0", "0")) tk.MustQuery("select stddev(b) from t1 group by a order by a;").Check(testkit.Rows("", "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) {