no-mistakes-postgres-sql 0.109.128

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::Scope;
use crate::statement_facts::{SqlBareViewRowRead, SqlBareViewRowScope};

pub(super) fn record_correlated_columns(
    scopes: &[Scope],
    inner: usize,
    name: &str,
    candidates: &mut [super::super::Candidate],
) {
    for scope in &scopes[..inner] {
        for &index in scope.candidates.iter().chain(
            scope
                .join_outputs
                .values()
                .flat_map(|output| &output.candidates),
        ) {
            let read = bare_read(scopes, index, name.to_owned());
            let columns = &mut candidates[index].correlated_columns;
            if !columns.contains(&read) {
                columns.push(read);
            }
        }
    }
}

pub(super) fn bare_read(scopes: &[Scope], index: usize, name: String) -> SqlBareViewRowRead {
    let scopes = scopes
        .iter()
        .rev()
        .map(|scope| {
            let mut level = SqlBareViewRowScope {
                view_input: false,
                known_peer_column: scope.known_columns.contains(&name),
                peers: Vec::new(),
                opaque_peer: false,
            };
            let joins = scope
                .join_outputs
                .iter()
                .filter(|(name, _)| scope.bindings.contains_key(*name));
            level.opaque_peer = scope.inputs
                > scope.known_inputs + scope.physical_inputs.len() + joins.clone().count();
            for (_, output) in joins.clone() {
                level.known_peer_column |= output.known_columns.contains(&name);
                level.opaque_peer |= output.opaque_input;
            }
            let physical = scope
                .physical_inputs
                .iter()
                .chain(joins.flat_map(|(_, output)| &output.physical_inputs));
            for input in physical {
                if input.candidate == Some(index) {
                    level.view_input = true;
                } else if input.renamed {
                    level.opaque_peer = true;
                } else {
                    level.peers.push(input.relation.clone());
                }
            }
            level.peers.sort_unstable();
            level.peers.dedup();
            level
        })
        .collect();
    SqlBareViewRowRead { name, scopes }
}

pub(super) fn record_bare_row_read(
    candidate: &mut super::super::Candidate,
    read: SqlBareViewRowRead,
) {
    if read
        .scopes
        .iter()
        .any(|scope| scope.known_peer_column || scope.opaque_peer || !scope.peers.is_empty())
    {
        if !candidate.bare_row_reads.contains(&read) {
            candidate.bare_row_reads.push(read);
        }
    } else {
        candidate.other_use = true;
    }
}