Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: do not allow inaccessible columns in primary keys
Browse files Browse the repository at this point in the history
Release note: None
mgartner committed Jul 3, 2021
1 parent 29c4a5e commit d5c08cf
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/sql/alter_primary_key.go
Original file line number Diff line number Diff line change
@@ -133,6 +133,9 @@ func (p *planner) AlterPrimaryKey(
return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,
"column %q is being dropped", col.GetName())
}
if col.IsInaccessible() {
return pgerror.Newf(pgcode.InvalidSchemaDefinition, "cannot use inaccessible column %q in primary key", col.GetName())
}
if col.IsNullable() {
return pgerror.Newf(pgcode.InvalidSchemaDefinition, "cannot use nullable column %q in primary key", col.GetName())
}
13 changes: 13 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/expression_index
Original file line number Diff line number Diff line change
@@ -181,6 +181,10 @@ CREATE TABLE public.t (
statement error column \"crdb_internal_idx_expr_4\" is inaccessible and cannot be referenced
ALTER TABLE t ADD CONSTRAINT err CHECK (crdb_internal_idx_expr_4 > 0)

# Referencing an inaccessible column in a NOT NULL constraint is not allowed.
statement error column \"crdb_internal_idx_expr_4\" is inaccessible and cannot be referenced
ALTER TABLE t ALTER COLUMN crdb_internal_idx_expr_4 SET NOT NULL

# Referencing an inaccessible column in a UNIQUE constraint is not allowed.
statement error column \"crdb_internal_idx_expr_4\" is inaccessible and cannot be referenced by a unique constraint
ALTER TABLE t ADD CONSTRAINT err UNIQUE (crdb_internal_idx_expr_4)
@@ -310,6 +314,15 @@ CREATE INDEX err ON other ((t.a + 10))
statement error column \"crdb_internal_idx_expr\" does not exist
SELECT * FROM t WHERE crdb_internal_idx_expr > 0

statement ok
CREATE TABLE pk (
k INT PRIMARY KEY,
UNIQUE INDEX ((k + 10))
)

statement error cannot use inaccessible column \"crdb_internal_idx_expr\" in primary key
ALTER TABLE pk ALTER PRIMARY KEY USING COLUMNS (crdb_internal_idx_expr)

# Test anonymous index name generation.

statement ok

0 comments on commit d5c08cf

Please sign in to comment.