Skip to main content

no_mistakes_postgres_sql/statement_facts/
keyed_lookups.rs

1/// One duplicate keyed lookup of the same relation within a single statement scope.
2#[derive(Debug, Clone, PartialEq, Eq)]
3pub struct SqlDuplicateKeyedLookupFact {
4    /// Canonical target relation name (e.g. `people` or `public.people`).
5    pub relation: String,
6    /// Human-readable normalized key summary (e.g. `id = n.owner_id`).
7    pub key_display: String,
8    /// 1-based line of the first lookup (rebased to physical source line for embedded SQL).
9    pub first_line: usize,
10    /// 1-based SQL column of the first lookup.
11    pub first_column: usize,
12    /// 1-based line of the duplicate lookup (rebased to physical source line for embedded SQL).
13    pub line: usize,
14    /// 1-based SQL column of the duplicate lookup.
15    pub column: usize,
16}
17
18/// A relation read from a join or a scalar subquery, before catalog classification.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct SqlViewUseFact {
21    /// Canonical relation identity within the current database.
22    pub relation: String,
23    /// Full identity when SQL explicitly qualifies the relation with a database.
24    pub database_qualified_relation: Option<String>,
25    /// Whole-row embedding is the only use of the view and the lookup uses its ID.
26    pub whole_row_on_id: bool,
27    /// The only key is a bare ID with a possible outer column of that name.
28    pub correlated_bare_id: bool,
29    /// A syntactically confirmed ID equality, assuming the relation exposes its ID.
30    pub proven_id: bool,
31    /// Candidate ID predicates whose source column requires catalog resolution.
32    pub column_keys: Vec<SqlBareViewRowRead>,
33    /// Candidate keys using a visible qualified peer with the same short name as the view.
34    pub qualified_peer_keys: Vec<SqlQualifiedViewKey>,
35    /// Positional aliases applied to the relation columns at this reference.
36    pub column_aliases: Vec<String>,
37    /// Unqualified columns whose binding needs catalog evidence.
38    pub unqualified_columns: Vec<String>,
39    /// A join output aliases a read column at an unknown source position.
40    pub unresolved_column_ownership: bool,
41    /// Qualified reads whose binding depends on the default schema.
42    pub schema_qualified_reads: Vec<SqlSchemaQualifiedViewRead>,
43    pub schema_qualified_embeddings: Vec<SqlSchemaQualifiedViewRead>,
44    /// An explicit qualified row argument proves an embedding.
45    pub qualified_embedding: bool,
46    /// Bare JSON arguments whose column ownership must be resolved.
47    pub row_arguments: Vec<SqlBareViewRowRead>,
48    /// Additional bare references that can resolve to a sibling column instead of the view row.
49    pub bare_row_reads: Vec<SqlBareViewRowRead>,
50    /// Bare USING row references and the merged column each may read.
51    pub using_row_reads: Vec<SqlUsingViewRowRead>,
52    /// Column references that can bind to this view in an enclosing query.
53    pub correlated_columns: Vec<SqlBareViewRowRead>,
54    /// A VALUES sort identifier may refer to a widened wildcard output.
55    pub values_sort_reads: Vec<SqlViewValuesSortRead>,
56    /// Output columns on the other side of an implicit NATURAL join.
57    pub natural_join_peers: Vec<SqlNaturalJoinPeers>,
58    /// One-based source line.
59    pub line: usize,
60    /// One-based SQL column.
61    pub column: usize,
62}
63
64#[derive(Debug, Clone, PartialEq, Eq)]
65pub struct SqlUsingViewRowRead {
66    pub binding: SqlBareViewRowRead,
67    /// None means this view definitively supplies the merged column.
68    pub column: Option<SqlBareViewRowRead>,
69}
70
71#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
72pub struct SqlSchemaQualifiedViewRead {
73    pub relation: String,
74    pub matches_default_schema: bool,
75}
76
77#[derive(Debug, Clone, PartialEq, Eq)]
78pub struct SqlQualifiedViewKey {
79    pub peer_relations: Vec<String>,
80    pub dependencies: SqlViewKeyDependencies,
81    pub bare_id: Option<SqlBareViewRowRead>,
82}
83
84#[derive(Debug, Clone, PartialEq, Eq, Default)]
85pub struct SqlNaturalJoinPeers {
86    pub known_columns: Vec<String>,
87    pub relations: Vec<String>,
88    pub aliased_relations: Vec<SqlAliasedViewPeer>,
89    pub opaque: bool,
90    pub constrains_id: bool,
91    pub dependencies: SqlViewKeyDependencies,
92}
93
94#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
95pub struct SqlAliasedViewPeer {
96    pub relation: String,
97    pub column_aliases: Vec<String>,
98}
99
100/// A key depends on the view if every condition in any one alternative holds.
101pub type SqlViewKeyDependencies = Vec<Vec<SqlViewKeyCondition>>;
102
103#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
104pub enum SqlViewKeyCondition {
105    Unknown,
106    Schema(SqlSchemaQualifiedViewRead),
107    SharedColumn {
108        left: SqlAliasedViewPeer,
109        right: SqlAliasedViewPeer,
110    },
111    Column {
112        peer: SqlAliasedViewPeer,
113        column: String,
114        present: bool,
115    },
116}
117
118/// A bare reference sharing a name with a view's whole-row alias.
119#[derive(Debug, Clone, PartialEq, Eq)]
120pub struct SqlBareViewRowRead {
121    /// The ambiguous bare name.
122    pub name: String,
123    /// Name-resolution scopes, from the innermost query outward.
124    pub scopes: Vec<SqlBareViewRowScope>,
125}
126
127#[derive(Debug, Clone, PartialEq, Eq, Default)]
128pub struct SqlViewValuesWidth {
129    pub known: usize,
130    pub relations: Vec<String>,
131    pub unknown: bool,
132}
133
134#[derive(Debug, Clone, PartialEq, Eq)]
135pub struct SqlViewValuesSortRead {
136    pub binding: SqlBareViewRowRead,
137    pub ordinal: usize,
138    pub width: std::sync::Arc<SqlViewValuesWidth>,
139    pub whole_row: bool,
140    pub using_columns: Vec<Option<SqlBareViewRowRead>>,
141}
142
143/// Inputs whose columns can bind a bare name at a single lexical scope.
144#[derive(Debug, Clone, PartialEq, Eq, Default)]
145pub struct SqlBareViewRowScope {
146    /// This scope contains the view whose row alias was referenced.
147    pub view_input: bool,
148    /// A peer has a matching column proven directly from its SQL projection.
149    pub known_peer_column: bool,
150    /// Physical peers whose catalog columns can claim this name.
151    pub peers: Vec<String>,
152    pub aliased_peers: Vec<SqlAliasedViewPeer>,
153    /// Direct FROM items that can own an explicit bare system-column read.
154    pub system_column_peers: Vec<String>,
155    /// At least one peer has unknown output names.
156    pub opaque_peer: bool,
157}