Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sum_decimal method for aggfunc #7096

Merged
merged 13 commits into from
Jul 26, 2018
Merged

add sum_decimal method for aggfunc #7096

merged 13 commits into from
Jul 26, 2018

Conversation

laidahe
Copy link
Contributor

@laidahe laidahe commented Jul 18, 2018

add sum_decimal method for aggfunc

What have you changed? (mandatory)

partially implement the SUM function under the new aggregate function framework, only implemented the following SUM functions:

  • SUM(DECIMAL)
    the following SUM functions are not implemented:

  • SUM (DISTINCT DECIMAL)

  • SUM(DISTINCT FLOAT64)

  • SUM (FLOAT64)

What is the type of the changes? (mandatory)

  • Improvement (non-breaking change which is an improvement to an existing feature)

How has this PR been tested? (mandatory)

Unit test

Does this PR affect documentation (docs/docs-cn) update? (mandatory)

No

Does this PR affect tidb-ansible update? (mandatory)

No

Does this PR need to be added to the release notes? (mandatory)

No

Refer to a related PR or issue link (optional)

6952

Benchmark result if necessary (optional)

Add a few positive/negative examples (optional)

add sum_decimal of aggfunc
@sre-bot
Copy link
Contributor

sre-bot commented Jul 18, 2018

Hi contributor, thanks for your PR.

This patch needs to be approved by someone of admins. They should reply with "/ok-to-test" to accept this PR for running test automatically.

@CLAassistant
Copy link

CLAassistant commented Jul 18, 2018

CLA assistant check
All committers have signed the CLA.


type partialResult4SumDecimal struct {
sum types.MyDecimal
count int64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could use a bool value isNull to indicate whether there exists a sum value in the partial result?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use isNull instead, you can take the MAX/MIN for example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but i can not find the file func_max.go in master.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i find it

}


type sumPartial4Decimal struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably there is no need to implement the sumPartial4Decimal? Because no matter what mode is the sum aggregate function stays in, it always has one parameter and the type is decimal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep,i find out that the logic of sumPartial4Decimal and sumOriginal4Decimal are the same.
Would you give me some suggestion on renaming sumOriginal4Decimal?

@@ -0,0 +1,127 @@
// Copyright 2018 PingCAP, Inc.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd better name this file to func_sum.go to follow the same file naming style with other aggregate functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to separate SUM(decimal) and SUM(float) to single file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to follow the existing flavors. Not only aggregate functions, but also other components, for example, expression and executors, they all put the code with same functionality in the same source file. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok,follow the existing flavors

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please rename this file to func_sum.go

return nil
}

type sumOriginal4Decimal struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if you add this implementation to the aggfuncs.go for code navigating, you can take other aggregate functions listed in aggfuncs.go as examples 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@zz-jason zz-jason added type/enhancement The issue or PR belongs to an enhancement. contribution This PR is from a community contributor. sig/execution SIG execution labels Jul 19, 2018
@zz-jason zz-jason added this to the 2.1 milestone Jul 19, 2018
go fmt the file
@zz-jason
Copy link
Member

@laidahe please add build functions for SUM, and add the implementations to aggfuncs.go

laidahe added 3 commits July 21, 2018 11:38
support  int / uint / decimal for sum
support  int / uint / decimal for sum
Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

executor/ aggfuncs/ builder.go
buildSum also should be implemented.

"github.com/pingcap/tidb/util/chunk"
)

// All the following sum function implementations return the decimal result,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why put the comment here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i delete them


func (e *sumAggFunc4Decimal) AllocPartialResult() PartialResult {
p := new(partialResult4SumDecimal)
//p.val = *types.NewDecFromInt(0) should the line un-commented?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


func (e *sumAggFunc4Decimal) ResetPartialResult(pr PartialResult) {
p := (*partialResult4SumDecimal)(pr)
//p.val = *types.NewDecFromInt(0) should the line un-commented?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

// All the following sum function implementations return the decimal result,
// which store the partial results in "partialResult4SumDecimal".

type partialResult4SumInt struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not implement partialResult4SumInt/partialResult4SumUint.
We only need partialResult4SumDecimal and partialResult4SumFloat64.

MySQL only returns type double or type decimal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@laidahe
Copy link
Contributor Author

laidahe commented Jul 24, 2018

PTAL

@zz-jason
Copy link
Member

@laidahe Please merge master and fix CI

@@ -112,8 +137,8 @@ func buildAvg(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
case aggregation.DedupMode:
return nil // not implemented yet.

// Build avg functions which consume the original data and update their
// partial results.
// Build avg functions which consume the original data and update their
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why make this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the editor mistake.i change it back.

func (e *sum4Float64) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error {
p := (*partialResult4SumFloat64)(pr)
for _, row := range rowsInGroup {
input, isNull, err := e.args[0].EvalReal(sctx, row)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EvalReal(sctx, &row) can gain better performance.

p := (*partialResult4SumDecimal)(pr)
newSum := new(types.MyDecimal)
for _, row := range rowsInGroup {
input, isNull, err := e.args[0].EvalDecimal(sctx, row)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

func (e *sum4DistinctFloat64) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error {
p := (*partialResult4SumDistinctFloat64)(pr)
for _, row := range rowsInGroup {
input, isNull, err := e.args[0].EvalReal(sctx, row)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

p := (*partialResult4SumDistinctDecimal)(pr)
var newSum types.MyDecimal
for _, row := range rowsInGroup {
input, isNull, err := e.args[0].EvalDecimal(sctx, row)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@zz-jason
Copy link
Member

@laidahe you can fix ci by make dev and check the result in you computer.

@@ -55,6 +55,9 @@ var (
_ AggFunc = (*avgPartial4Float64)(nil)
_ AggFunc = (*avgOriginal4DistinctFloat64)(nil)

_ AggFunc = (*sumAggFunc4Float64)(nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_ AggFunc = (*sum4DistinctFloat64)(nil)
_ AggFunc = (*sum4DistinctDecimal)(nil)
_ AggFunc = (*sum4Decimal)(nil)
_ AggFunc = (*sum4Float64)(nil)

@zz-jason
Copy link
Member

@laidahe CI stil fails. please fix CI. you can run make dev or make check to find the failed cases.

@zz-jason
Copy link
Member

/run-all-tests

Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@XuHuaiyu XuHuaiyu merged commit 1562f25 into pingcap:master Jul 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution This PR is from a community contributor. sig/execution SIG execution type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants