use super::functions::{
data_backed_projection, projection_can_expand, projection_traversal_boundary,
};
use crate::idents::visit_child_exprs;
use sqlparser::ast::{Distinct, Expr, Function, OrderByKind, Query, Select, SelectItem, SetExpr};
mod implicit_group;
use implicit_group::contains_plain_aggregate;
pub(super) use implicit_group::has_implicit_group;
pub(super) fn pure_aggregate(select: &Select) -> bool {
one_group(select)
&& (projected(select)
.iter()
.any(|expr| contains_plain_aggregate(expr))
|| select.having.is_some())
}
pub(super) fn expands_from_data(
select: &Select,
positions: super::super::value::PlaceholderPositions<'_>,
) -> bool {
if super::predicate::rejects_groups(select.having.as_ref()) {
return false;
}
projected(select).iter().any(|expr| {
contains_projection_call(expr, &|function| {
data_backed_projection(function, positions)
})
})
}
pub(super) fn projection_may_erase_input(select: &Select, scope: &super::Scope) -> bool {
!select.from.is_empty()
&& projected(select)
.iter()
.any(|expr| contains_expanding_call(expr, scope))
}
fn contains_expanding_call(expr: &Expr, scope: &super::Scope) -> bool {
if let Expr::Function(function) = expr {
if projection_can_expand(function) && !scope.scalar_function(&function.name) {
return true;
}
}
let mut found = false;
visit_child_exprs(expr, &mut |child| {
found |= contains_expanding_call(child, scope)
});
found
}
pub(super) fn order_may_erase_input(query: &Query, scope: &super::Scope) -> bool {
let Some(order) = &query.order_by else {
return false;
};
let OrderByKind::Expressions(expressions) = &order.kind else {
return false;
};
expressions
.iter()
.any(|expression| contains_expanding_call(&expression.expr, scope))
}
pub(super) fn orders_by_aggregate(query: &Query) -> bool {
let (SetExpr::Select(select), Some(order)) = (&*query.body, &query.order_by) else {
return false;
};
let OrderByKind::Expressions(expressions) = &order.kind else {
return false;
};
one_group(select)
&& !orders_can_expand(query)
&& expressions
.iter()
.any(|expression| contains_plain_aggregate(&expression.expr))
}
pub(super) fn orders_can_expand(query: &Query) -> bool {
let Some(order) = &query.order_by else {
return false;
};
let OrderByKind::Expressions(expressions) = &order.kind else {
return false;
};
expressions
.iter()
.any(|expression| contains_projection_call(&expression.expr, &projection_can_expand))
}
pub(super) fn order_expansion_predicates_reject(
query: &Query,
positions: super::super::value::PlaceholderPositions<'_>,
) -> Option<bool> {
if !orders_can_expand(query) {
return None;
}
let (select, nested_capped) = select_body(&query.body, positions)?;
Some(
nested_capped
|| super::predicate::rejects_groups(select.having.as_ref())
|| (!has_implicit_group(query, select)
&& super::predicate::rejects_all(select.selection.as_ref())),
)
}
fn select_body<'a>(
set: &'a SetExpr,
positions: super::super::value::PlaceholderPositions<'_>,
) -> Option<(&'a Select, bool)> {
match set {
SetExpr::Select(select) => Some((select, false)),
SetExpr::Query(query) => {
let (select, capped) = select_body(&query.body, positions)?;
Some((
select,
capped || super::super::limit::is_limited_at(query, positions),
))
}
_ => None,
}
}
fn one_group(select: &Select) -> bool {
implicit_group::ungrouped(select)
&& !projected(select)
.iter()
.any(|expr| contains_projection_call(expr, &projection_can_expand))
&& (!distinct_on_expands(select) || super::predicate::rejects_all(select.having.as_ref()))
}
pub(super) fn distinct_on_expands(select: &Select) -> bool {
matches!(
&select.distinct,
Some(Distinct::On(expressions))
if expressions.iter().any(|expr| contains_projection_call(expr, &|function| {
projection_can_expand(function)
}))
)
}
fn projected(select: &Select) -> Vec<&Expr> {
select
.projection
.iter()
.filter_map(|item| match item {
SelectItem::UnnamedExpr(expr) | SelectItem::ExprWithAlias { expr, .. } => Some(expr),
_ => None,
})
.collect()
}
fn contains_projection_call(expr: &Expr, test: &impl Fn(&Function) -> bool) -> bool {
if matches!(expr, Expr::Case { .. }) {
return false;
}
if let Expr::Function(function) = expr {
if test(function) {
return true;
}
if projection_traversal_boundary(function) {
return false;
}
}
let mut found = false;
visit_child_exprs(expr, &mut |child| {
found = found || contains_projection_call(child, test)
});
found
}