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: ignore shard column during unique constraint searching #74140

Conversation

chengxiong-ruan
Copy link
Contributor

Fixes #69192

Referencing table does not know about the shard column. So
need to ignore it when looking for a valid unique constraint.

Release note (bug fix): Foreign key referencing hash sharded
key won't fail anymore.

@chengxiong-ruan chengxiong-ruan requested review from ajwerner, postamar and a team December 21, 2021 14:35
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@chengxiong-ruan chengxiong-ruan force-pushed the ignore-shard-column-when-looking-for-ref-index branch from 1dff2d8 to 60b2a8f Compare December 21, 2021 15:59
Copy link
Contributor

@ajwerner ajwerner 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 @chengxiong-ruan and @postamar)


pkg/sql/catalog/descpb/index.go, line 86 at r1 (raw file):

// key constraint with the provided set of referencedColumnIDs.
func (desc *IndexDescriptor) IsValidReferencedUniqueConstraint(referencedColIDs ColumnIDs) bool {
	colIDs := desc.ExplicitColumnIDsWithoutShardColumn()

why not just invoke this directly in the last part of the condition and short-circuit it most of the time. Also, why export it?

@chengxiong-ruan
Copy link
Contributor Author


pkg/sql/catalog/descpb/index.go, line 86 at r1 (raw file):

Previously, ajwerner wrote…

why not just invoke this directly in the last part of the condition and short-circuit it most of the time. Also, why export it?

Just wanted to make the line of return shorter, yeah good idea to short-circuit it to save some computation.
Definitely no reason at this moment, was thinking it might be useful in some other cases. Will unexport it, may export it later if found real use cases.

@chengxiong-ruan chengxiong-ruan force-pushed the ignore-shard-column-when-looking-for-ref-index branch from 60b2a8f to 26adda0 Compare December 21, 2021 20:08
Copy link
Contributor

@ajwerner ajwerner 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 @postamar)


pkg/sql/catalog/descpb/index.go, line 73 at r2 (raw file):

	explicitColIDs := desc.KeyColumnIDs[desc.ExplicitColumnStartIdx():]
	explicitColNames := desc.KeyColumnNames[desc.ExplicitColumnStartIdx():]
	colIDs := make([]ColumnID, 0, len(explicitColIDs))

nit: make this make(ColumnIDs, 0, len(explicitColIDs)), return ColumnIDs, and avoid the cast below?


pkg/sql/logictest/testdata/logic_test/hash_sharded_index, line 694 at r2 (raw file):

statement ok
CREATE TABLE parent (
id INT PRIMARY KEY USING HASH WITH BUCKET_COUNT = 8

nit: https://sqlfum.pt/ or cockroach sqlfmt your sql? It's jarring to see the newlines without the indentation.

Fixes cockroachdb#69192

Referencing table does not know about the shard column. So
need to ignore it when looking for a valid unique constraint.

Release note (bug fix): Foreign key referencing hash sharded
key won't fail anymore.
@chengxiong-ruan chengxiong-ruan force-pushed the ignore-shard-column-when-looking-for-ref-index branch from 26adda0 to 473e91c Compare December 21, 2021 21:16
Copy link
Contributor Author

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

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


pkg/sql/catalog/descpb/index.go, line 73 at r2 (raw file):

Previously, ajwerner wrote…

nit: make this make(ColumnIDs, 0, len(explicitColIDs)), return ColumnIDs, and avoid the cast below?

👍 done


pkg/sql/logictest/testdata/logic_test/hash_sharded_index, line 694 at r2 (raw file):

Previously, ajwerner wrote…

nit: https://sqlfum.pt/ or cockroach sqlfmt your sql? It's jarring to see the newlines without the indentation.

TIL the sqlfmt! nice~

@chengxiong-ruan
Copy link
Contributor Author

TFTR!

bors r+

@craig
Copy link
Contributor

craig bot commented Dec 21, 2021

Build succeeded:

@craig craig bot merged commit a7accab into cockroachdb:master Dec 21, 2021
@chengxiong-ruan chengxiong-ruan deleted the ignore-shard-column-when-looking-for-ref-index branch December 22, 2021 00:10
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.

Foreign key references to hash sharded primary key fail
3 participants