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