use super::{function_wildcards, has_scoped_reference, local_relation_key, WildcardOwners};
use crate::idents::relation_part_key;
use sqlparser::ast::{Expr, Function, FunctionArguments, Visit};
#[derive(Clone, Copy)]
pub(super) enum ReferenceScope<'a> {
Outside,
Visible(&'a WildcardOwners, Option<&'a WildcardOwners>),
AtRoot(&'a WildcardOwners, bool),
}
impl ReferenceScope<'_> {
pub(super) fn includes_qualifier(self, qualifier: &str) -> bool {
match self {
Self::Outside => true,
Self::Visible(current, _) => current.contains(qualifier),
Self::AtRoot(current, root) => current.qualifier_is_at_root(qualifier) == Some(root),
}
}
}
impl ReferenceScope<'_> {
pub(super) fn includes_column(self, column: &str, inner: &WildcardOwners) -> bool {
let qualifier = relation_part_key(column);
let may_refer_outside = !inner.contains_column(column)
&& !inner.has_unknown_columns()
&& !inner.contains(&qualifier);
match self {
Self::Outside => may_refer_outside,
Self::Visible(current, enclosing) => {
may_refer_outside
&& (current.contains_column(column)
|| current.has_unknown_columns()
|| current.contains(&qualifier)
&& enclosing.is_none_or(|scope| !scope.contains_column(column)))
}
Self::AtRoot(current, root) => {
!inner.contains_column(column)
&& !inner.has_unknown_columns()
&& current.column_is_at_root(column, root).or_else(|| {
let qualifier = relation_part_key(column);
(!inner.contains(&qualifier))
.then(|| current.qualifier_is_at_root(&qualifier))
.flatten()
}) == Some(root)
}
}
}
}
pub(in crate::statements::keyed_lookups) fn belongs_to_root_select(
function: &Function,
current: &WildcardOwners,
) -> bool {
function.over.is_none()
&& argument_references(function, ReferenceScope::AtRoot(current, true))
&& !argument_references(function, ReferenceScope::AtRoot(current, false))
}
fn argument_references(function: &Function, scope: ReferenceScope<'_>) -> bool {
let includes = |expr: &Expr| has_scoped_reference(expr, scope);
let filter = function.filter.as_deref().is_some_and(includes);
if !function.within_group.is_empty() {
return filter
|| function
.within_group
.iter()
.any(|item| includes(&item.expr));
}
filter
|| matches!(&function.args, FunctionArguments::List(list) if has_scoped_reference(list, scope))
|| function_wildcards(function)
.any(|name| scope.includes_qualifier(&local_relation_key(name)))
}
fn has_reference(input: &impl Visit) -> bool {
has_scoped_reference(input, ReferenceScope::Outside)
}
fn has_current_reference(
input: &impl Visit,
current: &WildcardOwners,
enclosing: Option<&WildcardOwners>,
) -> bool {
has_scoped_reference(input, ReferenceScope::Visible(current, enclosing))
}
pub(in crate::statements::keyed_lookups) fn belongs_to_current_select(
function: &Function,
current: &WildcardOwners,
) -> bool {
function.over.is_some()
|| !qualified_outer_argument(function)
|| current_argument(function, current, None)
}
pub(in super::super) fn qualified_outer_argument(function: &Function) -> bool {
if !function.within_group.is_empty() {
return function
.within_group
.iter()
.any(|item| has_reference(&item.expr))
|| function.filter.as_deref().is_some_and(has_reference);
}
matches!(&function.args, FunctionArguments::List(list) if has_reference(list))
|| WildcardOwners::default().uses_outside_wildcard(function, None) == Some(true)
|| function.filter.as_deref().is_some_and(has_reference)
}
pub(in super::super) fn current_argument(
function: &Function,
current: &WildcardOwners,
enclosing: Option<&WildcardOwners>,
) -> bool {
let local_filter = function
.filter
.as_deref()
.is_some_and(|expr| current.uses_local_row(expr, enclosing));
if !function.within_group.is_empty() {
return local_filter
|| function
.within_group
.iter()
.any(|item| has_current_reference(&item.expr, current, enclosing));
}
matches!(&function.args, FunctionArguments::List(list) if has_current_reference(list, current, enclosing))
|| current.uses_outside_wildcard(function, enclosing) == Some(false)
|| local_filter
}