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 optsteps crash caused by constraint expressions #43405

Merged
merged 3 commits into from
Jan 2, 2020

Conversation

RaduBerinde
Copy link
Member

@RaduBerinde RaduBerinde commented Dec 20, 2019

opt: make FormatExpr easier to use

During debugging, it's sometimes useful to add a statement to print an
expression. This is fairly hard and can crash easily because the memo
or the catalog isn't set in the formatting context. In particular, it
is impossible to print a scalar expression that contains a relational
expression because FormatExpr only sets the memo when we print
relational expressions.

This change improves the situation by allowing the caller of
FormatExpr to pass the *Memo and by adding a convenience wrapper
method on the Optimizer. We also automatically unset the "fully
qualify" flag if there is no catalog (which would otherwise crash).

Release note: None

opt: fix optsteps crash caused by TableMeta expressions

The optbuilder creates scalar constraint and computed column
expressions and hangs them off the TableMeta. If a normalization
rule fires for one of these expressions, optsteps panics because it
can't find a path to the expression in the memo.

This change fixes the problem by adding special code in opt_steps
which effectively treats these expressions as children of Scan
expressions.

Release note: None

opt: show TableMeta expressions when formatting expressions

We build check constraint and computed column expressions and attach
them to TableMeta, to be used later by exploration rules. These are
currently invisible. This change adds them under "canonical" scans
(the normalized scan expression before any exploration rules).

Release note: None

@RaduBerinde RaduBerinde requested a review from a team as a code owner December 20, 2019 16:39
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@andy-kimball andy-kimball 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 @andy-kimball and @RaduBerinde)


pkg/sql/opt/xform/testdata/external/foo, line 1 at r1 (raw file):

exec-ddl

Did you mean to add this file to the PR?

Copy link
Collaborator

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

:lgtm:

Reviewed 5 of 5 files at r1, 2 of 2 files at r2, 8 of 8 files at r3.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @RaduBerinde)


pkg/sql/opt/testutils/opttester/opt_tester.go, line 904 at r1 (raw file):

		}

		next = memo.FormatExpr(os.Root(), ot.Flags.ExprFormat, os.Root().(memo.RelExpr).Memo(), ot.catalog)

why not use the convenience wrapper here and below?


pkg/sql/opt/testutils/opttester/testdata/opt_steps, line 120 at r2 (raw file):


exec-ddl
CREATE TABLE customers (                        

Lots of extra spaces here and below

During debugging, it's sometimes useful to add a statement to print an
expression. This is fairly hard and can crash easily because the memo
or the catalog isn't set in the formatting context. In particular, it
is impossible to print a scalar expression that contains a relational
expression because `FormatExpr` only sets the memo when we print
relational expressions.

This change improves the situation by allowing the caller of
`FormatExpr` to pass the `*Memo` and by adding a convenience wrapper
method on the `Optimizer`. We also automatically unset the "fully
qualify" flag if there is no catalog (which would otherwise crash).

Release note: None
The optbuilder creates scalar constraint and computed column
expressions and hangs them off the `TableMeta`. If a normalization
rule fires for one of these expressions, `optsteps` panics because it
can't find a path to the expression in the memo.

This change fixes the problem by adding special code in opt_steps
which effectively treats these expressions as children of Scan
expressions.

Release note: None
Copy link
Member Author

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

Updated the commits to apply the same treatment to the new computed column expressions.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @andy-kimball and @rytaft)


pkg/sql/opt/testutils/opttester/opt_tester.go, line 904 at r1 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

why not use the convenience wrapper here and below?

Done.


pkg/sql/opt/testutils/opttester/testdata/opt_steps, line 120 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

Lots of extra spaces here and below

Done.


pkg/sql/opt/xform/testdata/external/foo, line 1 at r1 (raw file):

Previously, andy-kimball (Andy Kimball) wrote…

Did you mean to add this file to the PR?

Heh, no, fixed.

Copy link
Collaborator

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

:lgtm: It would be helpful if there were an easy way to associate the computed column expression with the column it represents (in the printed output), but I don't really have a specific idea.

Reviewed 3 of 11 files at r4, 1 of 2 files at r5, 17 of 17 files at r6.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @andy-kimball)

@RaduBerinde
Copy link
Member Author

Interesting, we do have the colIDs, I'll try to think of something. Maybe show them as a list as the first child, like we would do on a project

We build check constraint and computed column expressions and attach
them to `TableMeta`, to be used later by exploration rules. These are
currently invisible. This change adds them under "canonical" scans
(the normalized scan expression before any exploration rules).

Release note: None
@RaduBerinde
Copy link
Member Author

Done, I added each computed column as the parent of its expression.

Copy link
Collaborator

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

Nice! This will be really helpful. :lgtm:

Reviewed 14 of 14 files at r7.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @andy-kimball)

@RaduBerinde
Copy link
Member Author

TFTRs!

bors r+

craig bot pushed a commit that referenced this pull request Jan 2, 2020
43405: opt: fix optsteps crash caused by constraint expressions r=RaduBerinde a=RaduBerinde

#### opt: make FormatExpr easier to use

During debugging, it's sometimes useful to add a statement to print an
expression. This is fairly hard and can crash easily because the memo
or the catalog isn't set in the formatting context. In particular, it
is impossible to print a scalar expression that contains a relational
expression because `FormatExpr` only sets the memo when we print
relational expressions.

This change improves the situation by allowing the caller of
`FormatExpr` to pass the `*Memo` and by adding a convenience wrapper
method on the `Optimizer`. We also automatically unset the "fully
qualify" flag if there is no catalog (which would otherwise crash).

Release note: None

#### opt: fix optsteps crash caused by TableMeta expressions

The optbuilder creates scalar constraint and computed column
expressions and hangs them off the `TableMeta`. If a normalization
rule fires for one of these expressions, `optsteps` panics because it
can't find a path to the expression in the memo.

This change fixes the problem by adding special code in opt_steps
which effectively treats these expressions as children of Scan
expressions.

Release note: None

#### opt: show TableMeta expressions when formatting expressions

We build check constraint and computed column expressions and attach
them to `TableMeta`, to be used later by exploration rules. These are
currently invisible. This change adds them under "canonical" scans
(the normalized scan expression before any exploration rules).

Release note: None


43424: reducesql: also attempt to remove columns from PKs r=mjibson a=mjibson

Release note: None

Co-authored-by: Radu Berinde <[email protected]>
Co-authored-by: Matt Jibson <[email protected]>
@craig
Copy link
Contributor

craig bot commented Jan 2, 2020

Build succeeded

And happy new year from bors! 🎉

@craig craig bot merged commit 6cb068d into cockroachdb:master Jan 2, 2020
@RaduBerinde RaduBerinde deleted the optsteps branch January 3, 2020 23:27
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.

4 participants