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

opt: add rule to fold limits #49931

Merged
merged 1 commit into from
Jun 8, 2020
Merged

Conversation

DrewKimball
Copy link
Collaborator

@DrewKimball DrewKimball commented Jun 6, 2020

Previously, there was no rule to fold a Limit on top of a Limit
when the outer limit value is smaller than the inner limit value.

This patch adds a rule to fold two Limits together when the
outer Limit has a smaller limit value than the inner Limit and
the inner ordering implies the outer ordering.

Release note (sql change): The optimizer can now fold two Limit
operators together.

@DrewKimball DrewKimball requested a review from a team as a code owner June 6, 2020 05:50
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@andy-kimball andy-kimball left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball)


pkg/sql/opt/norm/rules/limit.opt, line 160 at r1 (raw file):

[FoldLimits, Normalize]
(Limit
    $input:(Limit

NIT: For consistency, I'd make this $outerInput.


pkg/sql/opt/norm/testdata/rules/limit, line 50 at r1 (raw file):

 └── 100

# Don't eliminate the outer limit if it's less than the inner.

NIT: Should update this comment to be more clear, since outer limit is in fact "eliminated" (just not by the EliminateLimit rule).


pkg/sql/opt/norm/testdata/rules/limit, line 88 at r1 (raw file):


# Don't eliminate in case of negative limit.
norm expect-not=EliminateLimit

NIT: Similar comment issue here.


pkg/sql/opt/norm/testdata/rules/limit, line 1113 at r1 (raw file):

 └── 5

# Case where the outer limit ordering implies the inner ordering.

I don't think this is the most interesting case, because b is functionally dependent on a. I'd add a different case, where the ordered columns are independent of one another. Actually, two different cases, where the inner and outer orderings are swapped.

Copy link
Collaborator Author

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @andy-kimball)


pkg/sql/opt/norm/rules/limit.opt, line 160 at r1 (raw file):

Previously, andy-kimball (Andy Kimball) wrote…

NIT: For consistency, I'd make this $outerInput.

Actually, I just realized that doesn't have to be bound at all. Done.


pkg/sql/opt/norm/testdata/rules/limit, line 50 at r1 (raw file):

Previously, andy-kimball (Andy Kimball) wrote…

NIT: Should update this comment to be more clear, since outer limit is in fact "eliminated" (just not by the EliminateLimit rule).

Done.


pkg/sql/opt/norm/testdata/rules/limit, line 88 at r1 (raw file):

Previously, andy-kimball (Andy Kimball) wrote…

NIT: Similar comment issue here.

Done.


pkg/sql/opt/norm/testdata/rules/limit, line 1113 at r1 (raw file):

Previously, andy-kimball (Andy Kimball) wrote…

I don't think this is the most interesting case, because b is functionally dependent on a. I'd add a different case, where the ordered columns are independent of one another. Actually, two different cases, where the inner and outer orderings are swapped.

Done.

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 4 files at r1, 3 of 3 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @andy-kimball and @DrewKimball)


pkg/sql/opt/norm/rules/limit.opt, line 208 at r2 (raw file):

# FoldLimits replaces a Limit on top of a Limit with a single Limit operator
# when the outer limit value is smaller than or equal to the inner limit value
# and the orderings intersect.

[nit] might be worth mentioning that the opposite case, when the outer limit is larger than the inner limit, is already handled by the EliminateLimit rule.


pkg/sql/opt/norm/testdata/rules/limit, line 1322 at r2 (raw file):

 └── 5

# No-op case where the outer limit is larger than the inner limit.

(add a note that the limit is removed by EliminateLimit)


pkg/sql/opt/xform/testdata/rules/limit, line 65 at r2 (raw file):

 │    └── scan a@s_idx
 │         ├── columns: i:2 s:4
 │         ├── limit: 10

Hmm this seems suboptimal that we're removing the limit in this case. Is there a way to avoid this?

Copy link
Collaborator Author

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @andy-kimball and @rytaft)


pkg/sql/opt/norm/rules/limit.opt, line 208 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

[nit] might be worth mentioning that the opposite case, when the outer limit is larger than the inner limit, is already handled by the EliminateLimit rule.

Done.


pkg/sql/opt/norm/testdata/rules/limit, line 1322 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

(add a note that the limit is removed by EliminateLimit)

Done.


pkg/sql/opt/xform/testdata/rules/limit, line 65 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

Hmm this seems suboptimal that we're removing the limit in this case. Is there a way to avoid this?

Interesting, I didn't notice that. I'm not sure it could be done as a norm rule; this might suggest that this should be an exploration rule. Or we could only match when the inner ordering implies the outer ordering.

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r3.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @andy-kimball and @DrewKimball)


pkg/sql/opt/norm/rules/limit.opt, line 209 at r3 (raw file):

# when the outer limit value is smaller than or equal to the inner limit value
# and the orderings intersect. Note: the case when the outer limit value is
# larger than the smaller is handled by EliminateLimit.

larger than the smaller -> larger than the inner


pkg/sql/opt/xform/testdata/rules/limit, line 65 at r2 (raw file):

Or we could only match when the inner ordering implies the outer ordering.

This sounds like a good solution to me. I think if we can avoid adding unnecessary exploration rules that's a good thing.

Copy link
Collaborator Author

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @andy-kimball and @rytaft)


pkg/sql/opt/norm/rules/limit.opt, line 209 at r3 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

larger than the smaller -> larger than the inner

Done.


pkg/sql/opt/xform/testdata/rules/limit, line 65 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

Or we could only match when the inner ordering implies the outer ordering.

This sounds like a good solution to me. I think if we can avoid adding unnecessary exploration rules that's a good thing.

Done.

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 4 of 4 files at r4.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @andy-kimball and @DrewKimball)


pkg/sql/opt/norm/testdata/rules/limit, line 1276 at r3 (raw file):

 └── 5

# Case where the outer limit ordering implies the inner ordering.

I'd leave this test case, but use expect-not.

Previously, there was no rule to fold a Limit on top of a Limit
when the outer limit value is smaller than the inner limit value.

This patch adds a rule to fold two Limits together when the
outer Limit has a smaller limit value than the inner Limit and
the inner ordering implies the outer ordering.

Release note (sql change): The optimizer can now fold two Limit
operators together.
Copy link
Collaborator Author

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @andy-kimball and @rytaft)


pkg/sql/opt/norm/testdata/rules/limit, line 1276 at r3 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

I'd leave this test case, but use expect-not.

Done.

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r5.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @andy-kimball and @rytaft)

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @andy-kimball)

Copy link
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @andy-kimball)


pkg/sql/opt/xform/testdata/rules/limit, line 65 at r2 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

Done.

I agree!

@DrewKimball
Copy link
Collaborator Author

bors r+

@craig
Copy link
Contributor

craig bot commented Jun 8, 2020

Build succeeded

@craig craig bot merged commit 31d7475 into cockroachdb:master Jun 8, 2020
@DrewKimball DrewKimball deleted the limit_folding branch June 18, 2020 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants