Skip to content

sql: enforce nullability when evaluating expressions in index backfills#81679

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
ajwerner:ajwerner/enforce-nullability-for-physical-column-expressions-in-secondary-indexes
May 25, 2022
Merged

sql: enforce nullability when evaluating expressions in index backfills#81679
craig[bot] merged 1 commit intocockroachdb:masterfrom
ajwerner:ajwerner/enforce-nullability-for-physical-column-expressions-in-secondary-indexes

Conversation

@ajwerner
Copy link
Copy Markdown
Contributor

Prior to this change, we never enforced nullability of values when performing
index backfills. Up until #76983, all column additions used the legacy schema
changer and its column backfill protocol. The column backfiller indeed checks
on nullability violations for columns here.

Release note (bug fix): Prior to this change, virtual computed columns which
were marked as NOT NULL could be added to new secondary index. After this
change, attempts to add such columns to a secondary index will result in an
error. Note that such invalid columns can still be added to tables. Work to
resolve that bug is tracked in #81675.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@ajwerner ajwerner requested review from a team and mgartner May 23, 2022 20:14
@ajwerner ajwerner force-pushed the ajwerner/enforce-nullability-for-physical-column-expressions-in-secondary-indexes branch from 8d7dac8 to 4abe444 Compare May 23, 2022 20:16
@chengxiong-ruan
Copy link
Copy Markdown
Contributor

pkg/sql/logictest/testdata/logic_test/alter_table line 2521 at r1 (raw file):


statement error pgcode 23502 null value in column "j" violates not-null constraint
ALTER TABLE t_add_column_not_null ADD COLUMN j INT NOT NULL DEFAULT (NULL:::INT)

NULL:::INT being a valid casting surprised me actually.... Any other use cases outside of this test case? just curious :)
But thanks for fixing this as later on...I guess UDF could return NULL as well...

ajwerner added a commit to ajwerner/cockroach that referenced this pull request May 23, 2022
Before this change, we did not ever validate that newly added
virtual computed columns which were marked NOT NULL actually were
not NULL. This is because there historically, in the legacy schema changer,
we'd validate the `NULL` nature of the column when performing a column
backfill. In the declarative schema changer, we don't do a column backfill,
so we have bugs aplenty related to the general failure to check the `NULL`
disposition of the data (see cockroachdb#81679).

This change only affects the legacy schema changer so that it can be
backported. A separate issue (cockroachdb#81690) has been filed for parity in the
declarative schema changer.

Release note (bug fix): Before this change, not validation was performed
when adding a virtual computed column which was marked `NOT NULL`. This meant
that it was possible to have a virtual computed column with an active `NOT
NULL`
constraint despite having rows in the table for which the column was `NULL`.
This has now been fixed.
Copy link
Copy Markdown
Contributor Author

@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 @mgartner)


pkg/sql/logictest/testdata/logic_test/alter_table line 2521 at r1 (raw file):

Previously, chengxiong-ruan (Chengxiong Ruan) wrote…

NULL:::INT being a valid casting surprised me actually.... Any other use cases outside of this test case? just curious :)
But thanks for fixing this as later on...I guess UDF could return NULL as well...

you can cast or type annotate NULL as anything. It's not needed except that without it I was tickling #81699.

@ajwerner ajwerner marked this pull request as ready for review May 24, 2022 01:17
Copy link
Copy Markdown
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.

:lgtm:

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

Copy link
Copy Markdown
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @chengxiong-ruan and @mgartner)

