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

release-22.2: scbuildstmt: fallback if adding a virtual column with NOT NULL #87545

Merged
merged 1 commit into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions pkg/sql/logictest/testdata/logic_test/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -2649,21 +2649,9 @@ ALTER TABLE t_add_column_not_null ADD COLUMN j INT GENERATED ALWAYS AS (NULL:::I
statement error pgcode 23502 null value in column "j" violates not-null constraint
ALTER TABLE t_add_column_not_null ADD COLUMN j INT GENERATED ALWAYS AS (NULL::INT) VIRTUAL NOT NULL UNIQUE;

# Note that this should absolutely not succeed, but it does because of #81675.
# We'll rely on that in order to test that the subsequent index build fails.
# When addressing #81675, this portion of this test will need to be removed.
skipif config local-legacy-schema-changer
statement ok
ALTER TABLE t_add_column_not_null ADD COLUMN j INT GENERATED ALWAYS AS (NULL::INT) VIRTUAL NOT NULL;

onlyif config local-legacy-schema-changer
statement error validation of NOT NULL constraint failed: validation of CHECK "j IS NOT NULL" failed on row: i=1, j=NULL
ALTER TABLE t_add_column_not_null ADD COLUMN j INT GENERATED ALWAYS AS (NULL::INT) VIRTUAL NOT NULL;

skipif config local-legacy-schema-changer
statement error pgcode 23502 null value in column "j" violates not-null constraint
CREATE INDEX idx_j ON t_add_column_not_null (j);

statement ok
DROP TABLE t_add_column_not_null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func alterTableAddColumn(
// We don't support handling zone config related properties for tables, so
// throw an unsupported error.
fallBackIfZoneConfigExists(b, d, tbl.TableID)
fallBackIfVirtualColumnWithNotNullConstraint(t)
// Check column non-existence.
{
elts := b.ResolveColumn(tbl.TableID, d.Name, ResolveParams{
Expand Down
14 changes: 14 additions & 0 deletions pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,17 @@ func fallBackIfZoneConfigExists(b BuildCtx, n tree.NodeFormatter, id catid.DescI
}
}
}

// fallBackIfVirtualColumnWithNotNullConstraint throws an unimplemented error
// if the to-be-added column `d` is a virtual column with not null constraint.
// This is a quick, temporary fix for the following troubled stmt in the
// declarative schema changer:
// `ALTER TABLE t ADD COLUMN j INT AS (NULL::INT) VIRTUAL NOT NULL;` succeeded
// but expectedly failed in the legacy schema changer.
func fallBackIfVirtualColumnWithNotNullConstraint(t *tree.AlterTableAddColumn) {
d := t.ColumnDef
if d.IsVirtual() && d.Nullable.Nullability == tree.NotNull {
panic(scerrors.NotImplementedErrorf(t,
"virtual column with NOT NULL constraint is not supported"))
}
}