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

expression, planner: fix decimal results for aggregate functions (#20017) #20984

Merged
merged 6 commits into from
Dec 1, 2020

Conversation

ti-srebot
Copy link
Contributor

cherry-pick #20017 to release-4.0


What problem does this PR solve?

Issue Number: close #19426, fix PR #19592

Problem Summary:

Execute

create table t(a int primary key, b int);
insert into t values (1, 11), (4, 44), (2, 22), (3, 33);

Obtained

mysql> select sum(case when a <= 0 or a > 1000 then 0.0 else b end) from t;
+-------------------------------------------------------+
| sum(case when a <= 0 or a > 1000 then 0.0 else b end) |
+-------------------------------------------------------+
|                                                   110 |
+-------------------------------------------------------+
1 row in set (0.01 sec)

mysql> select group_concat(case when a <= 0 or a > 1000 then 0.0 else b end) from t;
+----------------------------------------------------------------+
| group_concat(case when a <= 0 or a > 1000 then 0.0 else b end) |
+----------------------------------------------------------------+
| 11,22,33,44                                                    |
+----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select distinct case when a <= 0 or a > 1000 then 0.0 else b end from t;
+--------------------------------------------------+
| case when a <= 0 or a > 1000 then 0.0 else b end |
+--------------------------------------------------+
|                                               11 |
|                                               22 |
|                                               33 |
|                                               44 |
+--------------------------------------------------+
4 rows in set (0.00 sec)

Expect

mysql> select sum(case when a <= 0 or a > 1000 then 0.0 else b end) from t;
+-------------------------------------------------------+
| sum(case when a <= 0 or a > 1000 then 0.0 else b end) |
+-------------------------------------------------------+
|                                                 110.0 |
+-------------------------------------------------------+
1 row in set (0.00 sec)

mysql>  select group_concat(case when a <= 0 or a > 1000 then 0.0 else b end) from t;
+----------------------------------------------------------------+
| group_concat(case when a <= 0 or a > 1000 then 0.0 else b end) |
+----------------------------------------------------------------+
| 11.0,22.0,33.0,44.0                                            |
+----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select distinct case when a <= 0 or a > 1000 then 0.0 else b end from t;
+--------------------------------------------------+
| case when a <= 0 or a > 1000 then 0.0 else b end |
+--------------------------------------------------+
|                                             11.0 |
|                                             22.0 |
|                                             33.0 |
|                                             44.0 |
+--------------------------------------------------+
4 rows in set (0.00 sec)

What is changed and how it works?

  • wrap cast as decimal for arguments of group_concat to preserve fraction part digits of the results.
  • round the decimal final result in AppendFinalResult2Chunk to the argument's precision.

Related changes

  • Need to cherry-pick to the release branch 3.0, 4.0

Check List

Tests

  • Unit test
  • Integration test

Side effects

  • Performance regression
    • Consumes more CPU
    • Consumes more MEM

Release note

  • fix the fraction part of decimal results for aggregate functions

@ti-srebot
Copy link
Contributor Author

/run-all-tests

Copy link
Member

@winoros winoros left a comment

Choose a reason for hiding this comment

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

lgtm

@ti-srebot ti-srebot added the status/LGT1 Indicates that a PR has LGTM 1. label Nov 11, 2020
Copy link
Contributor

@qw4990 qw4990 left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-srebot ti-srebot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Nov 12, 2020
@qw4990
Copy link
Contributor

qw4990 commented Nov 12, 2020

/merge

@ti-srebot ti-srebot added the status/can-merge Indicates a PR has been approved by a committer. label Nov 12, 2020
@ti-srebot
Copy link
Contributor Author

/run-all-tests

@ti-srebot
Copy link
Contributor Author

@ti-srebot merge failed.

@dyzsr
Copy link
Contributor

dyzsr commented Nov 12, 2020

/run-common-test tidb-test=pr/1100
/run-integration-common-test tidb-test=pr/1100

@dyzsr
Copy link
Contributor

dyzsr commented Nov 14, 2020

/run-common-test
/run-integration-common-test

@dyzsr
Copy link
Contributor

dyzsr commented Nov 16, 2020

/run-common-test tidb-test=pr/1104
/run-integration-common-test tidb-test=pr/1104

@dyzsr
Copy link
Contributor

dyzsr commented Nov 17, 2020

Need to merge https://github.com/pingcap/tidb-test/pull/1104 first.

@lzmhhh123
Copy link
Contributor

/merge

@ti-srebot
Copy link
Contributor Author

Sorry @lzmhhh123, you don't have permission to trigger auto merge event on this branch.

@qw4990
Copy link
Contributor

qw4990 commented Nov 30, 2020

/merge

@ti-srebot
Copy link
Contributor Author

Sorry @qw4990, you don't have permission to trigger auto merge event on this branch.

@qw4990
Copy link
Contributor

qw4990 commented Nov 30, 2020

/run-all-tests

@dyzsr
Copy link
Contributor

dyzsr commented Nov 30, 2020

/run-common-test tidb-test=pr/1104
/run-integration-common-test tidb-test=pr/1104

1 similar comment
@dyzsr
Copy link
Contributor

dyzsr commented Nov 30, 2020

/run-common-test tidb-test=pr/1104
/run-integration-common-test tidb-test=pr/1104

@dyzsr
Copy link
Contributor

dyzsr commented Nov 30, 2020

/run-integration-common-test tidb-test=pr/1104

@zz-jason zz-jason added the priority/release-blocker This issue blocks a release. Please solve it ASAP. label Nov 30, 2020
@jebter
Copy link

jebter commented Dec 1, 2020

/merge

@ti-srebot
Copy link
Contributor Author

/run-all-tests

@ti-srebot
Copy link
Contributor Author

@ti-srebot merge failed.

@jebter
Copy link

jebter commented Dec 1, 2020

/run-common-test

@jebter
Copy link

jebter commented Dec 1, 2020

/run-integration-common-test

@jebter
Copy link

jebter commented Dec 1, 2020

/merge

@ti-srebot
Copy link
Contributor Author

/run-all-tests

@ti-srebot
Copy link
Contributor Author

@ti-srebot merge failed.

@jebter
Copy link

jebter commented Dec 1, 2020

/merge

@ti-srebot
Copy link
Contributor Author

Your auto merge job has been accepted, waiting for:

  • 21336

@ti-srebot
Copy link
Contributor Author

/run-all-tests

@ti-srebot
Copy link
Contributor Author

@ti-srebot merge failed.

@jebter
Copy link

jebter commented Dec 1, 2020

/run-unit-test

@jebter jebter merged commit 8a2b35f into pingcap:release-4.0 Dec 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
challenge-program priority/release-blocker This issue blocks a release. Please solve it ASAP. sig/execution SIG execution status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. type/4.0-cherry-pick
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants