feat: ADR-0035 4a.2 — per-column CHECK/DEFAULT + composite UNIQUE

Advanced-mode SQL CREATE TABLE gains the constraints that need no new
internal table (the 4a.2 slice):

- Grammar (sql_create_table.rs): column-level DEFAULT/CHECK and
  table-level UNIQUE(cols). DEFAULT is a literal or a *parenthesised*
  expression (standard SQL) — a bare sql_expr greedily eats a following
  NOT (NOT IN/LIKE/BETWEEN), breaking `DEFAULT 0 NOT NULL`; the parens
  bound it. CHECK is paren-bounded already.
- Builder (ddl.rs): captures CHECK/DEFAULT raw SQL text by byte span
  (sql_expr builds no AST) via capture_parenthesised_span /
  capture_expr_span; routes single-column table UNIQUE into the
  column's flag and composite UNIQUE into unique_constraints.
- Command/worker: ColumnSpec gains check_sql/default_sql (raw, preferred
  over the typed Expr/Value); Command::SqlCreateTable + Request +
  do_create_table gain unique_constraints; do_create_table emits raw
  CHECK/DEFAULT and composite UNIQUE clauses.
- Round-trip (part D): ReadSchema/TableSchema gain unique_constraints;
  read_schema detects composite UNIQUE via PRAGMA index_list origin 'u'
  (single-column still folds to the column flag); schema_to_ddl emits
  them; YAML RawTable/write_table round-trips (optional-on-read).
  CHECK round-trips via __rdbms_playground_columns.check_expr, DEFAULT
  via PRAGMA table_info — no new metadata table.

Table-level/multi-column CHECK remains 4a.3 (rejected "not yet
supported"); FK is 4b.

Tests: +7 builder (raw-text capture incl. the DEFAULT 0 NOT NULL
boundary the fix was found by; single/composite UNIQUE routing) and +4
Tier-3 (CHECK enforced, DEFAULT applied, composite UNIQUE enforced, and
all three survive a rebuild — the part-D round-trip). 1752 pass / 0 fail
/ 1 ignored; clippy clean. Plan + requirements.md updated.
This commit is contained in:
claude@clouddev1
2026-05-25 11:04:59 +00:00
parent 1c50133438
commit c0f5626787
10 changed files with 627 additions and 59 deletions
+17 -2
View File
@@ -30,11 +30,20 @@ pub struct ColumnSpec {
/// `UNIQUE` — non-`NULL` values must be distinct (ADR-0029).
pub unique: bool,
/// `DEFAULT <literal>` — the value used when an `insert`
/// omits this column (ADR-0029).
/// omits this column (ADR-0029). Simple-mode form.
pub default: Option<Value>,
/// `CHECK (<expr>)` — every row must satisfy this boolean
/// expression (ADR-0029).
/// expression (ADR-0029). Simple-mode form (a typed `Expr`).
pub check: Option<Expr>,
/// Advanced-mode raw-SQL `DEFAULT` (ADR-0035 §4a.2): the
/// expression text captured from a SQL `CREATE TABLE`, since
/// `sql_expr` yields no `Expr`. When `Some`, it takes precedence
/// over `default` in DDL emission. `None` in simple mode.
pub default_sql: Option<String>,
/// Advanced-mode raw-SQL `CHECK` (ADR-0035 §4a.2): the inner
/// expression text (without the `CHECK ( … )` wrapper). When
/// `Some`, it takes precedence over `check`. `None` in simple mode.
pub check_sql: Option<String>,
}
impl ColumnSpec {
@@ -49,6 +58,8 @@ impl ColumnSpec {
unique: false,
default: None,
check: None,
default_sql: None,
check_sql: None,
}
}
}
@@ -133,6 +144,10 @@ pub enum Command {
name: String,
columns: Vec<ColumnSpec>,
primary_key: Vec<String>,
/// Composite (multi-column) `UNIQUE (a, b)` table constraints
/// (ADR-0035 §4a.2). Single-column table-level `UNIQUE` is
/// folded into the column's `unique` flag instead.
unique_constraints: Vec<Vec<String>>,
if_not_exists: bool,
},
/// Add a column to an existing table. The column carries