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 incorrect results returned by non-recursive WITH RECURSIVE CTE #98042

Merged
merged 1 commit into from
Mar 6, 2023

Conversation

rytaft
Copy link
Collaborator

@rytaft rytaft commented Mar 6, 2023

Prior to this commit, queries that used a CTE marked as WITH RECURSIVE which did not reference itself (i.e., it was not actually recursive) could return incorrect results. This was because the UNION ALL in the CTE was incorrectly converted to a UNION by the optimizer. This commit fixes the problem by ensuring that UNION ALL is used if the original CTE contained UNION ALL.

Fixes #93370

Release note (bug fix): Fixed a bug in which CTEs marked as WITH RECURSIVE which were not actually recursive could return incorrect results. This could happen if the CTE used a UNION ALL, because the optimizer incorrectly converted the UNION ALL to a UNION. This bug has existed since suppport for recursive CTEs was first added in v20.1, and has now been fixed.

@rytaft rytaft requested a review from a team as a code owner March 6, 2023 00:13
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator

@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.

:lgtm: Nice fix!

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

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.

:lgtm:

Reviewed 4 of 4 files at r1, all commit messages.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @RaduBerinde)

Prior to this commit, queries that used a CTE marked as WITH RECURSIVE
which did not reference itself (i.e., it was not actually recursive)
could return incorrect results. This was because the UNION ALL in the
CTE was incorrectly converted to a UNION by the optimizer. This commit
fixes the problem by ensuring that UNION ALL is used if the original CTE
contained UNION ALL.

Fixes cockroachdb#93370

Release note (bug fix): Fixed a bug in which CTEs marked as WITH RECURSIVE
which were not actually recursive could return incorrect results. This
could happen if the CTE used a UNION ALL, because the optimizer incorrectly
converted the UNION ALL to a UNION. This bug has existed since suppport for
recursive CTEs was first added in v20.1, and has now been fixed.
@rytaft rytaft force-pushed the recursive-cte-fix branch from 4bc9330 to 74fa893 Compare March 6, 2023 15:47
@RaduBerinde
Copy link
Member

Oopsie.. LGTM

Copy link
Collaborator Author

@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.

TFTRs!

bors r+

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @DrewKimball, @mgartner, and @RaduBerinde)

@craig
Copy link
Contributor

craig bot commented Mar 6, 2023

Build succeeded:

@craig craig bot merged commit 709d765 into cockroachdb:master Mar 6, 2023
@blathers-crl
Copy link

blathers-crl bot commented Mar 6, 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 74fa893 to blathers/backport-release-22.1-98042: 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 74fa893 to blathers/backport-release-22.2-98042: 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.

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.

sql: possible incorrect results with complex recursive CTE
5 participants