-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Conversation
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
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 |
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. |
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. |
FWIW, NOT NULL is enforced in mutations here: Line 425 in dfd5205
|
There was a problem hiding this 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()) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Thanks for the review.
You're completely correct and that's exactly were we're headed at with the new schema changer. |
bors r+ |
Build succeeded: |
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