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

planner: support 3 stage aggregation for single scalar distinct agg #37203

Merged
merged 21 commits into from
Sep 17, 2022

Conversation

fixdb
Copy link
Contributor

@fixdb fixdb commented Aug 18, 2022

What problem does this PR solve?

Issue Number: close #37202

Problem Summary:
Currently, count(distinct) in MPP mode still execute in a single worker (PassThrough Mode), which is bad when there are large number of distinct values. e.g.:

mysql> explain select count(distinct L_ORDERKEY) from lineitem;
+------------------------------------+----------+--------------+----------------+-----------------------------------------------------------+
| id                                 | estRows  | task         | access object  | operator info                                             |
+------------------------------------+----------+--------------+----------------+-----------------------------------------------------------+
| TableReader_18                     | 1.00     | root         |                | data:ExchangeSender_17                                    |
| └─ExchangeSender_17                | 1.00     | mpp[tiflash] |                | ExchangeType: PassThrough                                 |
|   └─Projection_13                  | 1.00     | mpp[tiflash] |                | Column#18                                                 |
|     └─HashAgg_14                   | 1.00     | mpp[tiflash] |                | funcs:count(distinct test.lineitem.l_orderkey)->Column#18 |
|       └─ExchangeReceiver_16        | 1.00     | mpp[tiflash] |                |                                                           |
|         └─ExchangeSender_15        | 1.00     | mpp[tiflash] |                | ExchangeType: PassThrough                                 |
|           └─HashAgg_6              | 1.00     | mpp[tiflash] |                | group by:test.lineitem.l_orderkey,                        |
|             └─TableFullScan_12     | 10000.00 | mpp[tiflash] | table:lineitem | keep order:false, stats:pseudo                            |
+------------------------------------+----------+--------------+----------------+-----------------------------------------------------------+
8 rows in set (0.01 sec)

What is changed and how it works?

In this patch, we are able to generate a plan with 3 stage aggregation for scalar distinct agg.
NB. only for cases where there is only 1 distinct agg function.

e.g.
explain select count(distinct a), count(b) from foo

  HashAgg sum(#1), sum(#2)                              -> final agg
   +- Exchange Passthrough
       +- HashAgg count(distinct a) #1, sum(#3) #2      -> middle agg
          +- Exchange HashPartition by a
               +- HashAgg count(b) #3, group by a       -> partial agg
                   +- TableScan foo

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Aug 18, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • AilinKid
  • winoros

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Aug 18, 2022
@fixdb fixdb requested a review from a team as a code owner September 1, 2022 01:10
@ti-chi-bot
Copy link
Member

"name": "TestMPPSingleDistinct3Stage",
"cases": [
"set @@tidb_allow_mpp=1;set @@tidb_enforce_mpp=1;",
"EXPLAIN select count(distinct c) from t;",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does the second stage in explain show count(distinct c) instead of group by c?
(json files cannot be commented, so I put the comments 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.

See here:

e.g.
explain select count(distinct a), count(b) from foo

  HashAgg sum(#1), sum(#2)                              -> final agg
   +- Exchange Passthrough
       +- HashAgg count(distinct a) #1, sum(#3) #2      -> middle agg
          +- Exchange HashPartition by a
               +- HashAgg count(b) #3, group by a       -> partial agg
                   +- TableScan foo

After the 1st stage agg, we shuffle by the distinct key. So it is ok to compute count(distinct) in 2nd stage.

@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 4, 2022
newArgs = append(newArgs, middleSchema.Columns[i])
} else {
for _, arg := range fun.Args {
newCol, err := arg.RemapColumn(schemaMap)
Copy link
Member

Choose a reason for hiding this comment

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

We could use ColumnSubstitute?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, great. will do

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It turned out ColumnSubstitute is not applicable here, because the 2 schemas are different.

@@ -1578,6 +1578,29 @@ func (p *basePhysicalAgg) newPartialAggregate(copTaskType kv.StoreType, isMPPTas
return partialAgg, finalAgg
}

// can this agg use 3 stage for distinct aggregation
Copy link
Member

Choose a reason for hiding this comment

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

Better with the format funcName do something.

" └─ExchangeSender_31 1.00 mpp[tiflash] ExchangeType: PassThrough",
" └─HashAgg_28 1.00 mpp[tiflash] funcs:count(distinct Column#13)->Column#15, funcs:sum(Column#14)->Column#16",
" └─ExchangeReceiver_30 1.00 mpp[tiflash] ",
" └─ExchangeSender_29 1.00 mpp[tiflash] ExchangeType: HashPartition, Hash Cols: ",
Copy link
Contributor

Choose a reason for hiding this comment

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

Hash Cols: here should be filled with Column#18

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, I think it is caused by this issue: #35417
I will bail out for cases where distinct column is an expression.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok

@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 13, 2022
@fixdb
Copy link
Contributor Author

fixdb commented Sep 13, 2022

@winoros @AilinKid I have addressed your comments, please take another look, thanks.

@ti-chi-bot ti-chi-bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Sep 13, 2022
Copy link
Contributor

@AilinKid AilinKid left a comment

Choose a reason for hiding this comment

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

Rest LGTM

@fixdb
Copy link
Contributor Author

fixdb commented Sep 14, 2022

Rest LGTM

Which part not LGTM? :)

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Sep 14, 2022
@fixdb fixdb requested a review from AilinKid September 14, 2022 06:27
@ti-chi-bot ti-chi-bot removed the status/LGT1 Indicates that a PR has LGTM 1. label Sep 16, 2022
@AilinKid
Copy link
Contributor

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: a5f7872

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Sep 16, 2022
@fixdb
Copy link
Contributor Author

fixdb commented Sep 16, 2022

/merge

@ti-chi-bot
Copy link
Member

@fixdb: /merge is only allowed for the committers, you can assign this pull request to the committer in list by filling /assign @committer in the comment to help merge this pull request.

In response to this:

/merge

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@AilinKid
Copy link
Contributor

/merge

@qw4990
Copy link
Contributor

qw4990 commented Sep 17, 2022

/merge

@ti-chi-bot ti-chi-bot merged commit efc0720 into pingcap:master Sep 17, 2022
@fixdb fixdb deleted the single_dqa branch September 17, 2022 06:12
@sre-bot
Copy link
Contributor

sre-bot commented Sep 17, 2022

TiDB MergeCI notify

✅ Well Done! New fixed [1] after this pr merged.

CI Name Result Duration Compare with Parent commit
idc-jenkins-ci-tidb/common-test 🔴 failed 1, success 10, total 11 52 min Existing failure
idc-jenkins-ci-tidb/integration-ddl-test ✅ all 6 tests passed 22 min Fixed
idc-jenkins-ci/integration-cdc-test 🟢 all 37 tests passed 26 min Existing passed
idc-jenkins-ci-tidb/integration-common-test 🟢 all 17 tests passed 7 min 53 sec Existing passed
idc-jenkins-ci-tidb/tics-test 🟢 all 1 tests passed 4 min 44 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-2 🟢 all 28 tests passed 4 min 24 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-1 🟢 all 26 tests passed 3 min 44 sec Existing passed
idc-jenkins-ci-tidb/mybatis-test 🟢 all 1 tests passed 3 min 17 sec Existing passed
idc-jenkins-ci-tidb/integration-compatibility-test 🟢 all 1 tests passed 3 min 1 sec Existing passed
idc-jenkins-ci-tidb/plugin-test 🟢 build success, plugin test success 4min Existing passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

planner should support 3 stage aggregation for single scalar distinct agg
7 participants