no_mistakes_postgres_sql/statement_facts/select.rs
1/// One `SELECT` that names relations in FROM/JOIN.
2#[derive(Clone, PartialEq, Eq)]
3pub struct SqlSelectFact {
4 #[doc(hidden)]
5 pub sql_position: (usize, usize),
6 pub line: usize,
7 /// Physical relations, excluding visible CTE aliases (all siblings for recursive WITH).
8 pub tables: Vec<String>,
9 pub predicate_sql: String,
10 pub exists_set_operations: Vec<SqlExistsSetOpFact>,
11 /// Base-table instances in this SELECT, with columns proven constrained.
12 pub relations: Vec<SqlRelationPredicateFact>,
13 /// True when this SELECT is the query of `INSERT … SELECT` (including nested
14 /// selects inside that query).
15 pub in_insert_select: bool,
16 /// Lines of `NOT IN (SELECT …)` and `NOT (… IN (SELECT …))`.
17 pub not_in_subqueries: Vec<usize>,
18 /// Original SQL columns corresponding to `not_in_subqueries`.
19 pub not_in_columns: Vec<usize>,
20 /// `COUNT(*)` compared with 0 or 1 to test existence.
21 ///
22 /// Each fact keeps the comparison's line, SQL column, and `negated`
23 /// (true when the comparison tests zero rows).
24 pub count_existence_checks: Vec<SqlCountExistenceFact>,
25 /// `*` / `alias.*` projections over base FROM relations.
26 pub star_projections: Vec<SqlStarProjectionFact>,
27 /// Bare column references in WHERE, JOIN ON, and ORDER BY.
28 pub column_uses: Vec<SqlColumnUseFact>,
29 pub catalog_predicates: Vec<SqlCatalogPredicate>,
30}
31
32/// Single-relation WHERE disjunctions that require catalog evidence.
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct SqlCatalogPredicate {
35 pub table: String,
36 pub line: usize,
37 pub column: usize,
38 pub vacuous_columns: Vec<String>,
39 pub equal_parameter_columns: Vec<String>,
40 pub parameter_cast_types: Vec<String>,
41}
42
43/// Where a bare column reference was used.
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45pub enum SqlColumnClause {
46 Where,
47 Join,
48 OrderBy,
49}
50
51/// A bare column reference resolved to a base relation when possible.
52///
53/// `table` is empty when the name is unqualified and more than one base
54/// relation is in scope. The rule may still assign it from schema facts.
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct SqlColumnUseFact {
57 pub table: String,
58 pub column: String,
59 /// Base relations visible at this expression; None when a CTE or derived relation prevents ownership proof.
60 pub candidate_tables: Option<Vec<String>>,
61 pub clause: SqlColumnClause,
62 pub line: usize,
63}
64
65/// A star projection resolved to one base relation.
66///
67/// `within_function` is set when the star is an argument (`row_to_json(o.*)`).
68/// Bare `COUNT(*)` is not recorded. The rule applies `allowWholeRowFunctions`
69/// to that name; the statement pass does not know the configured list.
70#[derive(Debug, Clone, PartialEq, Eq)]
71pub struct SqlStarProjectionFact {
72 pub relation: String,
73 pub qualified: bool,
74 pub within_function: Option<String>,
75 pub line: usize,
76 pub column: usize,
77}
78
79/// One base-table instance and the columns a predicate constrains on it.
80///
81/// `constrained_columns` are resolved (qualified, or unqualified in a
82/// single-item FROM). `unqualified_columns` are bare names from a
83/// multi-relation FROM whose owner a configured catalog may still prove.
84#[derive(Debug, Clone, PartialEq, Eq)]
85pub struct SqlRelationPredicateFact {
86 pub table: String,
87 pub alias: Option<String>,
88 pub constrained_columns: Vec<String>,
89 pub unqualified_columns: Vec<String>,
90 pub line: usize,
91}
92
93/// An `EXISTS` whose subquery uses a set operation.
94#[derive(Debug, Clone, PartialEq, Eq)]
95pub struct SqlExistsSetOpFact {
96 pub restricted: bool,
97 /// Qualified `table.column` whose qualifier is not a local FROM/WITH name.
98 pub correlated: bool,
99 pub line: usize,
100 /// SQL column of the `EXISTS` keyword. Embedded SQL rebases with this column.
101 pub column: usize,
102}
103
104/// A scalar `COUNT(*)` comparison testing presence or absence of matching rows.
105///
106/// `negated` is true when the comparison tests zero matching rows.
107#[derive(Debug, Clone, PartialEq, Eq)]
108pub struct SqlCountExistenceFact {
109 pub line: usize,
110 /// Original SQL column used to map embedded clauses to their source line.
111 pub column: usize,
112 /// True when the comparison tests zero matching rows (NOT EXISTS).
113 pub negated: bool,
114 /// The COUNT belongs to an intact scalar subquery.
115 pub scalar_subquery: bool,
116}