Skip to content

optbuilder: handle unnest returning a tuple#85069

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
DrewKimball:unnest
Jul 29, 2022
Merged

optbuilder: handle unnest returning a tuple#85069
craig[bot] merged 1 commit intocockroachdb:masterfrom
DrewKimball:unnest

Conversation

@DrewKimball
Copy link
Copy Markdown
Collaborator

Currently, the return types of SRFs that return multiple columns are
represented as tuples with labels. The tuple labels are used to decide
whether or not to create a single output column for the SRF, or multiple.
The unnest function can return a single column if it has a single argument,
and the type of that column can be a tuple with labels. This could cause the
old logic to mistakenly create multiple output columns for unnest, which
could lead to panics down the line and incorrect behavior otherwise.

This commit adds a special case for unnest in the optbuilder to only expand
tuple return types if there is more than one argument (implying more than one
output column). Other SRFs do not have the same problem because they either
always return the same number of columns, cannot return tuples, or both.

Fixes #58438

Release note (bug fix): Fixed a bug existing since release 20.1 that could
cause a panic in rare cases when the unnest function was used with a
tuple return type.

Currently, the return types of SRFs that return multiple columns are
represented as tuples with labels. The tuple labels are used to decide
whether or not to create a single output column for the SRF, or multiple.
The `unnest` function can return a single column if it has a single argument,
and the type of that column can be a tuple with labels. This could cause the
old logic to mistakenly create multiple output columns for `unnest`, which
could lead to panics down the line and incorrect behavior otherwise.

This commit adds a special case for `unnest` in the `optbuilder` to only expand
tuple return types if there is more than one argument (implying more than one
output column). Other SRFs do not have the same problem because they either
always return the same number of columns, cannot return tuples, or both.

Fixes cockroachdb#58438

Release note (bug fix): Fixed a bug existing since release 20.1 that could
cause a panic in rare cases when the unnest function was used with a
tuple return type.
@DrewKimball DrewKimball requested a review from a team as a code owner July 26, 2022 15:13
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
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: Nice work! Are you confident that unnest is the only function that behaves this way?

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

@rytaft
Copy link
Copy Markdown
Collaborator

rytaft commented Jul 29, 2022

(also please add labels to backport to 22.1 and 21.2 -- thanks!)

@DrewKimball
Copy link
Copy Markdown
Collaborator Author

TFTR!

Are you confident that unnest is the only function that behaves this way?

It's hard to be absolutely certain since it requires manually looking through the functions, but I'm fairly confident. This bug requires an srf to be capable of returning a labeled tuple column that isn't supposed to be expanded into multiple columns - the only other generator functions I can see that return tuples are a few JSONPopulate ones, and for those it is expected that the tuple be expanded in all cases.

Copy link
Copy Markdown
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.

Sounds good (if we find such a function we can always handle it later, anyway)

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

@DrewKimball
Copy link
Copy Markdown
Collaborator Author

bors r+

@craig craig bot merged commit 7e2df69 into cockroachdb:master Jul 29, 2022
@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jul 29, 2022

Build succeeded:

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.

opt: top-level relational expression cannot have outer columns

3 participants