1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Scalar-aggregate terminal — `QuerySet::aggregate(...)` + its
//! [`AggregateQuery<T, Out, K>`] pending handle.
//!
//! # What
//!
//! `QuerySet<T>::aggregate(|f| f.col().sum())` returns an
//! [`AggregateQuery<T, Out, K>`] whose terminal
//! [`AggregateQuery::fetch_one`] issues
//!
//! ```sql
//! SELECT <agg_expr> FROM <table> [WHERE ...]
//! ```
//!
//! and decodes the single scalar result into `Out`. `Out` is the Rust
//! return type carried by [`crate::expr::AggregateExpr<Out, K>`] — `i64`
//! for `COUNT`, `f64` for `AVG`, `V` for `SUM`/`MIN`/`MAX` where `V` is
//! the underlying column's scalar type. `K` is the modifier-family
//! marker; it defaults to [`crate::expr::ValueAgg`] and is inferred
//! from the aggregate the closure returns so non-value families
//! (`PERCENTILE_CONT`, `RANK_OF`, `GROUPING`, etc.) flow through this
//! terminal without explicit annotation.
//!
//! # Why a dedicated pending handle
//!
//! The aggregate terminal is a scalar decode, not a row decode, so it
//! needs a different entry point from the row terminals. Keeping the
//! typed-scalar pending struct separate from [`QuerySet<T>`] preserves
//! Phase 2's terminal signatures byte-for-byte — no call site that
//! reaches `.fetch_all(ctx)` is forced to learn a new return type. The
//! cost is one tiny wrapper struct; the benefit is clean additivity.
//!
//! # Clause set
//!
//! `SELECT <agg>, FROM <table> [WHERE ...]` — no `ORDER BY`, no
//! `LIMIT`, no `OFFSET`, no `GROUP BY`. Ungrouped aggregates always
//! collapse to exactly one result row regardless of cardinality, so
//! those clauses would be meaningless / syntax errors. Grouped
//! aggregates (`annotate(|f| f.col.count()).group_by(...)`) ship in a
//! later phase; Task 4's scalar path stays single-row by design.
//!
//! # Empty short-circuit
//!
//! `QuerySet::none()` on the upstream queryset is honoured — the
//! terminal short-circuits to a sentinel value without issuing any
//! SQL. For `COUNT`-shaped aggregates the sentinel is `0`; for
//! `MIN`/`MAX` it would be `NULL` in SQL (the queryset matched no
//! rows), but we cannot conjure a `V` at the Rust level without a
//! trait bound. Task 4 ships the straightforward version: structural-
//! empty querysets still run the SQL and Postgres returns `NULL` /
//! `0` / `0.0` per the per-aggregate rules. If a later task adds
//! zero-value defaults per `Out`, it will close this gap additively.
use crateDjogiError;
use crateDjogiContext;
use crateAggregateExpr;
use crate;
use crateModel;
use crateas_params;
use crateQuerySet;
use cratebuild_aggregate_select;
use Future;
use PhantomData;
/// Pending aggregate query — produced by [`QuerySet::aggregate`] and
/// terminated with [`Self::fetch_one`].
///
/// Holds the upstream queryset (for the `FROM` + `WHERE` clauses) plus
/// the aggregate expression itself. `Out` is the Rust type the driver
/// decodes the scalar result into; `K` is the aggregate's modifier
/// family ([`crate::expr::ValueAgg`] / [`crate::expr::MetadataAgg`] /
/// [`crate::expr::OrderedSetAgg`] / [`crate::expr::HypotheticalSetAgg`]),
/// threaded from the wrapped [`AggregateExpr<Out, K>`].
///
/// `K` defaults to [`crate::expr::ValueAgg`] so pre-#89 call sites that
/// passed a value aggregate (`f.col().sum()`, `f.col().count()`, etc.)
/// keep working without explicit annotation. Non-value families
/// (`f.col().percentile_cont(0.5)`, `f.col().rank_of(7_500)`,
/// `f.col().grouping()`) infer `K` from the returned aggregate's kind
/// marker.
///
/// `#[must_use]` because an unawaited pending query is always a
/// mistake; the `.fetch_one(ctx)` call is what actually runs the SQL.
// Entry point on QuerySet — builder method that consumes the queryset
// and returns the pending aggregate.