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

sql: handle implicit record return types better #96696

Merged
merged 1 commit into from Feb 14, 2023

Conversation

rharding6373
Copy link
Collaborator

@rharding6373 rharding6373 commented Feb 7, 2023

This PR validates the UDF return type at build time if it is a user-defined type. If the return type is no longer compatible with what the UDF body returns, we return an error instead. This is more in line with postgres behavior.

Fixes: #95558
Epic: CRDB-19496
Release note (sql change): UDFs with implicit record return types will return an error when called if the return type has been altered and is no longer compatible with the body of the UDF.

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

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

@rharding6373 rharding6373 force-pushed the 20230131_udf_implicitrecord branch 2 times, most recently from 66aede2 to 86386f9 Compare February 13, 2023 18:08
@rharding6373
Copy link
Collaborator Author

TFTR!

bors r=DrewKimball

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 1 of 3 files at r1, 2 of 2 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @rharding6373)


pkg/sql/opt/optbuilder/scalar.go line 694 at r2 (raw file):

			// defined in the function.
			rtyp := f.ResolvedType()
			if rtyp.UserDefined() {

Is there any downside to performing this check for non-user-defined types?

Can you move this check outside the loop (probably above) because it doesn't reference any specific stmts within the body.

@rharding6373
Copy link
Collaborator Author

bors r-

@craig
Copy link
Contributor

craig bot commented Feb 13, 2023

Canceled.

This PR validates the UDF return type at build time if it is a
user-defined type. If the return type is no longer compatible with what
the UDF body returns, we return an error instead. This is more in line
with postgres behavior.

Fixes: 95558
Epic: CRDB-19496
Release note (sql change): UDFs with implicit record return types will
return an error when called if the return type has been altered and is
no longer compatible with the body of the UDF.
@rharding6373 rharding6373 requested a review from a team February 14, 2023 21:41
Copy link
Collaborator Author

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

TFTR!

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


pkg/sql/opt/optbuilder/scalar.go line 694 at r2 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

Is there any downside to performing this check for non-user-defined types?

Can you move this check outside the loop (probably above) because it doesn't reference any specific stmts within the body.

Good catch, moved to the beginning of the function.

Unfortunately there is some assumption under the hood of ResolveTypeByOID that it's a user defined type. Apparently this has to do with the fact that only UDTs need to be hydrated.

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.

LGTM

@rharding6373
Copy link
Collaborator Author

Thanks all!

bors r+

@craig
Copy link
Contributor

craig bot commented Feb 14, 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.

sql: UDF with implicit record return types should fail when column is added to table
5 participants