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: fix wrong agg function when agg push down union #17022

Merged

Conversation

lzmhhh123
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #16861

Problem Summary: when the aggregation is pushed down the union, we should append the first row for group by items.

What is changed and how it works?

What's Changed: append the first row when the partial aggregation is built under the union.

Related changes

  • Need to cherry-pick to the release branch

Check List

Tests

  • Unit test
  • Integration test

Side effects

  • Performance regression
    • Consumes more CPU

Release note

Fix the panic when aggregation push down enable on the partition table.

@lzmhhh123 lzmhhh123 added type/bugfix This PR fixes a bug. sig/planner SIG: Planner needs-cherry-pick-4.0 labels May 7, 2020
@lzmhhh123 lzmhhh123 requested review from a team as code owners May 7, 2020 10:04
@ghost ghost requested review from qw4990 and eurekaka and removed request for a team May 7, 2020 10:04
@github-actions github-actions bot added the sig/execution SIG execution label May 7, 2020
@codecov
Copy link

codecov bot commented May 7, 2020

Codecov Report

Merging #17022 into master will increase coverage by 0.0048%.
The diff coverage is 60.0000%.

@@               Coverage Diff                @@
##             master     #17022        +/-   ##
================================================
+ Coverage   79.8021%   79.8070%   +0.0048%     
================================================
  Files           520        520                
  Lines        139733     139623       -110     
================================================
- Hits         111510     111429        -81     
+ Misses        19252      19226        -26     
+ Partials       8971       8968         -3     

" └─Union_12 16000.00 root ",
" ├─HashAgg_16 8000.00 root group by:test.pt.b, funcs:firstrow(test.pt.b)->test.pt.b",
" ├─HashAgg_16 8000.00 root group by:test.pt.b, funcs:firstrow(test.pt.b)->test.pt.b, funcs:firstrow(test.pt.b)->test.pt.b",
Copy link
Member

Choose a reason for hiding this comment

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

The firstrow is duplicated 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.

Yes. For correctness.

Copy link
Member

Choose a reason for hiding this comment

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

Can we check if the firstrow is already existed?
BTW, I'm confused why we need to add firstrow here, it is supposed be added by planbuilder?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the partial aggregation should return the group by columns. In that way, final aggregation could execute.

@XuHuaiyu XuHuaiyu added needs-cherry-pick-3.0 priority/release-blocker This issue blocks a release. Please solve it ASAP. labels May 15, 2020
PARTITION p3 VALUES LESS THAN (40),
PARTITION p4 VALUES LESS THAN (MAXVALUE)
)`)
tk.MustExec("insert into t1 values (0, 0), (1, 1), (1, 2), (1, 3), (2, 4), (2, 5), (2, 6), (3, 7), (3, 10), (3, 11), (12, 12), (12, 13), (14, 14), (14, 15), (20, 20), (20, 21), (20, 22), (23, 23), (23, 24), (23, 25), (31, 30), (31, 31), (31, 32), (33, 33), (33, 34), (33, 35), (36, 36), (80, 80), (90, 90), (100, 100)")
Copy link
Contributor

Choose a reason for hiding this comment

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

set @@tidb_opt_agg_push_down=1

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

@@ -340,6 +340,11 @@ func (a *aggregationPushDownSolver) pushAggCrossUnion(agg *LogicalAggregation, u
for _, gbyExpr := range agg.GroupByItems {
newExpr := expression.ColumnSubstitute(gbyExpr, unionSchema, expression.Column2Exprs(unionChild.Schema().Columns))
newAgg.GroupByItems = append(newAgg.GroupByItems, newExpr)
firstRow, err := aggregation.NewAggFuncDesc(agg.ctx, ast.AggFuncFirstRow, []expression.Expression{gbyExpr}, false)
Copy link
Contributor

Choose a reason for hiding this comment

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

Check if first_row(gbyExpr) already exists before build a new one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we do this, we need to change the schema for partial aggregation. I'm afraid there would be some other corner cases, so I prefer to keep the duplicated first_row.

Copy link
Contributor

@eurekaka eurekaka left a comment

Choose a reason for hiding this comment

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

LGTM

@lzmhhh123
Copy link
Contributor Author

/merge

@sre-bot sre-bot added the status/can-merge Indicates a PR has been approved by a committer. label May 20, 2020
@sre-bot
Copy link
Contributor

sre-bot commented May 20, 2020

/run-all-tests

@sre-bot sre-bot merged commit b248783 into pingcap:master May 20, 2020
sre-bot pushed a commit to sre-bot/tidb that referenced this pull request May 20, 2020
Signed-off-by: sre-bot <sre-bot@pingcap.com>
@sre-bot
Copy link
Contributor

sre-bot commented May 20, 2020

cherry pick to release-3.0 in PR #17326

sre-bot pushed a commit to sre-bot/tidb that referenced this pull request May 20, 2020
Signed-off-by: sre-bot <sre-bot@pingcap.com>
@sre-bot
Copy link
Contributor

sre-bot commented May 20, 2020

cherry pick to release-3.1 in PR #17327

@sre-bot
Copy link
Contributor

sre-bot commented May 20, 2020

cherry pick to release-4.0 in PR #17328

@lzmhhh123 lzmhhh123 deleted the bug-fix/agg_to_cop_panic_on_partition branch May 21, 2020 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority/release-blocker This issue blocks a release. Please solve it ASAP. sig/execution SIG execution sig/planner SIG: Planner status/can-merge Indicates a PR has been approved by a committer. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

agg_to_cop get panic on partitioned table
6 participants