zero-client 0.2.0

A native Rust client for Zero.
Documentation
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
//! The typed query builder that `zero-rust codegen` generates code for: row
//! structs read from views, columns and relationships named by typed
//! constants, and [`Query`], a typed wrapper of [`AnyQuery`] (`Query` in
//! `packages/zql/src/query/query.ts`).
//!
//! The generated code holds a row struct per table with a [`Column`]
//! constant per column and a [`Relationship`] constant per relationship,
//! and a function per table that starts a query:
//!
//! ```ignore
//! zql::issue()
//!     .where_eq(Issue::STATUS, IssueStatus::Open)
//!     .where_expr(|e| e.or(vec![Issue::PRIORITY.ge(2.0), Issue::ASSIGNEE_ID.eq(None)]))
//!     .where_exists(Issue::LABELS, ExistsOptions::default(), |q| q.where_eq(Label::NAME, "bug".into()))
//!     .related(Issue::COMMENTS, |q| q.order_by(Comment::CREATED, Direction::Desc).limit(10))
//!     .order_by(Issue::MODIFIED, Direction::Desc)
//!     .limit(50)
//! ```
//!
//! A typed query builds the same AST as the [`AnyQuery`] calls with the
//! same names; [`Query::read`] reads a view of it as the query's result
//! type. In a mutator, the generated `mutate` functions return a
//! [`TypedTableMutator`] per table, whose updates and deletes take the
//! generated `<Row>Update` and `<Row>Key` structs. The items marked "for generated code" are what the generated
//! `from_entry`s and value conversions call.

pub use crate::ast::{Condition, Direction, JsonPathSegment, SimpleOperator};
pub use crate::client::Transaction;
pub use crate::ivm::{ViewEntry, ViewValue};
pub use crate::json::{Object, Value};
pub use crate::query::{AnyGroupedQuery, AnyQuery, ExistsOptions, StaticParameter};
pub use crate::schema::Schema;

use crate::ast::{System, ValuePosition};
use crate::client::Error;
use crate::query::{ExpressionBuilder, JsonPathReference};
use std::fmt;
use std::marker::PhantomData;
use std::sync::Arc;

/// A value that doesn't fit the Rust type a row declares for it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ZeroValueError(pub String);

impl ZeroValueError {
    /// A value that isn't the `expected` one.
    pub fn mismatch(expected: &str, value: &Value) -> Self {
        ZeroValueError(format!(
            "expected {expected}, got {} {}",
            value.type_name(),
            crate::json::stringify(value)
        ))
    }
}

impl fmt::Display for ZeroValueError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for ZeroValueError {}

/// A Rust type a column holds. Generated row structs use these for their
/// columns, and the query builder encodes compared values with them.
pub trait ZeroValue: Sized {
    /// Reads a value from a row; a missing column reads as `null`.
    fn from_zero_value(value: &Value) -> Result<Self, ZeroValueError>;
    /// Writes the value as the row and the query builder hold it.
    fn to_zero_value(&self) -> Value;
    /// What a row's `null` reads as when the schema says the column can't
    /// be null; `None` to fail. Databases drift from schemas, and JS shows
    /// such a row with the null rather than failing, so strings, numbers,
    /// and booleans read as `""`, `0`, and `false`. Declare the column
    /// `optional()` to read the null.
    fn value_for_null() -> Option<Self> {
        None
    }
}

impl ZeroValue for String {
    fn from_zero_value(value: &Value) -> Result<Self, ZeroValueError> {
        value
            .as_str()
            .map(str::to_owned)
            .ok_or_else(|| ZeroValueError::mismatch("a string", value))
    }
    fn to_zero_value(&self) -> Value {
        Value::String(self.clone())
    }
    fn value_for_null() -> Option<Self> {
        Some(String::new())
    }
}

impl ZeroValue for f64 {
    fn from_zero_value(value: &Value) -> Result<Self, ZeroValueError> {
        value
            .as_f64()
            .ok_or_else(|| ZeroValueError::mismatch("a number", value))
    }
    fn to_zero_value(&self) -> Value {
        Value::Number(*self)
    }
    fn value_for_null() -> Option<Self> {
        Some(0.0)
    }
}

/// For a `number()` column the codegen config declares an integer: the
/// value must be exact, which every integer up to 2^53 is.
impl ZeroValue for i64 {
    fn from_zero_value(value: &Value) -> Result<Self, ZeroValueError> {
        const MAX_SAFE: f64 = 9_007_199_254_740_992.0;
        match value.as_f64() {
            Some(n) if n.fract() == 0.0 && n.abs() <= MAX_SAFE => Ok(n as i64),
            _ => Err(ZeroValueError::mismatch("an integer", value)),
        }
    }
    fn to_zero_value(&self) -> Value {
        Value::Number(*self as f64)
    }
    fn value_for_null() -> Option<Self> {
        Some(0)
    }
}

impl ZeroValue for bool {
    fn from_zero_value(value: &Value) -> Result<Self, ZeroValueError> {
        value
            .as_bool()
            .ok_or_else(|| ZeroValueError::mismatch("a boolean", value))
    }
    fn to_zero_value(&self) -> Value {
        Value::Bool(*self)
    }
    fn value_for_null() -> Option<Self> {
        Some(false)
    }
}

impl ZeroValue for Value {
    fn from_zero_value(value: &Value) -> Result<Self, ZeroValueError> {
        Ok(value.clone())
    }
    fn to_zero_value(&self) -> Value {
        self.clone()
    }
}

/// An optional column: `null` reads as `None`.
impl<T: ZeroValue> ZeroValue for Option<T> {
    fn from_zero_value(value: &Value) -> Result<Self, ZeroValueError> {
        if value.is_null() {
            Ok(None)
        } else {
            T::from_zero_value(value).map(Some)
        }
    }
    fn to_zero_value(&self) -> Value {
        self.as_ref().map_or(Value::Null, T::to_zero_value)
    }
}

/// The JSON arrays a `json()` column can hold.
impl<T: ZeroValue> ZeroValue for Vec<T> {
    fn from_zero_value(value: &Value) -> Result<Self, ZeroValueError> {
        value
            .as_array()
            .ok_or_else(|| ZeroValueError::mismatch("an array", value))?
            .iter()
            .map(T::from_zero_value)
            .collect()
    }
    fn to_zero_value(&self) -> Value {
        Value::Array(self.iter().map(T::to_zero_value).collect())
    }
}

/// A column `sum` and `avg` can reduce: a number (`NumericSelector`).
pub trait NumericZeroValue: ZeroValue {}
impl NumericZeroValue for f64 {}
impl NumericZeroValue for i64 {}
impl<T: NumericZeroValue> NumericZeroValue for Option<T> {}

/// A column `min` and `max` can reduce: a number or a string, enumerations
/// included (`OrderedSelector`). `Unwrapped` is the type of its non-null
/// values, the aggregate's type.
pub trait OrderedZeroValue: ZeroValue {
    /// The type of the column's non-null values.
    type Unwrapped: ZeroValue + OptionalQueryElement;
}
impl OrderedZeroValue for f64 {
    type Unwrapped = f64;
}
impl OrderedZeroValue for i64 {
    type Unwrapped = i64;
}
impl OrderedZeroValue for String {
    type Unwrapped = String;
}
impl<T: OrderedZeroValue> OrderedZeroValue for Option<T> {
    type Unwrapped = T::Unwrapped;
}

/// Reads `name`'s value of a row or object, for generated code. A `null`
/// in a column that can't be null reads as [`ZeroValue::value_for_null`].
pub fn decode<V: ZeroValue>(name: &str, value: Option<&Value>) -> Result<V, ZeroValueError> {
    let value = value.unwrap_or(&Value::Null);
    if value.is_null()
        && let Some(fallback) = V::value_for_null()
    {
        return Ok(fallback);
    }
    V::from_zero_value(value).map_err(|e| ZeroValueError(format!("{name}: {e}")))
}

/// A column of a row, for generated `from_entry`s: `index` is its position
/// in the table's schema, where the row has it unless it came from
/// elsewhere. Reading it there skips looking the name up.
pub fn column<V: ZeroValue>(
    entry: &ViewEntry,
    name: &str,
    index: usize,
) -> Result<V, ZeroValueError> {
    match entry.row.get_index(index) {
        Some((k, v)) if k == name => decode(name, Some(v)),
        _ => decode(name, entry.row.get(name)),
    }
}

/// A many relationship of a row, for generated `from_entry`s: `None` when
/// the query didn't ask for its rows, also when it asked for an aggregate
/// of them.
pub fn many<R: QueryElement>(
    entry: &ViewEntry,
    name: &str,
) -> Result<Option<Vec<R>>, ZeroValueError> {
    match entry.relationship(name) {
        None | Some(ViewValue::Value(_)) => Ok(None),
        Some(v) => v
            .entries()
            .into_iter()
            .map(R::from_entry)
            .collect::<Result<_, _>>()
            .map(Some),
    }
}

/// A one relationship of a row, for generated `from_entry`s: `None` when
/// the query didn't ask for it or no row matches.
pub fn one<R: QueryElement>(
    entry: &ViewEntry,
    name: &str,
) -> Result<Option<Box<R>>, ZeroValueError> {
    match entry
        .relationship(name)
        .and_then(|v| v.entries().first().copied())
    {
        Some(e) => R::from_entry(e).map(|r| Some(Box::new(r))),
        None => Ok(None),
    }
}

/// The object a generated `json<T>()` type reads its fields from.
pub fn object<'a>(value: &'a Value, type_name: &str) -> Result<&'a Object, ZeroValueError> {
    value
        .as_object()
        .ok_or_else(|| ZeroValueError::mismatch(&format!("a {type_name} object"), value))
}

/// A field of a `json<T>()` object; a missing one reads as `null`, so only
/// an `Option` takes it.
pub fn field<V: ZeroValue>(object: &Object, name: &str) -> Result<V, ZeroValueError> {
    V::from_zero_value(object.get(name).unwrap_or(&Value::Null))
        .map_err(|e| ZeroValueError(format!("{name}: {e}")))
}

/// The schema of generated code, from the JSON form [`Schema::from_json`]
/// reads. Panics when it isn't one, which generated code never is.
pub fn schema_from_json(json: &str) -> Arc<Schema> {
    let value = crate::json::parse(json).expect("the generated schema is JSON");
    Arc::new(Schema::from_json(&value).expect("the generated schema is a schema"))
}

/// A row of a query's result: a row struct, or a row of `group_by` or
/// `distinct`.
pub trait QueryElement: Sized {
    /// Reads the row from a view's entry.
    fn from_entry(entry: &ViewEntry) -> Result<Self, ZeroValueError>;
}

/// A row struct of a table, as `zero-rust codegen` generates it: the
/// table's columns as fields, and a field per relationship that a query
/// fills when it asks for it with `related`.
pub trait ZeroRow: QueryElement {
    /// The client name of the row's table.
    const TABLE: &'static str;
    /// What [`TypedTableMutator::update`] takes: the primary key, and the
    /// other columns to set.
    type Update: ColumnValues;
    /// What [`TypedTableMutator::delete`] takes: the primary key.
    type Key: ColumnValues;
    /// The row's columns, by client column name.
    fn column_values(&self) -> Object;
}

/// Some of a row's columns, by client column name: what an update or a
/// delete writes. For generated code.
pub trait ColumnValues {
    /// The columns, by client column name.
    fn column_values(&self) -> Object;
}

