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: fix ordering-related optimizer panics #100776

Merged
merged 1 commit into from
Apr 10, 2023

Conversation

DrewKimball
Copy link
Collaborator

It is possible for some functional-dependency information to be visible to a child operator but invisible to its parent. This could previously cause panics when a child provided an ordering that could be proven to satisfy the required ordering with the child FDs, but not with the parent's FDs.

This patch adds a step to the logic that builds provided orderings that ensures a provided ordering can be proven to respect the required ordering without needing additional FD information. This ensures that a parent never needs to know its child's FDs in order to prove that the provided ordering is correct. The extra step is a no-op in the common case when the provided ordering can already be proven to respect the required ordering.

Informs #85393
Informs #87806
Fixes #96288

Release note (bug fix): Fixed a rare internal error in the optimizer that has existed since before version 22.1, which could occur while enforcing orderings between SQL operators.

@DrewKimball DrewKimball requested a review from a team as a code owner April 6, 2023 01:03
@DrewKimball DrewKimball requested a review from michae2 April 6, 2023 01:03
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@DrewKimball DrewKimball added backport-23.1.x Flags PRs that need to be backported to 23.1 backport-22.1.x labels Apr 6, 2023
@DrewKimball DrewKimball requested a review from mgartner April 7, 2023 19:29
Copy link
Contributor

@msirek msirek left a comment

Choose a reason for hiding this comment

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

Nice fix!

Reviewed all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball, @mgartner, and @michae2)


pkg/sql/opt/ordering/ordering.go line 91 at r1 (raw file):

	}
	provided := funcMap[expr.Op()].buildProvidedOrdering(expr, required)
	provided = finalizeProvided(provided, required, expr.Relational().OutputCols)

BuildProvided can be called both during planning when building the memo and also at the end when finding the lowest cost tree in the memo. If we finalize provided during planning, it seems like it would restrict the plans that we can search as it's locking in some columns over others. Maybe a boolean parameter should be added to BuildProvided to control when to finalize, and only finalize when called from setLowestCostTree.


pkg/sql/opt/ordering/ordering.go line 445 at r1 (raw file):

	// First check if the given provided is already suitable.
	providedCols := provided.ColSet()
	if len(provided) == len(required.Columns) && providedCols.SubsetOf(outCols) {

Should this be changed to len(provided) >= len(required.Columns) ?
An ordering on more columns than required should be able to satisfy the required ordering as well.

Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

Very nice! :lgtm:

Reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @DrewKimball and @michae2)

It is possible for some functional-dependency information to be visible
to a child operator but invisible to its parent. This could previously
cause panics when a child provided an ordering that could be proven to
satisfy the required ordering with the child FDs, but not with the
parent's FDs.

This patch adds a step to the logic that builds provided orderings that
ensures a provided ordering can be proven to respect the required ordering
without needing additional FD information. This ensures that a parent never
needs to know its child's FDs in order to prove that the provided ordering
is correct. The extra step is a no-op in the common case when the provided
ordering can already be proven to respect the required ordering.

Informs cockroachdb#85393
Informs cockroachdb#87806
Fixes cockroachdb#96288

Release note (bug fix): Fixed a rare internal error in the optimizer that has
existed since before version 22.1, which could occur while enforcing orderings
between SQL operators.
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 @michae2 and @msirek)


pkg/sql/opt/ordering/ordering.go line 91 at r1 (raw file):

Previously, msirek (Mark Sirek) wrote…

BuildProvided can be called both during planning when building the memo and also at the end when finding the lowest cost tree in the memo. If we finalize provided during planning, it seems like it would restrict the plans that we can search as it's locking in some columns over others. Maybe a boolean parameter should be added to BuildProvided to control when to finalize, and only finalize when called from setLowestCostTree.

Provided orderings necessarily lock in some columns, since they don't provided alternatives like required orderings do. Also, there's no reason AFAICT why the panic this is fixing couldn't occur during planning (rather than just the end), so I think it's necessary to do this unconditionally. FWIW in the vast majority of cases this shouldn't actually change the provided ordering - that's why not many tests changed.


pkg/sql/opt/ordering/ordering.go line 445 at r1 (raw file):

Previously, msirek (Mark Sirek) wrote…

Should this be changed to len(provided) >= len(required.Columns) ?
An ordering on more columns than required should be able to satisfy the required ordering as well.

We already remove optional columns in remapProvided and trimProvided. Do you know of a case where we'd be able to take advantage of additional columns in provided beyond what was requested in required?

Copy link
Contributor

@msirek msirek 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! 2 of 0 LGTMs obtained (waiting on @DrewKimball and @michae2)


