no-mistakes-postgres-sql 0.109.130

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
/// 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,
}