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

rowenc: error when encoding NULLs for PK columns #70507

Merged
merged 1 commit into from
Oct 12, 2021

Conversation

postamar
Copy link
Contributor

This commit adds a validation check to rowenc.EncodeIndexKey to have it
return an error when it encodes a NULL value for a primary key column.

Fixes #69867.

Release note: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

This commit adds a validation check to rowenc.EncodeIndexKey to have it
return an error when it encodes a NULL value for a primary key column.

Fixes cockroachdb#69867.

Release note: None
@jordanlewis
Copy link
Member

Interesting fix! One thing I don't understand - how do we avoid this situation under normal circumstances without this check? It seems to me that we're adding a check at a very low level of the system, which in and of itself isn't bad (defense in depth), but are we missing a higher level of validation that normally exists in all cases besides this CREATE TABLE AS scenario that this PR is designed to solve?

@ajwerner
Copy link
Contributor

One thing I don't understand - how do we avoid this situation under normal circumstances without this check?

I had a similar question. It turns out that in the implicit transaction case where we use a job, we end up relying on a similarly low-level check. An alternative approach might be to validate the non-nil properties of the columns by running a validation query. That would have cost and complexity and, thus, wouldn't be as suitable for backport.

I still favor such a check and will probably want us to plan one in the declarative schema changer.

@ajwerner ajwerner requested a review from a team October 12, 2021 14:26
@ajwerner
Copy link
Contributor

Tagging @cockroachdb/sql-queries for a review. The current thinking is that we should merge this approach and backport it and then file a separate issue to do higher level constraint checking.

@mgartner
Copy link
Collaborator

FWIW, NOT NULL is enforced in mutations here:

func enforceLocalColumnConstraints(row tree.Datums, cols []catalog.Column) error {

Copy link
Collaborator

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

Looks find to me, though it does seem a bit low level. I wonder if CREATE TABLE t (...) AS ... should just be syntactic sugar for:

BEGIN;
CREATE TABLE t (...);
INSERT INTO t ...;
COMMIT;

Then the mutation logic would enforce NOT NULL constraints.

return nil, true, errors.WithAssertionFailure(findErr)
}
if col.IsNullable() {
return nil, true, errors.AssertionFailedf("primary key column %q should not be nullable", col.GetName())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't we mark all PK columns as non-nullable in the descriptor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Weird things happen with CREATE TABLE ... AS ... unfortunately. It's sad.

@postamar
Copy link
Contributor Author

Thanks for the review.

I wonder if CREATE TABLE t (...) AS ... should just be syntactic sugar for BEGIN ... CREATE ... INSERT ... COMMIT.

You're completely correct and that's exactly were we're headed at with the new schema changer.

@postamar
Copy link
Contributor Author

bors r+

@craig
Copy link
Contributor

craig bot commented Oct 12, 2021

Build succeeded:

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: null-value constraint is not enforced in CREATE TABLE ... AS (values)
5 participants