mod aggregate;
mod bind_values;
mod bodies;
pub use bind_values::BindValues;
mod body_owner;
mod catalog_functions;
pub(super) use catalog_functions::needs_catalog;
mod compact_pins;
mod dml;
pub(super) mod functions;
mod inserted;
mod items;
mod limit_columns;
mod pins;
mod predicate;
mod query;
mod returning;
mod scalar_limit;
mod scalar_scope;
mod table;
mod temporary;
pub(crate) use table::TableTokenCursor;
pub(crate) use table::TableTokenIndex;
pub(super) use temporary::TemporaryRelations;
#[cfg(all(test, any(nm_shard_all, nm_shard_1)))]
mod tests;
mod using;
use super::{SqlBoundFact, SqlBoundKind, SqlBoundQuery};
use sqlparser::ast::{CopySource, Query, Spanned, Statement};
use std::collections::{BTreeMap, BTreeSet};
use std::{cell::RefCell, rc::Rc};
pub(super) fn is_aggregate(name: &sqlparser::ast::ObjectName) -> bool {
functions::is_aggregate(name)
}
#[derive(Clone, Default)]
pub(super) struct Scope {
frame: Rc<ScopeFrame>,
bodies: Option<Rc<RefCell<bodies::Collected>>>,
body_owner: Option<SqlBoundKind>,
table_tokens: Option<Rc<RefCell<TableTokenCursor>>>,
limit_columns: limit_columns::Relations,
scalar_functions: std::sync::Arc<BTreeSet<Vec<String>>>,
scalar_scope: Rc<scalar_scope::ScalarScope>,
bind_values: BindValues,
}
#[derive(Clone, Default)]
struct ScopeFrame {
parent: Option<Rc<ScopeFrame>>,
ctes: BTreeMap<String, Rc<SqlBoundQuery>>,
columns: BTreeMap<String, Option<BTreeSet<String>>>,
fixed_counts: BTreeMap<String, BTreeSet<String>>,
}
mod scope;
pub(super) fn collect(
statement: &Statement,
scope: &Scope,
positions: super::value::PlaceholderPositions<'_>,
out: &mut Vec<SqlBoundFact>,
) {
collect_in(statement, scope, positions, out);
scope.finish_bodies(out);
}
fn collect_in(
statement: &Statement,
scope: &Scope,
positions: super::value::PlaceholderPositions<'_>,
out: &mut Vec<SqlBoundFact>,
) {
let scope = scope.with_body_owner(Some(statement));
match statement {
Statement::Query(query) => collect_query(query, &scope, positions, out),
Statement::Copy {
source: CopySource::Query(query),
..
} => collect_query(query, &scope, positions, out),
Statement::Update(update) => dml::update(update, &scope, positions, out),
Statement::Delete(delete) => dml::delete(delete, &scope, positions, out),
_ => {}
}
}
fn collect_query(
query: &Query,
scope: &Scope,
positions: super::value::PlaceholderPositions<'_>,
out: &mut Vec<SqlBoundFact>,
) {
let scope = scope.with_body_owner(query::modifying_statement(query));
let scope = query::with_scope_visiting_writes(query, &scope, positions, |cte, visible| {
collect_query(cte, visible, positions, out);
});
if let Some(with) = query.with.as_ref().filter(|with| with.recursive) {
for cte in &with.cte_tables {
if query::modifying_statement(&cte.query).is_some() {
collect_query(&cte.query, &scope, positions, out);
}
}
}
if let Some(statement) = query::modifying_statement(query) {
collect_in(statement, &scope, positions, out);
return;
}
let (line, column) = start(query.span());
out.push(SqlBoundFact {
body: None,
kind: SqlBoundKind::Select,
line,
column,
statement_start: None,
query: query::bound_body(query, &scope, positions),
target: None,
scalar_reads: None,
lateral_qualified_reads: Vec::new(),
});
}
fn start(span: sqlparser::tokenizer::Span) -> (usize, usize) {
(
(span.start.line as usize).max(1),
(span.start.column as usize).max(1),
)
}
pub fn lookup_query(query: &Query) -> SqlBoundQuery {
query::bound_query(query, &Scope::default(), None)
}