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

opt: fix detection of correlated subqueries in complex filters #46153

Merged
merged 1 commit into from
Mar 16, 2020

Conversation

rytaft
Copy link
Collaborator

@rytaft rytaft commented Mar 16, 2020

Prior to this commit, it was possible for a correlated subquery to
go undetected if it was buried in a complex filter. In particular,
a filter of the form:

  <correlated subquery> OR <non-correlated subquery>

would incorrectly be marked as not containing a correlated subquery.
This was because although the logical property HasCorrelatedSubquery
was initially set to true upon encountering the first (correlated)
subquery, the left-to-right recursive traversal of the OR expression
caused HasCorrelatedSubquery to be overwritten to false upon encountering
the second (non-correlated) subquery.

This commit fixes the issue by never overwriting HasCorrelatedSubquery
to false.

Fixes #46151

Release note (bug fix): Fixed an internal error that could occur in the
optimizer when a WHERE filter contained at least one correlated subquery
and one non-correlated subquery.

Release justification: This bug fix falls into the category "low risk,
high benefit changes to existing functionality".

@rytaft rytaft requested a review from a team as a code owner March 16, 2020 16:08
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@@ -1095,6 +1098,149 @@ inner-join (hash)
└── filters
└── d.x:11 = xy.x:14 [outer=(11,14), constraints=(/11: (/NULL - ]; /14: (/NULL - ]), fd=(11)==(14), (14)==(11)]

# Regression test for #46151. Do not push down a filter with a correlated
# subquery.
norm expect-not=PushFilterIntoJoinLeftAndRight
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this test be further simplified? These huge SQL-Smith style queries are a real pain to deal with when plans change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

@RaduBerinde RaduBerinde 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! 0 of 0 LGTMs obtained (waiting on @RaduBerinde and @rytaft)


pkg/sql/opt/memo/logical_props_builder.go, line 1351 at r1 (raw file):

// expression's subtree. It will only recurse into a child when it is not
// already caching properties.
func BuildSharedProps(e opt.Expr, shared *props.Shared) {

I think we ran into a similar bug before.. Let's add a comment here explaining that shared is an "input-output" argument, and should be assumed to be "partially filled in" already. It's hard to explain in general, maybe it would also help to specify exactly which fields are filled in.

Prior to this commit, it was possible for a correlated subquery to
go undetected if it was buried in a complex filter. In particular,
a filter of the form:

  <correlated subquery> OR <non-correlated subquery>

would incorrectly be marked as *not* containing a correlated subquery.
This was because although the logical property HasCorrelatedSubquery
was initially set to true upon encountering the first (correlated)
subquery, the left-to-right recursive traversal of the OR expression
caused HasCorrelatedSubquery to be overwritten to false upon encountering
the second (non-correlated) subquery.

This commit fixes the issue by never overwriting HasCorrelatedSubquery
to false.

Fixes cockroachdb#46151

Release note (bug fix): Fixed an internal error that could occur in the
optimizer when a WHERE filter contained at least one correlated subquery
and one non-correlated subquery.

Release justification: This bug fix falls into the category "low risk,
high benefit changes to existing functionality".
Copy link
Collaborator Author

@rytaft rytaft 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! 0 of 0 LGTMs obtained (waiting on @andy-kimball)


pkg/sql/opt/memo/logical_props_builder.go, line 1351 at r1 (raw file):

Previously, RaduBerinde wrote…

I think we ran into a similar bug before.. Let's add a comment here explaining that shared is an "input-output" argument, and should be assumed to be "partially filled in" already. It's hard to explain in general, maybe it would also help to specify exactly which fields are filled in.

Done.

Copy link
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

:lgtm: Thanks for the quick fix!

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

@rytaft
Copy link
Collaborator Author

rytaft commented Mar 16, 2020

TFTR!

bors r+

@craig
Copy link
Contributor

craig bot commented Mar 16, 2020

Build failed (retrying...)

@craig
Copy link
Contributor

craig bot commented Mar 16, 2020

Build succeeded

@craig craig bot merged commit 7af379d into cockroachdb:master Mar 16, 2020
rytaft added a commit to rytaft/cockroach that referenced this pull request Mar 17, 2020
In a small oversight, cockroachdb#46153 added some new checks to the
check_expr.go file, but did not wrap the error messages with
log.Safe. This commit fixes these error messages to match
the rest of the file.

Release justification: This is a low-risk bug fix to new functionality.
Release note: None
craig bot pushed a commit that referenced this pull request Mar 18, 2020
46229: opt: add safe logging to new test code r=rytaft a=rytaft

In a small oversight, #46153 added some new checks to the
`check_expr.go file`, but did not wrap the error messages with
`log.Safe`. This commit fixes these error messages to match
the rest of the file.

Release justification: This is a low-risk bug fix to new functionality.
Release note: None

Co-authored-by: Rebecca Taft <[email protected]>
rytaft added a commit to rytaft/cockroach that referenced this pull request Mar 18, 2020
In a small oversight, cockroachdb#46153 added some new checks to the
check_expr.go file, but did not wrap the error messages with
log.Safe. This commit fixes these error messages to match
the rest of the file.

Release justification: This is a low-risk bug fix to new functionality.
Release note: None
@rytaft rytaft deleted the indexed-var branch April 2, 2020 22:14
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.

opt: internal error due to outer variable in left side of join
4 participants