no_mistakes_postgres_sql/statement_facts/bounds.rs
1mod kind;
2pub use kind::SqlBoundKind;
3/// One executed SELECT, UPDATE or DELETE, reduced to its row-count facts.
4/// Key uniqueness is determined later against the schema catalog.
5#[derive(Clone, PartialEq, Eq)]
6pub struct SqlBoundFact {
7 /// Owning executed statement, including for independently judged query bodies.
8 pub kind: SqlBoundKind,
9 /// Independent body obligation, present only when explicitly requested.
10 pub body: Option<String>,
11 pub line: usize,
12 pub column: usize,
13 /// Original outer SQL statement position, including WITH or an executing wrapper.
14 pub statement_start: Option<(usize, usize)>,
15 pub query: SqlBoundQuery,
16 pub scalar_reads: Option<SqlScalarReads>,
17 /// Qualified enclosing rows fixed for one explicitly LATERAL body invocation.
18 pub lateral_qualified_reads: Vec<SqlQualifiedRead>,
19 /// For `UPDATE` and `DELETE`: the index in `query.items` of the relation being changed.
20 pub target: Option<usize>,
21}
22/// A query body: its FROM items, and whether it caps its own row count.
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct SqlBoundQuery {
25 /// A `LIMIT` / `FETCH FIRST`, or a pure aggregate (no `GROUP BY`).
26 pub capped: bool,
27 /// Input execution and target-row projection policy.
28 pub input_mode: SqlBoundInputMode,
29 pub items: Vec<SqlBoundItem>,
30 /// Output columns in ordinal order; caller-sized values stay finite despite repeated rows.
31 pub outputs: Vec<SqlBoundOutput>,
32 /// Plain `GROUP BY` of single-value keys. A set-returning `ORDER BY` clears it.
33 pub group_constant: bool,
34 /// Plain `GROUP BY` keys that stay bounded only when their FROM items are bounded.
35 pub group_keys: Vec<SqlGroupKey>,
36 /// Parent column named by `LIMIT`, counted only when the catalog proves it `NOT NULL`.
37 pub catalog_limit: Option<(String, String)>,
38}
39
40/// One plain `GROUP BY` key. `constant` is a single value; `item` is the FROM item it reads.
41#[derive(Debug, Clone, PartialEq, Eq, Default)]
42pub struct SqlGroupKey {
43 pub item: Option<usize>,
44 pub constant: bool,
45}
46
47/// Syntactic value provenance for one projected column, independent of query row cardinality.
48#[derive(Debug, Clone, PartialEq, Eq)]
49pub struct SqlBoundOutput {
50 pub name: Option<String>,
51 pub caller_sized: bool,
52 /// A syntactically scalar value, or a column whose scalarity the catalog must prove.
53 pub scalar: bool,
54 pub source_column: Option<(usize, String)>,
55 /// A simple array_agg element column; its input rows need an independent bound.
56 pub array_aggregate_column: Option<(usize, String)>,
57 /// `NULL` or `NULL::type`. An equality on this output matches no row.
58 pub constant_null: bool,
59 /// Both recursive UNION arms project the same nonrecursive CTE column by ordinal.
60 pub recursive_domain: bool,
61}
62
63/// One FROM item and the columns or index expressions the statement pins on it.
64#[derive(Debug, Clone, PartialEq, Eq)]
65pub struct SqlBoundItem {
66 pub kind: SqlBoundItemKind,
67 /// A request-local temporary relation whose identity needs catalog evidence.
68 pub possible_temporary: Option<SqlPossibleTemporary>,
69 /// The name columns are qualified by: the alias, else the table's own (bare) name.
70 pub alias: Option<String>,
71 /// Whether `alias` was written in SQL rather than filled from a relation name.
72 pub alias_explicit: bool,
73 /// Positional column aliases as written; catalog column names are ambiguous when nonempty.
74 pub column_aliases: Vec<String>,
75 pub line: usize,
76 pub column: usize,
77 pub pins: Vec<SqlBoundPin>,
78 /// Catalog-independent, table-local conjuncts from WHERE and restricting JOIN ON arms.
79 pub predicate_conjuncts: Vec<Expr>,
80 /// Conjuncts that hold only inside one OR alternative, in alternative order.
81 pub alternative_predicates: Vec<Vec<Expr>>,
82 /// Columns made non-null by raw `=` equality. Sound for every catalog type.
83 pub nonnull_columns: Vec<String>,
84 /// Columns of unquoted `lower`, `upper`, or one-argument `btrim`.
85 /// Sound only for a text-family catalog column.
86 /// See docs/rules/postgres-bounded-statements-proof.md.
87 pub strict_nonnull_columns: Vec<String>,
88 /// Number of disjunctive alternatives; every alternative must complete a unique key.
89 pub pin_alternatives: usize,
90 /// Alternatives that reject every row of this item, so they need no key.
91 pub rejected_alternatives: Vec<usize>,
92 /// A certain bare outer read in a `LATERAL` source; its driver ownership is unproven.
93 /// Qualified outer reads retain separate scope evidence for driver-dependent bounds.
94 pub lateral: bool,
95 /// Bare columns of a `LATERAL` source that none of its own relations is known to own: it
96 /// reads the items before it when the catalog shows that none of their tables has them.
97 pub lateral_reads: Vec<SqlBareRead>,
98 /// Schema-qualified reads which may name preceding `LATERAL` items.
99 pub lateral_qualified_reads: Vec<SqlQualifiedRead>,
100 /// A synthetic transparent wrapper for one `UNION` / `INTERSECT` / `EXCEPT` arm.
101 #[doc(hidden)]
102 pub set_operation_arm: bool,
103}
104
105#[derive(Debug, Clone, PartialEq, Eq)]
106pub struct SqlPossibleTemporary {
107 /// A three-part `database.pg_temp.name` declaration must match the current database.
108 pub database_qualifier: Option<String>,
109 /// Explicit search-path schemas before `pg_temp`, in PostgreSQL resolution order.
110 pub earlier_schemas: Vec<String>,
111 /// An earlier DDL target was unknown: the table may be temporary, so its
112 /// catalog keys cannot safely bound another relation.
113 pub uncertain_lifetime: bool,
114}
115
116/// A bare column that a subquery reads, with the base tables that could own it.
117///
118/// PostgreSQL resolves a bare column in the innermost query whose relations have it, and
119/// otherwise in the query around it. The facts do not know a table's columns, so a subquery
120/// reads the enclosing row when the catalog shows that none of `tables` has `column`.
121/// Temporary-name projection removes possible temporary candidates because their column
122/// layout cannot be proved from a permanent catalog entry; an empty list proves no local owner.
123#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
124pub struct SqlBareRead {
125 pub column: String,
126 pub tables: Vec<String>,
127}
128
129/// A schema-qualified reference which did not match a relation name syntactically.
130#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
131pub struct SqlQualifiedRead {
132 /// The qualifier in SQL spelling, preserving quoted identifier boundaries.
133 pub qualifier: String,
134 /// Candidate scopes ordered from innermost outward.
135 pub scopes: Vec<SqlQualifiedScope>,
136}
137
138/// Candidate relations in one lexical query scope for a qualified read.
139#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
140pub struct SqlQualifiedScope {
141 pub tables: Vec<String>,
142 /// The scope contains a relation whose identity cannot be established from its base tables.
143 pub unknown: bool,
144}
145
146mod item;
147#[derive(Debug, Clone, PartialEq, Eq)]
148pub enum SqlBoundItemKind {
149 /// A base relation, as written and normalized.
150 Table(String),
151 /// A CTE reference, derived table, or set-operation arm.
152 Query(SqlBoundQuery),
153 /// A `VALUES` list, or a set-returning function sized by its caller-supplied arguments
154 /// (`unnest($1)`): it adds no unbounded relation and bounds what is pinned to it.
155 Other,
156 /// A table function the text does not size (`get_all_accounts()`), or the recursive
157 /// reference of a recursive CTE: never reported, and it bounds nothing pinned to it.
158 Opaque,
159}
160
161mod input_mode;
162mod lines;
163pub use input_mode::SqlBoundInputMode;
164use sqlparser::ast::Expr;
165mod debug;
166mod row_inputs;
167
168mod pin;
169mod scalar;
170pub use pin::{SqlBoundPin, SqlPinSource};
171pub use scalar::SqlScalarReads;