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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//! Row-shape aggregate expressions — the typed surface for `ST_AsMVT` /
//! `ST_AsGeobuf`.
//!
//! # What
//!
//! [`RowAggregate<Out, K>`] is the row-shape sibling of
//! [`super::aggregate::AggregateExpr<Out, K>`]. Same shape — `PhantomData`
//! tags pinning the Rust decode type and a sealed kind marker — different
//! IR backing: [`super::node::ExprNode::RowAggregate`] (whole-row input,
//! no modifier slots) instead of [`super::node::ExprNode::Aggregate`]
//! (column input, full modifier surface).
//!
//! ## Two aggregate families, one design pattern
//!
//! | | Column aggregate | Row aggregate |
//! |---|---|---|
//! | Examples | `COUNT(col)`, `SUM(col)`, `ST_Centroid(col)` | `ST_AsMVT(t)`, `ST_AsGeobuf(t)` |
//! | Input | single column / expression | the entire row |
//! | Modifiers (`distinct` / `filter` / `over` / `order_by` / `within_group_order_by`) | yes (per-Kind) | **no** |
//! | IR variant | [`super::node::ExprNode::Aggregate`] | [`super::node::ExprNode::RowAggregate`] |
//! | Typed wrapper | [`super::aggregate::AggregateExpr`] | [`RowAggregate`] (this file) |
//! | Kind discipline | sealed [`super::aggregate::KindEvidence`] | sealed [`RowKindEvidence`] |
//!
//! ## Why a separate IR variant (not an `AggregateExpr` extension)
//!
//! Postgres rejects every column-aggregate modifier when applied to a
//! row-shape aggregate:
//!
//! - `ST_AsMVT(DISTINCT t, …)` — `42883: row-level expression cannot be DISTINCT`
//! - `ST_AsMVT(t, …) FILTER (WHERE …)` — `FILTER` is a column-aggregate-only modifier
//! - `ST_AsMVT(t, …) OVER (…)` — row aggregates are not windowable
//! - `ST_AsMVT(t, … ORDER BY …)` — ORDER BY inside the parens is rejected on row aggregates
//! - `ST_AsMVT(t, …) WITHIN GROUP (ORDER BY …)` — same
//!
//! Sharing [`super::aggregate::AggregateExpr`] would force every modifier
//! method onto a wrapper that semantically rejects them. A sibling type
//! makes the discipline structural: row aggregates simply have no
//! modifier methods.
//!
//! # Bytes at the Rust boundary
//!
//! Both shipped row aggregates return `bytea` at the Postgres level — MVT
//! protobuf bytes / Geobuf bytes. The typed surface decodes that into
//! `Vec<u8>` via `postgres_types::FromSql` on `Vec<u8>` (Postgres `bytea`
//! decodes natively into `Vec<u8>`). The `Out = Vec<u8>` parameter on
//! every constructor pins this at the type level.
//!
//! # Where
//!
//! - [`super::node::ExprNode::RowAggregate`] / [`super::node::RowAggOp`] —
//! the untyped IR payload.
//! - [`super::sql::emit_expr`] — renders the SQL tokens.
//! - [`crate::query::annotate::AnnotatedQuerySet::as_mvt`] /
//! [`crate::query::annotate::AnnotatedQuerySet::as_geobuf`] — annotated
//! terminal that wraps an annotation tuple's projection in the row
//! aggregate.
//! - [`crate::query::queryset::QuerySet::as_mvt`] /
//! [`crate::query::queryset::QuerySet::as_geobuf`] — bare terminal that
//! wraps the model's canonical projection.
use crate;
use PhantomData;
/// Crate-private seal for [`RowKindEvidence`]. Mirrors the seal pattern on
/// [`super::aggregate::sealed::Sealed`] — downstream crates cannot fabricate
/// kind tags because they cannot name `Sealed`.
pub
/// Compile-time marker for row-aggregate modifier families.
///
/// Row aggregates share a single kind family at v0.1.0 ([`BinaryRowAgg`]):
/// every shipped row aggregate (`ST_AsMVT` / `ST_AsGeobuf`) returns binary
/// `bytea` and admits no modifiers. The trait is sealed via [`sealed::Sealed`]
/// — only framework-internal markers implement it, mirroring the
/// [`super::aggregate::KindEvidence`] discipline.
///
/// The trait exists today even though the v0.1.0 family is singular because
/// future row-shape aggregates (text-returning encoders, JSON-returning
/// shape encoders) will slot in as additional kind markers without rev'ing
/// the [`RowAggregate`] type signature.
/// Binary `bytea`-returning row aggregate family — `ST_AsMVT` and
/// `ST_AsGeobuf`. The Rust decode type is always `Vec<u8>`.
///
/// No modifier impl block exists for this kind — the row aggregate
/// universe is modifier-free at v0.1.0. The phantom marker is still
/// load-bearing because it pins the kind discipline at type level and
/// keeps the surface uniform with [`super::aggregate::AggregateExpr`]'s
/// per-kind impl block pattern.
;
/// Typed row-shape aggregate expression — the result of
/// [`crate::query::annotate::AnnotatedQuerySet::as_mvt`] /
/// [`crate::query::annotate::AnnotatedQuerySet::as_geobuf`] (and the
/// matching helpers on [`crate::query::queryset::QuerySet`]).
///
/// Carries an [`ExprNode::RowAggregate`] payload plus phantom tags for the
/// Rust decode type `Out` and the modifier-kind marker `K`. The kind tag
/// is enforced via the sealed [`RowKindEvidence`] trait so downstream
/// crates cannot smuggle in custom kinds.
///
/// `#[must_use]` because a dropped row aggregate is always a mistake — the
/// terminal builder consumes it and dropping silently discards the entire
/// encoded output.
///
/// # Manual `Clone` / `Debug`
///
/// Same rationale as [`super::aggregate::AggregateExpr`]: the kind marker
/// types are ZSTs with no derives, so a `#[derive(Clone, Debug)]` would
/// add unwanted `K: Clone` / `K: Debug` bounds at every call site that
/// takes a `RowAggregate` by value through a clone-bounded slot.
// NOTE: No modifier impl block exists for any kind. `RowAggregate<Vec<u8>,
// BinaryRowAgg>` deliberately has no `.distinct()` / `.filter(...)` /
// `.over(...)` / `.order_by(...)` / `.within_group_order_by(...)` methods
// because Postgres rejects every column-aggregate modifier on a row-shape
// aggregate. The compile-fail fixtures under `djogi/tests/compile_fail/`
// (filenames `row_aggregate_no_*.rs`) pin this discipline at the
// rustc-diagnostic level.
// ── ST_AsMVT typed constructor ───────────────────────────────────────────
/// MVT (Mapbox Vector Tile) encoder options.
///
/// PostGIS's `ST_AsMVT(row, layer_name, extent, geom_name, feature_id_name)`
/// signature is variadic at the SQL level — all but the row argument carry
/// per-call defaults. The typed surface always emits explicit values for
/// the first three trailing arguments so the emission shape stays stable
/// across PostGIS releases (which could rev the SQL-side defaults), and
/// emits the optional `feature_id_name` only when set so the call falls
/// through to PostGIS's `NULL` default when omitted.
///
/// # Construction
///
/// Start with [`MvtOptions::new`] which seeds the PostGIS defaults that
/// are sound for most adopter workflows:
///
/// ```ignore
/// use djogi::expr::row_aggregate::MvtOptions;
/// let opts = MvtOptions::new("my_layer")
/// .with_extent(4096) // default — match PostGIS
/// .with_geom_name("geom") // default — match PostGIS
/// .with_feature_id_name("id"); // opt-in — adds feature_id to encoded protobuf
/// ```
/// Build a typed `RowAggregate` for `ST_AsMVT` over the supplied row
/// columns. Used by the terminal builders on
/// [`crate::query::annotate::AnnotatedQuerySet`] /
/// [`crate::query::queryset::QuerySet`]; not part of the public surface.
///
/// `columns` is the projected column list of the inner SELECT — the
/// emitter does not currently inspect it (PostGIS resolves columns from
/// the record type at runtime), but it is stored on the IR variant so
/// future row aggregates that need explicit column projection can
/// extend the emission without an IR rev.
pub
/// Build a typed `RowAggregate` for `ST_AsGeobuf` over the supplied row
/// columns. Used by the terminal builders on
/// [`crate::query::annotate::AnnotatedQuerySet`] /
/// [`crate::query::queryset::QuerySet`]; not part of the public surface.
pub