Skip to main content

no_mistakes_postgres_sql/statement_facts/bounds/
pin.rs

1use super::{SqlBareRead, SqlBoundQuery, SqlQualifiedRead};
2use sqlparser::ast::Expr;
3
4/// A conjunct that equates one column or index expression of an item with a value.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct SqlBoundPin {
7    /// Disjunctive alternative owning this pin; None means an unconditional conjunct.
8    pub alternative: Option<usize>,
9    pub column: String,
10    /// Bare column with multiple possible FROM owners; only catalog projection may credit it.
11    pub unqualified_candidate: bool,
12    /// The canonical table-local expression fixed by this equality, when it is not a column.
13    pub expression: Option<Expr>,
14    pub source: SqlPinSource,
15    /// `IS NOT DISTINCT FROM` also matches NULL; a nullable key needs NULLS NOT DISTINCT.
16    pub null_safe: bool,
17    /// Bare columns that subqueries in the value read: when one resolves to the enclosing query
18    /// the value depends on the row checked, and the pin fixes nothing.
19    pub reads: Vec<SqlBareRead>,
20    /// Schema-qualified reads retained for catalog identity resolution.
21    pub qualified_reads: Vec<SqlQualifiedRead>,
22    /// When `source` is one other item, the column of that item this equality names.
23    pub peer_column: Option<String>,
24    /// Unconditional `=` to one caller value. Lists, arrays, and null-safe matches are not one value.
25    pub single_value: bool,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub enum SqlPinSource {
30    /// A literal, placeholder, list, array or scalar subquery: a value the caller sizes.
31    Value,
32    /// An expression over columns of other items of the same statement (indexes into the items).
33    Items(Vec<usize>),
34    /// An array expression with unproven caller cardinality, including stored columns
35    /// and unknown function results. Indexes retain known source items; this supplies
36    /// no finite-key proof, even when an owning row is bounded.
37    StoredArray(Vec<usize>),
38    /// A syntactically finite array constructor over other items. Every referenced column
39    /// must be catalog-proven scalar before its row bound can size the flattened array.
40    Array {
41        items: Vec<usize>,
42        scalar_columns: Vec<(usize, String)>,
43        /// Columns accessed only through scalar indexes; their catalog array element must be scalar.
44        indexed_columns: Vec<(usize, String)>,
45        /// Custom cast targets that require catalog scalar/enum evidence.
46        cast_types: Vec<String>,
47    },
48    /// `IN (SELECT …)` or `= ANY (SELECT …)`: sized by the subquery.
49    Query(SqlBoundQuery),
50    /// ARRAY(SELECT ...) or a scalar array_agg: input rows and scalar elements need proof.
51    ArrayQuery(SqlBoundQuery),
52    /// An executed correlated IN/ANY subquery: retain its reads, never grant key credit.
53    ReadQuery(SqlBoundQuery),
54}