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: allow RETURN with no expression #120043

Merged
merged 2 commits into from Mar 11, 2024
Merged

Conversation

DrewKimball
Copy link
Collaborator

plpgsql: add parser support for RETURN with no expression

This commit adds support in the PL/pgSQL parser for RETURN statements
with no expression, like RETURN;.

plpgsql: allow RETURN with no expression

This commit adds support for RETURN statements with no expression.
This applies to routines with OUT-parameters, and those with a VOID
return type. This commit also refactors handling of implicit RETURN
statements for the previously mentioned cases. This ensures that implicit
returns are added in all possible control flow paths, including
exception handlers.

Informs #100405
Fixes #114849
Fixes #108298

Release note (sql change): Added support for RETURN statements with
no expression for routines with OUT-parameters and routines with a VOID
return type.

This commit adds support in the PL/pgSQL parser for RETURN statements
with no expression, like `RETURN;`.

Informs cockroachdb#108298

Release note: None
@DrewKimball DrewKimball requested a review from a team as a code owner March 7, 2024 11:05
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Member

@yuzefovich yuzefovich 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, thanks! :lgtm:

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


pkg/sql/opt/optbuilder/plpgsql.go line 337 at r2 (raw file):

	// building the variable declarations, so that expressions that reference
	// those variables can be type-checked.
	if types.IsRecordType(b.returnType) && !b.hasOutParam() {

nit: why we skip the type checking if we have an OUT param? Consider adjusting the comment below.

This commit adds support for `RETURN` statements with no expression.
This applies to routines with OUT-parameters, and those with a VOID
return type. This commit also refactors handling of implicit RETURN
statements for the previously mentioned cases. This ensures that implicit
returns are added in all possible control flow paths, including
exception handlers.

Informs cockroachdb#100405
Fixes cockroachdb#114849
Fixes cockroachdb#108298

Release note (sql change): Added support for `RETURN` statements with
no expression for routines with OUT-parameters and routines with a `VOID`
return type.
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 @mgartner and @yuzefovich)


pkg/sql/opt/optbuilder/plpgsql.go line 337 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: why we skip the type checking if we have an OUT param? Consider adjusting the comment below.

OUT params will have already determined the return type at this point. I modified the comment, and also removed the one above since it's redundant.

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: Nicely done!

Reviewed 4 of 4 files at r1, 7 of 7 files at r3, 7 of 7 files at r4, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @DrewKimball and @yuzefovich)


pkg/sql/opt/optbuilder/plpgsql.go line 1851 at r4 (raw file):

	)
	returnWithVoidParameterErr = pgerror.New(pgcode.DatatypeMismatch,
		"RETURN cannot have a parameter in function returning void",

nit: RETURN cannot have an expression here and above, if you agree


pkg/sql/plpgsql/parser/plpgsql.y line 1058 at r1 (raw file):

;

stmt_return: RETURN return_expr ';'

Instead of changing the lexer, would a RETURN ';' option work?

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.

TFYRs!

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @mgartner and @yuzefovich)


pkg/sql/opt/optbuilder/plpgsql.go line 1851 at r4 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

nit: RETURN cannot have an expression here and above, if you agree

Postgres does parameter instead of expression - do you think we should change that?


pkg/sql/plpgsql/parser/plpgsql.y line 1058 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

Instead of changing the lexer, would a RETURN ';' option work?

That would make the parser mad, unfortunately. PG does the same thing in pl_gram.y, FWIW.

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.

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


pkg/sql/opt/optbuilder/plpgsql.go line 1851 at r4 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

Postgres does parameter instead of expression - do you think we should change that?

Ah ok. Fine to leave as-is then.


pkg/sql/plpgsql/parser/plpgsql.y line 1058 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

That would make the parser mad, unfortunately. PG does the same thing in pl_gram.y, FWIW.

I figured. Oh well...

@DrewKimball
Copy link
Collaborator Author

bors r+

@craig
Copy link
Contributor

craig bot commented Mar 8, 2024

Timed out.

@DrewKimball
Copy link
Collaborator Author

bors retry

@craig
Copy link
Contributor

craig bot commented Mar 11, 2024

Build succeeded:

@craig craig bot merged commit 05d6206 into cockroachdb:master Mar 11, 2024
18 checks passed
@DrewKimball DrewKimball deleted the eof branch March 11, 2024 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants