inillucent-sql 2.1.0

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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Common table expressions: what a `WITH` binds, and how a recursive one is
//! filled.
//!
//! Invariant: **a CTE is bound once per reference and never bound inside
//! itself.** Two references to one CTE are two independent scans with their
//! own FROM-term numbers, which is why a binding holds an AST id rather than a
//! bound block; and a definition already being bound is a cycle, which is
//! answered rather than followed.
//!
//! ## Why this is its own module
//!
//! `bind.rs` was at its recorded ceiling and task-1913 added ninety-nine lines
//! to it, so the ratchet in `policy.rs` asked for an extraction rather than a
//! raised number. This is one question - what a name in a `WITH` stands for -
//! and the ten items here were the only ones asking it. Nothing moved changed
//! in the move.

use super::{subquery_table, unsupported, Binder, BoundSource, RecursiveBody, SourceRows};
use crate::ast::{self, CompoundOp, JoinKind, SelectId};
use crate::catalog_view::TableInfo;
use crate::diagnostic::{ParseError, ParseErrorKind};
use crate::lexer::Span;

/// The first number a derived table inside a correlated subquery keeps its rows
/// under, which is past any number a common table expression can have.
pub const FIRST_ANONYMOUS_SHARED: usize = 1 << 20;

/// One common table expression visible to a block.
///
/// The definition is kept as an AST id rather than a bound block because two
/// references to the same CTE are two independent scans: each gets its own
/// FROM-term numbers and its own materialisation. Binding once and cloning
/// would give both references the same source ids, and the second scan would
/// then read the first one's cursors.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CteBinding {
    /// The folded name a FROM term matches against.
    pub folded: Vec<u8>,
    /// The name as written, which the expansion is aliased to.
    pub name: Vec<u8>,
    /// The explicit column list, when the `WITH` wrote one.
    pub columns: Vec<Vec<u8>>,
    /// The query the name stands for.
    pub select: SelectId,
    /// Whether the `WITH` said `RECURSIVE`.
    pub recursive: bool,
    /// `Some(true)` for `MATERIALIZED`, `Some(false)` for `NOT MATERIALIZED`.
    pub materialized: Option<bool>,
}

/// One recursive CTE whose definition is being bound.
#[derive(Clone, Debug)]
pub(super) struct RecursiveTarget {
    /// The CTE's folded name.
    pub(super) folded: Vec<u8>,
    /// The statement-wide number of the FROM term that will hold its store.
    id: usize,
    /// The columns a reference to it exposes, taken from the seed arm.
    table: TableInfo,
    /// Whether any arm bound so far referred to it.
    referenced: bool,
}

