no-mistakes-postgres-sql 0.109.131

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::{LateralDependencies, RowDependencies};
use crate::fx::{FxHashMap, FxHashSet};
use crate::statement_facts::SqlViewKeyDependencies;
use sqlparser::ast::{Expr, Query, Select};

mod labels;
mod projection;
mod row_functions;

#[derive(Default)]
pub(super) struct OutputUses {
    projection: FxHashMap<*const Expr, String>,
    root_select: Option<*const Select>,
    pub(super) projection_depth: usize,
    inherited_limits: FxHashSet<*const Query>,
    pub(super) factor_queries: FxHashSet<*const Query>,
    direct_grouping: FxHashSet<*const Expr>,
    active_grouping: Option<ClauseDependencies>,
    active: Option<(*const Expr, String, RowDependencies)>,
    columns: FxHashMap<String, RowDependencies>,
    pub(super) wildcard_columns: Option<RowDependencies>,
    pub(super) labels: FxHashSet<*const Expr>,
    shared_outputs: FxHashSet<*const Expr>,
    aggregate_outputs: FxHashSet<*const Expr>,
    aggregate_names: Vec<(String, bool)>,
    grouping_outputs: FxHashSet<*const Expr>,
    conditional_shared_outputs: FxHashMap<*const Expr, SqlViewKeyDependencies>,
    conditional_grouping_outputs: FxHashMap<*const Expr, SqlViewKeyDependencies>,
    group_dependencies: RowDependencies,
    row_selecting_groups: FxHashSet<*const sqlparser::ast::Select>,
    sort_only: FxHashMap<*const Expr, bool>,
    active_sort: Option<ClauseDependencies>,
}

struct ClauseDependencies {
    expression: *const Expr,
    previous: RowDependencies,
    may_create_group: bool,
    affects_rows: bool,
    uses_grouping: bool,
}

impl OutputUses {
    pub(super) fn enter(&mut self, expression: &Expr, dependencies: &mut RowDependencies) {
        let pointer = std::ptr::from_ref(expression);
        if self.direct_grouping.remove(&pointer) {
            self.active_grouping = Some(ClauseDependencies {
                expression: pointer,
                previous: std::mem::take(dependencies),
                may_create_group: false,
                affects_rows: false,
                uses_grouping: false,
            });
        }
        if let Some(may_create_group) = self.sort_only.remove(&pointer) {
            self.active_sort = Some(ClauseDependencies {
                expression: pointer,
                previous: std::mem::take(dependencies),
                may_create_group,
                affects_rows: false,
                uses_grouping: false,
            });
        }
        if let Some(name) = self.projection.remove(&pointer) {
            self.active = Some((pointer, name, std::mem::take(dependencies)));
        }
    }

    pub(super) fn leave(&mut self, expression: &Expr, dependencies: &mut RowDependencies) {
        if let Some(grouping) = self
            .active_grouping
            .take_if(|grouping| std::ptr::eq(grouping.expression, expression))
        {
            let grouping_dependencies = std::mem::replace(dependencies, grouping.previous);
            if grouping.affects_rows {
                dependencies.extend(grouping_dependencies.iter().cloned());
            }
            self.group_dependencies.extend(grouping_dependencies);
        }
        if let Some(mut sort) = self
            .active_sort
            .take_if(|sort| std::ptr::eq(sort.expression, expression))
        {
            if sort.affects_rows {
                sort.previous.extend(std::mem::take(dependencies));
                if sort.uses_grouping {
                    sort.previous
                        .extend(self.group_dependencies.iter().cloned());
                }
            }
            *dependencies = sort.previous;
        }
        if let Some((pointer, name, mut shared)) = self
            .active
            .take_if(|(pointer, _, _)| std::ptr::eq(*pointer, expression))
        {
            if self.shared_outputs.contains(&pointer) {
                shared.extend(dependencies.iter().cloned());
            }
            if self.grouping_outputs.contains(&pointer) {
                self.group_dependencies.extend(dependencies.iter().cloned());
            }
            if let Some(guards) = self.conditional_shared_outputs.remove(&pointer) {
                shared.extend(guards.iter().flat_map(|guards| {
                    dependencies.iter().cloned().map(|dep| dep.guarded(guards))
                }));
            }
            if let Some(guards) = self.conditional_grouping_outputs.remove(&pointer) {
                self.group_dependencies
                    .extend(guards.iter().flat_map(|guards| {
                        dependencies.iter().cloned().map(|dep| dep.guarded(guards))
                    }));
            }
            if self.aggregate_outputs.contains(&pointer) {
                self.aggregate_names
                    .push((name.clone(), self.shared_outputs.contains(&pointer)));
            }
            self.columns
                .entry(name)
                .or_default()
                .extend(std::mem::replace(dependencies, shared));
        }
    }

    pub(super) fn finish(mut self, mut shared: RowDependencies) -> LateralDependencies {
        for (name, shares_grouping) in self.aggregate_names {
            if shares_grouping {
                shared.extend(self.group_dependencies.iter().cloned());
            }
            self.columns
                .entry(name)
                .or_default()
                .extend(self.group_dependencies.iter().cloned());
        }
        let mut result = LateralDependencies::with_columns(shared, self.columns);
        result
            .all
            .extend(self.wildcard_columns.into_iter().flatten());
        result
    }
}