/// The typed CRUD operations of a table in a mutator (`tx.mutate.<table>`
/// in JS), which the generated `mutate` module returns:
///
/// ```ignore
/// mutate::issue(tx).insert(&issue)?;
/// mutate::issue(tx).update(&IssueUpdate { closed: Some(true), ..IssueUpdate::new(id) })?;
/// ```
pub struct TypedTableMutator<'tx, R> {
    tx: &'tx Transaction,
    row: PhantomData<fn() -> R>,
}

impl<'tx, R: ZeroRow> TypedTableMutator<'tx, R> {
    /// For generated code.
    pub fn new(tx: &'tx Transaction) -> Self {
        TypedTableMutator {
            tx,
            row: PhantomData,
        }
    }

    /// Inserts a row unless one with its primary key exists.
    pub fn insert(&self, row: &R) -> Result<(), Error> {
        self.tx.table(R::TABLE).insert(&row.column_values())
    }

    /// Inserts a row, or replaces the columns of the existing one. Every
    /// column is written, so an optional one that is `None` sets null; JS
    /// keeps a column the value leaves out. Use `update` to set only some
    /// columns.
    pub fn upsert(&self, row: &R) -> Result<(), Error> {
        self.tx.table(R::TABLE).upsert(&row.column_values())
    }

    /// Sets the columns that `value` sets, of the row with its primary key,
    /// if the row exists.
    pub fn update(&self, value: &R::Update) -> Result<(), Error> {
        self.tx.table(R::TABLE).update(&value.column_values())
    }

    /// Deletes the row with the primary key `key`, if any.
    pub fn delete(&self, key: &R::Key) -> Result<(), Error> {
        self.tx.table(R::TABLE).delete(&key.column_values())
    }
}

/// A column in an update that can be null: left as it is (the default),
/// set to null, or set to a value. A column that can't be null is an
/// `Option` in an update instead, `None` leaving it.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub enum ColumnUpdate<V> {
    #[default]
    /// Leaves the column as it is.
    Unchanged,
    /// Sets the column to null.
    Null,
    /// Sets the column to the value.
    Set(V),
}

impl<V> From<Option<V>> for ColumnUpdate<V> {
    /// Sets the column to the value, or to null for `None`.
    fn from(value: Option<V>) -> Self {
        value.map_or(ColumnUpdate::Null, ColumnUpdate::Set)
    }
}

impl<V: ZeroValue> ColumnUpdate<V> {
    /// What the update writes to the column; `None` when it leaves it. For
    /// generated code.
    pub fn to_zero_value(&self) -> Option<Value> {
        match self {
            ColumnUpdate::Unchanged => None,
            ColumnUpdate::Null => Some(Value::Null),
            ColumnUpdate::Set(v) => Some(v.to_zero_value()),
        }
    }
}

/// What a query materializes to: its rows (`Vec<Row>`, `Option<Row>` after
/// `one()`), or the value of an aggregate, such as the `i64` of `count()`.
pub trait QueryValue: Sized {
    /// Reads the query's result from its view.
    fn from_view(view: &ViewValue) -> Result<Self, ZeroValueError>;
}

impl<E: QueryElement> QueryValue for Vec<E> {
    fn from_view(view: &ViewValue) -> Result<Self, ZeroValueError> {
        view.entries().into_iter().map(E::from_entry).collect()
    }
}

/// What an `Option` query value holds: the row of `one()`, or the value of
/// `sum`, `avg`, `min` or `max`, which is `None` over no rows. Generated
/// rows implement it with [`optional_row`], generated enumerations with
/// [`optional_value`].
pub trait OptionalQueryElement: Sized {
    /// Reads the query's result from its view: `None` for no row, or for an
    /// aggregate over no rows.
    fn optional_from_view(view: &ViewValue) -> Result<Option<Self>, ZeroValueError>;
}

impl<T: OptionalQueryElement> QueryValue for Option<T> {
    fn from_view(view: &ViewValue) -> Result<Self, ZeroValueError> {
        T::optional_from_view(view)
    }
}

/// The value of `count()`.
impl QueryValue for i64 {
    fn from_view(view: &ViewValue) -> Result<Self, ZeroValueError> {
        i64::from_zero_value(aggregate_value(view)?)
    }
}

fn aggregate_value(view: &ViewValue) -> Result<&Value, ZeroValueError> {
    match view {
        ViewValue::Value(v) => Ok(v),
        _ => Err(ZeroValueError(
            "expected the value of an aggregate, got rows".into(),
        )),
    }
}

/// The row of a `one()` view, for generated rows' [`OptionalQueryElement`].
pub fn optional_row<R: QueryElement>(view: &ViewValue) -> Result<Option<R>, ZeroValueError> {
    view.entries().first().map(|e| R::from_entry(e)).transpose()
}

/// The value of an aggregate's view: `None` when it's null.
pub fn optional_value<V: ZeroValue>(view: &ViewValue) -> Result<Option<V>, ZeroValueError> {
    let value = aggregate_value(view)?;
    if value.is_null() {
        Ok(None)
    } else {
        V::from_zero_value(value).map(Some)
    }
}

macro_rules! optional_values {
    ($($t:ty),*) => {$(
        impl OptionalQueryElement for $t {
            fn optional_from_view(view: &ViewValue) -> Result<Option<Self>, ZeroValueError> {
                optional_value(view)
            }
        }
    )*};
}
optional_values!(f64, i64, String, bool, Value);

/// A column of the row type `R`, holding `V`s: generated as a constant of
/// the row struct, such as `Issue::TITLE`.
pub struct Column<R, V> {
    name: &'static str,
    types: PhantomData<fn() -> (R, V)>,
}

impl<R, V> Clone for Column<R, V> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<R, V> Copy for Column<R, V> {}

impl<R, V> fmt::Debug for Column<R, V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Column").field(&self.name).finish()
    }
}

impl<R, V> Column<R, V> {
    /// The column named `name` (its client name). For generated code.
    pub const fn new(name: &'static str) -> Self {
        Column {
            name,
            types: PhantomData,
        }
    }