impl Binder<'_> {
    /// Pushes the CTEs of a `WITH` prefix, returning whether it pushed any.
    pub(crate) fn push_ctes(&mut self, with: &ast::With) -> Result<bool, ParseError> {
        if with.ctes.is_empty() {
            return Ok(false);
        }
        let mut bindings = Vec::with_capacity(with.ctes.len());
        for cte in &with.ctes {
            // **A name may be defined once in one `WITH`.** SQLite refuses the
            // second definition while it parses, in these words; an inner
            // `WITH` is a different clause and may reuse the name.
            let folded = self.ast.folded(cte.name);
            if bindings
                .iter()
                .any(|held: &CteBinding| held.folded.as_slice() == folded)
            {
                return Err(super::refused(
                    format!(
                        "duplicate WITH table name: {}",
                        String::from_utf8_lossy(self.ast.text(cte.name))
                    ),
                    crate::lexer::Span::default(),
                ));
            }
            bindings.push(CteBinding {
                folded: self.ast.folded(cte.name).to_vec(),
                name: self.ast.text(cte.name).to_vec(),
                columns: cte
                    .columns
                    .iter()
                    .map(|name| self.ast.text(*name).to_vec())
                    .collect(),
                select: cte.select,
                recursive: with.recursive,
                materialized: cte.materialized,
            });
        }
        self.ctes.push(bindings);
        Ok(true)
    }

    /// Drops the innermost level of CTE bindings.
    pub(crate) fn pop_ctes(&mut self) {
        self.ctes.pop();
    }

    /// Returns the innermost CTE a folded name matches.
    pub(super) fn find_cte(&self, folded: &[u8]) -> Option<CteBinding> {
        for level in self.ctes.iter().rev() {
            if let Some(found) = level.iter().find(|cte| cte.folded == folded) {
                return Some(found.clone());
            }
        }
        None
    }

    /// Reports whether the statement refers to a name in more than one FROM term.
    ///
    /// Counted over every FROM term the statement was parsed into, so an inner
    /// `WITH` that reuses the name is counted too. That only ever shares a CTE
    /// that did not need to be shared.
    ///
    /// @param folded - the folded name
    pub(super) fn name_is_used_twice(&self, folded: &[u8]) -> bool {
        let mut uses = 0usize;
        for at in 0..self.ast.from_term_count() {
            let Some(term) = self.ast.from_term(ast::FromTermId(at as u32)) else {
                continue;
            };
            if let ast::FromSource::Table {
                database: None,
                name,
                ..
            } = &term.source
            {
                if self.ast.folded(*name) == folded {
                    uses = uses.saturating_add(1);
                }
            }
        }
        uses > 1
    }

    /// Marks the block a CTE reference was just bound to as one that shares its
    /// evaluation with the other references, when that can be seen.
    ///
    /// SQLite evaluates a CTE used more than once a single time unless it is
    /// `NOT MATERIALIZED`. The difference can only be seen when the body is not
    /// a function of its tables, so only a body that calls `random()` or a
    /// function like it is marked, and one that reads a column of an enclosing
    /// query is left alone because it has a different answer for every row.
    ///
    /// @param cte - the definition
    pub(super) fn share_last_source(&mut self, cte: &CteBinding) {
        if cte.materialized == Some(false) || !self.name_is_used_twice(&cte.folded) {
            return;
        }
        let key = match self.shared_ctes.iter().position(|(arena, select)| {
            *arena == self.ast as *const _ as usize && *select == cte.select
        }) {
            Some(key) => key,
            None => {
                self.shared_ctes
                    .push((self.ast as *const _ as usize, cte.select));
                self.shared_ctes.len().saturating_sub(1)
            }
        };
        let Some(source) = self.sources.last_mut() else {
            return;
        };
        let SourceRows::Subquery(block) = &mut source.rows else {
            return;
        };
        if !block.correlations.is_empty() {
            return;
        }
        let mut volatile = false;
        let mut probe = (**block).clone();
        crate::rewrite::rewrite_select(&mut probe, &mut |expr: &mut super::BoundExpr| {
            if crate::plan::calls_a_volatile_function(expr) {
                volatile = true;
            }
        });
        if volatile {
            block.shared = Some(key);
        }
    }

    /// Makes the derived tables of a correlated subquery that read nothing of
    /// the enclosing query keep their rows for the whole statement.
    ///
    /// **SQLite materialises such a derived table once** (`OP_Once`), however
    /// many outer rows the subquery runs for. Read again for every row, a
    /// `SELECT ... FROM (SELECT sum(v) OVER () ...)` inside an `UPDATE`'s `SET`
    /// saw the rows the statement had already changed.
    ///
    /// @param block - a subquery that reads a column of an enclosing query
    pub(super) fn share_uncorrelated_sources(&mut self, block: &mut super::BoundSelect) {
        for source in &mut block.sources {
            let SourceRows::Subquery(inner) = &mut source.rows else {
                continue;
            };
            if inner.correlations.is_empty() {
                if inner.shared.is_none() {
                    inner.shared = Some(FIRST_ANONYMOUS_SHARED + self.shared_anonymous);
                    self.shared_anonymous = self.shared_anonymous.saturating_add(1);
                }
            } else {
                self.share_uncorrelated_sources(inner);
            }
        }
        for (_, arm) in &mut block.compounds {
            self.share_uncorrelated_sources(arm);
        }
    }

    /// Reports whether a CTE's own query names it in a FROM clause.
    ///
    /// **What makes a CTE recursive is the self-reference, not the keyword.**
    /// SQLite accepts `WITH c AS (SELECT 1 UNION ALL SELECT ... FROM c)` with
    /// no `RECURSIVE` written and answers it; this binder read only the
    /// keyword, so the same query bound `c`'s definition inside `c`'s
    /// definition until the process ran out of stack (task-1913).
    ///
    /// An inner `WITH` that binds the same name shadows the outer one, so
    /// nothing under it can be the recursion - which is why this stops there
    /// rather than reporting every mention of the name.
    ///
    /// @param select - the CTE's query
    /// @param folded - the CTE's folded name
    pub(super) fn select_names_itself(&self, select: ast::SelectId, folded: &[u8]) -> bool {
        let Some(query) = self.ast.select(select) else {
            return false;
        };
        if query
            .with
            .ctes
            .iter()
            .any(|inner| self.ast.folded(inner.name) == folded)
        {
            return false;
        }
        if self.core_names_cte(query.first, folded) {
            return true;
        }
        query
            .compounds
            .iter()
            .any(|(_, arm)| self.core_names_cte(*arm, folded))
    }

    /// Reports whether one arm of a compound names a CTE in its FROM clause.
    ///
    /// @param core - the arm
    /// @param folded - the CTE's folded name
    pub(super) fn core_names_cte(&self, core: ast::SelectCoreId, folded: &[u8]) -> bool {
        let Some(arm) = self.ast.core(core) else {
            return false;
        };
        let ast::SelectBody::Select { from, .. } = &arm.body else {
            return false;
        };
        self.terms_name_cte(from, folded)
    }

    /// Reports whether any FROM term names a CTE.
    ///
    /// @param terms - the FROM terms
    /// @param folded - the CTE's folded name
    pub(super) fn terms_name_cte(&self, terms: &[ast::FromTermId], folded: &[u8]) -> bool {
        terms.iter().any(|id| match self.ast.from_term(*id) {
            Some(term) => match &term.source {
                ast::FromSource::Table { database, name, .. } => {
                    database.is_none() && self.ast.folded(*name) == folded
                }
                ast::FromSource::Subquery(select) => self.select_names_itself(*select, folded),
                ast::FromSource::Join(inner) => self.terms_name_cte(inner, folded),
            },
            None => false,
        })
    }

    /// Registers a reference to the recursive CTE currently being bound.
    pub(super) fn push_recursive_self(
        &mut self,
        position: usize,
        alias: Option<ast::NameId>,
        join: JoinKind,
    ) -> Result<(), ParseError> {
        let Some(target) = self.recursing.get_mut(position) else {
            return Err(unsupported("unknown recursive reference", Span::default()));
        };
        target.referenced = true;
        let cte = target.id;
        let table = target.table.clone();
        let alias = match alias {
            Some(alias) => self.ast.text(alias).to_vec(),
            None => table.name.clone(),
        };
        let id = self.sources.len();
        self.sources.push(BoundSource {
            index_hint: crate::bind::IndexChoice::Any,
            id,
            rows: SourceRows::RecursiveSelf { cte },
            table: std::rc::Rc::new(table),
            alias,
            join,
            constraint: None,
            suppressed: Vec::new(),
            index_exprs: Vec::new(),
            written_schema: None,
        });
        if let Some(scope) = self.scopes.last_mut() {
            scope.push(id);
        }
        Ok(())
    }

    /// Binds a `WITH RECURSIVE` CTE reference.
    ///
    /// The seed arm is bound first, alone, because until it is bound nothing
    /// knows what columns the CTE has - and the step arm cannot be bound until
    /// a reference to the CTE has columns to resolve against. A CTE declared
    /// `RECURSIVE` that turns out not to reference itself is an ordinary
    /// compound, and is rebuilt as one rather than run through a queue that
    /// would never be fed.
    pub(super) fn bind_recursive_cte(
        &mut self,
        cte: &CteBinding,
        alias: Vec<u8>,
        join: JoinKind,
        span: Span,
    ) -> Result<(), ParseError> {
        let Some(select) = self.ast.select(cte.select) else {
            return Err(unsupported("missing select", span));
        };
        if select.compounds.is_empty() {
            return self.bind_subquery_term(
                cte.select,
                Some(alias),
                cte.columns.clone(),
                join,
                span,
            );
        }
        let arms: Vec<(CompoundOp, ast::SelectCoreId)> = select.compounds.clone();
        let order_by = select.order_by.clone();
        let limit = select.limit;
        let offset = select.offset;
        let first = select.first;

        let id = self.sources.len();
        // The store's FROM-term number is reserved before anything is bound, so
        // that a self-reference inside the step arm can name the store it will
        // read without the two being bound in an impossible order.
        self.sources.push(BoundSource {
            index_hint: crate::bind::IndexChoice::Any,
            id,
            rows: SourceRows::Table,
            table: std::rc::Rc::new(TableInfo::subquery(alias.clone(), 0, Vec::new())),
            alias: alias.clone(),
            join,
            constraint: None,
            suppressed: Vec::new(),
            index_exprs: Vec::new(),
            written_schema: None,
        });

        let seed = self.bind_isolated_arm(first)?;
        let table = subquery_table(&alias, &cte.columns, &seed);
        if !cte.columns.is_empty() && cte.columns.len() != seed.columns.len() {
            return Err(super::refusal::named_column_count(
                &alias,
                seed.columns.len(),
                cte.columns.len(),
                span,
            ));
        }
        self.recursing.push(RecursiveTarget {
            folded: cte.folded.clone(),
            id,
            table: table.clone(),
            referenced: false,
        });
        let mut seeds = vec![(CompoundOp::UnionAll, seed)];
        let mut steps = Vec::new();
        let mut outcome = Ok(());
        for (op, arm) in &arms {
            if !matches!(op, CompoundOp::Union | CompoundOp::UnionAll) {
                outcome = Err(ParseError::new(
                    ParseErrorKind::Unsupported("recursive query does not use UNION or UNION ALL"),
                    span,
                ));
                break;
            }
            if let Some(target) = self.recursing.last_mut() {
                target.referenced = false;
            }
            let bound = match self.bind_isolated_arm(*arm) {
                Ok(bound) => bound,
                Err(reason) => {
                    outcome = Err(reason);
                    break;
                }
            };
            let referenced = self
                .recursing
                .last()
                .is_some_and(|target| target.referenced);
            if referenced {
                steps.push((*op, bound));
            } else {
                seeds.push((*op, bound));
            }
        }
        self.recursing.pop();
        outcome?;
        // The ORDER BY, LIMIT and OFFSET belong to the whole recursive query:
        // SQLite orders its queue by them and stops the recursion at the limit,
        // so they are kept on the body rather than on the seed arm.
        let seed_columns = seeds
            .first()
            .map_or_else(Vec::new, |(_, seed)| seed.columns.clone());
        // An ORDER BY name may come from any arm, as in every other compound.
        let other_arms: Vec<(ast::CompoundOp, crate::bind::BoundSelect)> =
            seeds.iter().skip(1).chain(steps.iter()).cloned().collect();
        let order_by = self.bind_compound_order_by(&order_by, &seed_columns, &other_arms)?;
        let limit = limit.map(|expr| self.bind_expr(expr)).transpose()?;
        let offset = offset.map(|expr| self.bind_expr(expr)).transpose()?;
        let mut source = BoundSource {
            index_hint: crate::bind::IndexChoice::Any,
            id,
            rows: SourceRows::Recursive(Box::new(RecursiveBody {
                seeds,
                steps,
                order_by,
                limit,
                offset,
            })),
            table: std::rc::Rc::new(table),
            alias,
            join,
            constraint: None,
            suppressed: Vec::new(),
            index_exprs: Vec::new(),
            written_schema: None,
        };
        if let SourceRows::Recursive(body) = &mut source.rows {
            if body.steps.is_empty() {
                // Declared recursive, never refers to itself: an ordinary
                // compound wearing the keyword.
                let mut arms = core::mem::take(&mut body.seeds);
                if arms.is_empty() {
                    return Err(unsupported("missing select core", span));
                }
                let mut head = arms.remove(0).1;
                head.compounds = arms;
                head.order_by = core::mem::take(&mut body.order_by);
                head.limit = body.limit.take();
                head.offset = body.offset.take();
                source.rows = SourceRows::Subquery(Box::new(head));
            }
        }
        if let Some(slot) = self.sources.get_mut(id) {
            *slot = source;
        }
        if let Some(scope) = self.scopes.last_mut() {
            scope.push(id);
        }
        Ok(())
    }
}

