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}
30
31/// Where a bare column reference was used.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum SqlColumnClause {
34 Where,
35 Join,
36 OrderBy,
37}
38
39/// A bare column reference resolved to a base relation when possible.
40///
41/// `table` is empty when the name is unqualified and more than one base
42/// relation is in scope. The rule may still assign it from schema facts.
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct SqlColumnUseFact {
45 pub table: String,
46 pub column: String,
47 /// Base relations visible at this expression; None when a CTE or derived relation prevents ownership proof.
48 pub candidate_tables: Option<Vec<String>>,
49 pub clause: SqlColumnClause,
50 pub line: usize,
51}
52
53/// A star projection resolved to one base relation.
54///
55/// `within_function` is set when the star is an argument (`row_to_json(o.*)`).
56/// Bare `COUNT(*)` is not recorded. The rule applies `allowWholeRowFunctions`
57/// to that name; the statement pass does not know the configured list.
58#[derive(Debug, Clone, PartialEq, Eq)]
59pub struct SqlStarProjectionFact {
60 pub relation: String,
61 pub qualified: bool,
62 pub within_function: Option<String>,
63 pub line: usize,
64}
65
66/// One base-table instance and the columns a predicate constrains on it.
67///
68/// `constrained_columns` are resolved (qualified, or unqualified in a
69/// single-item FROM). `unqualified_columns` are bare names from a
70/// multi-relation FROM whose owner a configured catalog may still prove.
71#[derive(Debug, Clone, PartialEq, Eq)]
72pub struct SqlRelationPredicateFact {
73 pub table: String,
74 pub alias: Option<String>,
75 pub constrained_columns: Vec<String>,
76 pub unqualified_columns: Vec<String>,
77 pub line: usize,
78}
79
80/// An `EXISTS` whose subquery uses a set operation.
81#[derive(Debug, Clone, PartialEq, Eq)]
82pub struct SqlExistsSetOpFact {
83 pub restricted: bool,
84 /// Qualified `table.column` whose qualifier is not a local FROM/WITH name.
85 pub correlated: bool,
86 pub line: usize,
87 /// SQL column of the `EXISTS` keyword. Embedded SQL rebases with this column.
88 pub column: usize,
89}
90
91/// A scalar `COUNT(*)` comparison testing presence or absence of matching rows.
92///
93/// `negated` is true when the comparison tests zero matching rows.
94#[derive(Debug, Clone, PartialEq, Eq)]
95pub struct SqlCountExistenceFact {
96 pub line: usize,
97 /// Original SQL column used to map embedded clauses to their source line.
98 pub column: usize,
99 /// True when the comparison tests zero matching rows (NOT EXISTS).
100 pub negated: bool,
101}