cockroachdb / cockroachdb/cockroach
sql: support nested domain types (domain of domain)
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
## Summary
PostgreSQL supports creating a domain whose base type is another domain. When this happens, constraints from all domains in the chain are enforced. CockroachDB should support this for full PostgreSQL compatibility.
## PostgreSQL Behavior
```sql
CREATE DOMAIN positive_int AS INT CHECK (VALUE > 0);
CREATE DOMAIN small_positive_int AS positive_int CHECK (VALUE < 100);
SELECT (-5)::small_positive_int;
-- ERROR: value for domain positive_int violates check constraint "positive_int_check"
SELECT 200::small_positive_int;
-- ERROR: value for domain small_positive_int violates check constraint "small_positive_int_check"
SELECT 50::small_positive_int;
-- OK
```
Both the parent domain's constraints and the child domain's constraints are enforced.
## Current State
As of #165187, `CREATE DOMAIN d2 AS d1` is blocked with an unimplemented error when `d1` is a domain type. This issue tracks removing that restriction and properly supporting nested domains.
## Implementation Notes
Key considerations:
- Constraint flattening vs. chain-walking at validation time
- NOT NULL and DEFAULT inheritance from parent domains
- Back-reference management across the domain chain
- `DROP TYPE CASCADE` behavior when a parent domain is dropped
Relates to #27796.
Jira issue: CRDB-61352
Epic CRDB-66030
Contributor guide
Assessment
This issue has not been assessed yet.