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)
}
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,
}
}
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);
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);
if !bound.capped && bound.input_mode == SqlBoundInputMode::Skipped {
bound.input_mode = SqlBoundInputMode::Streaming;
}
bound
}
fn limit_bounds_input(query: &Query, scope: &Scope) -> bool {
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,
}
}
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,
}
}
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 => {
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
}
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),
_ => {}
}
}
pub(super) fn sized_by_itself(at: (usize, usize)) -> SqlBoundQuery {
SqlBoundQuery {
items: vec![items::other(at)],
..Default::default()
}
}