    /// The column's client name.
    pub fn name(&self) -> &'static str {
        self.name
    }

    fn compare(self, op: SimpleOperator, right: ValuePosition) -> Condition {
        Condition::Simple {
            op,
            left: ValuePosition::Column(self.name.to_owned()),
            right,
        }
    }

    /// Compares the column with a value of the context, such as the auth
    /// data.
    pub fn cmp_param(self, op: SimpleOperator, param: &StaticParameter) -> Condition {
        self.compare(
            op,
            ValuePosition::Static {
                anchor: param.anchor,
                field: param.field.clone(),
            },
        )
    }
}

impl<R, V: ZeroValue> Column<R, V> {
    /// Compares the column with `value` by `op`.
    pub fn cmp(self, op: SimpleOperator, value: V) -> Condition {
        self.compare(op, ValuePosition::Literal(value.to_zero_value()))
    }

    /// Equal to `value`. Comparing with null uses `IS`, since `= NULL`
    /// matches nothing.
    pub fn eq(self, value: V) -> Condition {
        let value = value.to_zero_value();
        let op = if value.is_null() {
            SimpleOperator::Is
        } else {
            SimpleOperator::Eq
        };
        self.compare(op, ValuePosition::Literal(value))
    }

    /// Not equal to `value`; with null, `IS NOT`.
    pub fn ne(self, value: V) -> Condition {
        let value = value.to_zero_value();
        let op = if value.is_null() {
            SimpleOperator::IsNot
        } else {
            SimpleOperator::Ne
        };
        self.compare(op, ValuePosition::Literal(value))
    }

    /// The column is less than `value`.
    pub fn lt(self, value: V) -> Condition {
        self.cmp(SimpleOperator::Lt, value)
    }
    /// The column is at most `value`.
    pub fn le(self, value: V) -> Condition {
        self.cmp(SimpleOperator::Le, value)
    }
    /// The column is greater than `value`.
    pub fn gt(self, value: V) -> Condition {
        self.cmp(SimpleOperator::Gt, value)
    }
    /// The column is at least `value`.
    pub fn ge(self, value: V) -> Condition {
        self.cmp(SimpleOperator::Ge, value)
    }
    /// The column `IS` `value`: as `=`, but null matches null.
    pub fn is(self, value: V) -> Condition {
        self.cmp(SimpleOperator::Is, value)
    }
    /// The column `IS NOT` `value`: as `!=`, but null matches non-null values.
    pub fn is_not(self, value: V) -> Condition {
        self.cmp(SimpleOperator::IsNot, value)
    }

    /// `IN`.
    pub fn is_in(self, values: Vec<V>) -> Condition {
        self.compare(SimpleOperator::In, ValuePosition::Literal(array(values)))
    }

    /// `NOT IN`.
    pub fn not_in(self, values: Vec<V>) -> Condition {
        self.compare(SimpleOperator::NotIn, ValuePosition::Literal(array(values)))
    }
}

fn array<V: ZeroValue>(values: Vec<V>) -> Value {
    Value::Array(values.iter().map(V::to_zero_value).collect())
}

macro_rules! like {
    ($t:ty) => {
        impl<R> Column<R, $t> {
            /// The column matches the SQL `LIKE` pattern, with `%` and `_` as
            /// wildcards (`like::escape_like` escapes them).
            pub fn like(self, pattern: &str) -> Condition {
                self.pattern(SimpleOperator::Like, pattern)
            }
            /// The column doesn't match the `LIKE` pattern.
            pub fn not_like(self, pattern: &str) -> Condition {
                self.pattern(SimpleOperator::NotLike, pattern)
            }
            /// The column matches the pattern ignoring case (`ILIKE`).
            pub fn ilike(self, pattern: &str) -> Condition {
                self.pattern(SimpleOperator::Ilike, pattern)
            }
            /// The column doesn't match the pattern ignoring case (`NOT
            /// ILIKE`).
            pub fn not_ilike(self, pattern: &str) -> Condition {
                self.pattern(SimpleOperator::NotIlike, pattern)
            }
            fn pattern(self, op: SimpleOperator, pattern: &str) -> Condition {
                self.compare(
                    op,
                    ValuePosition::Literal(Value::String(pattern.to_owned())),
                )
            }
        }
    };
}
like!(String);
like!(Option<String>);

/// A column of `R` of any type, for the lists of `group_by`, `distinct`
/// and `start_values`: `Issue::OWNER_ID.into()`.
pub struct ColumnRef<R> {
    name: &'static str,
    row: PhantomData<fn() -> R>,
}

impl<R> Clone for ColumnRef<R> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<R> Copy for ColumnRef<R> {}

impl<R> fmt::Debug for ColumnRef<R> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("ColumnRef").field(&self.name).finish()
    }
}

impl<R> ColumnRef<R> {
    /// The column's client name.
    pub fn name(&self) -> &'static str {
        self.name
    }
}

impl<R, V> From<Column<R, V>> for ColumnRef<R> {
    fn from(c: Column<R, V>) -> Self {
        ColumnRef {
            name: c.name,
            row: PhantomData,
        }
    }
}

/// A relationship from the row type `R` to the row type `D`: generated as a
/// constant of the row struct, such as `Issue::COMMENTS`.
pub struct Relationship<R, D> {
    name: &'static str,
    types: PhantomData<fn() -> (R, D)>,
}

impl<R, D> Clone for Relationship<R, D> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<R, D> Copy for Relationship<R, D> {}

impl<R, D> fmt::Debug for Relationship<R, D> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Relationship").field(&self.name).finish()
    }
}

impl<R, D> Relationship<R, D> {
    /// The relationship named `name`. For generated code.
    pub const fn new(name: &'static str) -> Self {
        Relationship {
            name,
            types: PhantomData,
        }
    }

    /// The relationship's name.
    pub fn name(&self) -> &'static str {
        self.name
    }
}

