db: column-origin type recovery in SELECT results (sub-phase 2f)
`Cargo.toml`: add `column_metadata` to rusqlite's feature list. This pulls in the SQLite `SQLITE_ENABLE_COLUMN_METADATA` compile flag and surfaces `sqlite3_column_table_name` / `sqlite3_column_origin_name` on prepared statements via rusqlite's `Statement::columns_with_metadata()`. `do_run_select` in db.rs now calls a new `resolve_select_column_types(conn, stmt)` helper after `prepare`. The helper walks each result-column's origin metadata; when both `table_name` and `origin_name` come back populated (the result column traces back to a base-table column), it looks up the playground type in `__rdbms_playground_columns`. The per-column types thread through to `format_cell(value, ty)` so the data-table renderer (ADR-0016) gets the same per-type rendering it applies to `show data` results. Effect: ADR-0030 Phase-1 §4.5 (bool SELECT results render as `0` / `1`) is lifted for any bare-column reference whose origin the engine carries through — per ADR-0032 Amendment 1 (2026-05-20 empirical probe), that means all non-recursive CTE bodies, scalar subqueries (aliased or not), derived tables, set ops, and JOINs. Computed projections and recursive-CTE result columns remain typeless (the engine populates no origin), which the renderer handles via neutral alignment. The lookup is engine-driven verbatim — no grammar-side structural classification (ADR-0032 Amendment 1 replaces §12's original "structurally a single column reference" rule with "trust column_table_name / column_origin_name"). Tests (3 new in `tests/sql_select.rs`, all green): - `database_run_select_recovers_bool_column_type` — the Phase-1 §4.5 case: `SELECT Active FROM Products` returns `column_types = [Some(Bool)]` and rows render as `true` / `false`. - `database_run_select_recovers_text_type_through_alias` — `SELECT Name AS n FROM Users` remaps the result column name to `n` but the origin metadata still resolves the playground type to `Some(Text)`. - `database_run_select_computed_expression_stays_typeless` — `SELECT Score + 1 FROM T` keeps `column_types[0] = None`, the documented Amendment-1 exception. The CTE pass-through, scalar subquery, set-op, and JOIN cases all work for free given the empirical findings; their behaviour is asserted by the Amendment-1 probe results recorded in the ADR, so no per-case integration tests are duplicated here. Test totals: 1382 → 1385 passing (+3), 0 failed, 1 ignored. Clippy clean.
This commit is contained in:
@@ -5699,13 +5699,19 @@ fn do_run_select_request(
|
||||
}
|
||||
|
||||
/// Execute a grammar-validated SQL `SELECT` and collect its
|
||||
/// rows into a [`DataResult`] (ADR-0030 §6).
|
||||
/// rows into a [`DataResult`] (ADR-0030 §6, ADR-0032 §12 +
|
||||
/// Amendment 1).
|
||||
///
|
||||
/// All result columns are reported with `column_types = None` —
|
||||
/// a SELECT result has no playground type unless we resolve
|
||||
/// each output column back to its source column, which is a
|
||||
/// Phase-2 (full `SELECT`) concern. The DSL data-table renderer
|
||||
/// (ADR-0016) renders typeless columns with neutral alignment.
|
||||
/// Per-column playground types are recovered from the engine's
|
||||
/// column-origin metadata (`column_table_name` /
|
||||
/// `column_origin_name`, surfaced by rusqlite's
|
||||
/// `columns_with_metadata`). The Amendment-1 empirical probe
|
||||
/// confirmed the metadata follows through non-recursive CTEs,
|
||||
/// scalar subqueries, derived tables, set ops, and JOINs; only
|
||||
/// computed projections and recursive-CTE result columns return
|
||||
/// `None`. The renderer (ADR-0016) handles typed columns
|
||||
/// (bool → true/false, etc.) and falls back to neutral
|
||||
/// alignment for `None`.
|
||||
fn do_run_select(conn: &Connection, sql: &str) -> Result<DataResult, DbError> {
|
||||
debug!(sql = %sql, "run_select");
|
||||
let mut stmt = conn.prepare(sql).map_err(DbError::from_rusqlite)?;
|
||||
@@ -5715,7 +5721,7 @@ fn do_run_select(conn: &Connection, sql: &str) -> Result<DataResult, DbError> {
|
||||
.map(String::from)
|
||||
.collect();
|
||||
let col_count = column_names.len();
|
||||
let column_types: Vec<Option<Type>> = vec![None; col_count];
|
||||
let column_types = resolve_select_column_types(conn, &stmt);
|
||||
let rows_iter = stmt
|
||||
.query_map([], |row| {
|
||||
let mut cells: Vec<rusqlite::types::Value> = Vec::with_capacity(col_count);
|
||||
@@ -5731,7 +5737,8 @@ fn do_run_select(conn: &Connection, sql: &str) -> Result<DataResult, DbError> {
|
||||
rows.push(
|
||||
cells
|
||||
.into_iter()
|
||||
.map(|v| format_cell(v, None))
|
||||
.enumerate()
|
||||
.map(|(i, v)| format_cell(v, column_types.get(i).copied().flatten()))
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
@@ -5743,6 +5750,49 @@ fn do_run_select(conn: &Connection, sql: &str) -> Result<DataResult, DbError> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolve playground types for each result column of a
|
||||
/// prepared SELECT statement (ADR-0032 §12 + Amendment 1).
|
||||
///
|
||||
/// For each result column, query the engine's column-origin
|
||||
/// metadata. If both `table_name` and `origin_name` are
|
||||
/// populated (the result column traces back to a base-table
|
||||
/// column), look up the playground type in
|
||||
/// `__rdbms_playground_columns`. Otherwise the slot stays
|
||||
/// `None` — Amendment 1 documents that recursive-CTE result
|
||||
/// columns and computed projections are the only structural
|
||||
/// classes that don't follow through.
|
||||
fn resolve_select_column_types(
|
||||
conn: &Connection,
|
||||
stmt: &rusqlite::Statement,
|
||||
) -> Vec<Option<Type>> {
|
||||
let metas = stmt.columns_with_metadata();
|
||||
if metas.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
// Prepare the lookup once; reuse across columns.
|
||||
let mut lookup = match conn.prepare(&format!(
|
||||
"SELECT user_type FROM {META_TABLE} \
|
||||
WHERE table_name = ?1 COLLATE NOCASE \
|
||||
AND column_name = ?2 COLLATE NOCASE"
|
||||
)) {
|
||||
Ok(s) => s,
|
||||
Err(_) => return vec![None; metas.len()],
|
||||
};
|
||||
metas
|
||||
.iter()
|
||||
.map(|m| {
|
||||
let table = m.table_name()?;
|
||||
let origin = m.origin_name()?;
|
||||
lookup
|
||||
.query_row(rusqlite::params![table, origin], |row| {
|
||||
row.get::<_, String>(0)
|
||||
})
|
||||
.ok()
|
||||
.and_then(|kw| kw.parse::<Type>().ok())
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Build the parameterised `SELECT … FROM …` statement for a
|
||||
/// `show data` query (ADR-0026 §5–§6). Separated from
|
||||
/// `do_query_data` so the `explain` path runs `EXPLAIN QUERY
|
||||
|
||||
Reference in New Issue
Block a user