kevy-sql 7.0.0

Declaration-time SQL compiler for kevy: CREATE TABLE/INDEX/VIEW compile ONCE into explicit TABLE.DECLARE / VIEW.CREATE commands and query cards — never a query engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Pass 2 — compile each `CREATE VIEW` against its table's *declared*
//! access paths. The compiler NEVER plans: the driving predicate either
//! matches a declared index / orderpath (leading-prefix rule) or the
//! compile errors naming the exact missing declaration. Deterministic
//! path choice, documented in the README:
//!
//! 1. constant-only predicates + expressible ORDER BY → an engine
//!    `VIEW.CREATE` (each predicate column individually indexed);
//! 2. else, all predicates on ONE indexed column → a direct-drive card;
//! 3. else, the predicate set matches an ORDERPATH leading prefix → a
//!    composite `WHERE` card;
//! 4. else, the FIRST predicate column (source order) with a usable
//!    index drives, and residuals compile to `FILTER` over the index's
//!    stored `INCLUDE` columns — or the error names what to add.

use crate::ast::CreateView;
use crate::schema::{OrderPath, Table};
use crate::{CardParam, QueryCard, SqlError, ValType};

use crate::viewplan_norm::{BV, ColPred, Shape, normalize};
use crate::viewplan_view::{try_engine_view, view_read_note};

/// One compiled view: an engine view command or a query card.
pub(crate) enum Planned {
    View(Vec<String>),
    Card(QueryCard),
}

pub(crate) fn plan_view(
    v: &CreateView,
    t: &Table,
    notes: &mut Vec<String>,
) -> Result<Planned, SqlError> {
    let (fields, order, preds) = prepare(v, t)?;
    let constant = !preds.iter().any(ColPred::has_param);
    if constant
        && v.offset.is_none()
        && let Some(argv) = try_engine_view(v, t, &preds, order.as_ref())
    {
        notes.push(view_read_note(v, &fields));
        return Ok(Planned::View(argv));
    }
    plan_card(v, t, &preds, order.as_ref(), &fields).map(Planned::Card)
}

/// One query as an `IDX.QUERY` card, never an engine view: what a
/// query answered now needs, where a view would be a declaration.
pub(crate) fn card_for(v: &CreateView, t: &Table) -> Result<QueryCard, SqlError> {
    let (fields, order, preds) = prepare(v, t)?;
    plan_card(v, t, &preds, order.as_ref(), &fields)
}

type Prepared = (Vec<String>, Option<(String, bool)>, Vec<ColPred>);

/// The checks every query passes before a path is chosen: the select
/// list and ORDER BY name declared columns, and a WHERE exists.
fn prepare(v: &CreateView, t: &Table) -> Result<Prepared, SqlError> {
    let fields = resolve_fields(v, t)?;
    check_counts(v)?;
    let order = resolve_order(v, t)?;
    if v.preds.is_empty() {
        return Err(no_where(v, t));
    }
    Ok((fields, order, normalize(v, t)?))
}

/// The card paths 1–3 below, in order, or the error naming what to add.
fn plan_card(
    v: &CreateView,
    t: &Table,
    preds: &[ColPred],
    order: Option<&(String, bool)>,
    fields: &[String],
) -> Result<QueryCard, SqlError> {
    let mut fail: Option<String> = None;
    if let Some(card) = card_direct(v, t, preds, order, fields, &mut fail)? {
        return Ok(card);
    }
    if let Some(card) = card_orderpath(v, t, preds, order, fields)? {
        return Ok(card);
    }
    if let Some(card) = card_residual(v, t, preds, order, fields, &mut fail)? {
        return Ok(card);
    }
    Err(no_path_error(v, t, preds, order, fail))
}

fn no_where(v: &CreateView, t: &Table) -> SqlError {
    err_v(
        v,
        format!(
            "a view with no WHERE would scan the table \u{2014} kevy has no scans; add a driving predicate, or page an index directly (IDX.QUERY {}.<col> RANGE \u{2026})",
            t.name
        ),
    )
}

fn err_v(v: &CreateView, msg: impl std::fmt::Display) -> SqlError {
    SqlError::at(v.line, v.col, format!("view '{}': {msg}", v.name))
}

/// `SELECT *` → every declared column; a list validates against the
/// declaration.
fn resolve_fields(v: &CreateView, t: &Table) -> Result<Vec<String>, SqlError> {
    match &v.select {
        None => Ok(t.columns.iter().map(|(n, _)| n.clone()).collect()),
        Some(cols) => {
            for c in cols {
                if t.column_type(c).is_none() {
                    return Err(err_v(
                        v,
                        format!("SELECT names unknown column '{c}' of table '{}'", t.name),
                    ));
                }
            }
            Ok(cols.clone())
        }
    }
}

/// Engine caps, named here instead of silently clamped there.
fn check_counts(v: &CreateView) -> Result<(), SqlError> {
    if let Some(n) = v.limit {
        if n == 0 {
            return Err(err_v(v, "LIMIT 0 selects nothing"));
        }
        if n > 10_000 {
            return Err(err_v(
                v,
                "the engine caps LIMIT at 10000 \u{2014} page with CURSOR (docs/indexes.md)",
            ));
        }
    }
    if let Some(m) = v.offset
        && m > 10_000
    {
        return Err(err_v(
            v,
            "the engine caps OFFSET at 10000 \u{2014} page with CURSOR instead (docs/indexes.md)",
        ));
    }
    Ok(())
}

fn resolve_order(v: &CreateView, t: &Table) -> Result<Option<(String, bool)>, SqlError> {
    match &v.order {
        None => Ok(None),
        Some((c, d)) => {
            if t.column_type(c).is_none() {
                return Err(err_v(
                    v,
                    format!("ORDER BY names unknown column '{c}' of table '{}'", t.name),
                ));
            }
            Ok(Some((c.clone(), *d)))
        }
    }
}

// ───────────── query cards ─────────────

/// Collects argv + `$N` params; `bind` refuses one `$N` spanning two
/// column types (the runtime value could not satisfy both).
struct CardB {
    argv: Vec<String>,
    params: Vec<CardParam>,
}

impl CardB {
    fn new(verb: &str, path: String) -> CardB {
        CardB { argv: vec!["IDX.QUERY".into(), path], params: Vec::new() }.tap_verb(verb)
    }

    fn tap_verb(mut self, verb: &str) -> CardB {
        if !verb.is_empty() {
            self.argv.push(verb.into());
        }
        self
    }

    fn bind(&mut self, v: &CreateView, bv: &BV, col: &str, ty: ValType) -> Result<(), SqlError> {
        match bv {
            BV::Lit(x) => self.argv.push(x.clone()),
            BV::Param(n) => {
                self.argv.push(format!("${n}"));
                if let Some(prev) = self.params.iter().find(|p| p.n == *n) {
                    if prev.ty != ty {
                        return Err(err_v(
                            v,
                            format!(
                                "${n} binds both '{}' ({}) and '{col}' ({}) \u{2014} use distinct parameters",
                                prev.column,
                                prev.ty.tag(),
                                ty.tag()
                            ),
                        ));
                    }
                } else {
                    self.params.push(CardParam { n: *n, column: col.into(), ty });
                }
            }
        }
        Ok(())
    }

    fn finish(mut self, v: &CreateView, fields: &[String]) -> QueryCard {
        if let Some(m) = v.offset {
            self.argv.push("OFFSET".into());
            self.argv.push(m.to_string());
        }
        if let Some(n) = v.limit {
            self.argv.push("LIMIT".into());
            self.argv.push(n.to_string());
        }
        self.argv.push("FIELDS".into());
        self.argv.extend(fields.iter().cloned());
        self.params.sort_by_key(|p| p.n);
        QueryCard { name: v.name.clone(), argv: self.argv, params: self.params }
    }
}

/// Card path 1 — every predicate on ONE column that carries a usable
/// single-column index.
fn card_direct(
    v: &CreateView,
    t: &Table,
    preds: &[ColPred],
    order: Option<&(String, bool)>,
    fields: &[String],
    fail: &mut Option<String>,
) -> Result<Option<QueryCard>, SqlError> {
    let [p] = preds else { return Ok(None) };
    let Some(ix) = t.index_on(&p.col) else { return Ok(None) };
    if matches!(p.shape, Shape::Range(..)) && ix.unique {
        return Ok(None);
    }
    let mut b = CardB::new("", format!("{}.{}", t.name, p.col));
    push_drive(&mut b, v, p)?;
    if !push_sort(&mut b, v, t, &ix.values, &p.col, order, fail)? {
        return Ok(None);
    }
    Ok(Some(b.finish(v, fields)))
}