/// The aggregates a row's query asked for in `related`, by relationship:
/// `related(Issue::COMMENTS, |q| q.count())` gives
/// `issue.aggregates.count(Issue::COMMENTS)`. The relationship's own field
/// stays `None`.
pub struct RelationshipAggregates<R> {
    /// The values by relationship name.
    pub values: Vec<(String, Value)>,
    row: PhantomData<fn() -> R>,
}

impl<R> Clone for RelationshipAggregates<R> {
    fn clone(&self) -> Self {
        RelationshipAggregates {
            values: self.values.clone(),
            row: PhantomData,
        }
    }
}

impl<R> fmt::Debug for RelationshipAggregates<R> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map()
            .entries(self.values.iter().map(|(k, v)| (k, v)))
            .finish()
    }
}

impl<R> PartialEq for RelationshipAggregates<R> {
    fn eq(&self, other: &Self) -> bool {
        self.values == other.values
    }
}

impl<R> Default for RelationshipAggregates<R> {
    fn default() -> Self {
        RelationshipAggregates {
            values: Vec::new(),
            row: PhantomData,
        }
    }
}

impl<R> RelationshipAggregates<R> {
    /// The aggregates of `entry`'s relationships, for generated
    /// `from_entry`s.
    pub fn from_entry(entry: &ViewEntry) -> Self {
        RelationshipAggregates {
            values: entry
                .relationships
                .iter()
                .filter_map(|(name, v)| match v {
                    ViewValue::Value(v) => Some((name.clone(), v.clone())),
                    _ => None,
                })
                .collect(),
            row: PhantomData,
        }
    }

    /// Whether the query asked for no aggregates.
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    /// The `count()` of a relationship; `None` when the query didn't ask
    /// for it.
    pub fn count<D>(&self, relationship: Relationship<R, D>) -> Option<i64> {
        self.value(relationship)
    }

    /// The `sum` of a relationship; `None` when the query didn't ask for it
    /// or it has no rows.
    pub fn sum<D>(&self, relationship: Relationship<R, D>) -> Option<f64> {
        self.value(relationship)
    }

    /// The `avg` of a relationship; `None` when the query didn't ask for it
    /// or it has no rows.
    pub fn avg<D>(&self, relationship: Relationship<R, D>) -> Option<f64> {
        self.value(relationship)
    }

    /// The aggregate of a relationship read as `V`, as for `min` and `max`;
    /// `None` when the query didn't ask for it, it's null, or it isn't a
    /// `V`.
    pub fn value<V: ZeroValue, D>(&self, relationship: Relationship<R, D>) -> Option<V> {
        let (_, v) = self.values.iter().find(|(k, _)| k == relationship.name)?;
        if v.is_null() {
            return None;
        }
        V::from_zero_value(v).ok()
    }
}

/// A query over the rows of `R`'s table that materializes to a `T`: the
/// rows (`Vec<R>`), the row of `one()` (`Option<R>`), or an aggregate.
pub struct Query<R, T = Vec<R>> {
    any: AnyQuery,
    types: PhantomData<fn() -> (R, T)>,
}

impl<R, T> Clone for Query<R, T> {
    fn clone(&self) -> Self {
        Query::from_any(self.any.clone())
    }
}

impl<R, T> fmt::Debug for Query<R, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Query").field(&self.any).finish()
    }
}

impl<R, T> Query<R, T> {
    /// The typed form of an untyped query of `R`'s table.
    pub fn from_any(any: AnyQuery) -> Self {
        Query {
            any,
            types: PhantomData,
        }
    }

    /// The untyped query this one builds.
    pub fn any(&self) -> &AnyQuery {
        &self.any
    }

    /// The untyped query this one wraps.
    pub fn into_any(self) -> AnyQuery {
        self.any
    }

    fn map(self, f: impl FnOnce(AnyQuery) -> AnyQuery) -> Self {
        Query::from_any(f(self.any))
    }

    fn to<U>(self, f: impl FnOnce(AnyQuery) -> AnyQuery) -> Query<R, U> {
        Query::from_any(f(self.any))
    }
}

impl<R: ZeroRow> Query<R> {
    /// All rows of `R`'s table, for the generated query functions.
    pub fn table(schema: Arc<Schema>) -> Self {
        Query::from_any(AnyQuery::new(schema, R::TABLE, System::Client))
    }
}

impl<R: ZeroRow, T> Query<R, T> {
    /// Rows whose `column` equals `value`; with null, `IS`.
    pub fn where_eq<V: ZeroValue>(self, column: Column<R, V>, value: V) -> Self {
        self.where_condition(column.eq(value))
    }

    /// Rows whose `column` compares to `value` by `op`.
    pub fn where_<V: ZeroValue>(self, column: Column<R, V>, op: SimpleOperator, value: V) -> Self {
        self.map(|q| q.where_(column.name, op, value.to_zero_value()))
    }

    /// Rows whose `column` is `IN` or `NOT IN` `values`.
    pub fn where_in<V: ZeroValue>(
        self,
        column: Column<R, V>,
        op: SimpleOperator,
        values: Vec<V>,
    ) -> Self {
        self.map(|q| q.where_(column.name, op, array(values)))
    }

    /// Rows whose `column` compares to a value of the context by `op`.
    pub fn where_param<V>(
        self,
        column: Column<R, V>,
        op: SimpleOperator,
        param: &StaticParameter,
    ) -> Self {
        self.map(|q| q.where_param(column.name, op, param))
    }

    /// Rows that match `condition`, and any earlier `where`.
    pub fn where_condition(self, condition: Condition) -> Self {
        self.map(|q| q.where_condition(condition))
    }

    /// Rows that match the condition `factory` builds from the row's
    /// expression: its JSON paths, `exists`, and `and`, `or` and `not`.
    pub fn where_expr(self, factory: impl FnOnce(&RowExpression<'_, '_, R>) -> Condition) -> Self {
        self.map(|q| {
            q.where_expr(|eb| {
                factory(&RowExpression {
                    builder: eb,
                    row: PhantomData,
                })
            })
        })
    }

