no-mistakes-postgres-sql 0.109.128

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::aggregate::orders_by_aggregate;
use super::{items, start, Scope};
use crate::statements::limit::is_zero_limited;
use crate::statements::{SqlBoundInputMode, SqlBoundItemKind, SqlBoundQuery};
use sqlparser::ast::{Query, SetExpr, SetOperator, SetQuantifier, Spanned, Statement};
mod blocking;
mod compact;
mod distinct_on;
mod group_by;
mod outputs;

pub(super) use outputs::caller_value;
mod recursive_depth;
mod recursive_domain;
mod recursive_order;
mod set;
mod with_scope;
pub(super) use with_scope::{with_scope, with_scope_visiting_writes};
#[cfg(all(test, any(nm_shard_all, nm_shard_0)))]
mod tests;
use blocking::BlockingStatuses;

pub(super) fn bound_query(
    query: &Query,
    scope: &Scope,
    positions: super::super::value::PlaceholderPositions<'_>,
) -> SqlBoundQuery {
    bound_body(query, &with_scope(query, scope, positions), positions)
}

/// The `INSERT` / `UPDATE` / `DELETE` / `MERGE` inside a data-modifying CTE.
pub(super) fn modifying_statement(query: &Query) -> Option<&Statement> {
    match &*query.body {
        SetExpr::Update(statement)
        | SetExpr::Delete(statement)
        | SetExpr::Insert(statement)
        | SetExpr::Merge(statement) => Some(statement),
        _ => None,
    }
}

/// The body of `query` under `scope`, which already holds its CTEs.
pub(super) fn bound_body(
    query: &Query,
    scope: &Scope,
    positions: super::super::value::PlaceholderPositions<'_>,
) -> SqlBoundQuery {
    bound_body_observed(query, scope, positions, |_| {})
}

fn bound_body_observed(
    query: &Query,
    scope: &Scope,
    positions: super::super::value::PlaceholderPositions<'_>,
    observe: impl FnOnce(&BlockingStatuses),
) -> SqlBoundQuery {
    let mut bound = set::bound(&query.body, scope, positions);
    scope.expressions_at(
        &query.order_by,
        &bound.items,
        || query.body.span().end,
        positions,
    );
    if let Some(capped) = super::aggregate::order_expansion_predicates_reject(query, positions) {
        bound.capped = capped;
    }
    let (limited, catalog_limit) = super::limit_columns::examine(query, scope, positions);
    // Parentheses keep an inner catalog column. Credit a new one only when LIMIT bounds input.
    if bound.catalog_limit.is_none() && limit_bounds_input(query, scope) {
        bound.catalog_limit = catalog_limit;
    }
    if is_zero_limited(query) {
        bound.capped = true;
        bound.input_mode = SqlBoundInputMode::Skipped;
    } else if (limited || super::scalar_limit::is_limited(query, scope, positions))
        && limit_bounds_input(query, scope)
    {
        if let Some(mut blocking) = blocking_statuses(&query.body) {
            cap_streaming_arms(&query.body, &mut bound, &mut blocking);
            observe(&blocking);
        } else {
            bound.capped = true;
        }
    }
    bound.capped |= orders_by_aggregate(query);
    group_by::apply_order(&mut bound, query, scope);
    // Aggregate/SRF expansion may emit rows despite an empty WHERE input. Preserve its
    // existing uncapped semantics rather than converting that input predicate to a row cap.
    if !bound.capped && bound.input_mode == SqlBoundInputMode::Skipped {
        bound.input_mode = SqlBoundInputMode::Streaming;
    }
    bound
}

fn limit_bounds_input(query: &Query, scope: &Scope) -> bool {
    // WHERE has already removed the input, or grouping has reduced it to the rule's
    // established one-group case before the projected set function runs.
    if let SetExpr::Select(select) = &*query.body {
        if super::predicate::rejects_all(select.selection.as_ref())
            || super::aggregate::has_implicit_group(query, select)
        {
            return true;
        }
    }
    !projection_may_erase_input(&query.body, scope)
        && !super::aggregate::order_may_erase_input(query, scope)
}

fn projection_may_erase_input(set: &SetExpr, scope: &Scope) -> bool {
    match set {
        SetExpr::Select(select) => super::aggregate::projection_may_erase_input(select, scope),
        SetExpr::Query(query) => projection_may_erase_input(&query.body, scope),
        SetExpr::SetOperation { left, right, .. } => {
            projection_may_erase_input(left, scope) || projection_may_erase_input(right, scope)
        }
        _ => false,
    }
}

// A fully streaming query needs only its root cap and no status storage. Once a blocking
// subtree exists, the second linear walk records each relevant subtree exactly once.
fn blocking_statuses(set: &SetExpr) -> Option<BlockingStatuses> {
    if !contains_blocking(set) {
        return None;
    }
    let mut blocking = BlockingStatuses::new();
    requires_complete_arms(set, &mut blocking);
    Some(blocking)
}

fn contains_blocking(set: &SetExpr) -> bool {
    match set {
        SetExpr::SetOperation {
            op, set_quantifier, ..
        } if *op != SetOperator::Union || *set_quantifier != SetQuantifier::All => true,
        SetExpr::SetOperation { left, right, .. } => {
            contains_blocking(left) || contains_blocking(right)
        }
        SetExpr::Query(query) => contains_blocking(&query.body),
        _ => false,
    }
}

/// Non-streaming set operations must inspect their input arms before an outer LIMIT can
/// produce the final rows; UNION ALL can stop as soon as that limit is satisfied.
fn requires_complete_arms(set: &SetExpr, blocking: &mut BlockingStatuses) -> bool {
    blocking.visits += 1;
    let complete = match set {
        SetExpr::SetOperation {
            op,
            set_quantifier,
            left,
            right,
        } if *op == SetOperator::Union && *set_quantifier == SetQuantifier::All => {
            // Visit both arms even when the left one blocks, because capping later
            // needs each streaming sibling's status without another AST scan.
            let left_complete = requires_complete_arms(left, blocking);
            let right_complete = requires_complete_arms(right, blocking);
            left_complete || right_complete
        }
        SetExpr::SetOperation { .. } => true,
        SetExpr::Query(query) => requires_complete_arms(&query.body, blocking),
        _ => false,
    };
    blocking.by_set.insert(set as *const SetExpr, complete);
    complete
}

/// A UNION ALL limit caps each streaming sibling independently. A blocking subtree keeps
/// its input findings even when another sibling can stop after the requested rows.
fn cap_streaming_arms(set: &SetExpr, bound: &mut SqlBoundQuery, blocking: &mut BlockingStatuses) {
    if !blocking.complete(set) {
        bound.capped = true;
        return;
    }
    match set {
        SetExpr::SetOperation {
            op: SetOperator::Union,
            set_quantifier: SetQuantifier::All,
            left,
            right,
        } => {
            for (set, item) in [left, right].into_iter().zip(&mut bound.items) {
                if let SqlBoundItemKind::Query(inner) = &mut item.kind {
                    cap_streaming_arms(set, inner, blocking);
                }
            }
        }
        SetExpr::Query(query) => cap_streaming_arms(&query.body, bound, blocking),
        _ => {}
    }
}

/// A body whose size nothing in the statement text decides: it adds no unbounded relation.
pub(super) fn sized_by_itself(at: (usize, usize)) -> SqlBoundQuery {
    SqlBoundQuery {
        items: vec![items::other(at)],
        ..Default::default()
    }
}