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 panic due to incorrect type of ArrayFlatten #39469

Merged
merged 1 commit into from
Aug 13, 2019

Conversation

rytaft
Copy link
Collaborator

@rytaft rytaft commented Aug 8, 2019

This commit fixes a panic caused by incorrect typing of an ArrayFlatten
expression. If the input to an ArrayFlatten expression is sorted, there
may be more than one output column (although the columns used for sorting
are hidden). If one of these hidden columns is chosen to infer the type
of the expression, the type could be incorrect. This commit fixes the
problem so that only the requested column is chosen for type inference.

Fixes #38867

Release note (bug fix): Fixed a panic due to incorrect type inference
of some ARRAY(...) expressions.

@rytaft rytaft requested a review from a team as a code owner August 8, 2019 19:41
@cockroach-teamcity
Copy link
Member

This change is Reviewable

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.

Thanks for the fix!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @justinj, @RaduBerinde, and @rytaft)


pkg/sql/opt/norm/custom_funcs.go, line 1630 at r1 (raw file):

// SubqueryRequestedCol returns the requested column from a SubqueryPrivate.
func (c *CustomFuncs) SubqueryRequestedCol(sub *memo.SubqueryPrivate) opt.ColumnID {
	return sub.RequestedCol

I found that this field is not set for SubqueryOp..

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.

hoistAll seems to have the same problem:

colID, _ := subqueryProps.OutputCols.Next(0)

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @justinj and @rytaft)

@rytaft rytaft force-pushed the fix-apply-panic branch 2 times, most recently from 3aebad6 to 45421a2 Compare August 12, 2019 14:07
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.

TFTR!

I tried to set RequestedCol for all types of subqueries, but it turned out to be a fair amount of trouble, especially for ANY and EXISTS. Since ArrayFlatten is the only subquery type where it's necessary, I think we should just make it clear that RequestedCol is only used by ArrayFlatten. I added a comment to that effect in the definition of SubqueryPrivate. I also updated hoistAll to make sure that if the subquery has type ArrayFlatten, we use RequestedCol instead of the first output column.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @justinj and @RaduBerinde)


pkg/sql/opt/norm/custom_funcs.go, line 1630 at r1 (raw file):

Previously, RaduBerinde wrote…

I found that this field is not set for SubqueryOp..

Added a comment to say this function should only be used with ArrayFlatten

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.

Consider adding a SubqueryColumn method to SubqueryExpr and others.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @justinj and @rytaft)


pkg/sql/opt/norm/custom_funcs.go, line 1630 at r1 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

Added a comment to say this function should only be used with ArrayFlatten

Maybe we can change it to take an ArrayFlattenExpr.


pkg/sql/opt/norm/decorrelate.go, line 805 at r2 (raw file):

		// Replace the Subquery operator with a Variable operator referring to
		// the output column of the hoisted query.
		colID, _ := subqueryProps.OutputCols.Next(0)

Ideally we should assert there is a single column (except for ArrayFlatten).

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.

Consider adding a SubqueryColumn method to SubqueryExpr and others.

I tried creating a Subquery interface with a SubqueryColumn method implemented by all four expressions, but it felt like a lot of overhead for the single use case of the hoistAll function. Instead I implemented your suggestion from the issue of adding a SingleColumn method to ColSet which panics if there is more than one column present.

Also added your other suggestion from the issue to update the comment for ConstructApplyJoin.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @justinj and @RaduBerinde)


pkg/sql/opt/norm/custom_funcs.go, line 1630 at r1 (raw file):

Previously, RaduBerinde wrote…

Maybe we can change it to take an ArrayFlattenExpr.

The problem is the one rule it is used with has ArrayFlatten as the root expression, so we can't pass that in.


pkg/sql/opt/norm/decorrelate.go, line 805 at r2 (raw file):

Previously, RaduBerinde wrote…

Ideally we should assert there is a single column (except for ArrayFlatten).

Changed to use SingleColumn method

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!

nit: maybe try doing a grep for other instances of Next(0) that can be replaced with the new method.

This commit fixes a panic caused by incorrect typing of an ArrayFlatten
expression. If the input to an ArrayFlatten expression is sorted, there
may be more than one output column (although the columns used for sorting
are hidden). If one of these hidden columns is chosen to infer the type
of the expression, the type could be incorrect. This commit fixes the
problem so that only the requested column is chosen for type inference.

Fixes cockroachdb#38867

Release note (bug fix): Fixed a panic due to incorrect type inference
of some ARRAY(...) expressions.
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.

nit: maybe try doing a grep for other instances of Next(0) that can be replaced with the new method.

Good idea - fixed a couple of cases

bors r+

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

craig bot pushed a commit that referenced this pull request Aug 13, 2019
39034: storage: use learner replicas for replica addition by default r=tbg a=danhhz

This was previously available by flipping a cluster setting that
defaulted to off, this defaults it to on. With any luck, we'll be
confident enough in this to remove the cluster setting in 19.2, which
will allow us to rip out a bunch of code at the beginning of the 20.1
cycle.

Closes #38902

Release note (general change): Replicas are now added using a raft
learner and going through the normal raft snapshot process to catch them
up, eliminating technical debt. No user facing changes are expected.

39469: opt: fix panic due to incorrect type of ArrayFlatten r=rytaft a=rytaft

This commit fixes a panic caused by incorrect typing of an `ArrayFlatten`
expression. If the input to an `ArrayFlatten` expression is sorted, there
may be more than one output column (although the columns used for sorting
are hidden). If one of these hidden columns is chosen to infer the type
of the expression, the type could be incorrect. This commit fixes the
problem so that only the requested column is chosen for type inference.

Fixes #38867

Release note (bug fix): Fixed a panic due to incorrect type inference
of some ARRAY(...) expressions.

Co-authored-by: Daniel Harrison <daniel.harrison@gmail.com>
Co-authored-by: Rebecca Taft <becca@cockroachlabs.com>
@craig
Copy link
Contributor

craig bot commented Aug 13, 2019

Build succeeded

@craig craig bot merged commit 1bceb6c into cockroachdb:master Aug 13, 2019
@rytaft rytaft deleted the fix-apply-panic branch April 2, 2020 22:15
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: mismatched type panic during apply join
3 participants