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: add support for CONSTANT variable declarations #107682

Merged
merged 2 commits into from
Jul 30, 2023

Conversation

DrewKimball
Copy link
Collaborator

plpgsql: validate PLpgSQL functions during creation

This patch ensures that PLpgSQL functions are built during function
creation, not just during invocation. This ensures that compile-time
errors are thrown when the function is created. This commit also tests
and corrects the codes for some of the errors that can be thrown while
building a PLpgSQL function.

Note that some of the existing tests had to be removed since they called
UDFs within other UDFs. We didn't catch this before because the restriction
is only checked during function creation when the body statements are first
built.

Fixes #107681

Release note: None

plpgsql: add support for CONSTANT variable declarations

It is possible to declare variables as CONSTANT in PLpgSQL. This will cause
any attempt to assign to the variable to result in a compile-time error.
This patch implements CONSTANT variables by adding a check whenever an
assignment is built that the assigned variable is non-constant.

Fixes #105241

Release note (sql change): Added support for CONSTANT variable
declarations in PLpgSQL routines. Any assignment to a variable declared
with the CONSTANT keyword will raise a compile-time error.

@DrewKimball DrewKimball requested a review from a team as a code owner July 27, 2023 05:48
@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.

:lgtm: Very nice!

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


pkg/sql/opt/optbuilder/create_function.go line 250 at r1 (raw file):

			stmtScope = plBuilder.build(stmt.AST, bodyScope)
		})
		checkStmtVolatility(targetVolatility, stmtScope, stmt)

Can you add tests for mismatched volatility, if they don't already exist?

Copy link
Contributor

@chengxiong-ruan chengxiong-ruan left a comment

Choose a reason for hiding this comment

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

cool.

This patch ensures that PLpgSQL functions are built during function
creation, not just during invocation. This ensures that compile-time
errors are thrown when the function is created. This commit also tests
and corrects the codes for some of the errors that can be thrown while
building a PLpgSQL function.

Note that some of the existing tests had to be removed since they called
UDFs within other UDFs. We didn't catch this before because the restriction
is only checked during function creation when the body statements are first
built.

Fixes cockroachdb#107681

Release note: None
It is possible to declare variables as CONSTANT in PLpgSQL. This will cause
any attempt to assign to the variable to result in a compile-time error.
This patch implements CONSTANT variables by adding a check whenever an
assignment is built that the assigned variable is non-constant.

Fixes cockroachdb#105241

Release note (sql change): Added support for CONSTANT variable
declarations in PLpgSQL routines. Any assignment to a variable declared
with the CONSTANT keyword will raise a compile-time error.
@DrewKimball
Copy link
Collaborator Author

TFTRs!

bors r+

@craig
Copy link
Contributor

craig bot commented Jul 30, 2023

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.

plpgsql: validate PLpgSQL UDFs during creation plpgsql: add support for CONSTANT variables
4 participants