cockroachdb / cockroachdb/cockroach
sql: improve ALTER DOMAIN ADD CONSTRAINT support for array-of-domain columns
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
## Summary
`ALTER DOMAIN ... ADD CONSTRAINT` currently rejects validation against
columns whose type is an array of the domain. The rejection happens at
validation time rather than at build time, and we do not yet implement
actual validation of array elements.
## Background
The validation path in
[pkg/sql/check.go validateDomainConstraint](https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/check.go)
returns an `unimplemented` error when it encounters an array-of-domain
column on any of the domain's referencing tables. The error triggers the
standard declarative schema-changer rollback, so the constraint is
cleanly removed — but the work is wasted, and the user has already paid
the round-trip to the validation stage before seeing the failure.
The rejection lives at validation time (rather than in scbuild) because
`BuildCtx` does not currently expose enumeration of a domain's
back-referencing tables. Adding that API and wiring it through is a
non-trivial change that was out of scope for the initial ADD CONSTRAINT
work.
## Resolution Plan
Two independent improvements:
### Part 1 — Reject at build time
Expose back-reference enumeration on `BuildCtx` (e.g. a method that
returns the IDs of tables referencing a given type), then move the
array-of-domain rejection from
[validateDomainConstraint](https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/check.go)
to
[alterDomainAddCheckConstraint](https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_domain.go).
The user gets a synchronous error before any descriptor changes are
attempted.
### Part 2 — Implement array validation
Replace the rejection with an actual validation query that unnests the
array column and checks the CHECK expression against every element. For
NOT NULL, decide on PostgreSQL-compatible semantics (PG treats array
nulls as the element nulls, not the array itself being null).
Epic CRDB-66030
Jira issue: CRDB-64694
Contributor guide
Assessment
This issue has not been assessed yet.