Skip to main content

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}
77
78/// One base-table instance and the columns a predicate constrains on it.
79///
80/// `constrained_columns` are resolved (qualified, or unqualified in a
81/// single-item FROM). `unqualified_columns` are bare names from a
82/// multi-relation FROM whose owner a configured catalog may still prove.
83#[derive(Debug, Clone, PartialEq, Eq)]
84pub struct SqlRelationPredicateFact {
85    pub table: String,
86    pub alias: Option<String>,
87    pub constrained_columns: Vec<String>,
88    pub unqualified_columns: Vec<String>,
89    pub line: usize,
90}
91
92/// An `EXISTS` whose subquery uses a set operation.
93#[derive(Debug, Clone, PartialEq, Eq)]
94pub struct SqlExistsSetOpFact {
95    pub restricted: bool,
96    /// Qualified `table.column` whose qualifier is not a local FROM/WITH name.
97    pub correlated: bool,
98    pub line: usize,
99    /// SQL column of the `EXISTS` keyword. Embedded SQL rebases with this column.
100    pub column: usize,
101}
102
103/// A scalar `COUNT(*)` comparison testing presence or absence of matching rows.
104///
105/// `negated` is true when the comparison tests zero matching rows.
106#[derive(Debug, Clone, PartialEq, Eq)]
107pub struct SqlCountExistenceFact {
108    pub line: usize,
109    /// Original SQL column used to map embedded clauses to their source line.
110    pub column: usize,
111    /// True when the comparison tests zero matching rows (NOT EXISTS).
112    pub negated: bool,
113    /// The COUNT belongs to an intact scalar subquery.
114    pub scalar_subquery: bool,
115}