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

Closed
sajjadrizvi opened this issue Sep 7, 2021 · 4 comments · Fixed by #70507
Closed

sql: null-value constraint is not enforced in CREATE TABLE ... AS (values) #69867

sajjadrizvi opened this issue Sep 7, 2021 · 4 comments · Fixed by #70507
Assignees
Labels
A-sql-execution Relating to SQL execution. C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-3-erroneous-edge-case Database produces or stores erroneous data without visible error/warning, in rare edge cases. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@sajjadrizvi
Copy link

The problem

A PK column can have a null value when a table is created using CREATE TABLE ... AS (values).

To Reproduce

BEGIN; CREATE TABLE foo (x PRIMARY KEY) AS VALUES (1), (NULL); END;

SELECT * FROM foo results in:

   x
--------
  NULL
     1
(2 rows)

I tested this using current master branch.

Expected behavior
The query should not succeed.

@sajjadrizvi sajjadrizvi added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label Sep 7, 2021
@sajjadrizvi
Copy link
Author

In addition, no jobs are created:

SELECT count(*) FROM [SHOW JOBS];
  count
---------
      0
(1 row)

@nvanbenschoten
Copy link
Member

Thanks for the report @sajjadrizvi!

I was able to reproduce this. However, I can only reproduce this when running the CREATE TABLE statement in an explicit transaction:

root@127.0.0.1:26257/movr> CREATE TABLE foo (x PRIMARY KEY) AS VALUES (1), (NULL);
ERROR: generate insert row: null value in column "x" violates not-null constraint
SQLSTATE: 23502

root@127.0.0.1:26257/movr> BEGIN; CREATE TABLE foo (x PRIMARY KEY) AS VALUES (1), (NULL); END;
COMMIT

@blathers-crl blathers-crl bot added the T-sql-queries SQL Queries Team label Sep 7, 2021
@nvanbenschoten nvanbenschoten added A-sql-execution Relating to SQL execution. and removed T-sql-queries SQL Queries Team labels Sep 7, 2021
@sajjadrizvi
Copy link
Author

I can only reproduce this when running the CREATE TABLE statement in an explicit transaction

Yes, I have also noticed that.

@blathers-crl blathers-crl bot added the T-sql-schema-deprecated Use T-sql-foundations instead label Sep 7, 2021
@ajwerner
Copy link
Contributor

ajwerner commented Sep 7, 2021

This is a @cockroachdb/sql-schema bug I think as we own the initial schema change. It's funny that these two are different cases.

@ajwerner ajwerner added the S-3-erroneous-edge-case Database produces or stores erroneous data without visible error/warning, in rare edge cases. label Sep 13, 2021
craig bot pushed a commit that referenced this issue Oct 12, 2021
70439: sql: fix username parsing for CURRENT_USER/SESSION_USER  r=rafiss a=RichardJCai

Release note (sql change): Fix bug where previously CURRENT_USER and
SESSION_USER were parsed incorrectly.

Fixes #54696

70507: rowenc: error when encoding NULLs for PK columns r=postamar a=postamar

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

Co-authored-by: richardjcai <[email protected]>
Co-authored-by: Marius Posta <[email protected]>
@craig craig bot closed this as completed in 48a8dd8 Oct 12, 2021
postamar pushed a commit to postamar/cockroach that referenced this issue Oct 13, 2021
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
postamar pushed a commit to postamar/cockroach that referenced this issue Oct 28, 2021
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 justification: Low risk addition of a correctness check.

Release note: None
postamar pushed a commit to postamar/cockroach that referenced this issue Oct 28, 2021
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 justification: Low risk addition of a correctness check.

Release note: None
postamar pushed a commit to postamar/cockroach that referenced this issue Nov 15, 2021
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 justification: Low risk addition of a correctness check.

Release note: None
@exalate-issue-sync exalate-issue-sync bot added T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) and removed T-sql-schema-deprecated Use T-sql-foundations instead labels May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-execution Relating to SQL execution. C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-3-erroneous-edge-case Database produces or stores erroneous data without visible error/warning, in rare edge cases. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants