inillucent-sql 2.3.3

First-party lexer, parser, AST, binder, semantic rewrites, and logical and physical plans.
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
//! Lowering a correlated `IN` subquery to `EXISTS`.
//!
//! Invariant: **the lowering answers what SQLite answers, NULLs included.**
//! `x IN (SELECT y FROM s WHERE p)` is not "is there a row where `y = x`": an
//! empty list is false even when `x` is NULL, a NULL `x` over a list with rows
//! is NULL, and a list that holds a NULL turns a non-match into NULL. A
//! lowering that kept only the first of those would answer false where SQLite
//! answers NULL, and `WHERE` treats the two the same - so the difference would
//! never show in a row count and would show in an `IS NULL` or a `CASE`.
//!
//! **Why it is a rewrite at all.** The physical pass computes a correlated
//! block once per outer row and hands the operator one column; an `IN` needs
//! the whole list, so `crates/inillucent-exec/src/correlate.rs` refused one by
//! name rather than answering wrongly. `EXISTS` over the same block is what the
//! engine already runs per row, so writing the `IN` as three `EXISTS` tests
//! reaches an execution path that exists (task-1979, section 8.2, gap 1).
//!
//! The three blocks are: does the list have any row, does it hold a row equal
//! to the operand, and does it hold a NULL. The `CASE` below orders them so
//! that each one is only consulted where its answer decides the result.
//!
//! **What is not lowered.** A block with `GROUP BY`, `HAVING`, `DISTINCT`,
//! `LIMIT`, `OFFSET`, a compound arm, a window or a `VALUES` list keeps the
//! refusal: the equality is pushed into the block's `WHERE`, and a `WHERE` is
//! applied before grouping and before a limit, so pushing it past either would
//! ask a different question.

use std::rc::Rc;

use inillucent_value::{Affinity, Collation};

use crate::ast::{BinaryOp, JoinKind};
use crate::bind::{BoundExpr, BoundSelect, BoundStatement, SourceRows, SubqueryKind};
use crate::catalog_view::{TableInfo, TableKind};

/// Rewrites every correlated `IN` in a statement into `EXISTS` tests.
///
/// Called after binding and before planning. A block this cannot lower is left
/// exactly as it was, so the refusal that names it still fires.
///
/// @param statement - the bound statement, rewritten in place
///
/// **The statement's triggers are lowered too.** A trigger body is bound
/// with the statement that fires it, and a correlated `IN` in a body was
/// refused as not built when the body ran, while the same query typed as a
/// statement of its own was lowered and answered.
pub fn lower(statement: &mut BoundStatement) {
    let mut highest = 0usize;
    let mut correlated_in = false;
    walk_statement(statement, &mut |expr: &mut BoundExpr| {
        if let BoundExpr::Subquery {
            id, kind, block, ..
        } = expr
        {
            highest = highest.max(*id);
            correlated_in |= *kind == SubqueryKind::In && !block.correlations.is_empty();
        }
    });
    // Every statement is lowered, and nearly none holds a correlated `IN`;
    // the tables are gathered only for one that does.
    if !correlated_in {
        return;
    }
    let mut next = highest.saturating_add(1);
    let tables = never_null_tables(statement);
    walk_statement(statement, &mut |expr: &mut BoundExpr| {
        lower_one(expr, &mut next, &tables)
    });
}

/// Returns the FROM terms whose `NOT NULL` columns can never read as NULL.
///
/// An ordinary table read in a block with no outer join. A term on either side
/// of a `LEFT`, `RIGHT` or `FULL` join can be null extended, so every term of
/// such a block is left out, which is the cautious half of the rule. So are
/// the terms of an `INSERT`, `UPDATE` or `DELETE`, which are not walked.
///
/// @param statement - the bound statement
fn never_null_tables(statement: &mut BoundStatement) -> Vec<(usize, Rc<TableInfo>)> {
    let mut found = Vec::new();
    if let BoundStatement::Select(select) = statement {
        gather_tables(select, &mut found);
    }
    walk_statement(statement, &mut |expr: &mut BoundExpr| {
        if let Some(block) = expr.block_mut() {
            gather_tables(block, &mut found);
        }
    });
    found
}

/// Adds one block's ordinary tables, and its derived tables' and arms', to a list.
///
/// @param select - the block
/// @param into - the terms found so far
fn gather_tables(select: &BoundSelect, into: &mut Vec<(usize, Rc<TableInfo>)>) {
    let outer = select.sources.iter().any(|source| {
        matches!(
            source.join,
            JoinKind::Left | JoinKind::Right | JoinKind::Full
        )
    });
    for source in &select.sources {
        match &source.rows {
            SourceRows::Table if !outer && source.table.kind == TableKind::Table => {
                into.push((source.id, Rc::clone(&source.table)));
            }
            SourceRows::Subquery(block) => gather_tables(block, into),
            SourceRows::Recursive(body) => {
                for (_, arm) in body.seeds.iter().chain(body.steps.iter()) {
                    gather_tables(arm, into);
                }
            }
            _ => {}
        }
    }
    for (_, arm) in &select.compounds {
        gather_tables(arm, into);
    }
}

/// Reports whether an expression is a column that can never be NULL.
///
/// A `NOT NULL` column or the rowid of a term [`never_null_tables`] listed.
///
/// @param expr - the expression
/// @param tables - the terms whose declared constraints hold
fn never_null(expr: &BoundExpr, tables: &[(usize, Rc<TableInfo>)]) -> bool {
    let table_of = |wanted: usize| {
        tables
            .iter()
            .find(|(id, _)| *id == wanted)
            .map(|(_, table)| table)
    };
    match expr {
        BoundExpr::Column { source, column, .. } => table_of(*source).is_some_and(|table| {
            table.rowid_alias == Some(*column)
                || table
                    .columns
                    .get(usize::from(*column))
                    .is_some_and(|held| held.not_null)
        }),
        BoundExpr::Rowid { source } => table_of(*source).is_some_and(|table| !table.without_rowid),
        _ => false,
    }
}

/// Applies a rewrite to a statement and to every trigger body it fires.
///
/// @param statement - the bound statement
/// @param rewrite - what to do to each expression
fn walk_statement(statement: &mut BoundStatement, rewrite: crate::rewrite::Rewrite<'_>) {
    match statement {
        BoundStatement::Select(select) => crate::rewrite::rewrite_select(select, rewrite),
        BoundStatement::Insert(insert) => {
            crate::rewrite::rewrite_insert(insert, rewrite);
            walk_triggers(&mut insert.triggers, rewrite);
            for clause in &mut insert.upsert {
                walk_triggers(&mut clause.triggers, rewrite);
            }
        }
        BoundStatement::Update(update) => {
            crate::rewrite::rewrite_update(update, rewrite);
            walk_triggers(&mut update.triggers, rewrite);
        }
        BoundStatement::Delete(delete) => {
            crate::rewrite::rewrite_delete(delete, rewrite);
            walk_triggers(&mut delete.triggers, rewrite);
        }
        BoundStatement::Directive(_) | BoundStatement::Empty => {}
    }
}

/// Applies a rewrite to every statement in some triggers' bodies, and to the
/// triggers those statements fire in turn.
///
/// @param triggers - the triggers
/// @param rewrite - what to do to each expression
fn walk_triggers(triggers: &mut [crate::dml::BoundTrigger], rewrite: crate::rewrite::Rewrite<'_>) {
    use crate::dml::BoundTriggerStatement;
    for trigger in triggers {
        for statement in &mut trigger.body {
            match statement {
                BoundTriggerStatement::Select(select) => {
                    crate::rewrite::rewrite_select(select, rewrite)
                }
                BoundTriggerStatement::Insert(insert) => {
                    crate::rewrite::rewrite_insert(insert, rewrite);
                    walk_triggers(&mut insert.triggers, rewrite);
                    for clause in &mut insert.upsert {
                        walk_triggers(&mut clause.triggers, rewrite);
                    }
                }
                BoundTriggerStatement::Update(update) => {
                    crate::rewrite::rewrite_update(update, rewrite);
                    walk_triggers(&mut update.triggers, rewrite);
                }
                BoundTriggerStatement::Delete(delete) => {
                    crate::rewrite::rewrite_delete(delete, rewrite);
                    walk_triggers(&mut delete.triggers, rewrite);
                }
            }
        }
    }
}

/// Replaces one expression when it is a correlated `IN` this can lower.
///
/// @param expr - the expression, replaced in place
/// @param next - the next free subquery number, advanced by up to three on a rewrite
/// @param tables - the terms whose `NOT NULL` columns can never read as NULL
fn lower_one(expr: &mut BoundExpr, next: &mut usize, tables: &[(usize, Rc<TableInfo>)]) {
    let BoundExpr::Subquery {
        kind: SubqueryKind::In,
        negated,
        operand: Some(operand),
        block,
        affinity,
        collation,
        ..
    } = expr
    else {
        return;
    };
    if block.correlations.is_empty() || !liftable(block) {
        return;
    }
    let listed = match block.columns.first() {
        Some(column) => column.expr.clone(),
        None => return,
    };
    let replacement = lowered(
        &Lowering {
            operand_never_null: never_null(operand, tables),
            listed_never_null: never_null(&listed, tables),
            operand: (**operand).clone(),
            listed,
            negated: *negated,
            affinity: *affinity,
            collation: *collation,
        },
        block,
        next,
    );
    *expr = replacement;
}

/// Returns whether a block's `WHERE` decides the same rows the block reports.
///
/// The equality is pushed into the block's `WHERE`, so anything that reads the
/// rows *after* the `WHERE` - grouping, a limit, a compound arm - would be
/// asked a different question by the rewritten block.
///
/// @param block - the subquery's own select
fn liftable(block: &BoundSelect) -> bool {
    block.group_by.is_empty()
        && block.having.is_none()
        && !block.distinct
        && block.limit.is_none()
        && block.offset.is_none()
        && block.compounds.is_empty()
        && block.windows.is_empty()
        && block.aggregates.is_empty()
        && block.values.is_empty()
        && !block.sources.is_empty()
}

/// What one `IN` was written as, which is what the lowering needs.
///
/// A struct rather than six parameters, because
/// `crates/inillucent-compat/tests/tooling/policy.rs` refuses an
/// `#[allow(clippy::too_many_arguments)]`: the threshold is set once in
/// `clippy.toml` with the argument for where it is, and an attribute moves the
/// bar for one function and says nothing about why.
struct Lowering {
    /// The left side of the `IN`.
    operand: BoundExpr,
    /// The block's first result column, which is what `IN` compares against.
    listed: BoundExpr,
    /// Whether `NOT IN` was written.
    negated: bool,
    /// The affinity `IN` applies to both sides.
    affinity: Option<Affinity>,
    /// The collation `IN` compares with.
    collation: Collation,
    /// Whether the operand is a column that can never be NULL.
    operand_never_null: bool,
    /// Whether the listed column can never be NULL.
    listed_never_null: bool,
}

/// Builds the `CASE` that answers what `IN` answers.
///
/// ```text
/// CASE WHEN <a row equals the operand>   THEN 1
///      WHEN <the operand is NULL>        THEN CASE WHEN <the list has a row> THEN NULL ELSE 0 END
///      WHEN <the list holds a NULL>      THEN NULL
///      ELSE 0 END
/// ```
///
/// The first arm cannot fire when the operand is NULL, because `y = NULL` is
/// NULL rather than true, so the second arm is reached exactly when the operand
/// is NULL. `NOT IN` is the same shape with the 1 and the 0 exchanged; NULL
/// stays NULL, which is what makes `NOT IN` over a list holding a NULL answer
/// nothing.
///
/// **An arm that cannot fire is left out (task-2183).** The correlation
/// operator answers every block of a row before the `CASE` reads any of them,
/// so each arm was a whole `EXISTS` run per outer row whether it decided the
/// answer or not. The second arm needs a NULL operand and the third a NULL in
/// the list, and a column declared `NOT NULL` in a block with no outer join
/// can be neither. `wide.id IN (SELECT owner FROM side_table WHERE owner =
/// wide.id)` ran three blocks per row and runs one.
///
/// @param about - what the `IN` was written as
/// @param block - the subquery's own select
/// @param next - the next free subquery number, advanced by three
fn lowered(about: &Lowering, block: &BoundSelect, next: &mut usize) -> BoundExpr {
    let matched = exists(
        block,
        Some(BoundExpr::Compare {
            op: BinaryOp::Equal,
            left: Box::new(widened(about.listed.clone(), about.affinity)),
            right: Box::new(widened(about.operand.clone(), about.affinity)),
            affinity: about.affinity,
            collation: about.collation,
        }),
        next,
    );
    // With neither NULL arm left the `CASE` is the `EXISTS` itself, which is
    // 1 or 0 and never NULL, or its negation for `NOT IN`.
    if about.operand_never_null && about.listed_never_null {
        let mut matched = matched;
        if let BoundExpr::Subquery { negated, .. } = &mut matched {
            *negated = about.negated;
        }
        return matched;
    }
    let (found, missing) = match about.negated {
        true => (BoundExpr::Integer(0), BoundExpr::Integer(1)),
        false => (BoundExpr::Integer(1), BoundExpr::Integer(0)),
    };
    let mut branches = vec![(matched, found)];
    if !about.operand_never_null {
        let any_row = exists(block, None, next);
        branches.push((
            BoundExpr::IsNull {
                negated: false,
                operand: Box::new(about.operand.clone()),
            },
            BoundExpr::Case {
                operand: None,
                branches: vec![(any_row, BoundExpr::Null)],
                otherwise: Some(Box::new(missing.clone())),
                comparisons: Vec::new(),
            },
        ));
    }
    if !about.listed_never_null {
        let any_null = exists(
            block,
            Some(BoundExpr::IsNull {
                negated: false,
                operand: Box::new(about.listed.clone()),
            }),
            next,
        );
        branches.push((any_null, BoundExpr::Null));
    }
    BoundExpr::Case {
        operand: None,
        branches,
        otherwise: Some(Box::new(missing)),
        comparisons: Vec::new(),
    }
}