/// The driving `EQ v` / `RANGE lo hi` tokens.
fn push_drive(b: &mut CardB, v: &CreateView, p: &ColPred) -> Result<(), SqlError> {
    match &p.shape {
        Shape::Eq(x) => {
            b.argv.push("EQ".into());
            b.bind(v, x, &p.col, p.ty)
        }
        Shape::Range(lo, hi) => {
            b.argv.push("RANGE".into());
            b.bind(v, lo, &p.col, p.ty)?;
            b.bind(v, hi, &p.col, p.ty)
        }
    }
}

/// ORDER BY on a direct-drive card: the driving column ascending is the
/// natural order; anything else needs the column among the index's
/// stored `INCLUDE` values (`SORT` reads stored values). `Ok(false)` =
/// not satisfiable here (the reason lands in `fail`).
fn push_sort(
    b: &mut CardB,
    v: &CreateView,
    t: &Table,
    values: &[String],
    drive_col: &str,
    order: Option<&(String, bool)>,
    fail: &mut Option<String>,
) -> Result<bool, SqlError> {
    let Some((c, d)) = order else { return Ok(true) };
    if c == drive_col && !*d {
        return Ok(true); // the index's own ascending order.
    }
    if values.iter().any(|x| x == c) {
        b.argv.push("SORT".into());
        b.argv.push(c.clone());
        b.argv.push(if *d { "DESC" } else { "ASC" }.into());
        return Ok(true);
    }
    let advice = if c == drive_col {
        // Descending on the driving column: the index is ordered by it, but
        // a Range index reads one way; an order path reads the other.
        format!(
            "ORDER BY {c} DESC reads the index on {c} backwards, which a Range index does not do \u{2014} declare CREATE INDEX {c}_desc ON {} ({c} DESC)",
            t.name
        )
    } else {
        format!(
            "ORDER BY {c}{} needs the column stored on the driving index \u{2014} add INCLUDE ({c}) to CREATE INDEX ON {} ({drive_col}), or declare CREATE INDEX ON {} ({drive_col}, {c}{})",
            if *d { " DESC" } else { "" },
            t.name,
            t.name,
            if *d { " DESC" } else { "" },
        )
    };
    fail.get_or_insert(format!("view '{}': {advice}", v.name));
    Ok(false)
}

/// Card path 2 — the predicate set is a leading prefix of a declared
/// ORDERPATH (eq columns pin the prefix, at most one range on the next
/// component), and ORDER BY (if any) is the path's next component.
fn card_orderpath(
    v: &CreateView,
    t: &Table,
    preds: &[ColPred],
    order: Option<&(String, bool)>,
    fields: &[String],
) -> Result<Option<QueryCard>, SqlError> {
    let eqs: Vec<&ColPred> = preds.iter().filter(|p| matches!(p.shape, Shape::Eq(_))).collect();
    let ranges: Vec<&ColPred> =
        preds.iter().filter(|p| matches!(p.shape, Shape::Range(..))).collect();
    if ranges.len() > 1 {
        return Ok(None);
    }
    let Some(op) =
        t.orderpaths.iter().find(|op| orderpath_matches(op, &eqs, ranges.first().copied(), order))
    else {
        return Ok(None);
    };
    let mut b = CardB::new("WHERE", format!("{}.{}", t.name, op.name));
    for (col, _) in &op.on[..eqs.len()] {
        let p = eqs.iter().find(|p| &p.col == col).expect("matched prefix");
        let Shape::Eq(x) = &p.shape else { unreachable!("eqs are Eq") };
        b.argv.push(p.col.clone());
        b.argv.push("EQ".into());
        b.bind(v, x, &p.col, p.ty)?;
    }
    if let Some(p) = ranges.first() {
        let Shape::Range(lo, hi) = &p.shape else { unreachable!("ranges are Range") };
        b.argv.push("RANGE".into());
        b.argv.push(p.col.clone());
        b.bind(v, lo, &p.col, p.ty)?;
        b.bind(v, hi, &p.col, p.ty)?;
    }
    Ok(Some(b.finish(v, fields)))
}