    /// Rows with a related row in `relationship` that `sub` matches.
    pub fn where_exists<D: ZeroRow>(
        self,
        relationship: Relationship<R, D>,
        options: ExistsOptions,
        sub: impl FnOnce(Query<D>) -> Query<D>,
    ) -> Self {
        self.map(|q| q.where_exists(relationship.name, options, untyped(sub)))
    }

    /// Fills `relationship` of each row, or, when `sub` ends in an
    /// aggregate, sets the row's aggregate of it: `related(Issue::COMMENTS,
    /// |q| q.count())` gives `aggregates.count(Issue::COMMENTS)`.
    pub fn related<D: ZeroRow, S>(
        self,
        relationship: Relationship<R, D>,
        sub: impl FnOnce(Query<D>) -> Query<D, S>,
    ) -> Self {
        self.map(|q| q.related(relationship.name, |a| sub(Query::from_any(a)).any))
    }

    /// Orders the rows by `column`, after any earlier `order_by`; the primary
    /// key breaks ties.
    pub fn order_by<V>(self, column: Column<R, V>, direction: Direction) -> Self {
        self.map(|q| q.order_by(column.name, direction))
    }

    /// At most `limit` rows.
    pub fn limit(self, limit: i64) -> Self {
        self.map(|q| q.limit(limit))
    }

    /// Starts after `row`, or at it when `inclusive`.
    pub fn start(self, row: &R, inclusive: bool) -> Self {
        self.map(|q| q.start(row.column_values(), inclusive))
    }

    /// Starts after the row with these column values, or at it when
    /// `inclusive`: a cursor that holds only the columns the query orders
    /// by.
    pub fn start_values(self, values: Vec<(ColumnRef<R>, Value)>, inclusive: bool) -> Self {
        let row = values.into_iter().map(|(c, v)| (c.name, v)).collect();
        self.map(|q| q.start(row, inclusive))
    }

    /// At most one row.
    pub fn one(self) -> Query<R, Option<R>> {
        self.to(AnyQuery::one)
    }

    /// Reduces the query to a count of the rows left after `where`, `start`
    /// and `limit`.
    pub fn count(self) -> Query<R, i64> {
        self.to(AnyQuery::count)
    }

    /// The sum of a number column; `None` over no rows.
    pub fn sum<V: NumericZeroValue>(self, column: Column<R, V>) -> Query<R, Option<f64>> {
        self.to(|q| q.sum(column.name))
    }

    /// The sum of the numbers at `path` in a `json` column. A missing path,
    /// or a value that isn't a number, is skipped.
    pub fn sum_json<V>(
        self,
        column: Column<R, V>,
        path: Vec<JsonPathSegment>,
    ) -> Query<R, Option<f64>> {
        self.to(|q| q.sum_json(column.name, path))
    }

    /// The average of a number column; `None` over no rows.
    pub fn avg<V: NumericZeroValue>(self, column: Column<R, V>) -> Query<R, Option<f64>> {
        self.to(|q| q.avg(column.name))
    }

    /// Reduces the query to the average of the numbers at `path` in the JSON
    /// `column`.
    pub fn avg_json<V>(
        self,
        column: Column<R, V>,
        path: Vec<JsonPathSegment>,
    ) -> Query<R, Option<f64>> {
        self.to(|q| q.avg_json(column.name, path))
    }

    /// The least value of a number or string column; `None` over no rows.
    pub fn min<V: OrderedZeroValue>(self, column: Column<R, V>) -> Query<R, Option<V::Unwrapped>> {
        self.to(|q| q.min(column.name))
    }

    /// Reduces the query to the least value at `path` in the JSON `column`.
    pub fn min_json<V>(
        self,
        column: Column<R, V>,
        path: Vec<JsonPathSegment>,
    ) -> Query<R, Option<Value>> {
        self.to(|q| q.min_json(column.name, path))
    }

    /// The greatest value of a number or string column; `None` over no
    /// rows.
    pub fn max<V: OrderedZeroValue>(self, column: Column<R, V>) -> Query<R, Option<V::Unwrapped>> {
        self.to(|q| q.max(column.name))
    }

    /// Reduces the query to the greatest value at `path` in the JSON `column`.
    pub fn max_json<V>(
        self,
        column: Column<R, V>,
        path: Vec<JsonPathSegment>,
    ) -> Query<R, Option<Value>> {
        self.to(|q| q.max_json(column.name, path))
    }

    /// The distinct combinations of `columns` among the rows `where` keeps.
    /// `count()` after it counts the combinations.
    pub fn distinct(self, columns: Vec<ColumnRef<R>>) -> Query<R, Vec<DistinctRow<R>>> {
        self.to(|q| q.distinct(names(columns)))
    }

    /// Groups the rows by `columns`, for the aggregate that reduces each
    /// group.
    pub fn group_by(self, columns: Vec<ColumnRef<R>>) -> GroupedQuery<R> {
        GroupedQuery {
            any: self.any.group_by(names(columns)),
            row: PhantomData,
        }
    }
}

impl<C, R, T> crate::query::ZeroQuery<C> for Query<R, T> {
    fn any_query(&self, _: &C) -> std::borrow::Cow<'_, AnyQuery> {
        std::borrow::Cow::Borrowed(&self.any)
    }
}

impl<R, T: QueryValue> Query<R, T> {
    /// Reads a view of this query as its result.
    pub fn read(&self, view: &ViewValue) -> Result<T, ZeroValueError> {
        T::from_view(view)
    }
}

fn names<R>(columns: Vec<ColumnRef<R>>) -> Vec<String> {
    columns.into_iter().map(|c| c.name.to_owned()).collect()
}