impl<'a> Binder<'a> {
    /// Binds a FROM term that names a common table expression.
    ///
    /// @param cte - the expression the name stands for
    /// @param folded - the folded name, to tell a recursive reference from a plain one
    /// @param alias - the alias written on the term, if any
    /// @param join - how the term joins the ones before it
    /// @param span - where the term is, for an error
    pub(super) fn bind_cte_term(
        &mut self,
        cte: CteBinding,
        folded: &[u8],
        alias: Option<ast::NameId>,
        join: JoinKind,
        span: Span,
    ) -> Result<(), ParseError> {
        let alias = match alias {
            Some(alias) => self.ast.text(alias).to_vec(),
            None => cte.name.clone(),
        };
        // A definition already being bound cannot be bound again: that
        // is a cycle, and following it does not end.
        if self.binding_ctes.contains(&cte.select) {
            // SQLite names the expression and points at nothing.
            let _ = span;
            return Err(ParseError::new(
                ParseErrorKind::Refused(format!(
                    "circular reference: {}",
                    String::from_utf8_lossy(&cte.name)
                )),
                Span::default(),
            ));
        }
        self.binding_ctes.push(cte.select);
        // **`RECURSIVE` is a keyword SQLite does not require.** A CTE
        // whose FROM names itself *is* the recursion, written or not,
        // and reading the keyword as the only evidence sent this
        // binder round the same definition until the stack ran out.
        let outcome = if cte.recursive || self.select_names_itself(cte.select, folded) {
            self.bind_recursive_cte(&cte, alias, join, span)
        } else {
            let bound =
                self.bind_subquery_term(cte.select, Some(alias), cte.columns.clone(), join, span);
            if bound.is_ok() {
                self.share_last_source(&cte);
            }
            bound
        };
        self.binding_ctes.pop();
        outcome
    }

    /// Finds the table a FROM term names, falling back to a table valued
    /// function when the view's own database does not hold the name.
    ///
    /// A name that is not a table of the view's database may still be a table
    /// valued function such as `json_each`, which belongs to no schema.
    ///
    /// @param database - the schema written on the term, if any
    /// @param database_name - the schema to look in, folded
    /// @param folded - the table name, folded
    pub(super) fn find_term_table(
        &self,
        database: Option<ast::NameId>,
        database_name: Option<Vec<u8>>,
        folded: &[u8],
    ) -> (Option<&'a TableInfo>, Option<Vec<u8>>) {
        let found = self.catalog.find_table(database_name.as_deref(), folded);
        if found.is_none() && database.is_none() && database_name.is_some() {
            let eponymous = self
                .catalog
                .find_table(None, folded)
                .filter(|table| table.kind == crate::catalog_view::TableKind::Virtual);
            if eponymous.is_some() {
                return (eponymous, None);
            }
        }
        (found, database_name)
    }
}