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/cascades: add transformation rule EliminateSingleMaxMin #14274

Merged
merged 18 commits into from
Feb 5, 2020

Conversation

Reminiscent
Copy link
Contributor

What problem does this PR solve?

This PR adds Transformation rule EliminateSingleMaxMin, which is a part of global max/min elimination in cascades planner.
part of #13709

What is changed and how it works?

The logic is the same with planner/core/rule_max_min_eliminate.go/maxMinEliminator.eliminateSingleMaxMin.

Check List

Tests

  • Unit test

@Reminiscent Reminiscent requested a review from a team as a code owner December 28, 2019 07:58
@ghost ghost requested review from alivxxx and lzmhhh123 December 28, 2019 07:58
@sre-bot sre-bot added the contribution This PR is from a community contributor. label Dec 28, 2019
@ghost ghost removed their request for review December 28, 2019 07:58
@zz-jason zz-jason added the sig/planner SIG: Planner label Dec 28, 2019
planner/cascades/transformation_rules.go Outdated Show resolved Hide resolved
planner/cascades/transformation_rules.go Show resolved Hide resolved
planner/cascades/transformation_rules.go Outdated Show resolved Hide resolved
planner/cascades/transformation_rules_test.go Outdated Show resolved Hide resolved
@Reminiscent
Copy link
Contributor Author

Sorry for the late address comments. PTAL @zz-jason @francis0407 @lzmhhh123

@Reminiscent Reminiscent requested a review from lzmhhh123 January 11, 2020 05:57
@Reminiscent
Copy link
Contributor Author

Update. PTAL @francis0407 @lzmhhh123

planner/cascades/transformation_rules.go Outdated Show resolved Hide resolved
// Since now there would be at most one row returned, the remained agg operator is not expensive anymore.
newAggExpr.SetChildren(childGroup)
newAggExpr.AddAppliedRule(r)
return []*memo.GroupExpr{newAggExpr}, true, false, nil
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 we'd better take more consideration on whether to erase the old plan piece. Consider this situation:

create table t(a bigint, b bigint) -- no index
select max(a) from t;
  1. The old plan maybe is: TableScan -> Aggregate(max(a))
  2. The new plan with TopN is: TableScan -> TopN(a, 1) -> Aggregate(max(a))

Since both TableScan is a full range table scan, it's hard to tell whether the new plan is better than the old one. Maybe we should keep the old one in this situation.

@Reminiscent Reminiscent requested a review from zz-jason January 16, 2020 07:27
@Reminiscent
Copy link
Contributor Author

@zz-jason @francis0407 @lzmhhh123 Update. PTAL

@Reminiscent
Copy link
Contributor Author

@lzmhhh123 @francis0407 @zz-jason Update. PTAL

@Reminiscent Reminiscent requested a review from zz-jason January 19, 2020 03:15
}

// Only one max() or min() in the AggFuncs slice, and
// the other aggregate functions in the AggFuncs slice should be FirstRow().
Copy link
Contributor

@alivxxx alivxxx Jan 19, 2020

Choose a reason for hiding this comment

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

Can we really elimate max/min in this case? After transformation, there is a selection between source and agg, so the first row becomes first row that the max/min arguments is not null, is it expected?

Copy link
Contributor Author

@Reminiscent Reminiscent Jan 20, 2020

Choose a reason for hiding this comment

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

It's unexpected. In mysql5.7:

mysql> select * from t;
+------+------+
| a    | b    |
+------+------+
| NULL |    1 |
|    1 |    2 |
+------+------+
2 rows in set (0.00 sec)

mysql> select max(a), b from t;
+--------+------+
| max(a) | b    |
+--------+------+
|      1 |    1 |
+--------+------+
1 row in set (0.00 sec)

I'll fix it. Thanks for your review.

Copy link
Contributor Author

@Reminiscent Reminiscent Jan 20, 2020

Choose a reason for hiding this comment

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

It seems it's not easy to include the first_row() aggFun in this transformation rule. Or we can add a check in the match function. We can't use this rule when the two conditions that first_row() and the column for max/min aggFunc can be NULL, exist at the same time. But I think it has too many restrictions leading to increased complexity. What's your opinion? @francis0407 @lamxTyler @zz-jason

Copy link
Member

Choose a reason for hiding this comment

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

@lamxTyler good catch !
I think we can revert this new feature. It’s hard to do that in the current framework. Just transform max/min like what we did before.
cc @zz-jason

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 we can rewrite the Match() condition to this:

  1. only one max or min, we can apply this transformation
  2. only one max or min, and all the max or min arguments are not null, we can apply this transformation as well.

BTW, how do calcite handle this problem?

Copy link
Member

Choose a reason for hiding this comment

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

Another problem is that currently we cannot check if the arguments are not null. mysql.HashNotNullFlag does not work here, since operators like OuterJoin may generate null values on not null columns.

Copy link
Member

Choose a reason for hiding this comment

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

We can add a TODO here, waiting for the not null prop is manitained.

@Reminiscent Reminiscent requested a review from alivxxx January 20, 2020 12:21
@Reminiscent Reminiscent requested a review from winoros February 4, 2020 11:05
@Reminiscent
Copy link
Contributor Author

Update. PTAL! @francis0407 @winoros

Copy link
Contributor

@alivxxx alivxxx 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
Member

@francis0407 francis0407 left a comment

Choose a reason for hiding this comment

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

LGTM

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

sre-bot commented Feb 5, 2020

/run-all-tests

@sre-bot sre-bot merged commit 43b8e13 into pingcap:master Feb 5, 2020
@Reminiscent Reminiscent deleted the XformMaxMinToTop1 branch August 5, 2021 02:03
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/planner SIG: Planner status/can-merge Indicates a PR has been approved by a committer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants