use crate::idents::object_name_identity;
use sqlparser::ast::{
Expr, Function, FunctionArg, FunctionArgExpr, FunctionArgumentClause, FunctionArguments,
ObjectName, Select, SelectItem, SelectItemQualifiedWildcardKind, TableFactor,
};
mod aggregate_scope;
mod bindings;
mod catalog;
mod filter;
mod shape;
mod visibility;
use visibility::{LocalScope, QueryScope, SavedInputs};
#[derive(Default)]
pub(in crate::statements::keyed_lookups) struct WildcardOwners {
local: Vec<LocalScope>,
saved: Vec<SavedInputs>,
predicates: Vec<(*const Expr, Vec<LocalScope>)>,
queries: Vec<QueryScope>,
}
impl WildcardOwners {
pub(in crate::statements::keyed_lookups) fn in_query(&self) -> bool {
!self.queries.is_empty()
}
pub(in crate::statements::keyed_lookups) fn enter_select(&mut self, select: &Select) {
self.enter_catalog_select(select, None);
}
pub(in crate::statements::keyed_lookups) fn leave_select(&mut self, select: &Select) {
if let Some(scope) = self
.queries
.last_mut()
.filter(|scope| scope.direct_select == Some(std::ptr::from_ref(select)))
{
scope.retained = true;
} else {
self.leave_scope();
}
}
fn leave_scope(&mut self) {
self.local.pop();
}
pub(in crate::statements::keyed_lookups) fn contains(&self, qualifier: &str) -> bool {
self.local
.iter()
.rev()
.any(|scope| scope.inputs.qualifiers.contains_key(qualifier))
}
pub(in crate::statements::keyed_lookups) fn contains_column(&self, column: &str) -> bool {
self.local
.iter()
.any(|scope| scope.inputs.columns.contains(column))
}
pub(in crate::statements::keyed_lookups) fn uses_local_row(
&self,
expr: &Expr,
enclosing: Option<&Self>,
) -> bool {
filter::uses_local_row(expr, self, enclosing)
}
pub(in crate::statements::keyed_lookups) fn has_unknown_columns(&self) -> bool {
self.local.iter().any(|scope| scope.inputs.unknown_columns)
}
pub(in crate::statements::keyed_lookups) fn current_column_exists(
&self,
name: &str,
) -> Option<bool> {
self.local.last().map_or(Some(false), |scope| {
if scope.inputs.columns.contains(name) {
Some(true)
} else {
(!scope.inputs.unknown_columns).then_some(false)
}
})
}
pub(in crate::statements::keyed_lookups) fn uses_outside_wildcard(
&self,
function: &Function,
enclosing: Option<&Self>,
) -> Option<bool> {
let FunctionArguments::List(list) = &function.args else {
return None;
};
let mut outside = None;
let mut local_argument = false;
for arg in &list.args {
let arg = argument_expr(arg);
let name = match arg {
FunctionArgExpr::QualifiedWildcard(name)
| FunctionArgExpr::Expr(Expr::QualifiedWildcard(name, _)) => name,
FunctionArgExpr::Expr(expr) => {
local_argument |= filter::uses_local_row(expr, self, enclosing);
continue;
}
_ => continue,
};
let qualifier = local_relation_key(name);
if self.contains(&qualifier) {
return Some(false);
}
outside = Some(true);
}
if outside.is_some()
&& (local_argument
|| function
.filter
.as_deref()
.is_some_and(|condition| filter::uses_local_row(condition, self, enclosing))
|| list.clauses.iter().any(|clause| match clause {
FunctionArgumentClause::OrderBy(ordering) => ordering
.iter()
.any(|item| filter::uses_local_row(&item.expr, self, enclosing)),
_ => false,
}))
{
return Some(false);
}
outside
}
}
fn argument_expr(arg: &FunctionArg) -> &FunctionArgExpr {
match arg {
FunctionArg::Unnamed(arg)
| FunctionArg::Named { arg, .. }
| FunctionArg::ExprNamed { arg, .. } => arg,
}
}
pub(in crate::statements::keyed_lookups) fn function_wildcards(
function: &Function,
) -> impl Iterator<Item = &ObjectName> {
let args = match &function.args {
FunctionArguments::List(list) => list.args.as_slice(),
_ => &[],
};
argument_wildcards(args)
}
pub(in crate::statements::keyed_lookups) fn factor_wildcards(
factor: &TableFactor,
) -> impl Iterator<Item = &ObjectName> {
let args = match factor {
TableFactor::Function { args, .. } => args.as_slice(),
TableFactor::Table {
args: Some(args), ..
} => args.args.as_slice(),
_ => &[],
};
argument_wildcards(args)
}
pub(in crate::statements::keyed_lookups) fn projection_wildcards(
projection: &[SelectItem],
) -> impl Iterator<Item = &ObjectName> {
projection.iter().filter_map(|item| match item {
SelectItem::QualifiedWildcard(SelectItemQualifiedWildcardKind::ObjectName(name), _) => {
Some(name)
}
_ => None,
})
}
fn argument_wildcards(args: &[FunctionArg]) -> impl Iterator<Item = &ObjectName> {
args.iter().filter_map(|arg| match argument_expr(arg) {
FunctionArgExpr::QualifiedWildcard(name) => Some(name),
_ => None,
})
}
pub(in crate::statements::keyed_lookups) fn local_relation_key(name: &ObjectName) -> String {
if name.0.len() == 3 {
object_name_identity(&ObjectName(name.0[1..].to_vec()))
} else {
object_name_identity(name)
}
}