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:
@@ -71,10 +71,17 @@ CHECK). Builds directly on the 4a `SqlCreateTable` command + grammar.
|
||||
### 4.1 Grammar (`src/dsl/grammar/sql_create_table.rs`)
|
||||
|
||||
- **Column constraints** — extend `COL_CONSTRAINT_CHOICES` with:
|
||||
- `DEFAULT <sql_expr>`: `Seq[ Word("default"),
|
||||
Subgrammar(&sql_expr::SQL_OR_EXPR) ]`.
|
||||
- `DEFAULT <value>` where `<value>` is a **literal** (number /
|
||||
string / `null` / `true` / `false`) **or a parenthesised**
|
||||
`( <sql_expr> )` — *not* a bare `sql_expr`. This matches standard
|
||||
SQL (a complex default must be parenthesised) **and** resolves a
|
||||
real ambiguity found in testing: a bare `sql_expr` greedily
|
||||
consumes a following `NOT` (as `NOT IN`/`NOT LIKE`/`NOT BETWEEN`),
|
||||
breaking the common `DEFAULT 0 NOT NULL`. The parens give the
|
||||
expression a clean right edge. (See §6.3.)
|
||||
- `CHECK ( <sql_expr> )`: `Seq[ Word("check"), Punct('('),
|
||||
Subgrammar(&sql_expr::SQL_OR_EXPR), Punct(')') ]`.
|
||||
Subgrammar(&sql_expr::SQL_OR_EXPR), Punct(')') ]` — already
|
||||
paren-bounded, so no ambiguity.
|
||||
- **Table element** — extend `ELEMENT_CHOICES` with table-level
|
||||
`UNIQUE ( col, … )`: `Seq[ Word("unique"), Punct('('),
|
||||
Repeated(uniq_column, ',', 1), Punct(')') ]`. (Distinct ident role,
|
||||
@@ -146,9 +153,13 @@ execution; `finalize_persistence` writes yaml/CSV/journal. No new
|
||||
unknown-column `[ERR]`. If they do, the grammar must suppress
|
||||
schema-existence checks in the CREATE-TABLE CHECK context (as the
|
||||
simple-mode path effectively does). Resolve during step 1.
|
||||
2. **DEFAULT expression boundary** — confirm the `sql_expr` match is
|
||||
maximal enough that the raw-text slice for `DEFAULT <expr>` ends
|
||||
cleanly before the next element. Covered by builder tests.
|
||||
2. **DEFAULT grammar — resolved during implementation (§4.1).** A bare
|
||||
`DEFAULT <sql_expr>` greedily ate a following `NOT` (start of
|
||||
`NOT IN`/`LIKE`/`BETWEEN`), so `DEFAULT 0 NOT NULL` failed to parse
|
||||
(caught by a builder test). Fixed by restricting a bare default to a
|
||||
**literal**, with complex defaults **parenthesised** (`DEFAULT
|
||||
(expr)`) — standard SQL, and the parens bound the expression. The
|
||||
raw-text capture keeps the parens so it re-emits as valid SQL.
|
||||
|
||||
## 7. Devil's Advocate review of this plan
|
||||
|
||||
|
||||
Reference in New Issue
Block a user