/// Widens an integer to a real when the `IN` compares under a REAL affinity.
///
/// SQLite tests `IN` membership through an ephemeral index that stores both
/// sides under the comparison's affinity, and a REAL affinity stores an integer
/// as a real. So `9223372036854775806 IN (SELECT a ...)` matches a REAL column
/// holding 9223372036854775807, while the `=` this lowering otherwise writes
/// keeps the integer exact and does not. The uncorrelated `IN` makes the same
/// conversion in `inillucent-exec`'s `membership_sides`; this is the same rule
/// for the lowered form, written as
/// `CASE WHEN typeof(x) = 'integer' THEN CAST(x AS REAL) ELSE x END`.
///
/// @param expr - one side of the equality
/// @param affinity - the affinity `IN` applies to both sides
fn widened(expr: BoundExpr, affinity: Option<Affinity>) -> BoundExpr {
    if affinity != Some(Affinity::Real) {
        return expr;
    }
    let is_integer = BoundExpr::Compare {
        op: BinaryOp::Equal,
        left: Box::new(BoundExpr::Function {
            func: crate::function::ScalarFunc::TypeOf,
            arguments: vec![expr.clone()],
            collation: Collation::Binary,
        }),
        right: Box::new(BoundExpr::Text(b"integer".to_vec())),
        affinity: None,
        collation: Collation::Binary,
    };
    BoundExpr::Case {
        operand: None,
        branches: vec![(
            is_integer,
            BoundExpr::Cast {
                operand: Box::new(expr.clone()),
                affinity: Affinity::Real,
            },
        )],
        otherwise: Some(Box::new(expr)),
        comparisons: Vec::new(),
    }
}

/// Returns the terms of a conjunction, left to right.
///
/// @param filter - a `WHERE` clause
fn conjuncts(filter: &BoundExpr) -> Box<dyn Iterator<Item = &BoundExpr> + '_> {
    match filter {
        BoundExpr::And(left, right) => Box::new(conjuncts(left).chain(conjuncts(right))),
        other => Box::new(std::iter::once(other)),
    }
}

/// Returns an `EXISTS` over a copy of the block, with one more `WHERE` term.
///
/// @param block - the subquery's own select
/// @param extra - the term to add to its `WHERE`, when there is one
/// @param next - the next free subquery number, advanced by one
fn exists(block: &BoundSelect, extra: Option<BoundExpr>, next: &mut usize) -> BoundExpr {
    let mut copy = block.clone();
    // The block's own result columns are not read by `EXISTS`, and one of them
    // may be the column the equality now tests - so they are replaced by a
    // constant rather than kept.
    copy.columns.truncate(1);
    if let Some(first) = copy.columns.first_mut() {
        first.expr = BoundExpr::Integer(1);
        first.origin = None;
    }
    copy.order_by.clear();
    // **An equality the `WHERE` already holds is not added twice (task-2183).**
    // `x IN (SELECT y FROM s WHERE y = x)` would test `y = x` twice, and the
    // second copy is a residual filter the access path does not consume.
    let held_already = |extra: &BoundExpr| {
        copy.filter
            .as_ref()
            .is_some_and(|filter| conjuncts(filter).any(|term| term == extra))
    };
    if let Some(extra) = extra.filter(|extra| !held_already(extra)) {
        copy.filter = Some(match copy.filter.take() {
            Some(held) => BoundExpr::And(Box::new(held), Box::new(extra)),
            None => extra,
        });
    }
    let id = *next;
    *next = next.saturating_add(1);
    BoundExpr::Subquery {
        id,
        kind: SubqueryKind::Exists,
        negated: false,
        operand: None,
        block: Box::new(copy),
        affinity: None,
        collation: Collation::Binary,
    }
}