pkg/sql/opt/ordering/ordering.go line 91 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

Provided orderings necessarily lock in some columns, since they don't provided alternatives like required orderings do. Also, there's no reason AFAICT why the panic this is fixing couldn't occur during planning (rather than just the end), so I think it's necessary to do this unconditionally. FWIW in the vast majority of cases this shouldn't actually change the provided ordering - that's why not many tests changed.

OK. I think alternatives might be handled by FDs.


pkg/sql/opt/ordering/ordering.go line 445 at r1 (raw file):

Do you know of a case where we'd be able to take advantage of additional columns in provided beyond what was requested in required?

No, looking at this closer, it looks like the other call, from populateBestProps, is not happening during optimization.

@DrewKimball
Copy link
Collaborator Author

TFYRs!

bors r+

@craig craig bot merged commit 668ec8d into cockroachdb:master Apr 10, 2023
@craig
Copy link
Contributor

craig bot commented Apr 10, 2023

Build succeeded:

@blathers-crl
Copy link

blathers-crl bot commented Apr 10, 2023

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from b9b8da6 to blathers/backport-release-22.1-100776: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.1.x failed. See errors above.


error creating merge commit from b9b8da6 to blathers/backport-release-22.2-100776: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.2.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@mgartner
Copy link
Collaborator

I added the backport-23.1.0 label because I believe we want to include this in the .0 release, correct @DrewKimball?

@DrewKimball
Copy link
Collaborator Author

I added the backport-23.1.0 label because I believe we want to include this in the .0 release, correct @DrewKimball?

Yeah, SGTM, thanks for doing that.

@DrewKimball
Copy link
Collaborator Author

blathers backport 23.1.0

@DrewKimball
Copy link
Collaborator Author

blathers backport 22.2

@blathers-crl
Copy link

blathers-crl bot commented Oct 25, 2023

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from b9b8da6 to blathers/backport-release-22.2-100776: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.2 failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

DrewKimball added a commit to DrewKimball/cockroach that referenced this pull request Oct 25, 2023
This patch adds a new session setting, `optimizer_use_provided_ordering_fix`,
which toggles whether to use the `finalizeProvided` function introduced in
required ordering. This setting is on by default, and will be used when
backporting cockroachdb#100776.

Informs cockroachdb#113072

Release note: None
DrewKimball added a commit to DrewKimball/cockroach that referenced this pull request Oct 25, 2023
This patch adds a new session setting, `optimizer_use_provided_ordering_fix`,
which toggles whether to use the `finalizeProvided` function introduced in
required ordering. This setting is on by default, and will be used when
backporting cockroachdb#100776.

Informs cockroachdb#113072

Release note: None
craig bot pushed a commit that referenced this pull request Oct 26, 2023
113097: opt: add setting for provided ordering fix r=DrewKimball a=DrewKimball

This patch adds a new session setting, `optimizer_use_provided_ordering_fix`, which toggles whether to use the `finalizeProvided` function introduced in required ordering. This setting is on by default, and will be used when backporting #100776.

Informs #113072

Release note: None

Co-authored-by: Drew Kimball <[email protected]>
blathers-crl bot pushed a commit that referenced this pull request Oct 26, 2023
This patch adds a new session setting, `optimizer_use_provided_ordering_fix`,
which toggles whether to use the `finalizeProvided` function introduced in
required ordering. This setting is on by default, and will be used when
backporting #100776.

Informs #113072

Release note: None
DrewKimball added a commit to DrewKimball/cockroach that referenced this pull request Oct 26, 2023
This patch adds a new session setting, `optimizer_use_provided_ordering_fix`,
which toggles whether to use the `finalizeProvided` function introduced in
required ordering. This setting is on by default, and will be used when
backporting cockroachdb#100776.

Informs cockroachdb#113072

Release note: None
blathers-crl bot pushed a commit that referenced this pull request Nov 1, 2023
This patch adds a new session setting, `optimizer_use_provided_ordering_fix`,
which toggles whether to use the `finalizeProvided` function introduced in
required ordering. This setting is on by default, and will be used when
backporting #100776.

Informs #113072

Release note: None
annrpom pushed a commit to annrpom/cockroach that referenced this pull request Nov 29, 2023
This patch adds a new session setting, `optimizer_use_provided_ordering_fix`,
which toggles whether to use the `finalizeProvided` function introduced in
required ordering. This setting is on by default, and will be used when
backporting cockroachdb#100776.

Informs cockroachdb#113072

Release note: None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.1.x Flags PRs that need to be backported to 23.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: Internal Error when executing query
4 participants