fn untyped<D>(sub: impl FnOnce(Query<D>) -> Query<D>) -> impl FnOnce(AnyQuery) -> AnyQuery {
    |a| sub(Query::from_any(a)).any
}

/// The row a `where_expr` condition is about: JSON paths into its columns,
/// `exists` over its relationships, and the connectives. Compare columns
/// with their constants' methods, such as `Issue::TITLE.like("%bug%")`.
pub struct RowExpression<'a, 'q, R> {
    builder: &'a ExpressionBuilder<'q>,
    row: PhantomData<fn() -> R>,
}

impl<R> RowExpression<'_, '_, R> {
    /// A value inside a `json` column: `e.json(Issue::METADATA,
    /// vec!["tags".into(), 0.into()]).eq("bug".into())`.
    pub fn json<V>(&self, column: Column<R, V>, path: Vec<JsonPathSegment>) -> JsonPath<'_, '_> {
        JsonPath {
            builder: self.builder,
            reference: self.builder.json(column.name, path),
        }
    }

    /// Whether a related row in `relationship` matches `sub`.
    pub fn exists<D: ZeroRow>(
        &self,
        relationship: Relationship<R, D>,
        options: ExistsOptions,
        sub: impl FnOnce(Query<D>) -> Query<D>,
    ) -> Condition {
        self.builder
            .exists(relationship.name, options, untyped(sub))
    }

    /// All of `conditions`; true when there are none.
    pub fn and(&self, conditions: Vec<Condition>) -> Condition {
        self.builder.and(conditions)
    }

    /// Any of `conditions`; false when there are none.
    pub fn or(&self, conditions: Vec<Condition>) -> Condition {
        self.builder.or(conditions)
    }

    /// Rows that `condition` doesn't match.
    pub fn not(&self, condition: Condition) -> Condition {
        self.builder.not(condition)
    }
}

/// A value inside a `json` column in a `where_expr`. Its JSON type isn't
/// known, so it compares with any [`Value`]: one of another type never
/// equals it. Comparing with null uses `IS`.
pub struct JsonPath<'a, 'q> {
    builder: &'a ExpressionBuilder<'q>,
    reference: JsonPathReference,
}

impl JsonPath<'_, '_> {
    /// The value at the path compares to `value` by `op`.
    pub fn cmp(&self, op: SimpleOperator, value: Value) -> Condition {
        self.builder.cmp_json(&self.reference, op, value)
    }

    /// The value at the path compares to a value of the context by `op`.
    pub fn cmp_param(&self, op: SimpleOperator, param: &StaticParameter) -> Condition {
        self.builder.cmp_json_param(&self.reference, op, param)
    }

    /// The value at the path equals `value` (`IS` for null).
    pub fn eq(&self, value: Value) -> Condition {
        let op = if value.is_null() {
            SimpleOperator::Is
        } else {
            SimpleOperator::Eq
        };
        self.cmp(op, value)
    }

    /// The value at the path differs from `value` (`IS NOT` for null).
    pub fn ne(&self, value: Value) -> Condition {
        let op = if value.is_null() {
            SimpleOperator::IsNot
        } else {
            SimpleOperator::Ne
        };
        self.cmp(op, value)
    }

    /// The value at the path is less than `value`.
    pub fn lt(&self, value: Value) -> Condition {
        self.cmp(SimpleOperator::Lt, value)
    }
    /// The value at the path is at most `value`.
    pub fn le(&self, value: Value) -> Condition {
        self.cmp(SimpleOperator::Le, value)
    }
    /// The value at the path is greater than `value`.
    pub fn gt(&self, value: Value) -> Condition {
        self.cmp(SimpleOperator::Gt, value)
    }
    /// The value at the path is at least `value`.
    pub fn ge(&self, value: Value) -> Condition {
        self.cmp(SimpleOperator::Ge, value)
    }
    /// The value at the path `IS` `value`.
    pub fn is(&self, value: Value) -> Condition {
        self.cmp(SimpleOperator::Is, value)
    }
    /// The value at the path `IS NOT` `value`.
    pub fn is_not(&self, value: Value) -> Condition {
        self.cmp(SimpleOperator::IsNot, value)
    }
    /// `values` must be of one JSON type.
    pub fn is_in(&self, values: Vec<Value>) -> Condition {
        self.cmp(SimpleOperator::In, Value::Array(values))
    }
    /// The value at the path is none of `values`.
    pub fn not_in(&self, values: Vec<Value>) -> Condition {
        self.cmp(SimpleOperator::NotIn, Value::Array(values))
    }
    /// The value at the path matches the `LIKE` pattern.
    pub fn like(&self, pattern: &str) -> Condition {
        self.cmp(SimpleOperator::Like, Value::String(pattern.to_owned()))
    }
    /// The value at the path doesn't match the `LIKE` pattern.
    pub fn not_like(&self, pattern: &str) -> Condition {
        self.cmp(SimpleOperator::NotLike, Value::String(pattern.to_owned()))
    }
    /// The value at the path matches the pattern ignoring case (`ILIKE`).
    pub fn ilike(&self, pattern: &str) -> Condition {
        self.cmp(SimpleOperator::Ilike, Value::String(pattern.to_owned()))
    }
    /// The value at the path doesn't match the pattern ignoring case.
    pub fn not_ilike(&self, pattern: &str) -> Condition {
        self.cmp(SimpleOperator::NotIlike, Value::String(pattern.to_owned()))
    }
}

/// A query of the [`GroupRow`]s of `group_by` and an aggregate.
pub type Grouped<R, V> = Query<R, Vec<GroupRow<R, V>>>;

/// A query grouped by columns, waiting for the aggregate that reduces each
/// group (`GroupedQuery` in `packages/zql/src/query/query.ts`): one
/// [`GroupRow`] per group.
pub struct GroupedQuery<R> {
    any: AnyGroupedQuery,
    row: PhantomData<fn() -> R>,
}

