use super::super::{items, start, Scope};
use super::compact::{compact, size, MAX_BOUND_ITEMS};
use super::{bound_query, modifying_statement, recursive_order};
use crate::idents::ident_key;
use crate::statements::SqlBoundQuery;
use sqlparser::ast::{Query, Spanned};
use std::borrow::Cow;
pub(in crate::statements::bounds) fn with_scope<'a>(
query: &Query,
outer: &'a Scope,
positions: super::super::super::value::PlaceholderPositions<'_>,
) -> Cow<'a, Scope> {
with_scope_visiting_writes(query, outer, positions, |_, _| {})
}
pub(in crate::statements::bounds) fn with_scope_visiting_writes<'a>(
query: &Query,
outer: &'a Scope,
positions: super::super::super::value::PlaceholderPositions<'_>,
mut visit: impl FnMut(&Query, &Scope),
) -> Cow<'a, Scope> {
let Some(with) = &query.with else {
return Cow::Borrowed(outer);
};
let mut scope = outer.child();
let donors = with
.recursive
.then(|| super::recursive_domain::stable_donors(&with.cte_tables));
if with.recursive {
for cte in &with.cte_tables {
let name = ident_key(&cte.alias.name);
scope.insert(name.clone(), opaque(start(cte.query.span())));
scope.limit_columns.record(name, None);
}
}
let order = if with.recursive {
recursive_order::indices(&with.cte_tables)
} else {
(0..with.cte_tables.len()).collect()
};
for index in order {
let cte = &with.cte_tables[index];
if !with.recursive && modifying_statement(&cte.query).is_some() {
visit(&cte.query, &scope);
}
let name = ident_key(&cte.alias.name);
let mut bound = if let Some(statement) = modifying_statement(&cte.query) {
if with.recursive {
opaque(start(cte.query.span()))
} else {
super::super::dml::returning_bound(&cte.query, statement, &scope, positions)
.unwrap_or_else(|| opaque(start(cte.query.span())))
}
} else if with.recursive {
let mut inner = scope.child();
inner.insert(
name.clone(),
if super::recursive_depth::finite(cte) {
super::sized_by_itself(start(cte.query.span()))
} else {
opaque(start(cte.query.span()))
},
);
bound_query(&cte.query, &inner, positions)
} else {
bound_query(&cte.query, &scope, positions)
};
if let Some(donors) = &donors {
super::recursive_domain::project(cte, donors, &mut bound);
}
if cte.materialized == Some(sqlparser::ast::CteAsMaterialized::Materialized) {
scope.record_body(
&bound,
|| start(cte.alias.name.span),
|| format!("MATERIALIZED CTE {name}"),
);
}
if size(&bound) > MAX_BOUND_ITEMS {
bound = compact(&bound, start(cte.query.span()));
}
for (output, alias) in bound.outputs.iter_mut().zip(&cte.alias.columns) {
output.name = Some(ident_key(&alias.name));
}
let columns = if cte.alias.columns.is_empty() {
super::super::pins::projection_columns(&cte.query)
} else {
Some(
cte.alias
.columns
.iter()
.map(|column| ident_key(&column.name))
.collect(),
)
};
scope.set_columns(name.clone(), columns);
let counts =
super::super::scalar_limit::outputs(&cte.query, &scope, positions, &cte.alias.columns);
scope.insert(name.clone(), bound);
if cte.materialized == Some(sqlparser::ast::CteAsMaterialized::Materialized) {
scope.mark_pool(&name);
}
if let Some(counts) = counts {
scope.insert_counts(name.clone(), counts);
}
let columns = cte
.alias
.columns
.iter()
.map(|column| ident_key(&column.name))
.collect::<Vec<_>>();
let proof = super::super::limit_columns::of_query(
&cte.query,
positions,
&scope.limit_columns,
&columns,
);
scope.limit_columns.record(name, proof);
}
Cow::Owned(scope)
}
fn opaque(at: (usize, usize)) -> SqlBoundQuery {
SqlBoundQuery {
input_mode: Default::default(),
outputs: Vec::new(),
capped: false,
items: vec![items::opaque(at)],
group_constant: false,
group_keys: Vec::new(),
catalog_limit: None,
}
}