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: consider virtual PK columns as stored in the optimizer #75898

Merged
merged 1 commit into from
Feb 8, 2022

Conversation

mgartner
Copy link
Collaborator

@mgartner mgartner commented Feb 2, 2022

Allowing virtual primary key columns created a contradiction of two
hard-coded assumptions of the optimizer:

  1. Primary key columns are written to the primary index (and all
    secondary indexes).
  2. Virtual columns are not written to the primary index.

To prevent this contradiction from causing bugs while planning, the opt
catalog presents columns that are marked as virtual PK columns in the
descriptor as stored columns to the optimizer. This is valid because
virtual computed PK columns behave exactly like stored computed PK
columns: they are written to all indexes.

The test catalog has been updated to mimic this behavior.

Fixes #75147
Fixes #75874

Release note (bug fix): A bug has been fixed that caused internal errors
when querying tables with virtual columns in the primary key. This bug
was only present since version 22.1.0-alpha.1 and does not appear in any
production releases.

@mgartner mgartner requested review from msirek, RaduBerinde and a team February 2, 2022 21:54
@mgartner mgartner requested a review from a team as a code owner February 2, 2022 21:55
@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.

:lgtm:

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


pkg/sql/opt_catalog.go, line 664 at r1 (raw file):

	// Determine the primary key columns.
	primaryIndex := desc.GetPrimaryIndex()

[nit] there is a CollectKeyColumnIDs method


pkg/sql/logictest/testdata/logic_test/virtual_columns, line 1257 at r1 (raw file):

  INDEX (a)
);

[nit] blank line

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.

thanks for fixing this!

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

Copy link
Contributor

@msirek msirek left a comment

Choose a reason for hiding this comment

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

Nice Solution.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mgartner, @msirek, and @RaduBerinde)


pkg/sql/opt/testutils/testcat/create_table.go, line 106 at r1 (raw file):

					def.Nullable.Nullability = tree.NotNull
					if def.Computed.Computed {
						def.Computed.Virtual = false

I saw an error in the hash_sharded index test. Could it be from this line? Maybe this needs something like what you did in opt_catalog.go with pkCols instead of set Virtual explicitly so there are no side effects.

@mgartner mgartner force-pushed the 75147-virtual-pk-columns branch from 835eeff to 45bfbb2 Compare February 3, 2022 15:38
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.

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


pkg/sql/opt_catalog.go, line 664 at r1 (raw file):

Previously, RaduBerinde wrote…

[nit] there is a CollectKeyColumnIDs method

Done.


pkg/sql/opt/testutils/testcat/create_table.go, line 106 at r1 (raw file):

Previously, msirek (Mark Sirek) wrote…

I saw an error in the hash_sharded index test. Could it be from this line? Maybe this needs something like what you did in opt_catalog.go with pkCols instead of set Virtual explicitly so there are no side effects.

The failures are in logic tests, which do not use the test catalog. The test catalog is only used in optimizer tests, which are most tests in pkg/sql/opt/**/testdata/*, except the logic tests that live in pkg/sql/opt/exec/exec/execbuilder. The test catalog mimics the opt catalog by implementing the cat.Table, cat.Index, etc. interfaces, so optimizer tests don't have to start up a full server and deal with real descriptors. One benefit is that optimizer tests run faster than logic tests.

The failing tests are a bit odd in that they are logic tests that print the optimizer catalog. I don't think we typically do this. In any case, to fix them I just needed to update the expected output with the -rewrite flag.


pkg/sql/logictest/testdata/logic_test/virtual_columns, line 1257 at r1 (raw file):

Previously, RaduBerinde wrote…

[nit] blank line

Done.

@mgartner mgartner force-pushed the 75147-virtual-pk-columns branch from 45bfbb2 to 7567518 Compare February 3, 2022 15:55
Copy link
Contributor

@msirek msirek 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 (and 1 stale) (waiting on @msirek and @RaduBerinde)


pkg/sql/opt/testutils/testcat/create_table.go, line 106 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

The failures are in logic tests, which do not use the test catalog. The test catalog is only used in optimizer tests, which are most tests in pkg/sql/opt/**/testdata/*, except the logic tests that live in pkg/sql/opt/exec/exec/execbuilder. The test catalog mimics the opt catalog by implementing the cat.Table, cat.Index, etc. interfaces, so optimizer tests don't have to start up a full server and deal with real descriptors. One benefit is that optimizer tests run faster than logic tests.

The failing tests are a bit odd in that they are logic tests that print the optimizer catalog. I don't think we typically do this. In any case, to fix them I just needed to update the expected output with the -rewrite flag.

OK. thanks. I hadn't gotten a chance to look into the code that deeply. I just wanted to make sure in the test catalog path it's not creating the table with the column as virtual, and based on your description it's not actually creating the table with full descriptors, etc.

:lgtm:

@mgartner
Copy link
Collaborator Author

mgartner commented Feb 3, 2022

TFTRs!

bors r+

@mgartner mgartner force-pushed the 75147-virtual-pk-columns branch from 7567518 to c13bd0e Compare February 3, 2022 19:30
@craig
Copy link
Contributor

craig bot commented Feb 3, 2022

Canceled.

@mgartner mgartner force-pushed the 75147-virtual-pk-columns branch from c13bd0e to 3ec67fd Compare February 4, 2022 17:00
Allowing virtual primary key columns created a contradiction of two
hard-coded assumptions of the optimizer:

  1. Primary key columns are written to the primary index (and all
     secondary indexes).
  2. Virtual columns are not written to the primary index.

To prevent this contradiction from causing bugs while planning, the opt
catalog presents columns that are marked as virtual PK columns in the
descriptor as stored columns to the optimizer. This is valid because
virtual computed PK columns behave exactly like stored computed PK
columns: they are written to all indexes.

The test catalog has been updated to mimic this behavior.

Fixes cockroachdb#75147

Release note (bug fix): A bug has been fixed that caused internal errors
when querying tables with virtual columns in the primary key. This bug
was only present since version 22.1.0-alpha.1 and does not appear in any
production releases.
@mgartner mgartner force-pushed the 75147-virtual-pk-columns branch from 3ec67fd to 37d08ab Compare February 4, 2022 17:06
@mgartner
Copy link
Collaborator Author

mgartner commented Feb 8, 2022

bors r+

@craig
Copy link
Contributor

craig bot commented Feb 8, 2022

Build succeeded:

@craig craig bot merged commit 8ec5bf1 into cockroachdb:master Feb 8, 2022
@mgartner mgartner deleted the 75147-virtual-pk-columns branch February 8, 2022 14: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
5 participants