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: do not type-check subqueries outside of optbuilder #106868

Merged
merged 1 commit into from
Jul 17, 2023

Conversation

mgartner
Copy link
Collaborator

This commit fixes a bug that was caused when attempting to re-type-check
a view's query that contains a subquery. This type-checking occurred
outside of optbuilder. However, the logic for type-checking subqueries
is only implemented within optbuilder, so it failed.

Fixes #105259

Release note (bug fix): A bug has been fixed that caused internal errors
when using user-defined types in views and user-defined functions that
have subqueries. This bug was present when using views since version
v21.2. It was present when using user-defined functions since v23.1.

@mgartner mgartner requested a review from a team as a code owner July 14, 2023 20:41
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@mgartner mgartner changed the title sql: do not type-check subqueries in views outside of optbuilder sql: do not type-check subqueries outside of optbuilder Jul 14, 2023
@mgartner mgartner added backport-22.2.x backport-23.1.x Flags PRs that need to be backported to 23.1 labels Jul 14, 2023
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

nice! glad we can keep it simple


# Renaming the enum value corrupts the UDF. This is expected behavior.
statement error pgcode 22P02 invalid input value for enum e105259: "foo"
SELECT f()
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: add a subtest end. (and also in the next file)

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:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @chengxiong-ruan and @rafiss)


pkg/sql/logictest/testdata/logic_test/udf line 3646 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: add a subtest end. (and also in the next file)

Educational question (since I only just started noticing subtest end in test files a couple days ago, so it doesn't seem to be used consistently): What is the benefit of using subtest end? It seems like subtests run correctly without it?

Copy link
Collaborator

@rafiss rafiss 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 @chengxiong-ruan and @rharding6373)


pkg/sql/logictest/testdata/logic_test/udf line 3646 at r1 (raw file):

Previously, rharding6373 (Rachael Harding) wrote…

Educational question (since I only just started noticing subtest end in test files a couple days ago, so it doesn't seem to be used consistently): What is the benefit of using subtest end? It seems like subtests run correctly without it?

they are not needed for correctness - the benefit is just that a future change that continues adding tests at the end of the file will not incorrectly be grouped with the subtest that was never ended. it can make things like viewing failures in teamcity harder to understand if that happens.

This commit fixes a bug that was caused when attempting to re-type-check
a view's query that contains a subquery. This type-checking occurred
outside of optbuilder. However, the logic for type-checking subqueries
is only implemented within optbuilder, so it failed.

Fixes cockroachdb#105259

Release note (bug fix): A bug has been fixed that caused internal errors
when using user-defined types in views and user-defined functions that
have subqueries. This bug was present when using views since version
v21.2. It was present when using user-defined functions since v23.1.
Copy link
Collaborator Author

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

nice! glad we can keep it simple

We can keep it simple for now, but I think in the long-term we should avoid this re-type checking entirely. We should be able to annotate the original AST and pass it along to the execution of these DDL statements so that the AST can be serialized correctly without having to traverse it again. And ideally, checks for things like cross-db references will happen in the optimizer and error-out early.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @chengxiong-ruan)


pkg/sql/logictest/testdata/logic_test/udf line 3646 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

they are not needed for correctness - the benefit is just that a future change that continues adding tests at the end of the file will not incorrectly be grouped with the subtest that was never ended. it can make things like viewing failures in teamcity harder to understand if that happens.

Done. I sprinkled a view more in too. I'll be more diligent about requiring these in the future.

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.

heh, easier than I thought!

@mgartner
Copy link
Collaborator Author

TFTRs!

bors r+

@craig craig bot merged commit 1778bf7 into cockroachdb:master Jul 17, 2023
7 checks passed
@craig
Copy link
Contributor

craig bot commented Jul 17, 2023

Build succeeded:

@blathers-crl
Copy link

blathers-crl bot commented Jul 17, 2023

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from bd38886 to blathers/backport-release-22.2-106868: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.2.x failed. See errors above.


error creating merge commit from bd38886 to blathers/backport-release-23.1-106868: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 23.1.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

mgartner added a commit to mgartner/cockroach that referenced this pull request Aug 7, 2023
This commit is a follow-up to cockroachdb#106868 after additional reproductions of
the original bug were found. For now, we disallow any CAST expressions
that contain a subquery in the input and the target type is an ENUM.
I've created cockroachdb#108184 to track this limitation.

Fixes cockroachdb#107654

There is no release note because the release note from cockroachdb#106868 should be
sufficient.

Release note: None
mgartner added a commit to mgartner/cockroach that referenced this pull request Aug 7, 2023
This commit is a follow-up to cockroachdb#106868 after additional reproductions of
the original bug were found. For now, we disallow any CAST expressions
that contain a subquery in the input and the target type is an ENUM.
I've created cockroachdb#108184 to track this limitation.

