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: resolve values types after building scalars in optbuilder #103078

Merged

Conversation

rharding6373
Copy link
Collaborator

Previously, we would build scalars in VALUES clauses after resolving the value column types. However, for UDFs, we modify the type if it's a record-returning function during the build. In this change we reverse the order so that we build scalars and then resolve types.

This bug was introduced by #98162.

Epic: None
Fixes: #102718

Release note: This fixes a bug in VALUES clauses containing a call to a record-returning UDF that could manifest as an internal error in some queries.

Previously, we would build scalars in VALUES clauses after resolving the
value column types. However, for UDFs, we modify the type if it's a
record-returning function during the build. In this change we reverse
the order so that we build scalars and then resolve types.

This bug was introduced by cockroachdb#98162.

Epic: None
Fixes: cockroachdb#102718

Release note: This fixes a bug in VALUES clauses containing a call to a
record-returning UDF that could manifest as an internal error in some
queries.
@rharding6373 rharding6373 requested a review from DrewKimball May 10, 2023 22:29
@rharding6373 rharding6373 requested a review from a team as a code owner May 10, 2023 22:29
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rharding6373 rharding6373 added the backport-23.1.x Flags PRs that need to be backported to 23.1 label May 10, 2023
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.

I think this fix is ok, but why can't the correct type be obtained during type-checking? Should we be doing the record-returning type resolving logic during function creation instead?

:lgtm:

Reviewed all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained

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. I'd consider refactoring UDFs to try to resolve record types at function creation in a future PR, I think that's doable. Thanks for the suggestion!

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

@rharding6373
Copy link
Collaborator Author

