Skip to content

Commit

Permalink
Merge pull request #87545 from cockroachdb/blathers/backport-release-…
Browse files Browse the repository at this point in the history
…22.2-87459
  • Loading branch information
Xiang-Gu authored Sep 8, 2022
2 parents f8e04f3 + c5d4a25 commit f37fdac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
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"))
}
}

0 comments on commit f37fdac

Please sign in to comment.