no-mistakes-postgres-sql 0.109.130

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::{Ctes, SingleOutput};
use crate::fx::FxHashMap;
use sqlparser::ast::{Cte, TableFactor, TableWithJoins};
use std::cell::{Cell, RefCell};

const MAX_LAYOUT_VISITS: usize = 512;

#[derive(Hash, PartialEq, Eq)]
pub(in crate::output_label::scalar) struct LayoutKey {
    definition: LayoutDefinition,
    enclosing: Vec<ScopeKey>,
}

#[derive(Hash, PartialEq, Eq)]
enum LayoutDefinition {
    Cte(*const Cte),
    Derived(*const TableFactor),
    Join(*const TableWithJoins),
}

#[derive(Hash, PartialEq, Eq)]
struct ScopeKey {
    local: *const [Cte],
    relations: *const [TableWithJoins],
    before: Option<*const TableFactor>,
}

pub(in crate::output_label::scalar) type Layouts<T> = RefCell<FxHashMap<LayoutKey, Option<T>>>;

#[derive(Default)]
pub(in crate::output_label) struct LayoutCache {
    visits: Cell<usize>,
    pub(in crate::output_label::scalar) output: Layouts<SingleOutput>,
    pub(in crate::output_label::scalar) columns: Layouts<Vec<String>>,
}

impl LayoutCache {
    pub(in crate::output_label::scalar) fn infer<T: Clone>(
        &self,
        layouts: &Layouts<T>,
        key: Option<LayoutKey>,
        visit: impl FnOnce() -> Option<T>,
    ) -> Option<T> {
        if let Some(cached) = key
            .as_ref()
            .and_then(|key| layouts.borrow().get(key).cloned())
        {
            return cached;
        }
        if !self.take_visit() {
            return None;
        }
        let value = visit();
        if let Some(key) = key {
            layouts.borrow_mut().insert(key, value.clone());
        }
        value
    }

    fn take_visit(&self) -> bool {
        let visits = self.visits.get();
        if visits == MAX_LAYOUT_VISITS {
            return false;
        }
        self.visits.set(visits + 1);
        true
    }
}

impl LayoutKey {
    pub(super) fn cte(definition: &Cte, owner: &Ctes<'_>) -> Option<Self> {
        if owner.recursive {
            return None;
        }
        Self::nonrecursive(LayoutDefinition::Cte(definition), owner.parent)
    }

    pub(in crate::output_label::scalar) fn derived(
        factor: &TableFactor,
        visible: &Ctes<'_>,
    ) -> Option<Self> {
        Self::nonrecursive(LayoutDefinition::Derived(factor), Some(visible))
    }

    pub(in crate::output_label::scalar) fn join(
        table: &TableWithJoins,
        visible: &Ctes<'_>,
    ) -> Option<Self> {
        Self::nonrecursive(LayoutDefinition::Join(table), Some(visible))
    }

    fn nonrecursive(definition: LayoutDefinition, visible: Option<&Ctes<'_>>) -> Option<Self> {
        let mut enclosing = Vec::new();
        for scope in std::iter::successors(visible, |scope| scope.parent) {
            if scope.recursive {
                return None;
            }
            enclosing.push(ScopeKey {
                local: std::ptr::from_ref(scope.local),
                relations: std::ptr::from_ref(scope.relations),
                before: scope.before.map(std::ptr::from_ref),
            });
        }
        Some(Self {
            definition,
            enclosing,
        })
    }
}

#[cfg(test)]
mod tests;