use crate::db::query::{
builder::AggregateExpr,
plan::{AggregateIdentity, AggregateKind, AggregateSemanticKey, FieldSlot, expr::Expr},
};
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db::sql::lowering::aggregate) struct AggregateTerminalSemanticKey {
semantic_key: AggregateSemanticKey,
}
impl AggregateTerminalSemanticKey {
#[must_use]
pub(in crate::db::sql::lowering::aggregate) fn from_aggregate_expr(
aggregate_expr: &AggregateExpr,
) -> Self {
Self {
semantic_key: AggregateSemanticKey::from_aggregate_expr(aggregate_expr),
}
}
pub(in crate::db::sql::lowering::aggregate) fn into_identity_and_filter(
self,
) -> (AggregateIdentity, Option<Expr>) {
self.semantic_key.into_identity_and_filter()
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db::sql::lowering::aggregate) enum PreparedAggregateTarget {
Rows,
Field(FieldSlot),
Expr(Expr),
}
impl PreparedAggregateTarget {
#[cfg(any(test, feature = "sql-explain"))]
pub(in crate::db::sql::lowering::aggregate) const fn field_slot(&self) -> Option<&FieldSlot> {
match self {
Self::Field(field_slot) => Some(field_slot),
Self::Rows | Self::Expr(_) => None,
}
}
#[cfg(test)]
pub(in crate::db::sql::lowering::aggregate) const fn input_expr(&self) -> Option<&Expr> {
match self {
Self::Expr(input_expr) => Some(input_expr),
Self::Rows | Self::Field(_) => None,
}
}
pub(in crate::db::sql::lowering::aggregate) fn into_terminal_inputs(
self,
) -> (Option<FieldSlot>, Option<Expr>) {
match self {
Self::Rows => (None, None),
Self::Field(target_slot) => (Some(target_slot), None),
Self::Expr(input_expr) => (None, Some(input_expr)),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db::sql::lowering::aggregate) enum PreparedAggregateSemantics {
Count {
target: PreparedAggregateTarget,
distinct: bool,
},
Sum {
target: PreparedAggregateTarget,
distinct: bool,
},
Avg {
target: PreparedAggregateTarget,
distinct: bool,
},
Min {
target: PreparedAggregateTarget,
},
Max {
target: PreparedAggregateTarget,
},
}
impl PreparedAggregateSemantics {
pub(in crate::db::sql::lowering::aggregate) fn from_kind_target_and_distinct(
kind: AggregateKind,
target: PreparedAggregateTarget,
distinct: bool,
) -> Self {
match kind {
AggregateKind::Count => Self::Count { target, distinct },
AggregateKind::Sum => Self::Sum { target, distinct },
AggregateKind::Avg => Self::Avg { target, distinct },
AggregateKind::Min => Self::Min { target },
AggregateKind::Max => Self::Max { target },
AggregateKind::Exists | AggregateKind::First | AggregateKind::Last => {
unreachable!("sql aggregate invariant")
}
}
}
#[cfg(any(test, feature = "sql-explain"))]
pub(in crate::db::sql::lowering::aggregate) const fn aggregate_kind(&self) -> AggregateKind {
match self {
Self::Count { .. } => AggregateKind::Count,
Self::Sum { .. } => AggregateKind::Sum,
Self::Avg { .. } => AggregateKind::Avg,
Self::Min { .. } => AggregateKind::Min,
Self::Max { .. } => AggregateKind::Max,
}
}
pub(in crate::db::sql::lowering::aggregate) const fn distinct_input(&self) -> bool {
match self {
Self::Count { distinct, .. }
| Self::Sum { distinct, .. }
| Self::Avg { distinct, .. } => *distinct,
Self::Min { .. } | Self::Max { .. } => false,
}
}
#[cfg(any(test, feature = "sql-explain"))]
const fn target(&self) -> &PreparedAggregateTarget {
match self {
Self::Count { target, .. }
| Self::Sum { target, .. }
| Self::Avg { target, .. }
| Self::Min { target }
| Self::Max { target } => target,
}
}
#[cfg(any(test, feature = "sql-explain"))]
pub(in crate::db::sql::lowering::aggregate) const fn target_slot(&self) -> Option<&FieldSlot> {
self.target().field_slot()
}
#[cfg(test)]
pub(in crate::db::sql::lowering::aggregate) const fn input_expr(&self) -> Option<&Expr> {
self.target().input_expr()
}
pub(in crate::db::sql::lowering::aggregate) fn into_terminal_inputs(
self,
) -> (Option<FieldSlot>, Option<Expr>) {
match self {
Self::Count { target, .. }
| Self::Sum { target, .. }
| Self::Avg { target, .. }
| Self::Min { target }
| Self::Max { target } => target.into_terminal_inputs(),
}
}
}