Skip to main content

no_mistakes_postgres_sql/
statement_facts.rs

1mod bounds;
2mod debug;
3mod grouping;
4mod insert;
5mod isolation;
6mod iteration;
7mod json_writes;
8mod keyed_lookups;
9mod locations;
10mod parse_failures;
11mod select;
12mod transformed;
13mod triggers;
14mod writes;
15pub use bounds::{
16    SqlBareRead, SqlBoundFact, SqlBoundInputMode, SqlBoundItem, SqlBoundItemKind, SqlBoundKind,
17    SqlBoundOutput, SqlBoundPin, SqlBoundQuery, SqlGroupKey, SqlPinSource, SqlPossibleTemporary,
18    SqlQualifiedRead, SqlQualifiedScope, SqlScalarReads,
19};
20#[doc(hidden)]
21pub use grouping::SqlGroupingQuery;
22pub use insert::SqlInsertFact;
23#[doc(hidden)]
24pub use isolation::{SqlIsolation, SqlIsolationChanges, SqlIsolationEffect};
25pub use iteration::{SqlConjunctFact, SqlCursorBound, SqlLimitFact, SqlLimitValue, SqlSweepFact};
26pub use json_writes::SqlJsonWriteFact;
27pub use keyed_lookups::{
28    SqlAliasedViewPeer, SqlBareViewRowScope, SqlNaturalJoinPeers, SqlQualifiedViewKey,
29    SqlSchemaQualifiedViewRead, SqlUsingViewRowRead,
30};
31pub use keyed_lookups::{
32    SqlBareViewRowRead, SqlDuplicateKeyedLookupFact, SqlViewKeyCondition, SqlViewKeyDependencies,
33    SqlViewUseFact, SqlViewValuesSortRead, SqlViewValuesWidth,
34};
35#[doc(hidden)]
36pub use locations::SqlLocationDemand;
37pub use locations::{SqlFactPosition, SqlFactSite, SqlVariantLocations};
38pub use select::{
39    SqlCatalogPredicate, SqlColumnClause, SqlColumnUseFact, SqlCountExistenceFact,
40    SqlExistsSetOpFact, SqlRelationPredicateFact, SqlSelectFact, SqlStarProjectionFact,
41};
42use std::path::PathBuf;
43pub use transformed::{
44    SqlTransformedColumnClause, SqlTransformedColumnProbeFact, SqlTransformedColumnUseFact,
45};
46pub use triggers::{SqlTriggerEvent, SqlTriggerFact, SqlTriggerPeriod};
47pub use writes::{SqlWriteColumns, SqlWriteFact};
48mod query_shapes;
49#[doc(hidden)]
50pub use query_shapes::{SqlQueryShape, SqlQueryShapeFact};
51mod lifecycle;
52pub use lifecycle::SqlLifecycleFacts;
53#[doc(hidden)]
54pub use lifecycle::{SqlBoundSourceLines, SqlFunctionBounds};
55#[doc(hidden)]
56pub use lifecycle::{SqlLifecycleBatch, SqlLifecycleStep, SqlViewReads};
57/// Statement facts for one SQL source (file or embedded call).
58#[derive(Clone, PartialEq, Eq, Default)]
59pub struct SqlStatementFileFacts {
60    #[doc(hidden)]
61    pub lock_inventory: Vec<super::lock_inventory::InventoryLockFact>,
62    #[doc(hidden)]
63    pub lock_inventory_unanalyzed: Vec<usize>,
64    #[doc(hidden)]
65    pub lock_isolation: SqlIsolationChanges,
66    #[doc(hidden)]
67    pub lock_writes: super::statements::lock_writes::LockWriteFacts,
68    #[doc(hidden)]
69    pub lookup_step: Option<super::statements::lock_writes::lookup::LookupStep>,
70    /// Request-local provenance for complete embedded alternatives; absent for legacy sources.
71    #[doc(hidden)]
72    pub variant_locations: Option<SqlVariantLocations>,
73    pub path: PathBuf,
74    /// Statement categories shared with schema policy, including static routine DDL.
75    pub statement_kinds: Vec<super::types::SqlStatementKind>,
76    pub setting_uses: Vec<super::types::SqlSettingUse>,
77    #[doc(hidden)]
78    pub clause_uses: Vec<super::SqlPolicyClauseUse>,
79    pub function_calls: Vec<super::SqlFunctionCallFact>,
80    /// Syntactic write targets; catalog resolution happens after preparation.
81    pub writes: Vec<SqlWriteFact>,
82    pub json_writes: Vec<SqlJsonWriteFact>,
83    pub duplicate_keyed_lookups: Vec<SqlDuplicateKeyedLookupFact>,
84    pub view_uses: Vec<SqlViewUseFact>,
85    #[doc(hidden)]
86    pub grouping_queries: Vec<SqlGroupingQuery>,
87    pub inserts: Vec<SqlInsertFact>,
88    pub selects: Vec<SqlSelectFact>,
89    #[doc(hidden)]
90    pub index_probes: std::collections::BTreeMap<(usize, usize), Vec<String>>,
91    /// One entry per `UPDATE`. Each entry is that statement's relation instances.
92    pub updates: Vec<Vec<SqlRelationPredicateFact>>,
93    /// One entry per `DELETE`. Each entry is that statement's relation instances.
94    pub deletes: Vec<Vec<SqlRelationPredicateFact>>,
95    pub triggers: Vec<SqlTriggerFact>,
96    /// `RETURNING *` / `RETURNING t.*` on INSERT, UPDATE, and DELETE.
97    pub returning_stars: Vec<SqlStarProjectionFact>,
98    /// Bare column comparisons on UPDATE/DELETE WHERE and JOIN ON predicates.
99    pub mutation_column_uses: Vec<SqlColumnUseFact>,
100    /// Cast and function inputs in WHERE, JOIN ON and HAVING predicates.
101    pub transformed_column_uses: Vec<SqlTransformedColumnUseFact>,
102    /// Executed OFFSET occurrences in source order.
103    pub offset_uses: Vec<super::offset::SqlOffsetFact>,
104    /// Row-count bounds of each executed SELECT, UPDATE and DELETE.
105    pub bounds: Vec<SqlBoundFact>,
106    /// Present only when a lifecycle decision needs per-catalog evidence.
107    #[doc(hidden)]
108    pub lifecycle: Option<SqlLifecycleFacts>,
109    /// Every `LIMIT` / `FETCH FIRST` in the executed statements, in source order.
110    pub limit_uses: Vec<SqlLimitFact>,
111    /// Limited single-table queries ordered by plain columns: pages of a key walk.
112    pub sweeps: Vec<SqlSweepFact>,
113    #[doc(hidden)]
114    pub query_shapes: Vec<SqlQueryShapeFact>,
115    pub parse_failed: bool,
116    /// Recovery found no complete source statement after a parse error.
117    pub no_complete_statements: bool,
118    /// Source-ordered failed statement starts from the existing recovery pass.
119    pub parse_failures: Vec<super::types::SqlUnanalyzableStatement>,
120    pub insert_keyword_count: usize,
121    pub has_top_level_not_exists: bool,
122    pub origin_line: usize,
123}
124/// `ON CONFLICT` action recovered from an INSERT.
125#[derive(Debug, Clone, PartialEq, Eq)]
126pub struct SqlOnConflictFact {
127    pub action: SqlOnConflictAction,
128    pub arbiter: SqlConflictArbiter,
129    pub assignments: Vec<SqlAssignmentFact>,
130    pub where_proof: SqlConflictWhereProof,
131    #[doc(hidden)]
132    pub copies_conflict_keys_only: bool,
133}
134#[derive(Debug, Clone, Copy, PartialEq, Eq)]
135pub enum SqlOnConflictAction {
136    DoNothing,
137    DoUpdate,
138}
139
140#[derive(Debug, Clone, PartialEq, Eq)]
141pub enum SqlConflictArbiter {
142    Columns(Vec<String>),
143    /// Source expression dependencies; `unknown` means membership cannot be proven.
144    Expressions {
145        columns: Vec<String>,
146        /// Columns that participate in one expression and must move together.
147        groups: Vec<Vec<String>>,
148        unknown: bool,
149    },
150    Constraint(String),
151    Unknown,
152}
153
154/// One SET/VALUES assignment classified without keeping the sqlparser AST.
155#[derive(Debug, Clone, PartialEq, Eq)]
156pub struct SqlAssignmentFact {
157    pub column: String,
158    pub form: SqlValueForm,
159}
160
161#[derive(Debug, Clone, PartialEq, Eq)]
162pub enum SqlValueForm {
163    Literal,
164    Null,
165    Excluded { column: String },
166    SelfRef { column: String },
167    Coalesce { args: Vec<SqlValueForm> },
168    Greatest { args: Vec<SqlValueForm> },
169    Least { args: Vec<SqlValueForm> },
170    Placeholder,
171    Volatile { name: String },
172    Subquery,
173    Other,
174}
175
176/// Conjunctive no-op proofs on `ON CONFLICT … WHERE`.
177#[derive(Debug, Clone, PartialEq, Eq, Default)]
178pub struct SqlConflictWhereProof {
179    pub distinct_from_excluded: Vec<String>,
180    pub null_and_excluded_not_null: Vec<String>,
181    pub disjunctive: bool,
182}