ajwerner added a commit to ajwerner/cockroach that referenced this pull request May 25, 2022
Before this change, we did not ever validate that newly added
virtual computed columns which were marked NOT NULL actually were
not NULL. This is because there historically, in the legacy schema changer,
we'd validate the `NULL` nature of the column when performing a column
backfill. In the declarative schema changer, we don't do a column backfill,
so we have bugs aplenty related to the general failure to check the `NULL`
disposition of the data (see cockroachdb#81679).

This change only affects the legacy schema changer so that it can be
backported. A separate issue (cockroachdb#81690) has been filed for parity in the
declarative schema changer.

Release note (bug fix): Before this change, not validation was performed
when adding a virtual computed column which was marked `NOT NULL`. This meant
that it was possible to have a virtual computed column with an active `NOT
NULL`
constraint despite having rows in the table for which the column was `NULL`.
This has now been fixed.
Prior to this change, we never enforced nullability of values when performing
index backfills. Up until cockroachdb#76983, all column additions used the legacy schema
changer and its column backfill protocol. The column backfiller indeed checks
on nullability violations for columns [here](https://github.com/cockroachdb/cockroach/blob/d5313438aaa61a71a5a66c5718963bbb0fd7268b/pkg/sql/backfill/backfill.go#L355-L357).

Release note (bug fix): Prior to this change, virtual computed columns which
were marked as NOT NULL could be added to new secondary index. After this
change, attempts to add such columns to a secondary index will result in an
error. Note that such invalid columns can still be added to tables. Work to
resolve that bug is tracked in cockroachdb#81675.
@ajwerner ajwerner force-pushed the ajwerner/enforce-nullability-for-physical-column-expressions-in-secondary-indexes branch from 4abe444 to 30fa449 Compare May 25, 2022 16:55
@ajwerner
Copy link
Copy Markdown
Contributor Author

TFTR!

bors r+

@craig craig bot merged commit 5b0453e into cockroachdb:master May 25, 2022
@craig
Copy link
Copy Markdown
Contributor

craig bot commented May 25, 2022

Build succeeded:

@blathers-crl
Copy link
Copy Markdown

blathers-crl bot commented May 25, 2022

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 30fa449 to blathers/backport-release-21.2-81679: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 21.2.x failed. See errors above.


error creating merge commit from 30fa449 to blathers/backport-release-22.1-81679: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.1.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

craig bot pushed a commit that referenced this pull request May 31, 2022
81693: sql: enforce NOT NULL when adding a virtual computed column r=ajwerner a=ajwerner

Before this change, we did not ever validate that newly added
virtual computed columns which were marked NOT NULL actually were
not NULL. This is because there historically, in the legacy schema changer,
we'd validate the `NULL` nature of the column when performing a column
backfill. In the declarative schema changer, we don't do a column backfill,
so we have bugs aplenty related to the general failure to check the `NULL`
disposition of the data (see #81679).

This change only affects the legacy schema changer so that it can be
backported. A separate issue (#81690) has been filed for parity in the
declarative schema changer.

Release note (bug fix): Before this change, not validation was performed
when adding a virtual computed column which was marked `NOT NULL`. This meant
that it was possible to have a virtual computed column with an active `NOT
NULL`
constraint despite having rows in the table for which the column was `NULL`.
This has now been fixed.

Co-authored-by: Andrew Werner <awerner32@gmail.com>
@rafiss
Copy link
Copy Markdown
Collaborator

rafiss commented Jun 24, 2022

Does this need a backport still?

@ajwerner
Copy link
Copy Markdown
Contributor Author

Does this need a backport still?

Yes, created #83353 and #83355. Thanks for the push!

ajwerner added a commit to ajwerner/cockroach that referenced this pull request Jun 27, 2022
Before this change, we did not ever validate that newly added
virtual computed columns which were marked NOT NULL actually were
not NULL. This is because there historically, in the legacy schema changer,
we'd validate the `NULL` nature of the column when performing a column
backfill. In the declarative schema changer, we don't do a column backfill,
so we have bugs aplenty related to the general failure to check the `NULL`
disposition of the data (see cockroachdb#81679).

This change only affects the legacy schema changer so that it can be
backported. A separate issue (cockroachdb#81690) has been filed for parity in the
declarative schema changer.

Release note (bug fix): Before this change, not validation was performed
when adding a virtual computed column which was marked `NOT NULL`. This meant
that it was possible to have a virtual computed column with an active `NOT
NULL`
constraint despite having rows in the table for which the column was `NULL`.
This has now been fixed.
ajwerner added a commit to ajwerner/cockroach that referenced this pull request Jun 27, 2022
Before this change, we did not ever validate that newly added
virtual computed columns which were marked NOT NULL actually were
not NULL. This is because there historically, in the legacy schema changer,
we'd validate the `NULL` nature of the column when performing a column
backfill. In the declarative schema changer, we don't do a column backfill,
so we have bugs aplenty related to the general failure to check the `NULL`
disposition of the data (see cockroachdb#81679).

This change only affects the legacy schema changer so that it can be
backported. A separate issue (cockroachdb#81690) has been filed for parity in the
declarative schema changer.

Release note (bug fix): Before this change, not validation was performed
when adding a virtual computed column which was marked `NOT NULL`. This meant
that it was possible to have a virtual computed column with an active `NOT
NULL`
constraint despite having rows in the table for which the column was `NULL`.
This has now been fixed.
@rafiss
Copy link
Copy Markdown
Collaborator

rafiss commented Jun 28, 2022

Thanks for doing that. Just to confirm, it looks like #81693 was backported, but this PR was not. (I only did a cursory check, but maybe the manual backport you did actually combined both.)

@ajwerner
Copy link
Copy Markdown
Contributor Author

Darn, wrong backport :(

@ajwerner
Copy link
Copy Markdown
Contributor Author

Thanks! Opened #83552 and #83551.

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.

6 participants