impl<R> GroupedQuery<R> {
    /// The untyped grouped query this one wraps.
    pub fn any(&self) -> &AnyGroupedQuery {
        &self.any
    }

    /// Each group's row count.
    pub fn count(self) -> Grouped<R, i64> {
        Query::from_any(self.any.count())
    }

    /// Each group's sum of `column`.
    pub fn sum<V: NumericZeroValue>(self, column: Column<R, V>) -> Grouped<R, Option<f64>> {
        Query::from_any(self.any.sum(column.name))
    }

    /// Each group's sum of the numbers at `path` in the JSON `column`.
    pub fn sum_json<V>(
        self,
        column: Column<R, V>,
        path: Vec<JsonPathSegment>,
    ) -> Grouped<R, Option<f64>> {
        Query::from_any(self.any.sum_json(column.name, path))
    }

    /// Each group's average of `column`.
    pub fn avg<V: NumericZeroValue>(self, column: Column<R, V>) -> Grouped<R, Option<f64>> {
        Query::from_any(self.any.avg(column.name))
    }

    /// Each group's average of the numbers at `path` in the JSON `column`.
    pub fn avg_json<V>(
        self,
        column: Column<R, V>,
        path: Vec<JsonPathSegment>,
    ) -> Grouped<R, Option<f64>> {
        Query::from_any(self.any.avg_json(column.name, path))
    }

    /// Each group's least value of `column`.
    pub fn min<V: OrderedZeroValue>(
        self,
        column: Column<R, V>,
    ) -> Grouped<R, Option<V::Unwrapped>> {
        Query::from_any(self.any.min(column.name))
    }

    /// Each group's least value at `path` in the JSON `column`.
    pub fn min_json<V>(
        self,
        column: Column<R, V>,
        path: Vec<JsonPathSegment>,
    ) -> Grouped<R, Option<Value>> {
        Query::from_any(self.any.min_json(column.name, path))
    }

    /// Each group's greatest value of `column`.
    pub fn max<V: OrderedZeroValue>(
        self,
        column: Column<R, V>,
    ) -> Grouped<R, Option<V::Unwrapped>> {
        Query::from_any(self.any.max(column.name))
    }

    /// Each group's greatest value at `path` in the JSON `column`.
    pub fn max_json<V>(
        self,
        column: Column<R, V>,
        path: Vec<JsonPathSegment>,
    ) -> Grouped<R, Option<Value>> {
        Query::from_any(self.any.max_json(column.name, path))
    }
}

/// A row of `group_by(...)` and an aggregate: the group's columns, read with
/// [`GroupRow::get`], and the aggregate's `value`.
pub struct GroupRow<R, V> {
    /// The aggregate of the group's rows.
    pub value: V,
    /// The group's columns by client name.
    pub columns: Object,
    row: PhantomData<fn() -> R>,
}

impl<R, V: Clone> Clone for GroupRow<R, V> {
    fn clone(&self) -> Self {
        GroupRow {
            value: self.value.clone(),
            columns: self.columns.clone(),
            row: PhantomData,
        }
    }
}

impl<R, V: fmt::Debug> fmt::Debug for GroupRow<R, V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("GroupRow")
            .field("value", &self.value)
            .field("columns", &self.columns)
            .finish()
    }
}

impl<R, V: PartialEq> PartialEq for GroupRow<R, V> {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value && self.columns == other.columns
    }
}

impl<R, V: ZeroValue> QueryElement for GroupRow<R, V> {
    fn from_entry(entry: &ViewEntry) -> Result<Self, ZeroValueError> {
        // `GroupProjection` puts the value after the group columns. Its name
        // is the function's, which can be another group column's.
        let mut columns: Object = (*entry.row).clone();
        let last = columns
            .get_index(columns.len().wrapping_sub(1))
            .map(|(k, v)| (k.clone(), v.clone()))
            .ok_or_else(|| ZeroValueError("a group row has no value".into()))?;
        columns.remove(&last.0);
        Ok(GroupRow {
            value: decode(&last.0, Some(&last.1))?,
            columns,
            row: PhantomData,
        })
    }
}

impl<R, V> GroupRow<R, V> {
    /// A column the query groups by, read as a row's column is.
    pub fn get<C: ZeroValue>(&self, column: Column<R, C>) -> Result<C, ZeroValueError> {
        group_column(&self.columns, column)
    }
}

/// A row of `distinct(...)`: the values of its columns, read with
/// [`DistinctRow::get`].
pub struct DistinctRow<R> {
    /// The columns by client name.
    pub columns: Object,
    row: PhantomData<fn() -> R>,
}

impl<R> Clone for DistinctRow<R> {
    fn clone(&self) -> Self {
        DistinctRow {
            columns: self.columns.clone(),
            row: PhantomData,
        }
    }
}

impl<R> fmt::Debug for DistinctRow<R> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("DistinctRow").field(&self.columns).finish()
    }
}

impl<R> PartialEq for DistinctRow<R> {
    fn eq(&self, other: &Self) -> bool {
        self.columns == other.columns
    }
}

impl<R> QueryElement for DistinctRow<R> {
    fn from_entry(entry: &ViewEntry) -> Result<Self, ZeroValueError> {
        Ok(DistinctRow {
            columns: (*entry.row).clone(),
            row: PhantomData,
        })
    }
}

impl<R> DistinctRow<R> {
    /// A column of the `distinct`, read as a row's column is.
    pub fn get<C: ZeroValue>(&self, column: Column<R, C>) -> Result<C, ZeroValueError> {
        group_column(&self.columns, column)
    }
}

fn group_column<R, C: ZeroValue>(
    columns: &Object,
    column: Column<R, C>,
) -> Result<C, ZeroValueError> {
    match columns.get(column.name) {
        Some(v) => decode(column.name, Some(v)),
        None => Err(ZeroValueError(format!(
            "{} is not a column of the group",
            column.name
        ))),
    }
}