/// The leading-prefix rule, compiler-side: eq columns = the path's
/// first k components (any source order), the range (if any) = the
/// next, and ORDER BY = an eq-pinned column (trivial) or the first
/// unpinned component with the matching direction.
fn orderpath_matches(
    op: &OrderPath,
    eqs: &[&ColPred],
    range: Option<&ColPred>,
    order: Option<&(String, bool)>,
) -> bool {
    let k = eqs.len();
    if op.on.len() < k {
        return false;
    }
    if !op.on[..k].iter().all(|(c, _)| eqs.iter().any(|p| &p.col == c)) {
        return false;
    }
    if let Some(r) = range {
        match op.on.get(k) {
            Some((c, _)) if *c == r.col => {}
            _ => return false,
        }
    }
    match order {
        None => true,
        Some((c, _)) if eqs.iter().any(|p| &p.col == c) => true,
        Some((c, d)) => match op.on.get(k) {
            Some((oc, od)) => oc == c && od == d,
            None => false,
        },
    }
}

/// Card path 3 — the FIRST predicate (source order) with a usable index
/// drives; residual predicates read the index's stored `INCLUDE`
/// columns as `FILTER` clauses.
fn card_residual(
    v: &CreateView,
    t: &Table,
    preds: &[ColPred],
    order: Option<&(String, bool)>,
    fields: &[String],
    fail: &mut Option<String>,
) -> Result<Option<QueryCard>, SqlError> {
    let Some(di) = preds.iter().position(|p| {
        t.index_on(&p.col).is_some_and(|ix| !(matches!(p.shape, Shape::Range(..)) && ix.unique))
    }) else {
        return Ok(None);
    };
    let drive = &preds[di];
    let ix = t.index_on(&drive.col).expect("position found it");
    let mut b = CardB::new("", format!("{}.{}", t.name, drive.col));
    push_drive(&mut b, v, drive)?;
    for (i, p) in preds.iter().enumerate() {
        if i == di {
            continue;
        }
        if !ix.values.iter().any(|x| x == &p.col) {
            fail.get_or_insert(format!(
                "view '{}': residual WHERE on '{}' reads a stored column that index {}.{} does not carry \u{2014} add INCLUDE ({}) to CREATE INDEX ON {} ({})",
                v.name, p.col, t.name, drive.col, p.col, t.name, drive.col,
            ));
            return Ok(None);
        }
        b.argv.push("FILTER".into());
        b.argv.push(p.col.clone());
        match &p.shape {
            Shape::Eq(x) => {
                b.argv.push("EQ".into());
                b.bind(v, x, &p.col, p.ty)?;
            }
            Shape::Range(lo, hi) => {
                b.argv.push("RANGE".into());
                b.bind(v, lo, &p.col, p.ty)?;
                b.bind(v, hi, &p.col, p.ty)?;
            }
        }
    }
    if !push_sort(&mut b, v, t, &ix.values, &drive.col, order, fail)? {
        return Ok(None);
    }
    Ok(Some(b.finish(v, fields)))
}

/// No declared path serves the view: name the exact declaration to add
/// (the accumulated specific reason wins when one exists).
fn no_path_error(
    v: &CreateView,
    t: &Table,
    preds: &[ColPred],
    order: Option<&(String, bool)>,
    fail: Option<String>,
) -> SqlError {
    if let Some(f) = fail {
        return SqlError::at(v.line, v.col, f);
    }
    let desc: Vec<String> = preds
        .iter()
        .map(|p| {
            format!("{} {}", p.col, if matches!(p.shape, Shape::Eq(_)) { "EQ" } else { "range" })
        })
        .collect();
    let mut cols: Vec<(String, bool)> = Vec::new();
    for p in preds.iter().filter(|p| matches!(p.shape, Shape::Eq(_))) {
        cols.push((p.col.clone(), false));
    }
    for p in preds.iter().filter(|p| matches!(p.shape, Shape::Range(..))) {
        cols.push((p.col.clone(), false));
    }
    if let Some((c, d)) = order
        && !cols.iter().any(|(n, _)| n == c)
    {
        cols.push((c.clone(), *d));
    } else if let Some((c, d)) = order
        && *d
        && let Some(last) = cols.iter_mut().find(|(n, _)| n == c)
    {
        last.1 = true;
    }
    let sugg: Vec<String> =
        cols.iter().map(|(c, d)| if *d { format!("{c} DESC") } else { c.clone() }).collect();
    err_v(
        v,
        format!(
            "WHERE ({}) matches no declared access path \u{2014} add: CREATE INDEX ON {} ({})",
            desc.join(", "),
            t.name,
            sugg.join(", "),
        ),
    )
}