#[cfg(feature = "sql")]
use crate::db::query::plan::expr::ProjectionSelection;
use crate::db::{
predicate::Predicate,
query::{
intent::{
IntentError, KeyAccess, KeyAccessKind, KeyAccessState,
state::{GroupedIntent, NormalizedFilter, QueryIntent},
},
plan::{
FieldSlot, GroupAggregateSpec, GroupedExecutionConfig, OrderSpec, OrderTerm,
expr::{BinaryOp, Expr, canonicalize_grouped_having_bool_expr, normalize_bool_expr},
},
},
};
impl<K> QueryIntent<K> {
pub(in crate::db::query::intent) fn append_filter_expr(&mut self, expr: Expr) {
self.append_normalized_filter(NormalizedFilter::from_normalized_expr(expr));
}
pub(in crate::db::query::intent) fn append_predicate(&mut self, predicate: Predicate) {
self.append_normalized_filter(NormalizedFilter::from_normalized_predicate(predicate));
}
pub(in crate::db::query::intent) fn append_filter_with_predicate_subset(
&mut self,
expr: Expr,
predicate: Predicate,
) {
self.append_normalized_filter(NormalizedFilter::from_normalized_expr_and_predicate_subset(
expr, predicate,
));
}
fn append_normalized_filter(&mut self, filter: NormalizedFilter) {
let scalar = self.scalar_mut();
match scalar.filter.as_mut() {
Some(existing) => existing.append(filter),
None => scalar.filter = Some(filter),
}
}
pub(in crate::db::query::intent) fn push_order_term(&mut self, term: OrderTerm) {
let scalar = self.scalar_mut();
scalar.order = Some(match scalar.order.take() {
Some(mut spec) => {
spec.fields.push(term);
spec
}
None => OrderSpec { fields: vec![term] },
});
}
pub(in crate::db::query::intent) fn set_order_spec(&mut self, order: OrderSpec) {
self.scalar_mut().order = Some(order);
}
pub(in crate::db::query::intent) const fn set_distinct(&mut self) {
self.scalar_mut().distinct = true;
}
#[cfg(feature = "sql")]
pub(in crate::db::query::intent) fn set_projection_selection(
&mut self,
projection_selection: ProjectionSelection,
) {
self.scalar_mut().projection_selection = projection_selection;
}
pub(in crate::db::query::intent) fn set_by_id(&mut self, id: K) {
self.set_key_access(KeyAccessKind::Single, KeyAccess::Single(id));
}
pub(in crate::db::query::intent) fn set_by_ids<I>(&mut self, ids: I)
where
I: IntoIterator<Item = K>,
{
self.set_key_access(
KeyAccessKind::Many,
KeyAccess::Many(ids.into_iter().collect()),
);
}
pub(in crate::db::query::intent) fn set_only(&mut self, id: K) {
self.set_key_access(KeyAccessKind::Only, KeyAccess::Single(id));
}
pub(in crate::db::query::intent) fn push_group_field_slot(&mut self, field_slot: FieldSlot) {
let Some(grouped) = self.grouped_mutation_target() else {
return;
};
let group = &mut grouped.group;
if !group
.group_fields
.iter()
.any(|existing| existing.index() == field_slot.index())
{
group.group_fields.push(field_slot);
}
}
pub(in crate::db::query::intent) fn push_group_aggregate(
&mut self,
aggregate: GroupAggregateSpec,
) {
let Some(grouped) = self.grouped_mutation_target() else {
return;
};
grouped.group.aggregates.push(aggregate);
}
pub(in crate::db::query::intent) fn set_grouped_limits(
&mut self,
max_groups: u64,
max_group_bytes: u64,
) {
let Some(grouped) = self.grouped_mutation_target() else {
return;
};
grouped.group.execution =
GroupedExecutionConfig::with_hard_limits(max_groups, max_group_bytes);
}
pub(in crate::db::query::intent) fn push_having_expr(
&mut self,
expr: Expr,
) -> Result<(), IntentError> {
self.push_having_expr_with_policy(expr, true)
}
pub(in crate::db::query::intent) fn push_having_expr_preserving_shape(
&mut self,
expr: Expr,
) -> Result<(), IntentError> {
self.push_having_expr_with_policy(expr, false)
}
fn push_having_expr_with_policy(
&mut self,
expr: Expr,
canonicalize_case_semantics: bool,
) -> Result<(), IntentError> {
if matches!(self, Self::Delete(_)) {
if self.is_grouped() {
self.mark_delete_grouping_requested();
return Ok(());
}
return Err(IntentError::having_requires_group_by());
}
let Some(grouped) = self.grouped_mut() else {
return Err(IntentError::having_requires_group_by());
};
let combined = match grouped.having_expr.take() {
Some(existing) => Expr::Binary {
op: BinaryOp::And,
left: Box::new(existing),
right: Box::new(expr),
},
None => expr,
};
let canonical = if canonicalize_case_semantics {
canonicalize_grouped_having_bool_expr(combined)
} else {
normalize_bool_expr(combined)
};
grouped.having_expr = Some(canonical);
Ok(())
}
fn set_key_access(&mut self, kind: KeyAccessKind, access: KeyAccess<K>) {
let scalar = self.scalar_mut();
if let Some(existing) = &scalar.key_access
&& existing.kind != kind
{
scalar.key_access_conflict = true;
}
scalar.key_access = Some(KeyAccessState { kind, access });
}
fn grouped_mutation_target(&mut self) -> Option<&mut GroupedIntent<K>> {
if matches!(self, Self::Delete(_)) {
self.mark_delete_grouping_requested();
return None;
}
Some(self.ensure_grouped_mut())
}
}