use crate::db::sql::lowering::{
LoweredBaseQueryShape, LoweredSqlCommand, LoweredSqlCommandInner, PreparedSqlStatement,
SqlLoweringError,
};
#[cfg(test)]
use crate::{db::query::intent::Query, traits::EntityKind};
use crate::{
db::{
predicate::MissingRowPolicy,
query::{
builder::aggregate::{avg, count, count_by, max_by, min_by, sum},
intent::StructuralQuery,
plan::{
AggregateKind, FieldSlot,
expr::{Expr, expr_references_only_fields},
resolve_aggregate_target_field_slot,
},
},
sql::parser::{
SqlAggregateCall, SqlAggregateKind, SqlExplainMode, SqlProjection,
SqlProjectionOperand, SqlRoundProjectionInput, SqlSelectItem, SqlSelectStatement,
SqlStatement,
},
},
model::entity::{EntityModel, resolve_field_slot},
};
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum SqlGlobalAggregateTerminal {
CountRows,
CountField { field: String, distinct: bool },
SumField { field: String, distinct: bool },
AvgField { field: String, distinct: bool },
MinField(String),
MaxField(String),
}
#[cfg(test)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum TypedSqlGlobalAggregateTerminal {
CountRows,
CountField {
target_slot: FieldSlot,
distinct: bool,
},
SumField {
target_slot: FieldSlot,
distinct: bool,
},
AvgField {
target_slot: FieldSlot,
distinct: bool,
},
MinField(FieldSlot),
MaxField(FieldSlot),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum PreparedSqlScalarAggregateDomain {
ExistingRows,
ProjectionField,
NumericField,
ScalarExtremaValue,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum PreparedSqlScalarAggregateOrderingRequirement {
None,
FieldOrder,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum PreparedSqlScalarAggregateRowSource {
ExistingRows,
ProjectedField,
NumericField,
ExtremalWinnerField,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum PreparedSqlScalarAggregateEmptySetBehavior {
Zero,
Null,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum PreparedSqlScalarAggregateDescriptorShape {
CountRows,
CountField,
SumField,
AvgField,
MinField,
MaxField,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum PreparedSqlScalarAggregateRuntimeDescriptor {
CountRows,
CountField,
NumericField { kind: AggregateKind },
ExtremalWinnerField { kind: AggregateKind },
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct PreparedSqlScalarAggregateDescriptorPolicy {
domain: PreparedSqlScalarAggregateDomain,
ordering_requirement: PreparedSqlScalarAggregateOrderingRequirement,
row_source: PreparedSqlScalarAggregateRowSource,
empty_set_behavior: PreparedSqlScalarAggregateEmptySetBehavior,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct PreparedSqlScalarAggregateStrategy {
target_slot: Option<FieldSlot>,
distinct_input: bool,
domain: PreparedSqlScalarAggregateDomain,
ordering_requirement: PreparedSqlScalarAggregateOrderingRequirement,
row_source: PreparedSqlScalarAggregateRowSource,
empty_set_behavior: PreparedSqlScalarAggregateEmptySetBehavior,
descriptor_shape: PreparedSqlScalarAggregateDescriptorShape,
}
impl PreparedSqlScalarAggregateStrategy {
const fn new(
target_slot: Option<FieldSlot>,
distinct_input: bool,
domain: PreparedSqlScalarAggregateDomain,
ordering_requirement: PreparedSqlScalarAggregateOrderingRequirement,
row_source: PreparedSqlScalarAggregateRowSource,
empty_set_behavior: PreparedSqlScalarAggregateEmptySetBehavior,
descriptor_shape: PreparedSqlScalarAggregateDescriptorShape,
) -> Self {
Self {
target_slot,
distinct_input,
domain,
ordering_requirement,
row_source,
empty_set_behavior,
descriptor_shape,
}
}
const fn descriptor_policy(
descriptor_shape: PreparedSqlScalarAggregateDescriptorShape,
) -> PreparedSqlScalarAggregateDescriptorPolicy {
match descriptor_shape {
PreparedSqlScalarAggregateDescriptorShape::CountRows => {
PreparedSqlScalarAggregateDescriptorPolicy {
domain: PreparedSqlScalarAggregateDomain::ExistingRows,
ordering_requirement: PreparedSqlScalarAggregateOrderingRequirement::None,
row_source: PreparedSqlScalarAggregateRowSource::ExistingRows,
empty_set_behavior: PreparedSqlScalarAggregateEmptySetBehavior::Zero,
}
}
PreparedSqlScalarAggregateDescriptorShape::CountField => {
PreparedSqlScalarAggregateDescriptorPolicy {
domain: PreparedSqlScalarAggregateDomain::ProjectionField,
ordering_requirement: PreparedSqlScalarAggregateOrderingRequirement::None,
row_source: PreparedSqlScalarAggregateRowSource::ProjectedField,
empty_set_behavior: PreparedSqlScalarAggregateEmptySetBehavior::Zero,
}
}
PreparedSqlScalarAggregateDescriptorShape::SumField
| PreparedSqlScalarAggregateDescriptorShape::AvgField => {
PreparedSqlScalarAggregateDescriptorPolicy {
domain: PreparedSqlScalarAggregateDomain::NumericField,
ordering_requirement: PreparedSqlScalarAggregateOrderingRequirement::None,
row_source: PreparedSqlScalarAggregateRowSource::NumericField,
empty_set_behavior: PreparedSqlScalarAggregateEmptySetBehavior::Null,
}
}
PreparedSqlScalarAggregateDescriptorShape::MinField
| PreparedSqlScalarAggregateDescriptorShape::MaxField => {
PreparedSqlScalarAggregateDescriptorPolicy {
domain: PreparedSqlScalarAggregateDomain::ScalarExtremaValue,
ordering_requirement: PreparedSqlScalarAggregateOrderingRequirement::FieldOrder,
row_source: PreparedSqlScalarAggregateRowSource::ExtremalWinnerField,
empty_set_behavior: PreparedSqlScalarAggregateEmptySetBehavior::Null,
}
}
}
}
const fn from_resolved_shape(
target_slot: Option<FieldSlot>,
distinct_input: bool,
descriptor_shape: PreparedSqlScalarAggregateDescriptorShape,
) -> Self {
let policy = Self::descriptor_policy(descriptor_shape);
Self::new(
target_slot,
distinct_input,
policy.domain,
policy.ordering_requirement,
policy.row_source,
policy.empty_set_behavior,
descriptor_shape,
)
}
#[cfg(test)]
pub(in crate::db::sql::lowering) fn from_typed_terminal(
terminal: &TypedSqlGlobalAggregateTerminal,
) -> Self {
match terminal {
TypedSqlGlobalAggregateTerminal::CountRows => Self::from_resolved_shape(
None,
false,
PreparedSqlScalarAggregateDescriptorShape::CountRows,
),
TypedSqlGlobalAggregateTerminal::CountField {
target_slot,
distinct,
} => Self::from_resolved_shape(
Some(target_slot.clone()),
*distinct,
PreparedSqlScalarAggregateDescriptorShape::CountField,
),
TypedSqlGlobalAggregateTerminal::SumField {
target_slot,
distinct,
} => Self::from_resolved_shape(
Some(target_slot.clone()),
*distinct,
PreparedSqlScalarAggregateDescriptorShape::SumField,
),
TypedSqlGlobalAggregateTerminal::AvgField {
target_slot,
distinct,
} => Self::from_resolved_shape(
Some(target_slot.clone()),
*distinct,
PreparedSqlScalarAggregateDescriptorShape::AvgField,
),
TypedSqlGlobalAggregateTerminal::MinField(target_slot) => Self::from_resolved_shape(
Some(target_slot.clone()),
false,
PreparedSqlScalarAggregateDescriptorShape::MinField,
),
TypedSqlGlobalAggregateTerminal::MaxField(target_slot) => Self::from_resolved_shape(
Some(target_slot.clone()),
false,
PreparedSqlScalarAggregateDescriptorShape::MaxField,
),
}
}
fn from_lowered_terminal_with_model(
model: &'static EntityModel,
terminal: &SqlGlobalAggregateTerminal,
) -> Result<Self, SqlLoweringError> {
let resolve_target_slot = |field: &str| {
resolve_aggregate_target_field_slot(model, field).map_err(SqlLoweringError::from)
};
match terminal {
SqlGlobalAggregateTerminal::CountRows => Ok(Self::from_resolved_shape(
None,
false,
PreparedSqlScalarAggregateDescriptorShape::CountRows,
)),
SqlGlobalAggregateTerminal::CountField { field, distinct } => {
let target_slot = resolve_target_slot(field.as_str())?;
Ok(Self::from_resolved_shape(
Some(target_slot),
*distinct,
PreparedSqlScalarAggregateDescriptorShape::CountField,
))
}
SqlGlobalAggregateTerminal::SumField { field, distinct } => {
let target_slot = resolve_target_slot(field.as_str())?;
Ok(Self::from_resolved_shape(
Some(target_slot),
*distinct,
PreparedSqlScalarAggregateDescriptorShape::SumField,
))
}
SqlGlobalAggregateTerminal::AvgField { field, distinct } => {
let target_slot = resolve_target_slot(field.as_str())?;
Ok(Self::from_resolved_shape(
Some(target_slot),
*distinct,
PreparedSqlScalarAggregateDescriptorShape::AvgField,
))
}
SqlGlobalAggregateTerminal::MinField(field) => {
let target_slot = resolve_target_slot(field.as_str())?;
Ok(Self::from_resolved_shape(
Some(target_slot),
false,
PreparedSqlScalarAggregateDescriptorShape::MinField,
))
}
SqlGlobalAggregateTerminal::MaxField(field) => {
let target_slot = resolve_target_slot(field.as_str())?;
Ok(Self::from_resolved_shape(
Some(target_slot),
false,
PreparedSqlScalarAggregateDescriptorShape::MaxField,
))
}
}
}
#[must_use]
pub(crate) const fn target_slot(&self) -> Option<&FieldSlot> {
self.target_slot.as_ref()
}
#[must_use]
pub(crate) const fn is_distinct(&self) -> bool {
self.distinct_input
}
#[cfg(test)]
#[must_use]
pub(crate) const fn domain(&self) -> PreparedSqlScalarAggregateDomain {
self.domain
}
#[cfg(test)]
#[must_use]
pub(crate) const fn descriptor_shape(&self) -> PreparedSqlScalarAggregateDescriptorShape {
self.descriptor_shape
}
#[must_use]
pub(crate) const fn runtime_descriptor(&self) -> PreparedSqlScalarAggregateRuntimeDescriptor {
match self.descriptor_shape {
PreparedSqlScalarAggregateDescriptorShape::CountRows => {
PreparedSqlScalarAggregateRuntimeDescriptor::CountRows
}
PreparedSqlScalarAggregateDescriptorShape::CountField => {
PreparedSqlScalarAggregateRuntimeDescriptor::CountField
}
PreparedSqlScalarAggregateDescriptorShape::SumField => {
PreparedSqlScalarAggregateRuntimeDescriptor::NumericField {
kind: AggregateKind::Sum,
}
}
PreparedSqlScalarAggregateDescriptorShape::AvgField => {
PreparedSqlScalarAggregateRuntimeDescriptor::NumericField {
kind: AggregateKind::Avg,
}
}
PreparedSqlScalarAggregateDescriptorShape::MinField => {
PreparedSqlScalarAggregateRuntimeDescriptor::ExtremalWinnerField {
kind: AggregateKind::Min,
}
}
PreparedSqlScalarAggregateDescriptorShape::MaxField => {
PreparedSqlScalarAggregateRuntimeDescriptor::ExtremalWinnerField {
kind: AggregateKind::Max,
}
}
}
}
#[must_use]
pub(crate) const fn aggregate_kind(&self) -> AggregateKind {
match self.descriptor_shape {
PreparedSqlScalarAggregateDescriptorShape::CountRows
| PreparedSqlScalarAggregateDescriptorShape::CountField => AggregateKind::Count,
PreparedSqlScalarAggregateDescriptorShape::SumField => AggregateKind::Sum,
PreparedSqlScalarAggregateDescriptorShape::AvgField => AggregateKind::Avg,
PreparedSqlScalarAggregateDescriptorShape::MinField => AggregateKind::Min,
PreparedSqlScalarAggregateDescriptorShape::MaxField => AggregateKind::Max,
}
}
#[must_use]
pub(crate) fn projected_field(&self) -> Option<&str> {
self.target_slot().map(FieldSlot::field)
}
#[cfg(test)]
#[must_use]
pub(crate) const fn ordering_requirement(
&self,
) -> PreparedSqlScalarAggregateOrderingRequirement {
self.ordering_requirement
}
#[cfg(test)]
#[must_use]
pub(crate) const fn row_source(&self) -> PreparedSqlScalarAggregateRowSource {
self.row_source
}
#[cfg(test)]
#[must_use]
pub(crate) const fn empty_set_behavior(&self) -> PreparedSqlScalarAggregateEmptySetBehavior {
self.empty_set_behavior
}
}
#[derive(Clone, Debug)]
pub(crate) struct LoweredSqlGlobalAggregateCommand {
pub(in crate::db::sql::lowering) query: LoweredBaseQueryShape,
pub(in crate::db::sql::lowering) terminals: Vec<SqlGlobalAggregateTerminal>,
pub(in crate::db::sql::lowering) output_remap: Vec<usize>,
}
impl LoweredSqlGlobalAggregateCommand {
fn from_select_statement(statement: SqlSelectStatement) -> Result<Self, SqlLoweringError> {
let SqlSelectStatement {
projection,
projection_aliases: _,
predicate,
distinct,
group_by,
having,
order_by,
limit,
offset,
entity: _,
} = statement;
if distinct {
return Err(SqlLoweringError::unsupported_select_distinct());
}
if !group_by.is_empty() {
return Err(SqlLoweringError::unsupported_select_group_by());
}
if !having.is_empty() {
return Err(SqlLoweringError::unsupported_select_having());
}
let lowered_terminals = LoweredSqlGlobalAggregateTerminals::from_projection(projection)?;
Ok(Self {
query: LoweredBaseQueryShape {
predicate,
order_by,
limit,
offset,
},
terminals: lowered_terminals.terminals,
output_remap: lowered_terminals.output_remap,
})
}
#[cfg(test)]
fn into_typed<E: EntityKind>(
self,
consistency: MissingRowPolicy,
) -> Result<SqlGlobalAggregateCommand<E>, SqlLoweringError> {
let terminals = bind_lowered_sql_global_aggregate_terminals::<E>(self.terminals)?;
Ok(SqlGlobalAggregateCommand {
query: Query::from_inner(crate::db::sql::lowering::apply_lowered_base_query_shape(
StructuralQuery::new(E::MODEL, consistency),
self.query,
)),
terminals,
output_remap: self.output_remap,
})
}
fn into_structural(
self,
model: &'static EntityModel,
consistency: MissingRowPolicy,
) -> SqlGlobalAggregateCommandCore {
SqlGlobalAggregateCommandCore {
query: crate::db::sql::lowering::apply_lowered_base_query_shape(
StructuralQuery::new(model, consistency),
self.query,
),
terminals: self.terminals,
output_remap: self.output_remap,
}
}
}
enum LoweredSqlAggregateShape {
CountRows,
CountField {
field: String,
distinct: bool,
},
FieldTarget {
kind: SqlAggregateKind,
field: String,
distinct: bool,
},
}
#[cfg(test)]
#[derive(Debug)]
pub(crate) struct SqlGlobalAggregateCommand<E: EntityKind> {
query: Query<E>,
terminals: Vec<TypedSqlGlobalAggregateTerminal>,
output_remap: Vec<usize>,
}
#[cfg(test)]
impl<E: EntityKind> SqlGlobalAggregateCommand<E> {
#[must_use]
pub(crate) const fn query(&self) -> &Query<E> {
&self.query
}
#[must_use]
pub(crate) fn terminals(&self) -> &[TypedSqlGlobalAggregateTerminal] {
self.terminals.as_slice()
}
#[cfg(test)]
#[must_use]
pub(crate) fn output_remap(&self) -> &[usize] {
self.output_remap.as_slice()
}
#[cfg(test)]
#[must_use]
pub(crate) fn terminal(&self) -> &TypedSqlGlobalAggregateTerminal {
self.terminals
.first()
.expect("global aggregate command must contain at least one terminal")
}
#[cfg(test)]
#[must_use]
pub(crate) fn prepared_scalar_strategy(&self) -> PreparedSqlScalarAggregateStrategy {
PreparedSqlScalarAggregateStrategy::from_typed_terminal(self.terminal())
}
}
#[derive(Clone, Debug)]
pub(crate) struct SqlGlobalAggregateCommandCore {
query: StructuralQuery,
terminals: Vec<SqlGlobalAggregateTerminal>,
output_remap: Vec<usize>,
}
impl SqlGlobalAggregateCommandCore {
#[must_use]
pub(in crate::db) const fn query(&self) -> &StructuralQuery {
&self.query
}
#[must_use]
pub(in crate::db) const fn output_remap(&self) -> &[usize] {
self.output_remap.as_slice()
}
pub(in crate::db) fn prepared_scalar_strategies_with_model(
&self,
model: &'static EntityModel,
) -> Result<Vec<PreparedSqlScalarAggregateStrategy>, SqlLoweringError> {
self.terminals
.iter()
.map(|terminal| {
PreparedSqlScalarAggregateStrategy::from_lowered_terminal_with_model(
model, terminal,
)
})
.collect()
}
}
pub(in crate::db) fn is_sql_global_aggregate_statement(statement: &SqlStatement) -> bool {
let SqlStatement::Select(statement) = statement else {
return false;
};
is_sql_global_aggregate_select(statement)
}
fn is_sql_global_aggregate_select(statement: &SqlSelectStatement) -> bool {
if statement.distinct || !statement.group_by.is_empty() || !statement.having.is_empty() {
return false;
}
LoweredSqlGlobalAggregateTerminals::from_projection(statement.projection.clone()).is_ok()
}
pub(crate) fn bind_lowered_sql_explain_global_aggregate_structural(
lowered: &LoweredSqlCommand,
model: &'static EntityModel,
consistency: MissingRowPolicy,
) -> Option<(SqlExplainMode, SqlGlobalAggregateCommandCore)> {
let LoweredSqlCommandInner::ExplainGlobalAggregate { mode, command } = &lowered.0 else {
return None;
};
Some((
*mode,
bind_lowered_sql_global_aggregate_command_structural(model, command.clone(), consistency),
))
}
#[cfg(test)]
pub(crate) fn compile_sql_global_aggregate_command<E: EntityKind>(
sql: &str,
consistency: MissingRowPolicy,
) -> Result<SqlGlobalAggregateCommand<E>, SqlLoweringError> {
let statement = crate::db::sql::parser::parse_sql(sql)?;
let prepared = crate::db::sql::lowering::prepare_sql_statement(statement, E::MODEL.name())?;
compile_sql_global_aggregate_command_from_prepared::<E>(prepared, consistency)
}
#[cfg(test)]
pub(crate) fn compile_sql_global_aggregate_command_from_prepared<E: EntityKind>(
prepared: PreparedSqlStatement,
consistency: MissingRowPolicy,
) -> Result<SqlGlobalAggregateCommand<E>, SqlLoweringError> {
let SqlStatement::Select(statement) = prepared.statement else {
return Err(SqlLoweringError::unsupported_select_projection());
};
bind_lowered_sql_global_aggregate_command::<E>(
lower_global_aggregate_select_shape(statement)?,
consistency,
)
}
pub(in crate::db) fn compile_sql_global_aggregate_command_core_from_prepared(
prepared: PreparedSqlStatement,
model: &'static EntityModel,
consistency: MissingRowPolicy,
) -> Result<SqlGlobalAggregateCommandCore, SqlLoweringError> {
let SqlStatement::Select(statement) = prepared.statement else {
return Err(SqlLoweringError::unsupported_select_projection());
};
Ok(bind_lowered_sql_global_aggregate_command_structural(
model,
lower_global_aggregate_select_shape(statement)?,
consistency,
))
}
#[cfg(test)]
fn bind_lowered_sql_global_aggregate_terminal<E: EntityKind>(
terminal: SqlGlobalAggregateTerminal,
) -> Result<TypedSqlGlobalAggregateTerminal, SqlLoweringError> {
let resolve_target_slot = |field: &str| {
resolve_aggregate_target_field_slot(E::MODEL, field).map_err(SqlLoweringError::from)
};
match terminal {
SqlGlobalAggregateTerminal::CountRows => Ok(TypedSqlGlobalAggregateTerminal::CountRows),
SqlGlobalAggregateTerminal::CountField { field, distinct } => {
Ok(TypedSqlGlobalAggregateTerminal::CountField {
target_slot: resolve_target_slot(field.as_str())?,
distinct,
})
}
SqlGlobalAggregateTerminal::SumField { field, distinct } => {
Ok(TypedSqlGlobalAggregateTerminal::SumField {
target_slot: resolve_target_slot(field.as_str())?,
distinct,
})
}
SqlGlobalAggregateTerminal::AvgField { field, distinct } => {
Ok(TypedSqlGlobalAggregateTerminal::AvgField {
target_slot: resolve_target_slot(field.as_str())?,
distinct,
})
}
SqlGlobalAggregateTerminal::MinField(field) => Ok(
TypedSqlGlobalAggregateTerminal::MinField(resolve_target_slot(field.as_str())?),
),
SqlGlobalAggregateTerminal::MaxField(field) => Ok(
TypedSqlGlobalAggregateTerminal::MaxField(resolve_target_slot(field.as_str())?),
),
}
}
#[cfg(test)]
fn bind_lowered_sql_global_aggregate_terminals<E: EntityKind>(
terminals: Vec<SqlGlobalAggregateTerminal>,
) -> Result<Vec<TypedSqlGlobalAggregateTerminal>, SqlLoweringError> {
terminals
.into_iter()
.map(bind_lowered_sql_global_aggregate_terminal::<E>)
.collect()
}
pub(in crate::db::sql::lowering) fn lower_global_aggregate_select_shape(
statement: SqlSelectStatement,
) -> Result<LoweredSqlGlobalAggregateCommand, SqlLoweringError> {
LoweredSqlGlobalAggregateCommand::from_select_statement(statement)
}
#[cfg(test)]
pub(in crate::db::sql::lowering) fn bind_lowered_sql_global_aggregate_command<E: EntityKind>(
lowered: LoweredSqlGlobalAggregateCommand,
consistency: MissingRowPolicy,
) -> Result<SqlGlobalAggregateCommand<E>, SqlLoweringError> {
lowered.into_typed::<E>(consistency)
}
fn bind_lowered_sql_global_aggregate_command_structural(
model: &'static EntityModel,
lowered: LoweredSqlGlobalAggregateCommand,
consistency: MissingRowPolicy,
) -> SqlGlobalAggregateCommandCore {
lowered.into_structural(model, consistency)
}
fn lower_global_aggregate_terminal(
item: SqlSelectItem,
) -> Result<SqlGlobalAggregateTerminal, SqlLoweringError> {
let SqlSelectItem::Aggregate(aggregate) = item else {
return Err(SqlLoweringError::unsupported_select_projection());
};
match lower_sql_aggregate_shape(aggregate)? {
LoweredSqlAggregateShape::CountRows => Ok(SqlGlobalAggregateTerminal::CountRows),
LoweredSqlAggregateShape::CountField { field, distinct } => {
Ok(SqlGlobalAggregateTerminal::CountField { field, distinct })
}
LoweredSqlAggregateShape::FieldTarget {
kind: SqlAggregateKind::Sum,
field,
distinct,
} => Ok(SqlGlobalAggregateTerminal::SumField { field, distinct }),
LoweredSqlAggregateShape::FieldTarget {
kind: SqlAggregateKind::Avg,
field,
distinct,
} => Ok(SqlGlobalAggregateTerminal::AvgField { field, distinct }),
LoweredSqlAggregateShape::FieldTarget {
kind: SqlAggregateKind::Min,
field,
..
} => Ok(SqlGlobalAggregateTerminal::MinField(field)),
LoweredSqlAggregateShape::FieldTarget {
kind: SqlAggregateKind::Max,
field,
..
} => Ok(SqlGlobalAggregateTerminal::MaxField(field)),
LoweredSqlAggregateShape::FieldTarget {
kind: SqlAggregateKind::Count,
..
} => Err(SqlLoweringError::unsupported_select_projection()),
}
}
struct LoweredSqlGlobalAggregateTerminals {
terminals: Vec<SqlGlobalAggregateTerminal>,
output_remap: Vec<usize>,
}
impl LoweredSqlGlobalAggregateTerminals {
fn from_projection(projection: SqlProjection) -> Result<Self, SqlLoweringError> {
let SqlProjection::Items(items) = projection else {
return Err(SqlLoweringError::unsupported_select_projection());
};
if items.is_empty() {
return Err(SqlLoweringError::unsupported_select_projection());
}
let mut terminals = Vec::<SqlGlobalAggregateTerminal>::with_capacity(items.len());
let mut output_remap = Vec::<usize>::with_capacity(items.len());
for item in items {
let terminal = lower_global_aggregate_terminal(item)?;
let unique_index = terminals
.iter()
.position(|current| current == &terminal)
.unwrap_or_else(|| {
let index = terminals.len();
terminals.push(terminal);
index
});
output_remap.push(unique_index);
}
Ok(Self {
terminals,
output_remap,
})
}
}
fn lower_sql_aggregate_shape(
call: SqlAggregateCall,
) -> Result<LoweredSqlAggregateShape, SqlLoweringError> {
match (call.kind, call.field, call.distinct) {
(SqlAggregateKind::Count, None, false) => Ok(LoweredSqlAggregateShape::CountRows),
(SqlAggregateKind::Count, Some(field), distinct) => {
Ok(LoweredSqlAggregateShape::CountField { field, distinct })
}
(
kind @ (SqlAggregateKind::Sum
| SqlAggregateKind::Avg
| SqlAggregateKind::Min
| SqlAggregateKind::Max),
Some(field),
distinct,
) => Ok(LoweredSqlAggregateShape::FieldTarget {
kind,
field,
distinct,
}),
_ => Err(SqlLoweringError::unsupported_select_projection()),
}
}
pub(in crate::db::sql::lowering) fn grouped_projection_aggregate_calls(
projection: &SqlProjection,
group_by_fields: &[String],
model: &'static EntityModel,
) -> Result<Vec<SqlAggregateCall>, SqlLoweringError> {
if group_by_fields.is_empty() {
return Err(SqlLoweringError::unsupported_select_group_by());
}
let SqlProjection::Items(items) = projection else {
return Err(SqlLoweringError::unsupported_select_group_by());
};
GroupedProjectionAggregateCollector::new(group_by_fields, model)?.collect_from_items(items)
}
struct GroupedProjectionAggregateCollector<'a> {
grouped_field_names: Vec<&'a str>,
model: &'static EntityModel,
aggregate_calls: Vec<SqlAggregateCall>,
seen_aggregate: bool,
}
impl<'a> GroupedProjectionAggregateCollector<'a> {
fn new(
group_by_fields: &'a [String],
model: &'static EntityModel,
) -> Result<Self, SqlLoweringError> {
if group_by_fields.is_empty() {
return Err(SqlLoweringError::unsupported_select_group_by());
}
Ok(Self {
grouped_field_names: group_by_fields.iter().map(String::as_str).collect(),
model,
aggregate_calls: Vec::new(),
seen_aggregate: false,
})
}
fn collect_from_items(
mut self,
items: &[SqlSelectItem],
) -> Result<Vec<SqlAggregateCall>, SqlLoweringError> {
for item in items {
self.collect_item(item)?;
}
if self.aggregate_calls.is_empty() {
return Err(SqlLoweringError::unsupported_select_group_by());
}
Ok(self.aggregate_calls)
}
fn collect_item(&mut self, item: &SqlSelectItem) -> Result<(), SqlLoweringError> {
let expr = crate::db::sql::lowering::select::lower_select_item_expr(item)?;
let contains_aggregate = expr_contains_aggregate(&expr);
if self.seen_aggregate && !contains_aggregate {
return Err(SqlLoweringError::unsupported_select_group_by());
}
if let Some(field) = first_unknown_field_in_expr(&expr, self.model) {
return Err(SqlLoweringError::unknown_field(field));
}
if !expr_references_only_fields(&expr, self.grouped_field_names.as_slice()) {
return Err(SqlLoweringError::unsupported_select_group_by());
}
if contains_aggregate {
self.seen_aggregate = true;
self.collect_item_aggregates(item);
}
Ok(())
}
fn collect_item_aggregates(&mut self, item: &SqlSelectItem) {
match item {
SqlSelectItem::Field(_) | SqlSelectItem::TextFunction(_) => {}
SqlSelectItem::Aggregate(aggregate) => {
self.push_unique_aggregate(aggregate.clone());
}
SqlSelectItem::Arithmetic(call) => {
self.collect_operand_aggregates(&call.left);
self.collect_operand_aggregates(&call.right);
}
SqlSelectItem::Round(call) => match &call.input {
SqlRoundProjectionInput::Operand(operand) => {
self.collect_operand_aggregates(operand);
}
SqlRoundProjectionInput::Arithmetic(call) => {
self.collect_operand_aggregates(&call.left);
self.collect_operand_aggregates(&call.right);
}
},
}
}
fn collect_operand_aggregates(&mut self, operand: &SqlProjectionOperand) {
if let SqlProjectionOperand::Aggregate(aggregate) = operand {
self.push_unique_aggregate(aggregate.clone());
}
}
fn push_unique_aggregate(&mut self, aggregate: SqlAggregateCall) {
if self
.aggregate_calls
.iter()
.all(|current| current != &aggregate)
{
self.aggregate_calls.push(aggregate);
}
}
}
fn expr_contains_aggregate(expr: &Expr) -> bool {
match expr {
Expr::Aggregate(_) => true,
Expr::Field(_) | Expr::Literal(_) => false,
Expr::FunctionCall { args, .. } => args.iter().any(expr_contains_aggregate),
Expr::Binary { left, right, .. } => {
expr_contains_aggregate(left) || expr_contains_aggregate(right)
}
#[cfg(test)]
Expr::Unary { expr, .. } | Expr::Alias { expr, .. } => expr_contains_aggregate(expr),
}
}
fn first_unknown_field_in_expr(expr: &Expr, model: &EntityModel) -> Option<String> {
match expr {
Expr::Field(field) => (resolve_field_slot(model, field.as_str()).is_none())
.then(|| field.as_str().to_string()),
Expr::Literal(_) | Expr::Aggregate(_) => None,
Expr::FunctionCall { args, .. } => args
.iter()
.find_map(|arg| first_unknown_field_in_expr(arg, model)),
Expr::Binary { left, right, .. } => first_unknown_field_in_expr(left, model)
.or_else(|| first_unknown_field_in_expr(right, model)),
#[cfg(test)]
Expr::Unary { expr, .. } | Expr::Alias { expr, .. } => {
first_unknown_field_in_expr(expr, model)
}
}
}
pub(in crate::db::sql::lowering) fn lower_aggregate_call(
call: SqlAggregateCall,
) -> Result<crate::db::query::builder::AggregateExpr, SqlLoweringError> {
match lower_sql_aggregate_shape(call)? {
LoweredSqlAggregateShape::CountRows => Ok(count()),
LoweredSqlAggregateShape::CountField {
field,
distinct: false,
} => Ok(count_by(field)),
LoweredSqlAggregateShape::CountField {
field,
distinct: true,
} => Ok(count_by(field).distinct()),
LoweredSqlAggregateShape::FieldTarget {
kind: SqlAggregateKind::Sum,
field,
distinct: false,
} => Ok(sum(field)),
LoweredSqlAggregateShape::FieldTarget {
kind: SqlAggregateKind::Sum,
field,
distinct: true,
} => Ok(sum(field).distinct()),
LoweredSqlAggregateShape::FieldTarget {
kind: SqlAggregateKind::Avg,
field,
distinct: false,
} => Ok(avg(field)),
LoweredSqlAggregateShape::FieldTarget {
kind: SqlAggregateKind::Avg,
field,
distinct: true,
} => Ok(avg(field).distinct()),
LoweredSqlAggregateShape::FieldTarget {
kind: SqlAggregateKind::Min,
field,
..
} => Ok(min_by(field)),
LoweredSqlAggregateShape::FieldTarget {
kind: SqlAggregateKind::Max,
field,
..
} => Ok(max_by(field)),
LoweredSqlAggregateShape::FieldTarget {
kind: SqlAggregateKind::Count,
..
} => Err(SqlLoweringError::unsupported_select_projection()),
}
}
pub(in crate::db::sql::lowering) fn resolve_having_aggregate_index(
target: &SqlAggregateCall,
grouped_projection_aggregates: &[SqlAggregateCall],
) -> Result<usize, SqlLoweringError> {
let mut matched = grouped_projection_aggregates
.iter()
.enumerate()
.filter_map(|(index, aggregate)| (aggregate == target).then_some(index));
let Some(index) = matched.next() else {
return Err(SqlLoweringError::unsupported_select_having());
};
if matched.next().is_some() {
return Err(SqlLoweringError::unsupported_select_having());
}
Ok(index)
}