use crate::{
db::query::{
builder::AggregateExpr,
plan::{
AggregateKind, FieldSlot, GroupAggregateSpec, GroupHavingClause, GroupHavingSpec,
GroupHavingSymbol, GroupSpec, GroupedExecutionConfig,
},
},
error::InternalError,
model::{
entity::{EntityModel, resolve_field_slot},
field::FieldKind,
},
value::Value,
};
impl GroupAggregateSpec {
#[must_use]
pub(in crate::db) fn from_aggregate_expr(aggregate: &AggregateExpr) -> Self {
Self {
kind: aggregate.kind(),
target_field: aggregate.target_field().map(str::to_string),
distinct: aggregate.is_distinct(),
}
}
#[must_use]
pub(crate) const fn kind(&self) -> AggregateKind {
self.kind
}
#[must_use]
pub(crate) fn target_field(&self) -> Option<&str> {
self.target_field.as_deref()
}
#[must_use]
pub(crate) const fn distinct(&self) -> bool {
self.distinct
}
#[must_use]
pub(in crate::db) const fn streaming_compatible_v1(&self) -> bool {
match self.kind {
AggregateKind::Count => !self.distinct,
AggregateKind::Sum | AggregateKind::Avg | AggregateKind::Min | AggregateKind::Max => {
!self.distinct && self.target_field.is_some()
}
AggregateKind::Exists | AggregateKind::First | AggregateKind::Last => {
self.target_field.is_none()
&& (!self.distinct || self.kind.supports_grouped_distinct_v1())
}
}
}
}
impl GroupSpec {
#[must_use]
pub(in crate::db) fn global_distinct_shape_from_aggregate_expr(
aggregate: &AggregateExpr,
execution: GroupedExecutionConfig,
) -> Self {
Self {
group_fields: Vec::new(),
aggregates: vec![GroupAggregateSpec::from_aggregate_expr(aggregate)],
execution,
}
}
}
impl GroupHavingSpec {
#[must_use]
pub(crate) const fn clauses(&self) -> &[GroupHavingClause] {
self.clauses.as_slice()
}
}
impl GroupHavingClause {
#[must_use]
pub(crate) const fn symbol(&self) -> &GroupHavingSymbol {
&self.symbol
}
#[must_use]
pub(crate) const fn op(&self) -> crate::db::predicate::CompareOp {
self.op
}
#[must_use]
pub(crate) const fn value(&self) -> &Value {
&self.value
}
pub(in crate::db) fn unsupported_operator(
op: crate::db::predicate::CompareOp,
) -> InternalError {
InternalError::query_executor_invariant(format!(
"unsupported grouped HAVING operator reached executor: {op:?}",
))
}
}
impl GroupHavingSymbol {
pub(in crate::db) fn grouped_key_must_be_list(value: &Value) -> InternalError {
InternalError::query_executor_invariant(format!(
"grouped HAVING requires list-shaped grouped keys, found {value:?}",
))
}
pub(in crate::db) fn field_not_in_group_key_projection(field: &str) -> InternalError {
InternalError::query_executor_invariant(format!(
"grouped HAVING field is not in grouped key projection: field='{field}'",
))
}
pub(in crate::db) fn group_key_offset_out_of_bounds(
clause_index: usize,
offset: usize,
key_len: usize,
) -> InternalError {
InternalError::query_executor_invariant(format!(
"grouped HAVING group key offset out of bounds: clause_index={clause_index}, offset={offset}, key_len={key_len}",
))
}
pub(in crate::db) fn aggregate_index_out_of_bounds(
clause_index: usize,
aggregate_index: usize,
aggregate_count: usize,
) -> InternalError {
InternalError::query_executor_invariant(format!(
"grouped HAVING aggregate index out of bounds: clause_index={clause_index}, aggregate_index={aggregate_index}, aggregate_count={aggregate_count}",
))
}
}
impl FieldSlot {
#[must_use]
pub(crate) fn resolve(model: &EntityModel, field: &str) -> Option<Self> {
let index = resolve_field_slot(model, field)?;
let canonical = model
.fields
.get(index)
.map_or(field, |model_field| model_field.name);
Some(Self {
index,
field: canonical.to_string(),
kind: model.fields.get(index).map(|field| field.kind),
})
}
#[must_use]
pub(crate) const fn index(&self) -> usize {
self.index
}
#[must_use]
pub(crate) fn field(&self) -> &str {
&self.field
}
#[must_use]
pub(crate) const fn kind(&self) -> Option<FieldKind> {
self.kind
}
}