Actually, let me try to do a refactor this week before submitting this PR (though if it's successful it may be hefty for a backport).

@rharding6373
Copy link
Collaborator Author

I looked into refactoring this on the train, and it's not a simple refactor. The main thing that would have to change is we would have to save information about the function (in this case the types to which the RECORD resolves). As of my current understanding we'd have to modify both the schema changer code path and exec code path. I think that where and how to save this information in a way that we can get it again when the function is referenced is the challenge.

As such, I'm going to move forward with merging this PR in order to fix the bug for now, and maybe look into refactoring in the future.

@rharding6373
Copy link
Collaborator Author

bors r+

@craig
Copy link
Contributor

craig bot commented May 18, 2023

Build succeeded:

@craig craig bot merged commit b62c5f7 into cockroachdb:master May 18, 2023
@rharding6373 rharding6373 deleted the 20230510_udf_decode_err_102718 branch June 8, 2023 03:33
DrewKimball added a commit to DrewKimball/cockroach that referenced this pull request Aug 27, 2024
The type of a RECORD-returning UDF is not fully resolved until after it
is fully built within the optbuilder. Therefore, a VALUES expression with
a UDF can only determine its column types after building its rows. We
accounted for this in cockroachdb#103078, but that fix was incomplete - building the row
resolves the type of the UDF, but does not update the type annotation for
any expressions that wrap it (for example, a COALESCE).

This commit fully fixes the type resolution bug by type checking the row
once again after it is built. This ensures that the UDF's resolved type is
correctly propagated to parent expressions.

Fixes cockroachdb#117101

Release note (bug fix): Fixed a bug that would cause an internal error
when the result of a RECORD-returning UDF was wrapped by another expression
(such as COALESCE) within a VALUES clause.
DrewKimball added a commit to DrewKimball/cockroach that referenced this pull request Dec 17, 2024
The type of a RECORD-returning UDF is not fully resolved until after it
is fully built within the optbuilder. Therefore, a VALUES expression with
a UDF can only determine its column types after building its rows. We
accounted for this in cockroachdb#103078, but that fix was incomplete - building the row
resolves the type of the UDF, but does not update the type annotation for
any expressions that wrap it (for example, a COALESCE).

This commit fully fixes the type resolution bug by type checking the row
once again after it is built. This ensures that the UDF's resolved type is
correctly propagated to parent expressions.

Fixes cockroachdb#117101

Release note (bug fix): Fixed a bug that would cause an internal error
when the result of a RECORD-returning UDF was wrapped by another expression
(such as COALESCE) within a VALUES clause.
DrewKimball added a commit to DrewKimball/cockroach that referenced this pull request Dec 17, 2024
The type of a RECORD-returning UDF is not fully resolved until after it
is fully built within the optbuilder. Therefore, a VALUES expression with
a UDF can only determine its column types after building its rows. We
accounted for this in cockroachdb#103078, but that fix was incomplete - building the row
resolves the type of the UDF, but does not update the type annotation for
any expressions that wrap it (for example, a COALESCE).

This commit fully fixes the type resolution bug by type checking the row
once again after it is built. This ensures that the UDF's resolved type is
correctly propagated to parent expressions.

Fixes cockroachdb#117101

Release note (bug fix): Fixed a bug that would cause an internal error
when the result of a RECORD-returning UDF was wrapped by another expression
(such as COALESCE) within a VALUES clause.
DrewKimball added a commit to DrewKimball/cockroach that referenced this pull request Dec 17, 2024
The type of a RECORD-returning UDF is not fully resolved until after it
is fully built within the optbuilder. Therefore, a VALUES expression with
a UDF can only determine its column types after building its rows. We
accounted for this in cockroachdb#103078, but that fix was incomplete - building the row
resolves the type of the UDF, but does not update the type annotation for
any expressions that wrap it (for example, a COALESCE).

This commit fully fixes the type resolution bug by type checking the row
once again after it is built. This ensures that the UDF's resolved type is
correctly propagated to parent expressions.

Fixes cockroachdb#117101

Release note (bug fix): Fixed a bug that would cause an internal error
when the result of a RECORD-returning UDF was wrapped by another expression
(such as COALESCE) within a VALUES clause.
DrewKimball added a commit to DrewKimball/cockroach that referenced this pull request Dec 18, 2024
The type of a RECORD-returning UDF is not fully resolved until after it
is fully built within the optbuilder. Therefore, a VALUES expression with
a UDF can only determine its column types after building its rows. We
accounted for this in cockroachdb#103078, but that fix was incomplete - building the row
resolves the type of the UDF, but does not update the type annotation for
any expressions that wrap it (for example, a COALESCE).

This commit fully fixes the type resolution bug by type checking the row
once again after it is built. This ensures that the UDF's resolved type is
correctly propagated to parent expressions.

Fixes cockroachdb#117101

Release note (bug fix): Fixed a bug that would cause an internal error
when the result of a RECORD-returning UDF was wrapped by another expression
(such as COALESCE) within a VALUES clause.
craig bot pushed a commit that referenced this pull request Dec 18, 2024
129706: opt: type check VALUES rows after building them r=DrewKimball a=DrewKimball

The type of a RECORD-returning UDF is not fully resolved until after it is fully built within the optbuilder. Therefore, a VALUES expression with a UDF can only determine its column types after building its rows. We accounted for this in #103078, but that fix was incomplete - building the row resolves the type of the UDF, but does not update the type annotation for any expressions that wrap it (for example, a COALESCE).

This commit fully fixes the type resolution bug by type checking the row once again after it is built. This ensures that the UDF's resolved type is correctly propagated to parent expressions.

Fixes #117101

Release note (bug fix): Fixed a bug that would cause an internal error when the result of a RECORD-returning UDF was wrapped by another expression (such as COALESCE) within a VALUES clause.

137681: sql: Internal sessions exempt from disallow_full_table_scans setting r=spilchen a=spilchen

Previously, enabling the cluster setting sql.defaults.disallow_full_table_scans.enabled prevented the creation of indexes due to the internal table scans required during the process. This change ensures that the setting is bypassed for internal operations, while still applying to user queries.

Epic: None
Closes: #137404
Release note (bug fix): Internal scans are now exempt from the sql.defaults.disallow_full_table_scans.enabled setting, allowing index creation even when the cluster setting is active.

Co-authored-by: Drew Kimball <drewk@cockroachlabs.com>
Co-authored-by: Matt Spilchen <matt.spilchen@cockroachlabs.com>
blathers-crl bot pushed a commit that referenced this pull request Dec 18, 2024
The type of a RECORD-returning UDF is not fully resolved until after it
is fully built within the optbuilder. Therefore, a VALUES expression with
a UDF can only determine its column types after building its rows. We
accounted for this in #103078, but that fix was incomplete - building the row
resolves the type of the UDF, but does not update the type annotation for
any expressions that wrap it (for example, a COALESCE).

This commit fully fixes the type resolution bug by type checking the row
once again after it is built. This ensures that the UDF's resolved type is
correctly propagated to parent expressions.

Fixes #117101

Release note (bug fix): Fixed a bug that would cause an internal error
when the result of a RECORD-returning UDF was wrapped by another expression
(such as COALESCE) within a VALUES clause.
blathers-crl bot pushed a commit that referenced this pull request Dec 18, 2024
The type of a RECORD-returning UDF is not fully resolved until after it
is fully built within the optbuilder. Therefore, a VALUES expression with
a UDF can only determine its column types after building its rows. We
accounted for this in #103078, but that fix was incomplete - building the row
resolves the type of the UDF, but does not update the type annotation for
any expressions that wrap it (for example, a COALESCE).

This commit fully fixes the type resolution bug by type checking the row
once again after it is built. This ensures that the UDF's resolved type is
correctly propagated to parent expressions.

Fixes #117101

Release note (bug fix): Fixed a bug that would cause an internal error
when the result of a RECORD-returning UDF was wrapped by another expression
(such as COALESCE) within a VALUES clause.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.1.x Flags PRs that need to be backported to 23.1
Projects
None yet
3 participants