use super::aggregate_filter::{Filter, NoFilter}; use super::aggregate_order::NoOrder; use super::partition_by::NoPartition; use super::prefix::NoPrefix; use super::IsWindowFunction; use super::NoFrame; use super::WindowFunctionFragment; use super::{AggregateExpression, IsAggregateFunction}; use crate::query_builder::QueryFragment; use crate::query_builder::{AstPass, QueryId}; use crate::QueryResult; /// Only aggregate functions allow to use filter pub(super) trait ValidAggregateFilterForWindow {} impl ValidAggregateFilterForWindow for NoFilter {} empty_clause!(NoWindow); impl ValidAggregateFilterForWindow for Filter {} impl WindowFunctionFragment for NoWindow where DB: crate::backend::Backend {} #[derive(Clone, Copy, QueryId, Debug)] #[doc(hidden)] // not even sure why rustc believes this is public #[diesel(diesel_internal_is_window = true)] pub struct OverClause { pub(crate) partition_by: Partition, pub(crate) order: Order, pub(crate) frame_clause: Frame, } impl ValidAggregateFilterForWindow> for Filter where Fn: IsAggregateFunction, { } impl QueryFragment for OverClause where Partition: QueryFragment, Order: QueryFragment, Frame: QueryFragment, DB: crate::backend::Backend, { fn walk_ast<'b>(&'b self, mut pass: AstPass<'_, 'b, DB>) -> QueryResult<()> { pass.push_sql(" OVER ("); self.partition_by.walk_ast(pass.reborrow())?; self.order.walk_ast(pass.reborrow())?; self.frame_clause.walk_ast(pass.reborrow())?; pass.push_sql(")"); Ok(()) } } pub trait OverDsl { type Output; fn over(self) -> Self::Output; } impl OverDsl for F where F: IsWindowFunction, { type Output = AggregateExpression; fn over(self) -> Self::Output { AggregateExpression { prefix: NoPrefix, function: self, order: NoOrder, filter: NoFilter, window: OverClause { partition_by: NoPartition, order: NoOrder, frame_clause: NoFrame, }, } } } impl OverDsl for AggregateExpression where Filter: ValidAggregateFilterForWindow, { type Output = AggregateExpression; fn over(self) -> Self::Output { AggregateExpression { prefix: NoPrefix, function: self.function, order: NoOrder, filter: self.filter, window: OverClause { partition_by: NoPartition, order: NoOrder, frame_clause: NoFrame, }, } } }