Skip to content

Commit

Permalink
util,executor: use MutableString as key for DecimalSet (#9913) (#13041)
Browse files Browse the repository at this point in the history
  • Loading branch information
XuHuaiyu authored and sre-bot committed Oct 30, 2019
1 parent d145386 commit 031451b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 49 deletions.
20 changes: 14 additions & 6 deletions executor/aggfuncs/func_avg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tidb/util/set"
)

Expand Down Expand Up @@ -153,7 +154,7 @@ func (e *avgPartial4Decimal) MergePartialResult(sctx sessionctx.Context, src Par

type partialResult4AvgDistinctDecimal struct {
partialResult4AvgDecimal
valSet set.DecimalSet
valSet set.StringSet
}

type avgOriginal4DistinctDecimal struct {
Expand All @@ -162,7 +163,7 @@ type avgOriginal4DistinctDecimal struct {

func (e *avgOriginal4DistinctDecimal) AllocPartialResult() PartialResult {
p := &partialResult4AvgDistinctDecimal{
valSet: set.NewDecimalSet(),
valSet: set.NewStringSet(),
}
return PartialResult(p)
}
Expand All @@ -171,7 +172,7 @@ func (e *avgOriginal4DistinctDecimal) ResetPartialResult(pr PartialResult) {
p := (*partialResult4AvgDistinctDecimal)(pr)
p.sum = *types.NewDecFromInt(0)
p.count = int64(0)
p.valSet = set.NewDecimalSet()
p.valSet = set.NewStringSet()
}

func (e *avgOriginal4DistinctDecimal) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error {
Expand All @@ -181,18 +182,25 @@ func (e *avgOriginal4DistinctDecimal) UpdatePartialResult(sctx sessionctx.Contex
if err != nil {
return errors.Trace(err)
}
if isNull || p.valSet.Exist(input) {
if isNull {
continue
}

hash, err := input.ToHashKey()
if err != nil {
return err
}
decStr := string(hack.String(hash))
if p.valSet.Exist(decStr) {
continue
}
p.valSet.Insert(decStr)
newSum := new(types.MyDecimal)
err = types.DecimalAdd(&p.sum, input, newSum)
if err != nil {
return errors.Trace(err)
}
p.sum = *newSum
p.count++
p.valSet.Insert(input)
}
return nil
}
Expand Down
19 changes: 14 additions & 5 deletions executor/aggfuncs/func_sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tidb/util/set"
)

Expand All @@ -38,7 +39,7 @@ type partialResult4SumDistinctFloat64 struct {

type partialResult4SumDistinctDecimal struct {
partialResult4SumDecimal
valSet set.DecimalSet
valSet set.StringSet
}

type baseSumAggFunc struct {
Expand Down Expand Up @@ -222,14 +223,14 @@ type sum4DistinctDecimal struct {
func (e *sum4DistinctDecimal) AllocPartialResult() PartialResult {
p := new(partialResult4SumDistinctDecimal)
p.isNull = true
p.valSet = set.NewDecimalSet()
p.valSet = set.NewStringSet()
return PartialResult(p)
}

func (e *sum4DistinctDecimal) ResetPartialResult(pr PartialResult) {
p := (*partialResult4SumDistinctDecimal)(pr)
p.isNull = true
p.valSet = set.NewDecimalSet()
p.valSet = set.NewStringSet()
}

func (e *sum4DistinctDecimal) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error {
Expand All @@ -239,10 +240,18 @@ func (e *sum4DistinctDecimal) UpdatePartialResult(sctx sessionctx.Context, rowsI
if err != nil {
return errors.Trace(err)
}
if isNull || p.valSet.Exist(input) {
if isNull {
continue
}
p.valSet.Insert(input)
hash, err := input.ToHashKey()
if err != nil {
return err
}
decStr := string(hack.String(hash))
if p.valSet.Exist(decStr) {
continue
}
p.valSet.Insert(decStr)
if p.isNull {
p.val = *input
p.isNull = false
Expand Down
2 changes: 1 addition & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ func (s *testSuite) TestUnion(c *C) {
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b decimal(6, 3))")
tk.MustExec("insert into t values(1, 1.000)")
tk.MustQuery("select count(distinct a) from (select a from t union select b from t) tmp;").Check(testkit.Rows("1"))
tk.MustQuery("select count(distinct a), sum(distinct a), avg(distinct a) from (select a from t union all select b from t) tmp;").Check(testkit.Rows("1 1.000 1.0000000"))
}

func (s *testSuite) TestNeighbouringProj(c *C) {
Expand Down
37 changes: 0 additions & 37 deletions util/set/decimal_set.go

This file was deleted.

0 comments on commit 031451b

Please sign in to comment.