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

plpgsql: don't exit early when SELECT INTO returns no rows #115650

Merged
merged 1 commit into from Dec 6, 2023

Conversation

DrewKimball
Copy link
Collaborator

Previously, a SELECT INTO statement that returned zero rows wouldn't call the continuation, since the continuation is called as a projection. This patch adds a RIGHT JOIN with a zero-column one-row Values operator to ensure that SELECT INTO statements are correctly null-extended when the underlying SQL statement returns no rows. See the postgres docs:

https://www.postgresql.org/docs/16/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW

Fixes #114826

Release note (bug fix): Fixed a bug that existed only in pre-release versions v23.2.0-beta.1 and v23.2.0-beta.2 which could cause PLpgSQL routines with SELECT INTO syntax to return early.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

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 elegant fix! :lgtm:

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


pkg/sql/opt/norm/factory.go line 469 at r1 (raw file):

// ConstructOneRowValues constructs a Values operator with zero columns and one
// row.
func (f *Factory) ConstructOneRowValues() memo.RelExpr {

nit: Just brainstorming some different name options: ConstructZeroColRow? ConstructSingleNullaryRowValues? Or use the same name of the custom func ConstructNoColsRows.

Copy link
Collaborator

@rharding6373 rharding6373 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!

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

Previously, a SELECT INTO statement that returned zero rows wouldn't
call the continuation, since the continuation is called as a projection.
This patch adds a RIGHT JOIN with a zero-column one-row Values operator
to ensure that SELECT INTO statements are correctly null-extended when
the underlying SQL statement returns no rows. See the postgres docs:
```
https://www.postgresql.org/docs/16/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW
```

Fixes cockroachdb#114826

Release note (bug fix): Fixed a bug that existed only in pre-release
versions v23.2.0-beta.1 and v23.2.0-beta.2 which could cause PLpgSQL
routines with SELECT INTO syntax to return early.
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! 2 of 0 LGTMs obtained (waiting on @mgartner)


pkg/sql/opt/norm/factory.go line 469 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

nit: Just brainstorming some different name options: ConstructZeroColRow? ConstructSingleNullaryRowValues? Or use the same name of the custom func ConstructNoColsRows.

Keeping ConstructNoColsRow is a good idea, I'll do that.

@DrewKimball DrewKimball added the backport-23.2.x Flags PRs that need to be backported to 23.2. label Dec 5, 2023
@DrewKimball
Copy link
Collaborator Author

TFTRs!

bors r+

@craig
Copy link
Contributor

craig bot commented Dec 6, 2023

Build succeeded:

@craig craig bot merged commit 51c554b into cockroachdb:master Dec 6, 2023
9 checks passed
@DrewKimball DrewKimball deleted the plpgsql-exec branch December 6, 2023 03:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.2.x Flags PRs that need to be backported to 23.2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: PL/pgSQL routine prematurely exits when SELECT ... INTO statements produce zero rows
4 participants