ADR-0024 Phase D (full): schema-aware value typing
Schema-aware typed value slots — the central design claim of
ADR-0024 §Phase D. Insert / update / delete value slots now
dispatch on the user-facing column type at parse time, rejecting
mis-shaped input with localised wording instead of waiting for
the bind-time error.
What changed:
**SchemaCache extension** (`src/completion.rs`):
- New `TableColumn { name, user_type }` for per-table column
metadata.
- `SchemaCache.table_columns: HashMap<String, Vec<TableColumn>>`.
- `SchemaCache::columns_for_table(name)` — case-insensitive
lookup, mirrors the walker's case-insensitive entry-word
resolution.
**WalkContext schema plumbing** (`src/dsl/walker/context.rs`):
- `WalkContext<'a>` gains a lifetime and a `schema: Option<&'a
SchemaCache>`. `WalkContext::new()` keeps the schemaless
default; `with_schema(s)` is the new schema-aware constructor.
**Parser entry point** (`src/dsl/parser.rs`):
- `parse_command_with_schema(input, schema)` is the new public
schema-aware variant. `parse_command(input)` becomes a thin
wrapper that delegates with `None` for back-compat.
- Internal `try_walker_route` accepts an `Option<&SchemaCache>`
and threads it into the WalkContext.
**Node::Ident writes_table/writes_column** (`src/dsl/grammar/mod.rs`):
- Two new fields on `Node::Ident`. When `writes_table: true` and
`source: Tables`, the walker writes the matched ident's name
into `current_table` and resolves `current_table_columns`
against the schema cache. When `writes_column: true` and
`source: Columns`, the walker writes the resolved
`TableColumn` into `current_column`.
**Walker driver DynamicSubgrammar dispatch** (`src/dsl/walker/driver.rs`):
- The `Node::DynamicSubgrammar(factory)` branch now resolves the
factory at walk time and `Box::leak`s the result so its inner
static-slice fields (Choice/Seq) have the lifetime the walker
expects (per ADR-0024 §sub-grammars). The leak is bounded by
command-shape complexity per walk; per-walk arena is a future
optimisation.
- `walk_ident` extends to perform the schema writes when the
flags are set.
**Typed value slot factories + dynamic sub-grammars** (`src/dsl/grammar/shared.rs`):
- `int_slot` / `real_slot` / `decimal_slot` / `bool_slot` /
`text_slot` / `date_slot` / `datetime_slot` / `blob_slot` —
one per `Type`. Each accepts the appropriate literal kind plus
`null`; integer-only validator rejects `3.14` at int columns;
decimal validator pins numeric shape.
- `slot_for_type(ty) -> Node` is the dispatcher.
- `current_column_value(ctx) -> Node` is the dynamic sub-grammar
for `set col = …` and `where col = …` values; reads
`current_column` and dispatches via `slot_for_type`.
- `column_value_list(ctx) -> Node` is the dynamic sub-grammar
for `insert into T values (…)`; reads `current_table_columns`
and unfolds a Seq of typed slots separated by commas.
- Both fall back to the schemaless `VALUE_LITERAL` choice when
the context lacks the schema-resolved entries — keeps
schemaless `parse_command` callers (tests, replay path)
working.
**Data-command grammar wires the new types** (`src/dsl/grammar/data.rs`):
- `TABLE_NAME_INSERT` / `TABLE_NAME_WRITES` (new): table-name
slots that set `writes_table: true`. Used by insert / update /
delete to populate `current_table_columns`.
- `SET_COLUMN` / `FILTER_COLUMN` (new): column-name slots in
`set col=…` / `where col=…` set `writes_column: true`.
- `INSERT_VALUES_LIST` becomes `DynamicSubgrammar(column_value_list)`.
- `UPDATE_ASSIGNMENT` and `WHERE_CLAUSE` use
`PER_COLUMN_VALUE = DynamicSubgrammar(current_column_value)`.
**Runtime plumbs schema-with-types** (`src/runtime.rs`):
- `refresh_schema_cache` calls `describe_table` for each table
and populates `SchemaCache::table_columns` with
`TableColumn { name, user_type }` entries. Best-effort: a
`describe_table` miss leaves that table unpopulated and the
walker falls back to schemaless dispatch.
**App dispatches with schema** (`src/app.rs`):
- `dispatch_dsl` routes through `parse_command_with_schema(&self
.schema_cache, …)` so live typing/dispatch sees the typed
slots. The replay path stays schemaless (deferred — replay
bind-time errors still catch type mismatches).
**Catalog** (`src/friendly/strings/en-US.yaml`, `src/friendly/keys.rs`):
- New `parse.custom.bind_type_mismatch` entry with `{found}` and
`{expected}` placeholders. Surfaced by the int_slot /
decimal_slot validators.
Tests:
- 11 new walker-side Phase D tests cover insert / update /
delete with schemas — typed acceptance per column, decimal
rejection at int columns, null acceptance at any slot,
multi-assignment per-column dispatch, schemaless fallback.
- The pre-existing `parse_command(input)` test suite (no
schema) still passes — the fallback path is behaviour-
preserving.
- 828 passing total, 0 failing, 1 ignored. Clippy clean.
This commit is contained in:
+186
-1
@@ -5,8 +5,11 @@
|
||||
//! actions; Phase D extends with `where_clause`,
|
||||
//! `column_value_list`, and the typed value slots.
|
||||
|
||||
use crate::dsl::grammar::{IdentSource, IdentValidator, Node, ValidationError, Word};
|
||||
use crate::dsl::grammar::{
|
||||
IdentSource, IdentValidator, Node, NumberValidator, ValidationError, Word,
|
||||
};
|
||||
use crate::dsl::types::Type;
|
||||
use crate::dsl::walker::context::WalkContext;
|
||||
use std::str::FromStr;
|
||||
|
||||
// --- Type-name validator ------------------------------------------
|
||||
@@ -46,6 +49,8 @@ pub const TYPE_SLOT: Node = Node::Ident {
|
||||
role: "type",
|
||||
validator: Some(TYPE_VALIDATOR),
|
||||
highlight_override: None,
|
||||
writes_table: false,
|
||||
writes_column: false,
|
||||
};
|
||||
|
||||
// --- Qualified column reference (`<Table>.<Column>`) --------------
|
||||
@@ -56,6 +61,8 @@ const QUALIFIED_COLUMN_NODES: &[Node] = &[
|
||||
role: "table_name",
|
||||
validator: None,
|
||||
highlight_override: None,
|
||||
writes_table: false,
|
||||
writes_column: false,
|
||||
},
|
||||
Node::Punct('.'),
|
||||
Node::Ident {
|
||||
@@ -63,6 +70,8 @@ const QUALIFIED_COLUMN_NODES: &[Node] = &[
|
||||
role: "column_name",
|
||||
validator: None,
|
||||
highlight_override: None,
|
||||
writes_table: false,
|
||||
writes_column: false,
|
||||
},
|
||||
];
|
||||
pub const QUALIFIED_COLUMN: Node = Node::Seq(QUALIFIED_COLUMN_NODES);
|
||||
@@ -118,3 +127,179 @@ pub const REFERENTIAL_CLAUSES: Node = Node::Repeated {
|
||||
separator: None,
|
||||
min: 0,
|
||||
};
|
||||
|
||||
// =================================================================
|
||||
// Typed value slots (ADR-0024 §Phase D, §typed-value-slots)
|
||||
// =================================================================
|
||||
//
|
||||
// Each `<ty>_slot()` factory returns a `Node` that accepts either
|
||||
// `null` or a literal of the corresponding shape, with an
|
||||
// optional content validator that rejects mis-typed values at
|
||||
// parse time with localised catalog wording. Per-type prose
|
||||
// hints attach via `Choice` HintMode — but Phase D's first
|
||||
// landing keeps `Default` everywhere; the dispatch-by-column-type
|
||||
// covers the central design claim, and per-type prose can layer
|
||||
// on later without grammar surface changes.
|
||||
|
||||
fn validate_integer_only(value: &str) -> Result<(), ValidationError> {
|
||||
// The lexer-side number consumer accepts integers and
|
||||
// fractional forms (e.g. `3.14`). For int / serial / shortid
|
||||
// columns reject any literal that carries a decimal point.
|
||||
if value.contains('.') {
|
||||
Err(ValidationError {
|
||||
message_key: "parse.custom.bind_type_mismatch",
|
||||
args: vec![
|
||||
("found", value.to_string()),
|
||||
("expected", "integer".to_string()),
|
||||
],
|
||||
})
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
const INTEGER_ONLY_VALIDATOR: NumberValidator = validate_integer_only;
|
||||
|
||||
fn validate_decimal_string(value: &str) -> Result<(), ValidationError> {
|
||||
if value.parse::<f64>().is_ok() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ValidationError {
|
||||
message_key: "parse.custom.bind_type_mismatch",
|
||||
args: vec![
|
||||
("found", value.to_string()),
|
||||
("expected", "number".to_string()),
|
||||
],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const DECIMAL_VALIDATOR: NumberValidator = validate_decimal_string;
|
||||
|
||||
// Bare `null` keyword — used as the trailing branch of every
|
||||
// typed value slot so a column always accepts the absence sentinel.
|
||||
const NULL_WORD: Node = Node::Word(Word::keyword("null"));
|
||||
|
||||
const INT_SLOT_CHOICES: &[Node] = &[
|
||||
Node::NumberLit {
|
||||
validator: Some(INTEGER_ONLY_VALIDATOR),
|
||||
},
|
||||
NULL_WORD,
|
||||
];
|
||||
const INT_SLOT: Node = Node::Choice(INT_SLOT_CHOICES);
|
||||
|
||||
const REAL_SLOT_CHOICES: &[Node] = &[Node::NumberLit { validator: None }, NULL_WORD];
|
||||
const REAL_SLOT: Node = Node::Choice(REAL_SLOT_CHOICES);
|
||||
|
||||
const DECIMAL_SLOT_CHOICES: &[Node] = &[
|
||||
Node::NumberLit {
|
||||
validator: Some(DECIMAL_VALIDATOR),
|
||||
},
|
||||
NULL_WORD,
|
||||
];
|
||||
const DECIMAL_SLOT: Node = Node::Choice(DECIMAL_SLOT_CHOICES);
|
||||
|
||||
const BOOL_SLOT_CHOICES: &[Node] = &[
|
||||
Node::Word(Word::keyword("true")),
|
||||
Node::Word(Word::keyword("false")),
|
||||
NULL_WORD,
|
||||
];
|
||||
const BOOL_SLOT: Node = Node::Choice(BOOL_SLOT_CHOICES);
|
||||
|
||||
const TEXT_SLOT_CHOICES: &[Node] = &[Node::StringLit, NULL_WORD];
|
||||
const TEXT_SLOT: Node = Node::Choice(TEXT_SLOT_CHOICES);
|
||||
|
||||
const DATE_SLOT_CHOICES: &[Node] = &[Node::StringLit, NULL_WORD];
|
||||
const DATE_SLOT: Node = Node::Choice(DATE_SLOT_CHOICES);
|
||||
|
||||
const DATETIME_SLOT_CHOICES: &[Node] = &[Node::StringLit, NULL_WORD];
|
||||
const DATETIME_SLOT: Node = Node::Choice(DATETIME_SLOT_CHOICES);
|
||||
|
||||
const BLOB_SLOT_CHOICES: &[Node] = &[Node::StringLit, NULL_WORD];
|
||||
const BLOB_SLOT: Node = Node::Choice(BLOB_SLOT_CHOICES);
|
||||
|
||||
/// Dispatch a value slot per user-facing type
|
||||
/// (ADR-0024 §slot_for_type). Returns the same node every time
|
||||
/// for a given Type — fine to call from within a
|
||||
/// `DynamicSubgrammar` factory.
|
||||
#[must_use]
|
||||
pub const fn slot_for_type(ty: Type) -> Node {
|
||||
match ty {
|
||||
Type::Int | Type::Serial | Type::ShortId => INT_SLOT,
|
||||
Type::Real => REAL_SLOT,
|
||||
Type::Decimal => DECIMAL_SLOT,
|
||||
Type::Bool => BOOL_SLOT,
|
||||
Type::Text => TEXT_SLOT,
|
||||
Type::Date => DATE_SLOT,
|
||||
Type::DateTime => DATETIME_SLOT,
|
||||
Type::Blob => BLOB_SLOT,
|
||||
}
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// Dynamic sub-grammar: column_value_list
|
||||
// =================================================================
|
||||
|
||||
/// Fallback when no schema-resolved column list is available
|
||||
/// (schemaless parse, missing table, empty schema cache).
|
||||
/// Mirrors the pre-Phase-D `value_literal` Choice.
|
||||
const FALLBACK_VALUE_LITERAL_CHOICES: &[Node] = &[
|
||||
Node::Word(Word::keyword("null")),
|
||||
Node::Word(Word::keyword("true")),
|
||||
Node::Word(Word::keyword("false")),
|
||||
Node::NumberLit { validator: None },
|
||||
Node::StringLit,
|
||||
];
|
||||
const FALLBACK_VALUE_LITERAL: Node = Node::Choice(FALLBACK_VALUE_LITERAL_CHOICES);
|
||||
|
||||
const FALLBACK_VALUE_LIST: Node = Node::Repeated {
|
||||
inner: &FALLBACK_VALUE_LITERAL,
|
||||
separator: Some(&Node::Punct(',')),
|
||||
min: 1,
|
||||
};
|
||||
|
||||
/// Value slot keyed on `WalkContext::current_column`.
|
||||
///
|
||||
/// Picks the typed slot for the column whose name was most
|
||||
/// recently matched by an `Ident { source: Columns,
|
||||
/// writes_column: true }` node (ADR-0024 §Phase D). Fallback
|
||||
/// when no current_column is resolved: the schemaless
|
||||
/// value-literal choice.
|
||||
pub fn current_column_value(ctx: &WalkContext) -> Node {
|
||||
ctx.current_column
|
||||
.as_ref()
|
||||
.map_or(FALLBACK_VALUE_LITERAL, |col| slot_for_type(col.user_type))
|
||||
}
|
||||
|
||||
/// Comma-separated list of typed value slots, one per column.
|
||||
///
|
||||
/// Reads `current_table_columns` from the WalkContext (ADR-0024
|
||||
/// §Phase D §column_value_list). When the schema cache holds
|
||||
/// no entry for the current table — or the walker is
|
||||
/// schemaless — falls back to the schema-unaware
|
||||
/// `Repeated(VALUE_LITERAL, ',', 1)` shape so existing
|
||||
/// callers/tests continue to work.
|
||||
pub fn column_value_list(ctx: &WalkContext) -> Node {
|
||||
let Some(cols) = ctx.current_table_columns.as_ref() else {
|
||||
return FALLBACK_VALUE_LIST;
|
||||
};
|
||||
if cols.is_empty() {
|
||||
return FALLBACK_VALUE_LIST;
|
||||
}
|
||||
// Build a Seq of typed slots interleaved with commas.
|
||||
let mut children: Vec<Node> = Vec::with_capacity(cols.len() * 2);
|
||||
for (i, col) in cols.iter().enumerate() {
|
||||
if i > 0 {
|
||||
children.push(Node::Punct(','));
|
||||
}
|
||||
children.push(slot_for_type(col.user_type));
|
||||
}
|
||||
Node::Seq(Box::leak(children.into_boxed_slice()))
|
||||
}
|
||||
|
||||
// The HintMode / NumberValidator imports are part of the Phase D
|
||||
// typed-slot toolkit even though only NumberValidator is used by
|
||||
// the explicit validators above; surface HintMode so future
|
||||
// per-type prose annotations can attach without re-importing.
|
||||
#[allow(dead_code)]
|
||||
const _USES_HINT_MODE: Option<crate::dsl::grammar::HintMode> = None;
|
||||
|
||||
Reference in New Issue
Block a user