Fixes cockroachdb#107654

There is no release note because the release note from cockroachdb#106868 should be
sufficient.

Release note: None
mgartner added a commit to mgartner/cockroach that referenced this pull request Aug 7, 2023
This commit is a follow-up to cockroachdb#106868 after additional reproductions of
the original bug were found. For now, we disallow any CAST expressions
that contain a subquery in the input and the target type is an ENUM.
I've created cockroachdb#108184 to track this limitation.

Fixes cockroachdb#107654

There is no release note because the release note from cockroachdb#106868 should be
sufficient.

Release note: None
craig bot pushed a commit that referenced this pull request Aug 7, 2023
108188: sql: refactor semantic analysis and fix some bugs r=mgartner a=mgartner

#### sql/sem/tree: simplify SemaCtx reject flag checks

Release note: None

#### sql/sem/tree: split derived SemaContext properties from contextual info

Properties derived about expressions during semantic analysis are
communicated to callers via ScalarProperties. Prior to this commit, this
type was also used to provide contextual information while traversing
sub-expressions during semantic analysis. For example, it would indicate
whether the current expression is a descendent of a window function
expression.

These two types of information, derived and contextual, are
fundamentally different. Derived properties bubble up from the bottom of
the tree to the top, while context propagates downward into
sub-expressions. This difference made it difficult to maintaining them
correctly in a single type and difficult to reason about. This commit
introduces the ScalarScene type which is used for providing internal
contextual information during semantic analysis.

Release note: None

#### sql/sem/tree: do not Restore SemaRejectFlags during semantic analysis

This commit fixes a bug introduced in #105582 that caused
SemaRejectFlags to be restored during semantic analysis, preventing the
analysis from detecting some forms of invalid expressions.

Fixes #108166

There is no release note because the related bug does not exist in any
releases.

Release note: None

#### sql: do not allow subqueries to be cast to enums in views and UDFs

This commit is a follow-up to #106868 after additional reproductions of
the original bug were found. For now, we disallow any CAST expressions
that contain a subquery in the input and the target type is an ENUM.
I've created #108184 to track this limitation.

Fixes #107654

There is no release note because the release note from #106868 should be
sufficient.

Release note: None

#### sql/randgen: fix typo in comment

Release note: None


Co-authored-by: Marcus Gartner <marcus@cockroachlabs.com>
mgartner added a commit to mgartner/cockroach that referenced this pull request Aug 22, 2023
This commit is a follow-up to cockroachdb#106868 after additional reproductions of
the original bug were found. For now, we disallow any CAST expressions
that contain a subquery in the input and the target type is an ENUM.
I've created cockroachdb#108184 to track this limitation.

Fixes cockroachdb#107654

There is no release note because the release note from cockroachdb#106868 should be
sufficient.

Release note: None
mgartner added a commit to mgartner/cockroach that referenced this pull request Aug 22, 2023
This commit is a follow-up to cockroachdb#106868 after additional reproductions of
the original bug were found. For now, we disallow any CAST expressions
that contain a subquery in the input and the target type is an ENUM.
I've created cockroachdb#108184 to track this limitation.

Fixes cockroachdb#107654

There is no release note because the release note from cockroachdb#106868 should be
sufficient.

Release note: None
mgartner added a commit to mgartner/cockroach that referenced this pull request Aug 23, 2023
This commit is a follow-up to cockroachdb#106868 after additional reproductions of
the original bug were found. For now, we disallow any CAST expressions
that contain a subquery in the input and the target type is an ENUM.
I've created cockroachdb#108184 to track this limitation.

Fixes cockroachdb#107654

There is no release note because the release note from cockroachdb#106868 should be
sufficient.

Release note: None
mgartner added a commit to mgartner/cockroach that referenced this pull request Aug 23, 2023
This commit is a follow-up to cockroachdb#106868 after additional reproductions of
the original bug were found. For now, we disallow any CAST expressions
that contain a subquery in the input and the target type is an ENUM.
I've created cockroachdb#108184 to track this limitation.

Fixes cockroachdb#107654

There is no release note because the release note from cockroachdb#106868 should be
sufficient.

Release note: None
mgartner added a commit to mgartner/cockroach that referenced this pull request Aug 23, 2023
This commit is a follow-up to cockroachdb#106868 after additional reproductions of
the original bug were found. For now, we disallow any CAST expressions
that contain a subquery in the input and the target type is an ENUM.
I've created cockroachdb#108184 to track this limitation.

Fixes cockroachdb#107654

There is no release note because the release note from cockroachdb#106868 should be
sufficient.

Release note: None
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
Development

Successfully merging this pull request may close these issues.

roachtest/sqlsmith: internal error: building declarative schema change targets for CREATE FUNCTION
5 participants