Skip to main content

inillucent_sql/
dml.rs

1//! Binding INSERT, UPDATE and DELETE.
2//!
3//! Invariant: a bound DML statement names every value it will write, in table
4//! column order, before anything is compiled. A column the statement did not
5//! mention is not left to be filled in later by whoever runs it - it carries
6//! its `DEFAULT`, or a NULL, as an expression like any other. That is what
7//! makes `INSERT INTO t(b) VALUES(1)` and `INSERT INTO t VALUES(NULL, 1)`
8//! compile to the same shape, and it is why the constraint checks can be
9//! written once against a row image rather than twice against two.
10//!
11//! Constraints are bound here too, out of the `CREATE TABLE` text the file
12//! stores. The catalog keeps them as source, because the catalog sits below
13//! the binder and cannot bind anything; the binder parses that source against
14//! the table it belongs to and gets an ordinary expression back. A CHECK is
15//! therefore evaluated by exactly the machinery that evaluates a WHERE clause,
16//! which is the only way to be sure the two agree about what `x > 0` means
17//! when `x` is text.
18
19use inillucent_base::limits::Limits;
20use inillucent_value::Collation;
21
22use crate::ast::{self, ConflictAction};
23use crate::bind::{
24    no_such_column, refused, unsupported, Binder, BoundExpr, BoundOrderTerm, BoundResultColumn,
25    BoundSelect, BoundSource,
26};
27use crate::catalog_view::{IndexInfo, TableInfo, TableKind, TriggerEventInfo, TriggerInfo};
28use crate::diagnostic::ParseError;
29use crate::lexer::Span;
30use crate::parser::parse_expression;
31
32/// The internal tables an application may write, as SQLite allows.
33///
34/// **Four, and two of them are the schema.** Every table whose
35/// name begins with `sqlite_` used to be refused, which is wrong for all four,
36/// because writing them is the documented way to use them:
37///
38/// - `sqlite_schema`, and `sqlite_master` which is its other name, are what
39///   `PRAGMA writable_schema` is for, and `.dump` emits
40///   `INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)VALUES(...)`
41///   for a virtual table - which is the only way a dump can restore one
42///   without building empty shadow tables over the ones it is about to fill
43///   (task-1979, R2). Whether the pragma is on is the *engine's* question and
44///   not the binder's: `ImportedDatabase::refuse_schema_write` refuses the
45///   statement when it is off, the way `refuse_shadow_write` refuses a write a
46///   defensive connection may not make.
47///
48/// - `sqlite_sequence` holds one row per `AUTOINCREMENT` table, and
49///   `UPDATE sqlite_sequence SET seq = 0 WHERE name = 't'` is how the counter is
50///   reset. `DELETE FROM sqlite_sequence` is how it is reset for every table at
51///   once. Refusing them left no way at all to do either.
52/// - `sqlite_stat1` is what `ANALYZE` writes, and `.dump` emits
53///   `INSERT INTO sqlite_stat1 VALUES(...)` for it - so a dump this engine
54///   produced could not be replayed into it.
55///
56/// They are ordinary tables in every other respect: the rows are what they are,
57/// and a value written into one is used exactly as `ANALYZE` or the rowid
58/// allocator would have used the one it replaced.
59const WRITABLE_INTERNAL: [&[u8]; 4] = [
60    b"sqlite_sequence",
61    b"sqlite_stat1",
62    b"sqlite_schema",
63    b"sqlite_master",
64];
65
66/// Where one column's value comes from in an INSERT.
67#[derive(Clone, Debug, PartialEq)]
68pub enum ColumnSource {
69    /// The value at this position of the source row.
70    Row(usize),
71    /// An expression evaluated once per row, which is what a `DEFAULT` is.
72    Expr(BoundExpr),
73    /// A generated column, computed from the rest of the row rather than from
74    /// anything the statement supplied.
75    ///
76    /// It is its own variant because it is evaluated at a different *time*: a
77    /// `DEFAULT` is a value like any other, while a generated column reads the
78    /// row it is part of and so cannot be computed until the rest of it is.
79    Generated(BoundExpr),
80}
81
82/// What an INSERT inserts.
83#[derive(Clone, Debug, PartialEq)]
84pub enum BoundInsertSource {
85    /// Literal rows, each already bound.
86    Values(Vec<Vec<BoundExpr>>),
87    /// A query, whose result columns feed the target columns in order.
88    Select(Box<BoundSelect>),
89}
90
91/// One `CHECK` constraint, bound against its table.
92#[derive(Clone, Debug, PartialEq)]
93pub struct BoundCheck {
94    /// The constraint's name, when it was written with one.
95    pub name: Option<Vec<u8>>,
96    /// The predicate.
97    pub expr: BoundExpr,
98}
99
100/// A `NOT NULL` column's `DEFAULT`, bound so a `REPLACE` can stand it in.
101///
102/// **REPLACE's rule for a `NOT NULL` violation is to substitute the column's
103/// default, and to fall back to `ABORT` only when there is no default.** So
104/// `UPDATE OR REPLACE t SET c = NULL` on `c TEXT NOT NULL DEFAULT 'd'` stores
105/// `'d'`, and this engine used to refuse the statement instead.
106///
107/// The write path cannot bind one for itself: a default is schema text, and by
108/// the time a row is being checked the parser is long out of scope. The binder
109/// already binds one for every column a statement *omits*; these are the same
110/// expressions bound for the columns it supplies, which is where a NULL that
111/// needs replacing can come from.
112///
113/// Only the columns that can need it are here - `NOT NULL` and with a default -
114/// so an ordinary table carries an empty vector and the write path skips the
115/// whole apparatus.
116#[derive(Clone, Debug, PartialEq)]
117pub struct BoundDefault {
118    /// The column the default belongs to.
119    pub column: u16,
120    /// The default expression, bound.
121    pub expr: BoundExpr,
122}
123
124/// The expressions one index needs evaluated per row to be maintained.
125///
126/// **An index is usually just columns of the row, and then it needs none of
127/// this.** A partial index holds only the rows its predicate accepts, and an
128/// index on an expression holds a value no column carries - so for those two,
129/// maintaining the index means evaluating something per row rather than
130/// copying a slot. They travel on the bound statement for the same reason the
131/// table's `CHECK` predicates do: the binder is what can turn schema text into
132/// a `BoundExpr`, and the write path is what runs it.
133///
134/// The list holds only the indexes that need it, so a table with neither kind
135/// leaves it empty and the write path's loop runs zero times - which is every
136/// table the gate measures.
137#[derive(Clone, Debug, PartialEq)]
138pub struct BoundIndexExprs {
139    /// The index's position in the table's `indexes`.
140    pub position: usize,
141    /// The partial-index predicate, when it has one.
142    pub predicate: Option<BoundExpr>,
143    /// One per key column: the expression it indexes, or `None` for a column.
144    pub keys: Vec<Option<BoundExpr>>,
145}
146
147/// One statement of a trigger body, bound.
148///
149/// The four the grammar allows and no more. A trigger body is not a general
150/// statement list: it cannot create objects, cannot open transactions, and
151/// cannot return rows to the caller, so a variant for anything else would be a
152/// shape the binder is required to refuse.
153#[derive(Clone, Debug, PartialEq)]
154pub enum BoundTriggerStatement {
155    /// `INSERT`.
156    Insert(Box<BoundInsert>),
157    /// `UPDATE`.
158    Update(Box<BoundUpdate>),
159    /// `DELETE`.
160    Delete(Box<BoundDelete>),
161    /// `SELECT`, which a body runs for its side effects - in practice for the
162    /// `RAISE()` inside it.
163    Select(Box<BoundSelect>),
164}
165
166/// A trigger, bound against the write that fires it.
167///
168/// It is bound per statement rather than once per schema because the body's
169/// FROM terms take statement-wide source numbers, and those only exist relative
170/// to the statement they are inlined into.
171#[derive(Clone, Debug, PartialEq)]
172pub struct BoundTrigger {
173    /// The trigger's name, for the diagnostic when its body fails.
174    pub name: Vec<u8>,
175    /// The folded name of the table it is attached to.
176    ///
177    /// Read by the executor to decide whether a body statement is writing the
178    /// trigger's *own* table, which is what `PRAGMA recursive_triggers` is
179    /// about: with it on, such a write fires this trigger again.
180    pub table: Vec<u8>,
181    /// Whether it fires before or after the row is written.
182    pub time: ast::TriggerTime,
183    /// The `WHEN` guard, when one was written.
184    pub when: Option<BoundExpr>,
185    /// The body statements, in written order.
186    pub body: Vec<BoundTriggerStatement>,
187    /// Whether the binder synthesised this from a `REFERENCES` clause rather
188    /// than reading it from a `CREATE TRIGGER`.
189    ///
190    /// **Read by `DROP TABLE` (task-1979, F6).** Dropping a table with foreign
191    /// keys on runs an implicit `DELETE FROM` first, so the keys that reference
192    /// it are enforced - and SQLite's rule is that the implicit delete fires no
193    /// triggers of its own while still performing every foreign key action. A
194    /// delete bound for that purpose keeps the triggers this flag marks and
195    /// drops the rest.
196    pub foreign_key: bool,
197    /// Whether the foreign key this enforces has one table as both its child
198    /// and its parent.
199    ///
200    /// **Also read by `DROP TABLE` (task-1979, F6).** The implicit delete keeps
201    /// the foreign key triggers and drops this one, because emptying a table
202    /// cannot leave a row of that same table pointing at nothing - see
203    /// `ForeignKeyTrigger::self_referencing`, which is where the value comes
204    /// from. Always false on a trigger the schema wrote.
205    pub self_referencing: bool,
206}
207
208/// A bound `INSERT`.
209#[derive(Clone, Debug, PartialEq)]
210pub struct BoundInsert {
211    /// The table being written.
212    pub table: TableInfo,
213    /// The statement-wide number of the FROM term being written.
214    ///
215    /// It used to be implicitly zero, because a DML statement had exactly one
216    /// source. A trigger body is compiled into the statement that fires it, so
217    /// its target takes the next number after the firing statement's - and a
218    /// compiler that assumed zero read the wrong cursor for every fire after
219    /// the first.
220    pub target_source: usize,
221    /// Where each table column's value comes from, in column order.
222    pub columns: Vec<ColumnSource>,
223    /// Where the rowid comes from, when the statement supplies one.
224    pub rowid: Option<ColumnSource>,
225    /// Which value of the supplied row is the rowid, when the statement named
226    /// it outright.
227    ///
228    /// `INSERT INTO t(rowid, a) VALUES (7, 'x')` is legal on any rowid table,
229    /// including one with no `INTEGER PRIMARY KEY` to alias it and including a
230    /// virtual table. It is recorded separately from `rowid` because it is not
231    /// a column: nothing writes it into the record.
232    pub named_rowid: Option<usize>,
233    /// The rows.
234    pub source: BoundInsertSource,
235    /// How many values each source row supplies.
236    pub arity: usize,
237    /// The statement's conflict algorithm, when it wrote one.
238    pub on_conflict: Option<ConflictAction>,
239    /// The table's `CHECK` constraints.
240    pub checks: Vec<BoundCheck>,
241    /// The `DEFAULT`s a `REPLACE` may stand in for a NULL, by column.
242    pub not_null_defaults: Vec<BoundDefault>,
243    /// The expressions the table's partial and expression indexes need.
244    pub index_exprs: Vec<BoundIndexExprs>,
245    /// The `ON CONFLICT ... DO UPDATE` clause, when there is one.
246    pub upsert: Vec<BoundUpsert>,
247    /// `sqlite_sequence`'s root page, when the target is `AUTOINCREMENT`.
248    ///
249    /// Resolved here rather than in the compiler because it is a fact about the
250    /// catalog, and the catalog is what the binder holds. It is zero for every
251    /// other table, which is also what it reads as before the first
252    /// `AUTOINCREMENT` table in a database is created.
253    pub sequence_root: u32,
254    /// The `RETURNING` columns.
255    pub returning: Vec<BoundResultColumn>,
256    /// The triggers this write fires, in schema order.
257    pub triggers: Vec<BoundTrigger>,
258    /// The foreign-key actions a `REPLACE` fires for the row it removes.
259    ///
260    /// A `REPLACE` that deletes a row to make room for another is a delete,
261    /// and the keys pointing at that row have to be told. Written `DELETE`
262    /// triggers are *not* fired - that is SQLite's rule with its default
263    /// `recursive_triggers = off` - so these are only the ones a key implies.
264    pub replace_triggers: Vec<BoundTrigger>,
265}
266
267/// A bound `ON CONFLICT ... DO UPDATE` clause.
268#[derive(Clone, Debug, PartialEq)]
269pub struct BoundUpsert {
270    /// The conflict target columns, when written; empty means any constraint.
271    ///
272    /// Sorted, because a conflict target names a *set* of columns and
273    /// `ON CONFLICT(a,b)` and `ON CONFLICT(b,a)` name the same one. Matching
274    /// them against an index's columns is a set comparison, and sorting here
275    /// is what makes it one comparison rather than a search per column.
276    pub target: Vec<u16>,
277    /// The assignments, or empty for `DO NOTHING`.
278    pub assignments: Vec<BoundAssignment>,
279    /// Whether the action is `DO UPDATE`.
280    pub do_update: bool,
281    /// The `WHERE` on the `DO UPDATE`.
282    pub filter: Option<BoundExpr>,
283}
284
285/// One `SET` assignment.
286#[derive(Clone, Debug, PartialEq)]
287pub struct BoundAssignment {
288    /// The column being assigned, as a declared position.
289    pub column: u16,
290    /// Whether the assignment names the row's own rowid rather than a declared
291    /// column, in which case `column` says nothing.
292    ///
293    /// **`UPDATE t SET rowid = 100` was `no such column: rowid` (task-1979,
294    /// F9).** An assignment target was looked up with `column_position`, which
295    /// only knows the columns the table declares, and a table with no INTEGER
296    /// PRIMARY KEY declares none for its rowid. SQLite accepts all three
297    /// spellings of the rowid on either kind of table and moves the row to the
298    /// new key.
299    pub rowid: bool,
300    /// The new value.
301    pub value: BoundExpr,
302}
303
304/// A bound `UPDATE`.
305#[derive(Clone, Debug, PartialEq)]
306pub struct BoundUpdate {
307    /// The table being written.
308    pub table: TableInfo,
309    /// The statement-wide number of the FROM term being written.
310    ///
311    /// It used to be implicitly zero, because a DML statement had exactly one
312    /// source. A trigger body is compiled into the statement that fires it, so
313    /// its target takes the next number after the firing statement's - and a
314    /// compiler that assumed zero read the wrong cursor for every fire after
315    /// the first.
316    pub source: usize,
317    /// The extra FROM terms of an `UPDATE ... FROM`, in written order.
318    ///
319    /// **The rows being updated come from a join.** `UPDATE t SET v = s.v FROM s
320    /// WHERE s.a = t.a` is the shape a migration writes to copy a column across
321    /// tables, and the values it assigns are not expressions over the target
322    /// row: they read a *different* row, one the join found. So the query that
323    /// finds the keys carries these terms too, and projects the assigned values
324    /// beside the key; see [`BoundUpdate::from`], which is this field.
325    ///
326    /// Empty for every ordinary `UPDATE`, which is what keeps the wider row off
327    /// the path the gate's `txn.large` measures.
328    pub from: Vec<crate::bind::BoundSource>,
329    /// The assignments, in table column order with duplicates already refused.
330    pub assignments: Vec<BoundAssignment>,
331    /// The `STORED` generated columns, recomputed after the assignments.
332    ///
333    /// **A stored generated column is part of the row, so a row that is
334    /// rewritten rewrites it (task-1913).** It is never named in a `SET`, so
335    /// an `UPDATE` used to leave whatever was written when the row was
336    /// inserted: `c GENERATED ALWAYS AS (a + 1) STORED` still read 2 after
337    /// `UPDATE g SET a = 5`, where SQLite reads 6. The wrong value is on the
338    /// disk rather than in an answer, so a later read of the same file is
339    /// wrong too, and an index on the column indexes the stale value.
340    ///
341    /// A `VIRTUAL` column is not here: it has no slot in the record and is
342    /// computed when it is read, which is why only this half needed fixing.
343    ///
344    /// These are evaluated against the row *after* the assignments, which is
345    /// the one difference from [`BoundUpdate::assignments`] - those read the
346    /// before image so `SET a = b, b = a` swaps.
347    pub generated: Vec<BoundAssignment>,
348    /// The `WHERE` clause.
349    pub filter: Option<BoundExpr>,
350    /// The statement's conflict algorithm, when it wrote one.
351    pub on_conflict: Option<ConflictAction>,
352    /// The table's `CHECK` constraints.
353    pub checks: Vec<BoundCheck>,
354    /// The `DEFAULT`s a `REPLACE` may stand in for a NULL, by column.
355    pub not_null_defaults: Vec<BoundDefault>,
356    /// The expressions the table's partial and expression indexes need.
357    pub index_exprs: Vec<BoundIndexExprs>,
358    /// `INDEXED BY` or `NOT INDEXED` on the target, which the query that finds
359    /// the rows to change obeys; `inillucent_exec::dml::hint_target` puts it there.
360    pub index_hint: crate::bind::IndexChoice,
361    /// The `RETURNING` columns.
362    pub returning: Vec<BoundResultColumn>,
363    /// The `ORDER BY` that decides which rows a `LIMIT` keeps.
364    ///
365    /// Empty unless the statement wrote one, and then always with a `LIMIT`,
366    /// because the binder refuses an order with nothing to limit. It goes onto
367    /// the query that finds the rows to change, which is where SQLite puts it
368    /// too: a limited write is `WHERE rowid IN (SELECT rowid ... ORDER BY ...
369    /// LIMIT ...)` there.
370    pub order_by: Vec<BoundOrderTerm>,
371    /// The `LIMIT`.
372    pub limit: Option<BoundExpr>,
373    /// The `OFFSET`.
374    pub offset: Option<BoundExpr>,
375    /// The triggers this write fires, in schema order.
376    pub triggers: Vec<BoundTrigger>,
377    /// The rows to fire an `INSTEAD OF` trigger for, when the target is a view.
378    ///
379    /// A view has no rows of its own, so `OLD` has to come from running the
380    /// view. This is that query, with the statement's `WHERE` on it and one
381    /// result column per view column.
382    pub view_rows: Option<Box<BoundSelect>>,
383}
384
385/// A bound `DELETE`.
386#[derive(Clone, Debug, PartialEq)]
387pub struct BoundDelete {
388    /// The table being written.
389    pub table: TableInfo,
390    /// The expressions the table's partial and expression indexes need.
391    ///
392    /// A delete needs them too: an entry only comes out of a partial index if
393    /// the row was in it, and a key the index computed has to be recomputed to
394    /// be found.
395    pub index_exprs: Vec<BoundIndexExprs>,
396    /// `INDEXED BY` or `NOT INDEXED` on the target, as on [`BoundUpdate`].
397    pub index_hint: crate::bind::IndexChoice,
398    /// The statement-wide number of the FROM term being written.
399    ///
400    /// It used to be implicitly zero, because a DML statement had exactly one
401    /// source. A trigger body is compiled into the statement that fires it, so
402    /// its target takes the next number after the firing statement's - and a
403    /// compiler that assumed zero read the wrong cursor for every fire after
404    /// the first.
405    pub source: usize,
406    /// The `WHERE` clause.
407    pub filter: Option<BoundExpr>,
408    /// The `RETURNING` columns.
409    pub returning: Vec<BoundResultColumn>,
410    /// The `ORDER BY` that decides which rows a `LIMIT` keeps.
411    ///
412    /// Empty unless the statement wrote one, and then always with a `LIMIT`,
413    /// because the binder refuses an order with nothing to limit. It goes onto
414    /// the query that finds the rows to change, which is where SQLite puts it
415    /// too: a limited write is `WHERE rowid IN (SELECT rowid ... ORDER BY ...
416    /// LIMIT ...)` there.
417    pub order_by: Vec<BoundOrderTerm>,
418    /// The `LIMIT`.
419    pub limit: Option<BoundExpr>,
420    /// The `OFFSET`.
421    pub offset: Option<BoundExpr>,
422    /// The triggers this write fires, in schema order.
423    pub triggers: Vec<BoundTrigger>,
424    /// The rows to fire an `INSTEAD OF` trigger for, when the target is a view.
425    pub view_rows: Option<Box<BoundSelect>>,
426}
427
428/// Reports whether an `INSERT` can resolve a conflict by deleting a row.
429///
430/// Either the statement said so, or one of the table's own constraints did.
431/// It is asked before the delete's keys are bound, because binding them costs
432/// a parse and a bind each and the answer is no for almost every insert.
433fn can_replace(table: &TableInfo, statement: Option<ConflictAction>) -> bool {
434    if statement == Some(ConflictAction::Replace) {
435        return true;
436    }
437    table
438        .indexes
439        .iter()
440        .any(|index| index.conflict == Some(ConflictAction::Replace))
441        || table.columns.iter().any(|column| {
442            column.not_null_conflict == Some(ConflictAction::Replace)
443                || column.primary_key_conflict == Some(ConflictAction::Replace)
444        })
445}
446
447/// Reports whether an unusable key's fault is one this write has to report.
448///
449/// A child's write reports a missing parent; a parent's write reports a
450/// mismatch. A statement that touches neither side of the broken key does not
451/// have to care, which is why the fault is carried rather than raised when the
452/// schema was read.
453fn fault_applies(
454    planned: &crate::catalog_view::ForeignKeyTrigger,
455    event: &TriggerEventInfo,
456) -> bool {
457    match event {
458        TriggerEventInfo::Insert => planned.is_check,
459        TriggerEventInfo::Delete => !planned.is_check,
460        TriggerEventInfo::Update(_) => true,
461    }
462}
463
464/// Marks a synthesised body's aborts as the foreign key's rather than a
465/// trigger's.
466///
467/// The generated text says `RAISE(ABORT, ...)` because that is what a person
468/// would have written, and what a person writes reports
469/// `SQLITE_CONSTRAINT_TRIGGER`. A foreign key reports its own code, and the
470/// only difference between the two is which constraint asked - so it is set
471/// here, on the bodies this binder generated, and nowhere else.
472fn report_as_foreign_key(trigger: &mut BoundTrigger) {
473    trigger.foreign_key = true;
474    for statement in &mut trigger.body {
475        let BoundTriggerStatement::Select(select) = statement else {
476            continue;
477        };
478        for column in &mut select.columns {
479            if let BoundExpr::Raise { foreign_key, .. } = &mut column.expr {
480                *foreign_key = true;
481            }
482        }
483    }
484}
485
486/// Returns whether a view has an `INSTEAD OF` trigger for one event.
487fn has_instead_of(table: &TableInfo, event: &TriggerEventInfo) -> bool {
488    table
489        .triggers
490        .iter()
491        .any(|trigger| trigger.time == ast::TriggerTime::InsteadOf && trigger.fires_for(event, &[]))
492}
493
494/// The target position that stands for the rowid rather than a column.
495///
496/// A table cannot have this many columns - SQLite's limit is two thousand - so
497/// there is no position it can collide with, and one sentinel is cheaper than
498/// a parallel `Option` threaded through every target list.
499const ROWID_TARGET: u16 = u16::MAX;
500
501/// Returns whether a name is one of the rowid's three spellings.
502fn is_rowid_name(folded: &[u8]) -> bool {
503    matches!(folded, b"rowid" | b"oid" | b"_rowid_")
504}
505
506/// How deep one write may drive triggers firing other triggers.
507///
508/// SQLite's own limit is `SQLITE_MAX_TRIGGER_DEPTH`, enforced when the frame is
509/// pushed. Trigger bodies are inlined here rather than run as frames, so the
510/// same limit is enforced where the inlining happens - and it has to be, or a
511/// schema in which two triggers write each other's tables would compile until
512/// the compiler ran out of memory.
513///
514/// **This is one number now, and it is the one `.limit` reports.** There used
515/// to be two constants of this name: this one at 32, which was the number
516/// actually enforced, and `inillucent-exec`'s at 1000, checked at run time over
517/// a tree the binder had already capped at 32 - so that check could never fire.
518/// `crates/inillucent-base/manifests/limits.toml` advertised 1000 and
519/// `inillucent diagnose` printed 1000, and a chain of forty distinct triggers
520/// that the oracle ran was refused here (task-1946, H3). The binder reads
521/// `Limit::TriggerDepth` from the connection now, which `.limit trigger_depth`
522/// and the driver both set; this constant is what a binder built without limits
523/// falls back to, and it is the manifest's default.
524pub const MAX_TRIGGER_DEPTH: usize = 1000;
525
526/// How deep one chain of foreign-key actions may go.
527///
528/// A cascade reaches this only when the keys form a cycle, which in practice
529/// means a table whose parent column points at itself. SQLite's own limit is a
530/// run-time recursion depth; this one is a compile-time inlining depth, and it
531/// is smaller for that reason.
532pub const MAX_FOREIGN_KEY_DEPTH: usize = 64;
533
534/// How many foreign-key action bodies one statement may inline in total.
535///
536/// The depth limit alone is not enough: a table with three keys that all cycle
537/// would inline three bodies per level, so the limit that matters is the total.
538/// A chain, which is what a self-referencing tree produces, spends one per
539/// level and reaches the depth limit first.
540pub const MAX_FOREIGN_KEY_STATEMENTS: usize = 256;
541
542impl<'a> Binder<'a> {
543    /// Binds an `INSERT` or `REPLACE`.
544    pub fn bind_insert(&mut self, insert: &ast::Insert) -> Result<BoundInsert, ParseError> {
545        // **A `WITH` on a DML statement is the same `WITH` a `SELECT` has.** The
546        // CTEs are in scope for the whole statement - the source query of an
547        // `INSERT`, the `WHERE` of an `UPDATE` or `DELETE` - and the binder's
548        // CTE stack already handles nesting, so pushing them here is all it
549        // takes. They were refused rather than bound, which is what a migration
550        // script written for SQLite hits first.
551        let pushed = self.push_ctes(&insert.with)?;
552        let bound = self.bind_insert_body(insert);
553        if pushed {
554            self.pop_ctes();
555        }
556        bound
557    }
558
559    /// Binds an `INSERT` with its CTEs already in scope.
560    fn bind_insert_body(&mut self, insert: &ast::Insert) -> Result<BoundInsert, ParseError> {
561        let table = self.writable_target(
562            insert.database,
563            insert.table,
564            Span::default(),
565            &TriggerEventInfo::Insert,
566        )?;
567        let alias = match insert.alias {
568            Some(alias) => self.ast.text(alias).to_vec(),
569            None => table.name.clone(),
570        };
571        let target_source = self.push_write_source(table.clone(), alias);
572        // `DEFAULT VALUES` supplies nothing, so every column takes its default
573        // - which is what an empty target list means here. The grammar does
574        // not allow a column list with it, so there is none to honour.
575        let targets = match insert.source {
576            ast::InsertSource::DefaultValues => Vec::new(),
577            ast::InsertSource::Select(_) => self.insert_targets(&table, &insert.columns)?,
578        };
579        let (source, arity) = self.bind_insert_source(&insert.source, &table, &targets)?;
580        if arity != targets.len() {
581            return Err(refused(
582                format!("{} values for {} columns", arity, targets.len()),
583                Span::default(),
584            ));
585        }
586        let (columns, rowid) = self.column_sources(&table, &targets)?;
587        let named_rowid = targets.iter().position(|target| *target == ROWID_TARGET);
588        let checks = self.bind_checks(&table)?;
589        let not_null_defaults = self.bind_not_null_defaults(&table)?;
590        let index_exprs = self.bind_index_exprs(&table)?;
591        let upsert = self.bind_upsert(&table, insert)?;
592        let returning = self.bind_returning(&insert.returning)?;
593        let mut triggers = self.bind_triggers(&table, TriggerEventInfo::Insert, &[])?;
594        triggers.extend(self.bind_foreign_keys(&table, TriggerEventInfo::Insert, &[])?);
595        let replace_triggers = if can_replace(&table, insert.on_conflict) {
596            self.bind_foreign_keys(&table, TriggerEventInfo::Delete, &[])?
597        } else {
598            Vec::new()
599        };
600        let sequence_root = if table.autoincrement {
601            self.catalog
602                .find_table(None, b"sqlite_sequence")
603                .map_or(0, |sequence| sequence.root)
604        } else {
605            0
606        };
607        Ok(BoundInsert {
608            table,
609            index_exprs,
610            target_source,
611            columns,
612            rowid,
613            named_rowid,
614            source,
615            arity,
616            on_conflict: insert.on_conflict,
617            checks,
618            not_null_defaults,
619            upsert,
620            sequence_root,
621            returning,
622            triggers,
623            replace_triggers,
624        })
625    }
626
627    /// Binds an `UPDATE`.
628    pub fn bind_update(&mut self, update: &ast::Update) -> Result<BoundUpdate, ParseError> {
629        let pushed = self.push_ctes(&update.with)?;
630        let bound = self.bind_update_body(update);
631        if pushed {
632            self.pop_ctes();
633        }
634        bound
635    }
636
637    /// Binds an `UPDATE` with its CTEs already in scope.
638    fn bind_update_body(&mut self, update: &ast::Update) -> Result<BoundUpdate, ParseError> {
639        if let Some(refusal) = order_without_limit(update.limited_at, update.limit, "UPDATE") {
640            return Err(refusal);
641        }
642        let (table, source) =
643            self.write_target_from_term(update.target, &TriggerEventInfo::Update(Vec::new()))?;
644        // **The `FROM` terms are bound after the target**, so the target keeps
645        // the lowest source number and every reference to an unqualified column
646        // resolves to it first - which is SQLite's rule and the reason
647        // `UPDATE t SET v = v + 1 FROM s` means the target's `v`.
648        let before = self.sources.len();
649        for term in &update.from {
650            self.bind_from_term(*term)?;
651        }
652        let joined: Vec<crate::bind::BoundSource> =
653            self.sources.get(before..).unwrap_or(&[]).to_vec();
654        let mut assignments = Vec::new();
655        for (names, value) in &update.assignments {
656            let bound = self.bind_expr(*value)?;
657            for name in names {
658                let folded = self.ast.folded(*name).to_vec();
659                // `rowid`, `oid` and `_rowid_` name the row's key rather than a
660                // declared column, unless the table declares a column by one of
661                // those names - which is what `is_rowid_name` decides.
662                if table.is_rowid_name(&folded) {
663                    if assignments.iter().any(|held: &BoundAssignment| held.rowid) {
664                        return Err(refused(
665                            format!(
666                                "column {} is assigned twice",
667                                String::from_utf8_lossy(self.ast.text(*name))
668                            ),
669                            Span::default(),
670                        ));
671                    }
672                    assignments.push(BoundAssignment {
673                        column: 0,
674                        rowid: true,
675                        value: bound.clone(),
676                    });
677                    continue;
678                }
679                let Some(position) = table.column_position(&folded) else {
680                    return Err(no_such_column(self.ast.text(*name), Span::default()));
681                };
682                // **An assignment to a generated column is refused, not
683                // ignored (task-1913).** SQLite answers `cannot UPDATE
684                // generated column "c"`; this accepted the statement, reported
685                // it as a success, and wrote nothing the caller asked for -
686                // either the record took the value and the column stopped
687                // agreeing with its own expression, or the recompute above put
688                // it back and the assignment was silently dropped. `INSERT`
689                // already refused the same thing.
690                self.refuse_generated(&table, position, "UPDATE", Span::default())?;
691                if assignments
692                    .iter()
693                    .any(|existing: &BoundAssignment| existing.column == position)
694                {
695                    return Err(refused(
696                        format!(
697                            "column {} is assigned twice",
698                            String::from_utf8_lossy(self.ast.text(*name))
699                        ),
700                        Span::default(),
701                    ));
702                }
703                assignments.push(BoundAssignment {
704                    column: position,
705                    rowid: false,
706                    value: bound.clone(),
707                });
708            }
709        }
710        // The rowid assignment sorts with the declared columns rather than
711        // ahead of them, because `column` says nothing for it and the order
712        // only has to be stable.
713        assignments.sort_by_key(|assignment| (assignment.rowid, assignment.column));
714        let filter = match update.filter {
715            Some(expr) => Some(self.bind_expr(expr)?),
716            None => None,
717        };
718        let generated = self.bind_stored_generated(&table)?;
719        let checks = self.bind_checks(&table)?;
720        let not_null_defaults = self.bind_not_null_defaults(&table)?;
721        let index_exprs = self.bind_index_exprs(&table)?;
722        let returning = self.bind_returning(&update.returning)?;
723        // Bound as expressions, the way an aggregate's own `ORDER BY` is: a
724        // write has no result columns, so a bare integer names no ordinal.
725        let order_by = self.bind_aggregate_order(&update.order_by)?;
726        let limit = match update.limit {
727            Some(expr) => Some(self.bind_expr(expr)?),
728            None => None,
729        };
730        let offset = match update.offset {
731            Some(expr) => Some(self.bind_expr(expr)?),
732            None => None,
733        };
734        // The rowid is not a declared column, so no `UPDATE OF` trigger and no
735        // foreign key can be keyed on it and it contributes no name here.
736        let changed: Vec<Vec<u8>> = assignments
737            .iter()
738            .filter(|assignment| !assignment.rowid)
739            .filter_map(|assignment| table.column(assignment.column))
740            .map(|column| column.folded.clone())
741            .collect();
742        let mut triggers =
743            self.bind_triggers(&table, TriggerEventInfo::Update(Vec::new()), &changed)?;
744        triggers.extend(self.bind_foreign_keys(
745            &table,
746            TriggerEventInfo::Update(Vec::new()),
747            &changed,
748        )?);
749        let view_rows = self
750            .view_rows(&table, filter.clone())
751            .map(|rows| limit_view_rows(rows, &order_by, &limit, &offset));
752        let index_hint = self.write_hint(source, &index_exprs, filter.as_ref(), &joined)?;
753        Ok(BoundUpdate {
754            table,
755            index_exprs,
756            index_hint,
757            source,
758            from: joined,
759            assignments,
760            generated,
761            filter,
762            on_conflict: update.on_conflict,
763            checks,
764            not_null_defaults,
765            returning,
766            order_by,
767            limit,
768            offset,
769            triggers,
770            view_rows,
771        })
772    }
773
774    /// Binds a `DELETE`.
775    pub fn bind_delete(&mut self, delete: &ast::Delete) -> Result<BoundDelete, ParseError> {
776        let pushed = self.push_ctes(&delete.with)?;
777        let bound = self.bind_delete_body(delete);
778        if pushed {
779            self.pop_ctes();
780        }
781        bound
782    }
783
784    /// Binds a `DELETE` with its CTEs already in scope.
785    fn bind_delete_body(&mut self, delete: &ast::Delete) -> Result<BoundDelete, ParseError> {
786        if let Some(refusal) = order_without_limit(delete.limited_at, delete.limit, "DELETE") {
787            return Err(refusal);
788        }
789        let (table, source) =
790            self.write_target_from_term(delete.target, &TriggerEventInfo::Delete)?;
791        let index_exprs = self.bind_index_exprs(&table)?;
792        let filter = match delete.filter {
793            Some(expr) => Some(self.bind_expr(expr)?),
794            None => None,
795        };
796        let returning = self.bind_returning(&delete.returning)?;
797        let order_by = self.bind_aggregate_order(&delete.order_by)?;
798        let limit = match delete.limit {
799            Some(expr) => Some(self.bind_expr(expr)?),
800            None => None,
801        };
802        let offset = match delete.offset {
803            Some(expr) => Some(self.bind_expr(expr)?),
804            None => None,
805        };
806        let mut triggers = self.bind_triggers(&table, TriggerEventInfo::Delete, &[])?;
807        triggers.extend(self.bind_foreign_keys(&table, TriggerEventInfo::Delete, &[])?);
808        let view_rows = self
809            .view_rows(&table, filter.clone())
810            .map(|rows| limit_view_rows(rows, &order_by, &limit, &offset));
811        let index_hint = self.write_hint(source, &index_exprs, filter.as_ref(), &[])?;
812        Ok(BoundDelete {
813            table,
814            index_exprs,
815            index_hint,
816            source,
817            filter,
818            returning,
819            order_by,
820            limit,
821            offset,
822            triggers,
823            view_rows,
824        })
825    }
826
827    /// Binds the triggers one write fires, bodies and all.
828    ///
829    /// The bodies are bound here, into the same binder, so their FROM terms take
830    /// statement-wide source numbers alongside the write's own. That is what
831    /// lets the compiler inline them: a trigger body is not a separate program
832    /// with a separate cursor space, it is more of this statement.
833    ///
834    /// A trigger already being bound is skipped rather than bound again, which
835    /// is SQLite's behaviour with its default `recursive_triggers = off` and is
836    /// also the only reason inlining terminates.
837    ///
838    /// **Walked newest first.** `live.triggers` is in the order
839    /// `inillucent_catalog::paged::tables_from_entries` appended them while
840    /// reading `sqlite_schema` - the order the triggers were created in - and
841    /// SQLite fires two triggers of the same timing and event in the opposite
842    /// order: it keeps each table's trigger list with the most recently
843    /// created one first, so that one fires first.
844    /// `dml_differential.rs`'s `row_triggers_match_sqlite` has two `AFTER
845    /// INSERT` triggers on one table - `t_ai`, created first, and `t_high`,
846    /// created after it - and the pinned reference fires `t_high` before
847    /// `t_ai` on every insert. Reversing the walk here, once, at the one place
848    /// that reads `live.triggers` into a statement's own trigger list, is
849    /// enough: nothing downstream reorders it again.
850    fn bind_triggers(
851        &mut self,
852        table: &TableInfo,
853        event: TriggerEventInfo,
854        changed: &[Vec<u8>],
855    ) -> Result<Vec<BoundTrigger>, ParseError> {
856        // The catalog reference is copied out of `self` first: the trigger's
857        // arena has to outlive the binder for the body to be bound in place,
858        // and a borrow taken through `&self` would end at the first `&mut self`.
859        let catalog = self.catalog;
860        let database = catalog.database_name(table.database).to_vec();
861        let Some(live) = catalog.find_table(Some(database.as_slice()), &table.folded) else {
862            return Ok(Vec::new());
863        };
864        let (old, new) = match event {
865            TriggerEventInfo::Insert => (false, true),
866            TriggerEventInfo::Delete => (true, false),
867            TriggerEventInfo::Update(_) => (true, true),
868        };
869        let mut bound = Vec::new();
870        for trigger in live.triggers.iter().rev() {
871            if !trigger.fires_for(&event, changed) {
872                continue;
873            }
874            if self.firing.contains(&trigger.folded) {
875                continue;
876            }
877            if self.firing.len() >= self.trigger_depth {
878                // The number is in the message because a settable limit that
879                // refuses without saying what it was leaves a reader guessing
880                // between the default and whatever `.limit` last set.
881                return Err(refused(
882                    format!(
883                        "too many levels of trigger recursion: the limit is {}",
884                        self.trigger_depth
885                    ),
886                    Span::default(),
887                ));
888            }
889            self.firing.push(trigger.folded.clone());
890            let saved_ast = self.ast;
891            let saved_scopes = core::mem::take(&mut self.scopes);
892            let saved_aliases = self.row_aliases.take();
893            let saved_target = self.view_target.take();
894            // A trigger body is schema text: the statements in it were written
895            // by whoever wrote the file, and they run because a write happened
896            // rather than because anybody submitted them.
897            let saved_site = self.call_site;
898            self.call_site = crate::function::CallSite::Schema;
899            self.ast = &trigger.ast;
900            self.row_aliases = Some(crate::bind::RowAliases {
901                table: table.clone(),
902                old,
903                new,
904            });
905            let result = self.bind_trigger_body(trigger, table);
906            self.call_site = saved_site;
907            self.ast = saved_ast;
908            self.scopes = saved_scopes;
909            self.row_aliases = saved_aliases;
910            self.view_target = saved_target;
911            self.firing.pop();
912            bound.push(result?);
913        }
914        Ok(bound)
915    }
916
917    /// Binds the triggers this write's foreign keys imply.
918    ///
919    /// The triggers themselves were generated when the schema was read - both
920    /// directions of every key, since nothing in the file records the reverse
921    /// one. What is decided here is which of them apply: whether keys are
922    /// enforced at all, whether a check waits for the commit, and whether this
923    /// particular write touches the columns a check is about.
924    fn bind_foreign_keys(
925        &mut self,
926        table: &TableInfo,
927        event: TriggerEventInfo,
928        changed: &[Vec<u8>],
929    ) -> Result<Vec<BoundTrigger>, ParseError> {
930        if !self.foreign_keys || table.kind != TableKind::Table {
931            return Ok(Vec::new());
932        }
933        let catalog = self.catalog;
934        let database = catalog.database_name(table.database).to_vec();
935        let Some(live) = catalog.find_table(Some(database.as_slice()), &table.folded) else {
936            return Ok(Vec::new());
937        };
938        let mut bound = Vec::new();
939        for planned in &live.foreign_key_triggers {
940            if planned.is_check && (planned.deferred || self.defer_foreign_keys) {
941                continue;
942            }
943            let Some(trigger) = planned.trigger.as_ref() else {
944                if fault_applies(planned, &event) {
945                    return Err(crate::bind::schema_refused(
946                        String::from_utf8_lossy(&planned.fault).into_owned(),
947                        Span::default(),
948                    ));
949                }
950                continue;
951            };
952            if !trigger.fires_for(&event, changed) {
953                continue;
954            }
955            if self.firing_foreign_keys.contains(&trigger.folded) {
956                continue;
957            }
958            let mut one = self.bind_foreign_key_trigger(table, trigger, &event)?;
959            one.self_referencing = planned.self_referencing;
960            bound.push(one);
961        }
962        Ok(bound)
963    }
964
965    /// Binds one synthesised trigger, inside the recursion budget.
966    ///
967    /// The budget is spent here rather than where the trigger was generated,
968    /// because what a cascade costs is the *bound* body: one copy per level it
969    /// can reach, and it can reach itself only when the keys form a cycle.
970    fn bind_foreign_key_trigger(
971        &mut self,
972        table: &TableInfo,
973        trigger: &'a TriggerInfo,
974        event: &TriggerEventInfo,
975    ) -> Result<BoundTrigger, ParseError> {
976        if self.foreign_key_depth >= MAX_FOREIGN_KEY_DEPTH || self.foreign_key_budget == 0 {
977            return Err(refused(
978                "too many levels of foreign key recursion",
979                Span::default(),
980            ));
981        }
982        self.foreign_key_depth = self.foreign_key_depth.saturating_add(1);
983        self.foreign_key_budget = self.foreign_key_budget.saturating_sub(1);
984        self.firing_foreign_keys.push(trigger.folded.clone());
985        let (old, new) = match event {
986            TriggerEventInfo::Insert => (false, true),
987            TriggerEventInfo::Delete => (true, false),
988            TriggerEventInfo::Update(_) => (true, true),
989        };
990        let saved_ast = self.ast;
991        let saved_scopes = core::mem::take(&mut self.scopes);
992        let saved_aliases = self.row_aliases.take();
993        let saved_target = self.view_target.take();
994        // A synthesised key action is generated from a `REFERENCES` clause the
995        // schema wrote, so it is schema too - the same site a written trigger
996        // gets, because the binder turns both into the same text.
997        let saved_site = self.call_site;
998        self.call_site = crate::function::CallSite::Schema;
999        self.ast = &trigger.ast;
1000        self.row_aliases = Some(crate::bind::RowAliases {
1001            table: table.clone(),
1002            old,
1003            new,
1004        });
1005        let result = self.bind_trigger_body(trigger, table);
1006        self.call_site = saved_site;
1007        self.ast = saved_ast;
1008        self.scopes = saved_scopes;
1009        self.row_aliases = saved_aliases;
1010        self.view_target = saved_target;
1011        self.foreign_key_depth = self.foreign_key_depth.saturating_sub(1);
1012        self.firing_foreign_keys.pop();
1013        let mut bound = result?;
1014        report_as_foreign_key(&mut bound);
1015        Ok(bound)
1016    }
1017
1018    /// Binds one trigger's guard and body statements.
1019    fn bind_trigger_body(
1020        &mut self,
1021        trigger: &TriggerInfo,
1022        table: &TableInfo,
1023    ) -> Result<BoundTrigger, ParseError> {
1024        let when = match trigger.when {
1025            Some(expr) => Some(self.bind_expr(expr)?),
1026            None => None,
1027        };
1028        let mut body = Vec::new();
1029        for statement in &trigger.body {
1030            // Each statement gets a fresh scope stack. A body statement's names
1031            // resolve against its own tables and against OLD and NEW, never
1032            // outward into the statement that fired it.
1033            let saved = core::mem::take(&mut self.scopes);
1034            let one = self.bind_trigger_statement(statement);
1035            self.scopes = saved;
1036            body.push(one?);
1037        }
1038        Ok(BoundTrigger {
1039            name: trigger.name.clone(),
1040            table: table.folded.clone(),
1041            time: trigger.time,
1042            when,
1043            body,
1044            foreign_key: false,
1045            self_referencing: false,
1046        })
1047    }
1048
1049    /// Binds one statement of a trigger body.
1050    pub(crate) fn bind_trigger_statement(
1051        &mut self,
1052        statement: &ast::Statement,
1053    ) -> Result<BoundTriggerStatement, ParseError> {
1054        match statement {
1055            ast::Statement::Insert(insert) => {
1056                if !insert.returning.is_empty() {
1057                    return Err(refused(
1058                        "RETURNING is not allowed on a trigger body statement",
1059                        Span::default(),
1060                    ));
1061                }
1062                Ok(BoundTriggerStatement::Insert(Box::new(
1063                    self.bind_insert(insert)?,
1064                )))
1065            }
1066            ast::Statement::Update(update) => {
1067                if !update.returning.is_empty() {
1068                    return Err(refused(
1069                        "RETURNING is not allowed on a trigger body statement",
1070                        Span::default(),
1071                    ));
1072                }
1073                Ok(BoundTriggerStatement::Update(Box::new(
1074                    self.bind_update(update)?,
1075                )))
1076            }
1077            ast::Statement::Delete(delete) => {
1078                if !delete.returning.is_empty() {
1079                    return Err(refused(
1080                        "RETURNING is not allowed on a trigger body statement",
1081                        Span::default(),
1082                    ));
1083                }
1084                Ok(BoundTriggerStatement::Delete(Box::new(
1085                    self.bind_delete(delete)?,
1086                )))
1087            }
1088            ast::Statement::Select(select) => Ok(BoundTriggerStatement::Select(Box::new(
1089                self.bind_select(*select)?,
1090            ))),
1091            _ => Err(unsupported(
1092                "that statement in a trigger body",
1093                Span::default(),
1094            )),
1095        }
1096    }
1097
1098    /// Resolves a write target and refuses the things that cannot be written.
1099    fn writable_target(
1100        &mut self,
1101        database: Option<ast::NameId>,
1102        name: ast::NameId,
1103        span: Span,
1104        event: &TriggerEventInfo,
1105    ) -> Result<TableInfo, ParseError> {
1106        let qualifier = database.map(|id| self.ast.folded(id).to_vec());
1107        let folded = self.ast.folded(name).to_vec();
1108        let Some(table) = self
1109            .catalog
1110            .find_table(qualifier.as_deref(), &folded)
1111            .cloned()
1112        else {
1113            return Err(crate::bind::no_such_table(self.ast.text(name), span));
1114        };
1115        match table.kind {
1116            TableKind::View => {
1117                // A view is writable exactly when it has an `INSTEAD OF`
1118                // trigger for this event: the trigger *is* the write, and the
1119                // view itself is never touched.
1120                if !has_instead_of(&table, event) {
1121                    return Err(unsupported("writing to a view", span));
1122                }
1123                let expanded = self.expanded_view(&table, span)?;
1124                self.record_write_dependency(table.database);
1125                return Ok(expanded);
1126            }
1127            TableKind::Virtual => {
1128                // A module decides whether it can be written; a module that
1129                // cannot refuses the call rather than the statement, because
1130                // "this table is read-only" is the module's fact and not the
1131                // binder's. What the binder still checks is that the table has
1132                // a module at all - a virtual table this build has no module
1133                // for has no columns either, and nothing can be written to it.
1134                if table.columns.is_empty() {
1135                    return Err(unsupported("that virtual table's module", span));
1136                }
1137                self.record_write_dependency(table.database);
1138                return Ok(table);
1139            }
1140            TableKind::Subquery => return Err(unsupported("writing to a subquery", span)),
1141            TableKind::Table => {}
1142        }
1143        if table.folded.starts_with(b"sqlite_")
1144            && !WRITABLE_INTERNAL.contains(&table.folded.as_slice())
1145        {
1146            return Err(unsupported(
1147                "writing to a table whose name begins with sqlite_",
1148                span,
1149            ));
1150        }
1151        self.record_write_dependency(table.database);
1152        Ok(table)
1153    }
1154
1155    /// Resolves the target of an UPDATE or DELETE, which is a FROM term.
1156    fn write_target_from_term(
1157        &mut self,
1158        id: ast::FromTermId,
1159        event: &TriggerEventInfo,
1160    ) -> Result<(TableInfo, usize), ParseError> {
1161        let Some(term) = self.ast.from_term(id) else {
1162            return Err(unsupported("missing target", Span::default()));
1163        };
1164        let ast::FromSource::Table {
1165            database,
1166            name,
1167            indexed_by,
1168            ..
1169        } = term.source
1170        else {
1171            return Err(unsupported("a target that is not a table", term.span));
1172        };
1173        let table = self.writable_target(database, name, term.span, event)?;
1174        // The same rule as a SELECT's: an `INDEXED BY` that names no index of
1175        // the table is refused rather than ignored (task-1979, F7). This path
1176        // has the table in hand rather than a bound source, so it asks the
1177        // table directly.
1178        if let ast::IndexHint::IndexedBy(index) = indexed_by {
1179            let folded = self.ast.folded(index).to_vec();
1180            if !table.indexes.iter().any(|held| held.folded == folded) {
1181                return Err(crate::bind::no_such_index(self.ast.text(index), term.span));
1182            }
1183        }
1184        let alias = match term.alias {
1185            Some(alias) => self.ast.text(alias).to_vec(),
1186            None => table.name.clone(),
1187        };
1188        if table.kind == TableKind::View {
1189            // The view goes in as an ordinary nested query, so the statement's
1190            // WHERE and SET bind against the view's own columns and against the
1191            // term the block producing OLD will iterate. Binding first and
1192            // re-pointing afterwards would be two chances to disagree.
1193            let inner = self.view_query(&table, term.span)?;
1194            let source = BoundSource {
1195                index_hint: crate::bind::IndexChoice::Any,
1196                id: self.sources.len(),
1197                rows: crate::bind::SourceRows::Subquery(Box::new(inner)),
1198                table: std::rc::Rc::new(table.clone()),
1199                alias,
1200                join: ast::JoinKind::Comma,
1201                constraint: None,
1202                suppressed: Vec::new(),
1203                index_exprs: Vec::new(),
1204            };
1205            self.view_target = Some(source.id);
1206            let scope = source.id;
1207            self.sources.push(source);
1208            self.scopes.push(vec![scope]);
1209            return Ok((table, scope));
1210        }
1211        let scope = self.push_write_source(table.clone(), alias);
1212        let choice = self.index_choice(indexed_by);
1213        if let Some(source) = self.sources.get_mut(scope) {
1214            source.index_hint = choice;
1215        }
1216        Ok((table, scope))
1217    }
1218
1219    /// Returns a view's `TableInfo` with the columns its body produces.
1220    ///
1221    /// A view's catalog entry carries no column list - its columns are whatever
1222    /// binding its `SELECT` says they are - so a statement that writes one needs
1223    /// the body bound before `new.column` can resolve to anything at all.
1224    pub(crate) fn expanded_view(
1225        &mut self,
1226        table: &TableInfo,
1227        span: Span,
1228    ) -> Result<TableInfo, ParseError> {
1229        let bound = self.view_query(table, span)?;
1230        let mut expanded = table.clone();
1231        expanded.columns = crate::bind::subquery_columns(&bound, &[]);
1232        Ok(expanded)
1233    }
1234
1235    /// Binds a view's body, out of the arena the catalog snapshot holds.
1236    fn view_query(&mut self, table: &TableInfo, span: Span) -> Result<BoundSelect, ParseError> {
1237        let catalog = self.catalog;
1238        let database = catalog.database_name(table.database).to_vec();
1239        let Some(live) = catalog.find_table(Some(database.as_slice()), &table.folded) else {
1240            return Err(crate::bind::no_such_table(&table.name, span));
1241        };
1242        let Some(body) = live.view.as_ref() else {
1243            return Err(unsupported(
1244                "a view whose definition could not be parsed",
1245                span,
1246            ));
1247        };
1248        let names = body.columns.clone();
1249        let saved_ast = self.ast;
1250        let saved_scopes = core::mem::take(&mut self.scopes);
1251        self.ast = &body.ast;
1252        let bound = self.bind_select(body.select);
1253        self.ast = saved_ast;
1254        self.scopes = saved_scopes;
1255        let mut bound = bound?;
1256        // `CREATE VIEW v (a, b)` renames the body's columns, and those are the
1257        // names `new.a` resolves against.
1258        for (position, name) in names.iter().enumerate() {
1259            if let Some(column) = bound.columns.get_mut(position) {
1260                column.name = name.clone();
1261            }
1262        }
1263        Ok(bound)
1264    }
1265
1266    /// Builds the block whose rows an `INSTEAD OF UPDATE` or `DELETE` fires for.
1267    ///
1268    /// It reads the term `write_target_from_term` already pushed, so the filter
1269    /// handed in here - bound against that same term - needs no adjustment.
1270    fn view_rows(
1271        &mut self,
1272        table: &TableInfo,
1273        filter: Option<BoundExpr>,
1274    ) -> Option<Box<BoundSelect>> {
1275        // The kind is checked before the target is taken. A trigger body's own
1276        // UPDATE binds through here too, and taking first meant the body's
1277        // statement - whose target is an ordinary table - consumed the view
1278        // target belonging to the statement that fired it, which then compiled
1279        // as a write to a view's root page of zero.
1280        if table.kind != TableKind::View {
1281            return None;
1282        }
1283        let id = self.view_target.take()?;
1284        let source = self.sources.get(id)?.clone();
1285        let columns = table
1286            .columns
1287            .iter()
1288            .enumerate()
1289            .map(|(position, column)| BoundResultColumn {
1290                expr: BoundExpr::Column {
1291                    source: id,
1292                    column: position as u16,
1293                    slot: position as u16,
1294                    affinity: column.affinity,
1295                    collation: Collation::from_name(
1296                        core::str::from_utf8(&column.collation).unwrap_or("BINARY"),
1297                    )
1298                    .unwrap_or(Collation::Binary),
1299                },
1300                name: column.name.clone(),
1301                origin: None,
1302                declared_type: column.declared_type.clone(),
1303            })
1304            .collect();
1305        Some(Box::new(crate::bind::block_over(source, filter, columns)))
1306    }
1307
1308    /// Returns the target's index hint, or refuses a write whose `INDEXED BY`
1309    /// index cannot find its rows.
1310    ///
1311    /// The same rule and the same test a `SELECT` gets from
1312    /// `crate::bind::refuse_unanswerable_hints`, asked of the query the write
1313    /// will run to find its rows: the target, any `UPDATE ... FROM` terms, and
1314    /// the statement's `WHERE`. The pinned 3.53.4 shell refuses
1315    /// `DELETE FROM h INDEXED BY h_part WHERE a = 1`, where `h_part` is declared
1316    /// `WHERE c > 3`, with `no query solution`.
1317    /// @param source - the target's statement-wide number
1318    /// @param index_exprs - the target's bound index expressions
1319    /// @param filter - the statement's `WHERE`
1320    /// @param joined - the `UPDATE ... FROM` terms, empty for a `DELETE`
1321    fn write_hint(
1322        &self,
1323        source: usize,
1324        index_exprs: &[BoundIndexExprs],
1325        filter: Option<&BoundExpr>,
1326        joined: &[BoundSource],
1327    ) -> Result<crate::bind::IndexChoice, ParseError> {
1328        let Some(target) = self.sources.get(source) else {
1329            return Ok(crate::bind::IndexChoice::Any);
1330        };
1331        if target.index_hint == crate::bind::IndexChoice::Any {
1332            return Ok(crate::bind::IndexChoice::Any);
1333        }
1334        let mut probe = target.clone();
1335        probe.index_exprs = index_exprs.to_vec();
1336        let mut block = crate::bind::block_over(probe, filter.cloned(), Vec::new());
1337        block.sources.extend(joined.iter().cloned());
1338        if crate::plan::unanswerable_index_hint(&block).is_some() {
1339            return Err(crate::bind::no_query_solution(Span::default()));
1340        }
1341        Ok(target.index_hint.clone())
1342    }
1343
1344    /// Makes the target table the statement's one visible source.
1345    ///
1346    /// It opens a scope holding just the target, so every name in the
1347    /// statement's `SET`, `WHERE` and `RETURNING` resolves against the table
1348    /// being written and nothing else.
1349    fn push_write_source(&mut self, table: TableInfo, alias: Vec<u8>) -> usize {
1350        let id = self.sources.len();
1351        self.sources.push(BoundSource {
1352            index_hint: crate::bind::IndexChoice::Any,
1353            id,
1354            rows: crate::bind::SourceRows::Table,
1355            table: std::rc::Rc::new(table),
1356            alias,
1357            join: ast::JoinKind::Comma,
1358            constraint: None,
1359            suppressed: Vec::new(),
1360            index_exprs: Vec::new(),
1361        });
1362        self.scopes.push(vec![id]);
1363        id
1364    }
1365
1366    /// Refuses an attempt to write a generated column.
1367    ///
1368    /// SQLite's message names the column, because the usual cause is a script
1369    /// that inserts every column of a table one of whose columns has since been
1370    /// made generated. It names the statement too - `INSERT` or `UPDATE` - and
1371    /// so does this.
1372    ///
1373    /// @param table - the table being written
1374    /// @param position - the column the statement named
1375    /// @param verb - `INSERT into` or `UPDATE`, as SQLite writes it
1376    /// @param span - where the name was written
1377    fn refuse_generated(
1378        &self,
1379        table: &TableInfo,
1380        position: u16,
1381        verb: &str,
1382        span: Span,
1383    ) -> Result<(), ParseError> {
1384        let Some(column) = table.column(position) else {
1385            return Ok(());
1386        };
1387        if !column.generated {
1388            return Ok(());
1389        }
1390        Err(refused(
1391            format!(
1392                "cannot {verb} generated column \"{}\"",
1393                String::from_utf8_lossy(&column.name)
1394            ),
1395            span,
1396        ))
1397    }
1398
1399    /// Returns the target column positions an INSERT writes, in source order.
1400    ///
1401    /// With no column list the targets are every column in declaration order,
1402    /// which is why adding a column to a table changes what a positional
1403    /// INSERT means - SQLite's behaviour, and the reason the column list is
1404    /// worth writing.
1405    fn insert_targets(
1406        &self,
1407        table: &TableInfo,
1408        columns: &[ast::NameId],
1409    ) -> Result<Vec<u16>, ParseError> {
1410        if columns.is_empty() {
1411            // A bare `INSERT INTO t VALUES (...)` supplies the columns a person
1412            // can write, which is every column that is not generated - so a
1413            // table with a generated column takes fewer values than it has
1414            // columns, exactly as SQLite counts them.
1415            // A hidden column is not one of them either: a module's arguments
1416            // and its `rank` are named by an application that wants them, and
1417            // an `INSERT INTO fts VALUES ('a', 'b')` supplies the two indexed
1418            // columns and nothing else.
1419            return Ok((0..table.columns.len() as u16)
1420                .filter(|position| {
1421                    table
1422                        .column(*position)
1423                        .is_some_and(|column| !column.generated && !column.hidden)
1424                })
1425                .collect());
1426        }
1427        let mut targets = Vec::with_capacity(columns.len());
1428        for name in columns {
1429            let folded = self.ast.folded(*name).to_vec();
1430            let position = match table.column_position(&folded) {
1431                Some(position) => position,
1432                // A rowid table lets the statement name its rowid, under any
1433                // of its three spellings, and that is not a column: it is the
1434                // key. A declared column of the same name wins, which is why
1435                // this is the fallback rather than the first thing tried.
1436                None if table.has_rowid() && is_rowid_name(&folded) => ROWID_TARGET,
1437                None => return Err(no_such_column(self.ast.text(*name), Span::default())),
1438            };
1439            if targets.contains(&position) {
1440                return Err(refused(
1441                    format!(
1442                        "column {} is named twice",
1443                        String::from_utf8_lossy(self.ast.text(*name))
1444                    ),
1445                    Span::default(),
1446                ));
1447            }
1448            if position != ROWID_TARGET {
1449                self.refuse_generated(table, position, "INSERT into", Span::default())?;
1450            }
1451            targets.push(position);
1452        }
1453        Ok(targets)
1454    }
1455
1456    /// Binds the rows an INSERT supplies.
1457    fn bind_insert_source(
1458        &mut self,
1459        source: &ast::InsertSource,
1460        table: &TableInfo,
1461        targets: &[u16],
1462    ) -> Result<(BoundInsertSource, usize), ParseError> {
1463        match source {
1464            ast::InsertSource::DefaultValues => {
1465                let _ = (table, targets);
1466                Ok((BoundInsertSource::Values(vec![Vec::new()]), 0))
1467            }
1468            ast::InsertSource::Select(id) => {
1469                // The target table is source zero while the rows are bound, so
1470                // that `INSERT INTO t SELECT ... FROM u` resolves `u`'s columns
1471                // and not `t`'s. Binding a SELECT replaces the source list, and
1472                // the target is pushed back afterwards.
1473                // The scope stack is emptied rather than pushed to, because a
1474                // pushed scope would still be searched *outward* into the
1475                // target's, and `INSERT INTO t SELECT a FROM u` would then
1476                // resolve `a` against `t` when `u` has no such column.
1477                let saved = core::mem::take(&mut self.scopes);
1478                let select = self.bind_select(*id);
1479                let bound = match select {
1480                    Ok(bound) => bound,
1481                    Err(error) => {
1482                        self.scopes = saved;
1483                        return Err(error);
1484                    }
1485                };
1486                self.scopes = saved;
1487                if bound.values.is_empty() {
1488                    let arity = bound.columns.len();
1489                    return Ok((BoundInsertSource::Select(Box::new(bound)), arity));
1490                }
1491                let arity = bound.values.first().map_or(0, Vec::len);
1492                for row in &bound.values {
1493                    if row.len() != arity {
1494                        return Err(unsupported(
1495                            "all VALUES rows must have the same number of columns",
1496                            Span::default(),
1497                        ));
1498                    }
1499                }
1500                Ok((BoundInsertSource::Values(bound.values), arity))
1501            }
1502        }
1503    }
1504
1505    /// Works out where every table column's value comes from.
1506    ///
1507    /// A column the statement named takes its value from the source row; a
1508    /// column it did not takes its `DEFAULT`, and a column with no default
1509    /// takes NULL. The rowid is separated out here rather than in the
1510    /// compiler, because an `INTEGER PRIMARY KEY` column *is* the rowid and
1511    /// writing it into the record as well would store a duplicate that SQLite
1512    /// does not.
1513    fn column_sources(
1514        &mut self,
1515        table: &TableInfo,
1516        targets: &[u16],
1517    ) -> Result<(Vec<ColumnSource>, Option<ColumnSource>), ParseError> {
1518        let mut columns = Vec::with_capacity(table.columns.len());
1519        for position in 0..table.columns.len() as u16 {
1520            if let Some(expr) = self.generated_expr(table, position)? {
1521                columns.push(ColumnSource::Generated(expr));
1522                continue;
1523            }
1524            let source = match targets.iter().position(|target| *target == position) {
1525                Some(index) => ColumnSource::Row(index),
1526                None => ColumnSource::Expr(self.default_expr(table, position)?),
1527            };
1528            columns.push(source);
1529        }
1530        let rowid = match table.rowid_alias {
1531            Some(position) => columns.get(position as usize).cloned(),
1532            None => None,
1533        };
1534        Ok((columns, rowid))
1535    }
1536
1537    /// Binds a generated column's expression, when the column is one.
1538    fn generated_expr(
1539        &mut self,
1540        table: &TableInfo,
1541        position: u16,
1542    ) -> Result<Option<BoundExpr>, ParseError> {
1543        let Some(column) = table.column(position) else {
1544            return Ok(None);
1545        };
1546        if !column.generated {
1547            return Ok(None);
1548        }
1549        let Some(sql) = column.generated_sql.clone() else {
1550            return Ok(Some(BoundExpr::Null));
1551        };
1552        Ok(Some(self.bind_schema_expr(&sql)?))
1553    }
1554
1555    /// Binds every `STORED` generated column's expression.
1556    ///
1557    /// Returns them as assignments, because that is what they are on the write
1558    /// path: a value the statement did not write and the row has to carry. See
1559    /// [`BoundUpdate::generated`] for why an `UPDATE` needs them and a
1560    /// `VIRTUAL` column does not.
1561    ///
1562    /// @param table - the table being written
1563    fn bind_stored_generated(
1564        &mut self,
1565        table: &TableInfo,
1566    ) -> Result<Vec<BoundAssignment>, ParseError> {
1567        let mut generated = Vec::new();
1568        for position in 0..table.columns.len() as u16 {
1569            let Some(column) = table.column(position) else {
1570                continue;
1571            };
1572            if !column.generated || !column.stored {
1573                continue;
1574            }
1575            let Some(expr) = self.generated_expr(table, position)? else {
1576                continue;
1577            };
1578            generated.push(BoundAssignment {
1579                column: position,
1580                rowid: false,
1581                value: expr,
1582            });
1583        }
1584        Ok(generated)
1585    }
1586
1587    /// Binds a column's `DEFAULT`, or NULL when it has none.
1588    fn default_expr(&mut self, table: &TableInfo, position: u16) -> Result<BoundExpr, ParseError> {
1589        let Some(column) = table.column(position) else {
1590            return Ok(BoundExpr::Null);
1591        };
1592        let Some(sql) = column.default_sql.as_ref() else {
1593            return Ok(BoundExpr::Null);
1594        };
1595        if sql.is_empty() {
1596            return Ok(BoundExpr::Null);
1597        }
1598        self.bind_schema_expr(sql)
1599    }
1600
1601    /// Binds the `DEFAULT` of every `NOT NULL` column that declares one.
1602    ///
1603    /// What `REPLACE` substitutes for a NULL in such a column - see
1604    /// [`BoundDefault`]. A column with no default is left out, which is what
1605    /// makes the write path's fallback to `ABORT` the absence of an entry
1606    /// rather than a second test.
1607    ///
1608    /// The rowid alias is left out too: the row image carries the key the
1609    /// statement is about to allocate, and the write path does not check it.
1610    ///
1611    /// @param table - the table being written
1612    fn bind_not_null_defaults(
1613        &mut self,
1614        table: &TableInfo,
1615    ) -> Result<Vec<BoundDefault>, ParseError> {
1616        let mut defaults = Vec::new();
1617        for (position, column) in table.columns.iter().enumerate() {
1618            if !column.not_null || Some(position as u16) == table.rowid_alias {
1619                continue;
1620            }
1621            let Some(sql) = column.default_sql.as_ref() else {
1622                continue;
1623            };
1624            if sql.is_empty() {
1625                continue;
1626            }
1627            let expr = self.bind_schema_expr(&sql.clone())?;
1628            defaults.push(BoundDefault {
1629                column: position as u16,
1630                expr,
1631            });
1632        }
1633        Ok(defaults)
1634    }
1635
1636    /// Binds every `CHECK` the table declares.
1637    fn bind_checks(&mut self, table: &TableInfo) -> Result<Vec<BoundCheck>, ParseError> {
1638        let mut checks = Vec::with_capacity(table.checks.len());
1639        for check in &table.checks {
1640            checks.push(BoundCheck {
1641                name: check.name.clone(),
1642                expr: self.bind_schema_expr(&check.expr_sql)?,
1643            });
1644        }
1645        Ok(checks)
1646    }
1647
1648    /// Binds the expressions the table's indexes need per row.
1649    ///
1650    /// Only the indexes that need any: a partial one, and one with an
1651    /// expression key. Everything else is a slot of the row and needs nothing.
1652    ///
1653    /// @param table - the table being written
1654    fn bind_index_exprs(&mut self, table: &TableInfo) -> Result<Vec<BoundIndexExprs>, ParseError> {
1655        let mut bound = Vec::new();
1656        for (position, index) in table.indexes.iter().enumerate() {
1657            let needs = index.partial_sql.is_some()
1658                || index.columns.iter().any(|key| key.expr_sql.is_some());
1659            if !needs {
1660                continue;
1661            }
1662            let predicate = match index.partial_sql.as_ref() {
1663                Some(sql) => Some(self.bind_schema_expr(sql)?),
1664                None => None,
1665            };
1666            let mut keys = Vec::with_capacity(index.columns.len());
1667            for key in &index.columns {
1668                keys.push(match key.expr_sql.as_ref() {
1669                    Some(sql) => Some(self.bind_schema_expr(sql)?),
1670                    None => None,
1671                });
1672            }
1673            bound.push(BoundIndexExprs {
1674                position,
1675                predicate,
1676                keys,
1677            });
1678        }
1679        Ok(bound)
1680    }
1681
1682    /// Parses and binds an expression that was written in the schema.
1683    ///
1684    /// It is parsed into its own arena and bound against the statement's
1685    /// current sources, so the result is an ordinary `BoundExpr` that refers to
1686    /// the target table by position and carries no reference to the schema
1687    /// text it came from.
1688    pub fn bind_schema_expr(&mut self, sql: &[u8]) -> Result<BoundExpr, ParseError> {
1689        let limits = Limits::default();
1690        let (ast, expr) = parse_expression(sql, &limits)?;
1691        let mut nested = Binder::new(self.catalog, &ast, self.authorizer);
1692        nested.trigger_depth = self.trigger_depth;
1693        // **This is where a `DEFAULT`, a `CHECK`, a generated column, an index
1694        // expression and a partial-index predicate all become a bound tree, so
1695        // it is where all five are told they are a schema (task-1972).** The
1696        // nested binder also inherits the connection's registrations and
1697        // collations, which it did not before: without the registrations
1698        // `bind_external_call` never sees the call at all, because the name
1699        // does not resolve to a registered function and the expression fails as
1700        // "no such function" - an error for the wrong reason, and one that
1701        // disappears the moment an application registers the same name at a
1702        // different arity.
1703        nested.externals = self.externals;
1704        nested.collations = self.collations;
1705        nested.trusted_schema = self.trusted_schema;
1706        nested.call_site = crate::function::CallSite::Schema;
1707        nested.sources = self.sources.clone();
1708        nested.scopes = self.scopes.clone();
1709        let bound = nested.bind_expr(expr)?;
1710        Ok(bound)
1711    }
1712
1713    /// Binds an `ON CONFLICT` clause.
1714    fn bind_upsert(
1715        &mut self,
1716        table: &TableInfo,
1717        insert: &ast::Insert,
1718    ) -> Result<Vec<BoundUpsert>, ParseError> {
1719        if insert.upserts.is_empty() {
1720            return Ok(Vec::new());
1721        }
1722        // **Every clause is bound, in written order.** A statement may carry
1723        // several - `ON CONFLICT(k) DO UPDATE ... ON CONFLICT(id) DO UPDATE ...`
1724        // - and which one runs is decided at *run time*, by which constraint
1725        // the row actually collided with. Binding only the first was the whole
1726        // of the old refusal.
1727        for upsert in &insert.upserts {
1728            if upsert.target_filter.is_some() {
1729                return Err(unsupported(
1730                    "a partial-index conflict target",
1731                    Span::default(),
1732                ));
1733            }
1734        }
1735        // A clause with no conflict target matches any constraint, so anything
1736        // written after it could never run. SQLite refuses that rather than
1737        // accepting a clause it will never reach.
1738        if let Some(position) = insert
1739            .upserts
1740            .iter()
1741            .position(|upsert| upsert.target.is_empty())
1742        {
1743            if position + 1 < insert.upserts.len() {
1744                return Err(crate::bind::schema_refused(
1745                    "ON CONFLICT clause with no conflict target must be last",
1746                    Span::default(),
1747                ));
1748            }
1749        }
1750        // `excluded` is in scope for the assignments and the WHERE, and only
1751        // there. Setting it around the binding rather than pushing a second
1752        // FROM term keeps unqualified names resolving to the target row, which
1753        // is what SQLite does and what a second source would have made
1754        // ambiguous - every column of the target is also a column of
1755        // `excluded`.
1756        self.excluded = Some(table.clone());
1757        let mut bound = Vec::with_capacity(insert.upserts.len());
1758        for upsert in &insert.upserts {
1759            match self.bind_upsert_body(table, upsert) {
1760                Ok(Some(one)) => bound.push(one),
1761                Ok(None) => {}
1762                Err(error) => {
1763                    self.excluded = None;
1764                    return Err(error);
1765                }
1766            }
1767        }
1768        self.excluded = None;
1769        Ok(bound)
1770    }
1771
1772    /// Binds an upsert's target, assignments and filter.
1773    fn bind_upsert_body(
1774        &mut self,
1775        table: &TableInfo,
1776        upsert: &ast::Upsert,
1777    ) -> Result<Option<BoundUpsert>, ParseError> {
1778        let mut target = Vec::new();
1779        for column in &upsert.target {
1780            let Some(name) = bare_indexed_column(self.ast, column) else {
1781                return Err(unsupported(
1782                    "an expression in a conflict target",
1783                    Span::default(),
1784                ));
1785            };
1786            let Some(position) = table.column_position(&name) else {
1787                return Err(no_such_column(&name, Span::default()));
1788            };
1789            target.push(position);
1790        }
1791        target.sort_unstable();
1792        let mut assignments = Vec::new();
1793        for (names, value) in &upsert.assignments {
1794            let bound = self.bind_expr(*value)?;
1795            for name in names {
1796                let folded = self.ast.folded(*name).to_vec();
1797                let Some(position) = table.column_position(&folded) else {
1798                    return Err(no_such_column(self.ast.text(*name), Span::default()));
1799                };
1800                assignments.push(BoundAssignment {
1801                    column: position,
1802                    rowid: false,
1803                    value: bound.clone(),
1804                });
1805            }
1806        }
1807        assignments.sort_by_key(|assignment| assignment.column);
1808        let filter = match upsert.filter {
1809            Some(expr) => Some(self.bind_expr(expr)?),
1810            None => None,
1811        };
1812        Ok(Some(BoundUpsert {
1813            target,
1814            assignments,
1815            do_update: upsert.do_update,
1816            filter,
1817        }))
1818    }
1819
1820    /// Binds a `RETURNING` list, which is a result-column list over the row
1821    /// that was written.
1822    fn bind_returning(
1823        &mut self,
1824        columns: &[ast::ResultColumn],
1825    ) -> Result<Vec<BoundResultColumn>, ParseError> {
1826        if columns.is_empty() {
1827            return Ok(Vec::new());
1828        }
1829        self.bind_result_columns_public(columns)
1830    }
1831}
1832
1833/// Returns an indexed column's bare folded name, when it names a column.
1834fn bare_indexed_column(ast: &crate::Ast, column: &ast::IndexedColumn) -> Option<Vec<u8>> {
1835    match ast.expr(column.expr) {
1836        Some(ast::Expr::Column {
1837            table: None,
1838            column: name,
1839            ..
1840        }) => Some(ast.folded(*name).to_vec()),
1841        _ => None,
1842    }
1843}
1844
1845/// The extended result codes a rejected write reports.
1846///
1847/// The numbers are SQLite's own extended codes. They are written out rather
1848/// than derived because an application matches on them, and a code that was
1849/// computed from an enum's discriminant would change the day the enum did.
1850///
1851/// They live here, beside the binder that decides which constraint a statement
1852/// can violate, because **both** engines report them: the virtual machine
1853/// compiles them into a `HaltError` and the vectorised executor returns them
1854/// from its write path. Two copies would agree until one of them was corrected.
1855pub mod codes {
1856    /// `SQLITE_CONSTRAINT_CHECK`.
1857    pub const CHECK: i32 = 275;
1858    /// `SQLITE_CONSTRAINT_DATATYPE`, which a STRICT table reports.
1859    pub const DATATYPE: i32 = 3091;
1860    /// `SQLITE_CONSTRAINT_NOTNULL`.
1861    pub const NOT_NULL: i32 = 1299;
1862    /// `SQLITE_CONSTRAINT_PRIMARYKEY`.
1863    pub const PRIMARY_KEY: i32 = 1555;
1864    /// `SQLITE_CONSTRAINT_UNIQUE`.
1865    pub const UNIQUE: i32 = 2067;
1866    /// `SQLITE_CONSTRAINT_ROWID`.
1867    pub const ROWID: i32 = 2579;
1868    /// `SQLITE_MISMATCH`, which an `INTEGER PRIMARY KEY` reports for a value
1869    /// that is not an integer.
1870    pub const MISMATCH: i32 = 20;
1871    /// `SQLITE_CONSTRAINT_TRIGGER`, which `RAISE()` reports.
1872    pub const TRIGGER: i32 = 1811;
1873    /// `SQLITE_CONSTRAINT_FOREIGNKEY`.
1874    pub const FOREIGN_KEY: i32 = 787;
1875}
1876
1877/// Returns the message a unique-index violation reports.
1878///
1879/// SQLite names every column of the index, comma separated, which is what an
1880/// application parses to find out which key collided.
1881///
1882/// @param table - the table the index belongs to
1883/// @param index - the index whose key collided
1884pub fn unique_message(table: &TableInfo, index: &IndexInfo) -> String {
1885    let names: Vec<String> = index
1886        .columns
1887        .iter()
1888        .filter_map(|key| key.column)
1889        .filter_map(|column| table.column(column))
1890        .map(|column| {
1891            format!(
1892                "{}.{}",
1893                String::from_utf8_lossy(&table.name),
1894                String::from_utf8_lossy(&column.name)
1895            )
1896        })
1897        .collect();
1898    format!("UNIQUE constraint failed: {}", names.join(", "))
1899}
1900
1901/// Returns the message a duplicate rowid reports, and its extended code.
1902///
1903/// SQLite names the aliasing column when the table has an `INTEGER PRIMARY
1904/// KEY` - and reports `SQLITE_CONSTRAINT_PRIMARYKEY` for it - and names the
1905/// hidden `rowid` under `SQLITE_CONSTRAINT_ROWID` when it does not.
1906///
1907/// **A `WITHOUT ROWID` table has no rowid to name.** Its own key *is* its
1908/// primary key, held in the one index whose root is the table's, so a collision
1909/// reports every column of that key under `SQLITE_CONSTRAINT_PRIMARYKEY` -
1910/// `UNIQUE constraint failed: t.a, t.b`. It used to answer `t.rowid`, naming a
1911/// column the table does not have, on `INSERT` as well as `UPDATE`.
1912///
1913/// @param table - the table whose key collided
1914pub fn rowid_message(table: &TableInfo) -> (i32, String) {
1915    if table.without_rowid {
1916        if let Some(index) = table.indexes.iter().find(|index| index.root == table.root) {
1917            return (codes::PRIMARY_KEY, unique_message(table, index));
1918        }
1919    }
1920    match table.rowid_alias.and_then(|column| table.column(column)) {
1921        Some(column) => (
1922            codes::PRIMARY_KEY,
1923            format!(
1924                "UNIQUE constraint failed: {}.{}",
1925                String::from_utf8_lossy(&table.name),
1926                String::from_utf8_lossy(&column.name)
1927            ),
1928        ),
1929        None => (
1930            codes::ROWID,
1931            format!(
1932                "UNIQUE constraint failed: {}.rowid",
1933                String::from_utf8_lossy(&table.name)
1934            ),
1935        ),
1936    }
1937}
1938
1939/// Puts a limited write's order, limit and offset on the query that finds a
1940/// view's rows.
1941///
1942/// A write through a view's `INSTEAD OF` trigger fires once per row the view
1943/// produces under the statement's `WHERE`, so a `LIMIT` on the write limits
1944/// that query. SQLite's `sqlite3MaterializeView` is handed the same three
1945/// clauses for the same reason.
1946///
1947/// @param rows - the query over the view the binder built
1948/// @param order_by - the statement's bound `ORDER BY`
1949/// @param limit - the statement's bound `LIMIT`
1950/// @param offset - the statement's bound `OFFSET`
1951fn limit_view_rows(
1952    mut rows: Box<BoundSelect>,
1953    order_by: &[BoundOrderTerm],
1954    limit: &Option<BoundExpr>,
1955    offset: &Option<BoundExpr>,
1956) -> Box<BoundSelect> {
1957    rows.order_by = order_by.to_vec();
1958    rows.limit = limit.clone();
1959    rows.offset = offset.clone();
1960    rows
1961}
1962
1963/// Returns the refusal a `DELETE` or `UPDATE` with `ORDER BY` and no `LIMIT`
1964/// earns, or `None` when the clause is allowed.
1965///
1966/// **`ORDER BY` and `LIMIT` on a write are run, not refused (task-2120).** They
1967/// were refused in the pinned reference's words, `near "ORDER": syntax error`,
1968/// because that build is not compiled with `SQLITE_ENABLE_UPDATE_DELETE_LIMIT`
1969/// and has no grammar for the clause. But the builds applications actually link
1970/// often are - Apple's is - and `DELETE FROM t WHERE ... LIMIT 1000` in a loop
1971/// is the ordinary way to trim a large table without one large transaction. A
1972/// consumer probing 0.1.8 against the macOS `sqlite3` reported the refusal as a
1973/// real gap, which it was.
1974///
1975/// What remains is the one rule a build compiled with the option enforces:
1976/// an order with nothing to limit is refused, in SQLite's own words, because
1977/// sorting the rows a statement changes all of changes nothing.
1978///
1979/// @param limited - which of the two words came first and where, from the parser
1980/// @param limit - the statement's `LIMIT`, when it wrote one
1981/// @param statement - `DELETE` or `UPDATE`, for the message
1982fn order_without_limit(
1983    limited: Option<(ast::Limited, Span)>,
1984    limit: Option<ast::ExprId>,
1985    statement: &str,
1986) -> Option<ParseError> {
1987    let (word, span) = limited?;
1988    if word != ast::Limited::OrderBy || limit.is_some() {
1989        return None;
1990    }
1991    Some(refused(
1992        format!("ORDER BY without LIMIT on {statement}"),
1993        span,
1994    ))
1995}
1996
1997#[cfg(test)]
1998mod tests {
1999    use super::*;
2000    use crate::catalog_view::{
2001        ColumnInfo, IndexColumnInfo, IndexInfo, IndexOrigin, TableInfo, TableKind,
2002    };
2003    use inillucent_value::Affinity;
2004
2005    /// Returns one plain column.
2006    ///
2007    /// @param name - the column's name
2008    fn a_column(name: &str) -> ColumnInfo {
2009        ColumnInfo {
2010            name: name.as_bytes().to_vec(),
2011            folded: name.to_ascii_lowercase().into_bytes(),
2012            declared_type: b"INTEGER".to_vec(),
2013            affinity: Affinity::Integer,
2014            collation: b"binary".to_vec(),
2015            not_null: false,
2016            not_null_conflict: None,
2017            primary_key_conflict: None,
2018            default_sql: None,
2019            primary_key_position: None,
2020            hidden: false,
2021            generated: false,
2022            stored: false,
2023            generated_sql: None,
2024        }
2025    }
2026
2027    /// Returns a rowid table with the columns named.
2028    ///
2029    /// @param name - the table's name
2030    /// @param columns - the column names, in declaration order
2031    fn a_table(name: &str, columns: &[&str]) -> TableInfo {
2032        TableInfo {
2033            name: name.as_bytes().to_vec(),
2034            folded: name.to_ascii_lowercase().into_bytes(),
2035            database: 0,
2036            root: 2,
2037            columns: columns.iter().map(|held| a_column(held)).collect(),
2038            rowid_alias: None,
2039            without_rowid: false,
2040            strict: false,
2041            autoincrement: false,
2042            kind: TableKind::Table,
2043            create_sql: Vec::new(),
2044            indexes: Vec::new(),
2045            view: None,
2046            triggers: Vec::new(),
2047            analysed_rows: None,
2048            foreign_key_triggers: Vec::new(),
2049            foreign_keys: Vec::new(),
2050            checks: Vec::new(),
2051            module: None,
2052        }
2053    }
2054
2055    /// Returns an index over the table columns named.
2056    ///
2057    /// @param name - the index's name
2058    /// @param root - its own tree, or the table's for a `WITHOUT ROWID` key
2059    /// @param columns - the table columns it keys on
2060    fn an_index(name: &str, root: u32, columns: &[u16]) -> IndexInfo {
2061        IndexInfo {
2062            name: name.as_bytes().to_vec(),
2063            folded: name.to_ascii_lowercase().into_bytes(),
2064            root,
2065            unique: true,
2066            columns: columns
2067                .iter()
2068                .map(|held| IndexColumnInfo {
2069                    column: Some(*held),
2070                    expr_sql: None,
2071                    collation: b"binary".to_vec(),
2072                    descending: false,
2073                    declared_descending: false,
2074                })
2075                .collect(),
2076            partial_sql: None,
2077            origin: IndexOrigin::Unique,
2078            conflict: None,
2079            prefix_rows: Vec::new(),
2080            analysed_rows: None,
2081            metric: None,
2082        }
2083    }
2084
2085    /// The three spellings of the rowid are the three SQLite accepts.
2086    ///
2087    /// **A fourth would be a column name a table could not have (T3,
2088    /// task-1962).** `rowid`, `oid` and `_rowid_` all name the hidden key, and
2089    /// a table that declares a column called any of them shadows it - so the
2090    /// list decides which names a `SELECT rowid` can mean.
2091    #[test]
2092    fn the_rowid_has_three_names() {
2093        assert!(is_rowid_name(b"rowid"));
2094        assert!(is_rowid_name(b"oid"));
2095        assert!(is_rowid_name(b"_rowid_"));
2096        assert!(!is_rowid_name(b"row_id"));
2097        assert!(!is_rowid_name(b"id"));
2098        assert!(
2099            !is_rowid_name(b"ROWID"),
2100            "the argument is already folded, so an unfolded name is not one this asks about"
2101        );
2102    }
2103
2104    /// A unique violation names every column of the index, table-qualified.
2105    ///
2106    /// **The message is what an application matches on.** SQLite's wording is
2107    /// `UNIQUE constraint failed: t.a, t.b`, and a library that switched on it
2108    /// would stop recognising a collision if the columns were listed any other
2109    /// way.
2110    #[test]
2111    fn a_unique_violation_names_every_column_of_the_index() {
2112        let table = a_table("t", &["a", "b", "c"]);
2113        let one = an_index("by_a", 3, &[0]);
2114        assert_eq!(
2115            unique_message(&table, &one),
2116            "UNIQUE constraint failed: t.a"
2117        );
2118        let two = an_index("by_a_b", 4, &[0, 1]);
2119        assert_eq!(
2120            unique_message(&table, &two),
2121            "UNIQUE constraint failed: t.a, t.b",
2122            "both columns, in key order, separated the way the reference separates them"
2123        );
2124    }
2125
2126    /// A rowid collision names the aliasing column when there is one, and the
2127    /// hidden `rowid` when there is not.
2128    ///
2129    /// The extended code differs with it: `SQLITE_CONSTRAINT_PRIMARYKEY` for an
2130    /// `INTEGER PRIMARY KEY` and `SQLITE_CONSTRAINT_ROWID` for the hidden one.
2131    #[test]
2132    fn a_rowid_collision_names_the_column_that_aliases_it() {
2133        let hidden = a_table("t", &["a"]);
2134        assert_eq!(
2135            rowid_message(&hidden),
2136            (
2137                codes::ROWID,
2138                "UNIQUE constraint failed: t.rowid".to_string()
2139            )
2140        );
2141        let mut aliased = a_table("t", &["id", "a"]);
2142        aliased.rowid_alias = Some(0);
2143        assert_eq!(
2144            rowid_message(&aliased),
2145            (
2146                codes::PRIMARY_KEY,
2147                "UNIQUE constraint failed: t.id".to_string()
2148            )
2149        );
2150    }
2151
2152    /// A `WITHOUT ROWID` table has no rowid to name, so it names its key.
2153    ///
2154    /// **It used to answer `t.rowid`, naming a column the table does not
2155    /// have.** Its own key *is* its primary key, held in the one index whose
2156    /// root is the table's.
2157    #[test]
2158    fn a_without_rowid_collision_names_the_primary_key() {
2159        let mut table = a_table("t", &["a", "b"]);
2160        table.without_rowid = true;
2161        table.indexes = vec![an_index("sqlite_autoindex_t_1", table.root, &[0, 1])];
2162        assert_eq!(
2163            rowid_message(&table),
2164            (
2165                codes::PRIMARY_KEY,
2166                "UNIQUE constraint failed: t.a, t.b".to_string()
2167            )
2168        );
2169    }
2170
2171    /// A constraint's own `ON CONFLICT REPLACE` makes a statement able to
2172    /// replace, with no `OR REPLACE` written anywhere.
2173    #[test]
2174    fn a_constraint_can_make_a_plain_insert_replace() {
2175        let plain = a_table("t", &["a"]);
2176        assert!(!can_replace(&plain, None));
2177        assert!(can_replace(&plain, Some(ConflictAction::Replace)));
2178
2179        let mut on_the_index = a_table("t", &["a"]);
2180        let mut index = an_index("by_a", 3, &[0]);
2181        index.conflict = Some(ConflictAction::Replace);
2182        on_the_index.indexes = vec![index];
2183        assert!(
2184            can_replace(&on_the_index, None),
2185            "`a UNIQUE ON CONFLICT REPLACE` replaces without the statement saying so"
2186        );
2187
2188        let mut on_the_column = a_table("t", &["a"]);
2189        if let Some(column) = on_the_column.columns.first_mut() {
2190            column.not_null_conflict = Some(ConflictAction::Replace);
2191        }
2192        assert!(can_replace(&on_the_column, None));
2193    }
2194}