From 031451b8956571c669751ead3b10746db1131db2 Mon Sep 17 00:00:00 2001 From: HuaiyuXu <391585975@qq.com> Date: Thu, 31 Oct 2019 01:00:23 +0800 Subject: [PATCH] util,executor: use MutableString as key for DecimalSet (#9913) (#13041) --- executor/aggfuncs/func_avg.go | 20 +++++++++++++------ executor/aggfuncs/func_sum.go | 19 +++++++++++++----- executor/executor_test.go | 2 +- util/set/decimal_set.go | 37 ----------------------------------- 4 files changed, 29 insertions(+), 49 deletions(-) delete mode 100644 util/set/decimal_set.go diff --git a/executor/aggfuncs/func_avg.go b/executor/aggfuncs/func_avg.go index b599d4f3e0779..26fe13b97fa13 100644 --- a/executor/aggfuncs/func_avg.go +++ b/executor/aggfuncs/func_avg.go @@ -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" ) @@ -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 { @@ -162,7 +163,7 @@ type avgOriginal4DistinctDecimal struct { func (e *avgOriginal4DistinctDecimal) AllocPartialResult() PartialResult { p := &partialResult4AvgDistinctDecimal{ - valSet: set.NewDecimalSet(), + valSet: set.NewStringSet(), } return PartialResult(p) } @@ -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 { @@ -181,10 +182,18 @@ 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 { @@ -192,7 +201,6 @@ func (e *avgOriginal4DistinctDecimal) UpdatePartialResult(sctx sessionctx.Contex } p.sum = *newSum p.count++ - p.valSet.Insert(input) } return nil } diff --git a/executor/aggfuncs/func_sum.go b/executor/aggfuncs/func_sum.go index c2935231d5dfb..b3f4424da4392 100644 --- a/executor/aggfuncs/func_sum.go +++ b/executor/aggfuncs/func_sum.go @@ -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" ) @@ -38,7 +39,7 @@ type partialResult4SumDistinctFloat64 struct { type partialResult4SumDistinctDecimal struct { partialResult4SumDecimal - valSet set.DecimalSet + valSet set.StringSet } type baseSumAggFunc struct { @@ -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 { @@ -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 diff --git a/executor/executor_test.go b/executor/executor_test.go index 78840e5f94163..351b2f9a69c2c 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -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) { diff --git a/util/set/decimal_set.go b/util/set/decimal_set.go deleted file mode 100644 index beec6c3671532..0000000000000 --- a/util/set/decimal_set.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2018 PingCAP, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// See the License for the specific language governing permissions and -// limitations under the License. - -package set - -import ( - "github.com/pingcap/tidb/types" -) - -// DecimalSet is a decimal set. -type DecimalSet map[types.MyDecimal]struct{} - -// NewDecimalSet builds a decimal set. -func NewDecimalSet() DecimalSet { - return make(map[types.MyDecimal]struct{}) -} - -// Exist checks whether `val` exists in `s`. -func (s DecimalSet) Exist(val *types.MyDecimal) bool { - _, ok := s[*val] - return ok -} - -// Insert inserts `val` into `s`. -func (s DecimalSet) Insert(val *types.MyDecimal) { - s[*val] = struct{}{} -}