zero-client 0.1.2

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
//! The untyped query builder (`QueryImpl` in
//! `packages/zql/src/query/query-impl.ts`) and the builder of `where`
//! conditions (`ExpressionBuilder` in `packages/zql/src/query/expression.ts`).
//!
//! The AST is always normalized: each step builds its part of the AST in
//! normalized form. Misuse that JS reports by throwing from the builder,
//! such as a negative `limit` or a `limit` inside a junction relationship,
//! leaves that step out and makes the query invalid: [`AnyQuery::invalid`]
//! holds JS's message. An unknown relationship is a programming error and
//! panics.

use crate::ast::*;
use crate::format::Format;
use crate::hash::query::hash_of_query_internals;
use crate::json::{self, Object, Value};
use crate::schema::{Cardinality, Relationship, RelationshipHop, Schema, ValueType};
use std::cell::RefCell;
use std::sync::Arc;

/// The prefix of the aliases the builder gives `exists` subqueries
/// (`SUBQ_PREFIX` in `packages/zero-protocol/src/ast.ts`).
pub const SUBQUERY_ALIAS_PREFIX: &str = "zsubq_";

/// The name and arguments of the named query a query was built by.
#[derive(Clone, Debug, PartialEq)]
pub struct CustomQueryId {
    /// The query's name.
    pub name: String,
    /// Its arguments.
    pub args: Vec<Value>,
}

/// A value taken from the context a query runs in, such as the auth data
/// (`staticParam` in `packages/zql/src/query/static-param.ts`).
#[derive(Clone, Debug, PartialEq)]
pub struct StaticParameter {
    /// What the value is taken from.
    pub anchor: Anchor,
    /// The field of it.
    pub field: StaticField,
}

impl StaticParameter {
    /// The field `field` of `anchor`.
    pub fn new(anchor: Anchor, field: impl Into<String>) -> Self {
        StaticParameter {
            anchor,
            field: StaticField::Single(field.into()),
        }
    }

    /// The value at `path` in `anchor`.
    pub fn path(anchor: Anchor, path: Vec<String>) -> Self {
        StaticParameter {
            anchor,
            field: StaticField::Path(path),
        }
    }

    fn position(&self) -> ValuePosition {
        ValuePosition::Static {
            anchor: self.anchor,
            field: self.field.clone(),
        }
    }
}

/// The options of `exists` and `whereExists`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ExistsOptions {
    /// Runs the subquery first and looks up the parents of its rows.
    pub flip: Option<bool>,
    /// Lets the server resolve the subquery to a value once.
    pub scalar: Option<bool>,
}

/// A query over a table, named by strings.
#[derive(Clone, Debug)]
pub struct AnyQuery {
    schema: Arc<Schema>,
    table: String,
    ast: Ast,
    format: Format,
    system: System,
    custom_query_id: Option<CustomQueryId>,
    /// The two-hop relationship this query is the destination of, if any.
    current_junction: Option<String>,
    /// Why the query is invalid: the first misuse JS's builder throws for.
    invalid: Option<String>,
}

impl AnyQuery {
    /// All rows of `table`, for the system `system` (an app uses
    /// `System::Client`).
    pub fn new(schema: Arc<Schema>, table: impl Into<String>, system: System) -> Self {
        let table = table.into();
        AnyQuery {
            schema,
            ast: Ast::new(table.clone()),
            table,
            format: Format::default(),
            system,
            custom_query_id: None,
            current_junction: None,
            invalid: None,
        }
    }

    /// The schema the query is built on.
    pub fn schema(&self) -> &Arc<Schema> {
        &self.schema
    }

    /// The table the query reads.
    pub fn table(&self) -> &str {
        &self.table
    }

    /// The query's AST.
    pub fn ast(&self) -> &Ast {
        &self.ast
    }

    /// How the query's view shapes its rows.
    pub fn format(&self) -> &Format {
        &self.format
    }

    /// The system the query is built for.
    pub fn system(&self) -> System {
        self.system
    }

    /// The name and arguments the query is named by, for a named query.
    pub fn custom_query_id(&self) -> Option<&CustomQueryId> {
        self.custom_query_id.as_ref()
    }

    /// Why the query is invalid: the message JS's builder throws.
    pub fn invalid(&self) -> Option<&str> {
        self.invalid.as_deref()
    }

    /// The query's hash as the client and server know it.
    pub fn hash(&self) -> String {
        hash_of_query_internals(
            &self.ast,
            &crate::decode::JsonCodable::to_json(&self.format),
            self.system,
            self.current_junction.as_deref(),
            self.custom_query_id
                .as_ref()
                .map(|c| (c.name.as_str(), Some(c.args.as_slice()))),
        )
    }

    fn with(mut self, ast: Ast, format: Option<Format>) -> Self {
        self.ast = ast;
        if let Some(format) = format {
            self.format = format;
        }
        self
    }

    /// This query, unchanged but invalid: a builder step JS throws for.
    fn invalidated(self, message: impl Into<String>) -> Self {
        self.with_invalid(Some(message.into()))
    }

    fn with_invalid(mut self, invalid: Option<String>) -> Self {
        if self.invalid.is_none() {
            self.invalid = invalid;
        }
        self
    }

    fn base(&self, table: &str, alias: String, format: Format, junction: Option<&str>) -> Self {
        let mut ast = Ast::new(table);
        ast.alias = Some(alias);
        AnyQuery {
            schema: self.schema.clone(),
            table: table.to_owned(),
            ast,
            format,
            system: self.system,
            custom_query_id: self.custom_query_id.clone(),
            current_junction: junction.map(str::to_owned),
            invalid: None,
        }
    }

    /// Names the query, as JS's `defineQueries` does: the server runs its own
    /// query by `name` with `args`, and the client syncs what that returns.
    pub fn name_and_args(mut self, name: impl Into<String>, args: Vec<Value>) -> Self {
        self.custom_query_id = Some(CustomQueryId {
            name: name.into(),
            args,
        });
        self
    }

    /// At most one row; the view holds the row or nothing rather than a list.
    pub fn one(self) -> Self {
        if let Some(m) = self
            .not_aggregated("one")
            .or_else(|| self.not_distinct("one"))
        {
            return self.invalidated(m);
        }
        let mut ast = self.ast.clone();
        ast.limit = Some(1.0);
        let mut format = self.format.clone();
        format.singular = true;
        self.with(ast, Some(format))
    }

    /// Rows whose `field` compares to `value` by `op`.
    pub fn where_(
        self,
        field: impl Into<String>,
        op: SimpleOperator,
        value: impl Into<Value>,
    ) -> Self {
        self.where_condition(cmp_condition(
            field.into(),
            op,
            ValuePosition::Literal(value.into()),
        ))
    }

    /// Rows whose `field` compares to a value of the context by `op`.
    pub fn where_param(
        self,
        field: impl Into<String>,
        op: SimpleOperator,
        param: &StaticParameter,
    ) -> Self {
        self.where_condition(cmp_condition(field.into(), op, param.position()))
    }

    /// Rows that match the condition `factory` builds.
    pub fn where_expr(self, factory: impl FnOnce(&ExpressionBuilder) -> Condition) -> Self {
        let builder = ExpressionBuilder::new(&self);
        let condition = factory(&builder);
        let invalid = builder.invalid.into_inner();
        self.where_condition(condition).with_invalid(invalid)
    }

    /// Rows that match `condition`, and any earlier `where`.
    pub fn where_condition(self, condition: Condition) -> Self {
        let merged = match &self.ast.where_ {
            Some(w) => and_conditions(vec![w.clone(), condition]),
            None => condition,
        };
        let mut ast = self.ast.clone();
        ast.where_ = Some(normalize_condition(&simplify_condition(merged)));
        self.with(ast, None)
    }

    /// Rows with a related row in `relationship` that `sub` matches.
    pub fn where_exists(
        self,
        relationship: &str,
        options: ExistsOptions,
        sub: impl FnOnce(AnyQuery) -> AnyQuery,
    ) -> Self {
        self.where_expr(|eb| eb.exists(relationship, options, sub))
    }

    /// Starts after the row with these columns, or at it when `inclusive`.
    pub fn start(self, row: Object, inclusive: bool) -> Self {
        if let Some(m) = self
            .not_aggregated("start")
            .or_else(|| self.not_distinct("start"))
        {
            return self.invalidated(m);
        }
        let mut ast = self.ast.clone();
        ast.start = Some(Bound {
            row,
            exclusive: !inclusive,
        });
        self.with(ast, None)
    }

    /// At most `limit` rows.
    pub fn limit(self, limit: i64) -> Self {
        if let Some(m) = self
            .not_aggregated("limit")
            .or_else(|| self.not_distinct("limit"))
        {
            return self.invalidated(m);
        }
        if limit < 0 {
            return self.invalidated("Limit must be non-negative");
        }
        if i32::try_from(limit).is_err() {
            return self.invalidated("Limit must be an integer");
        }
        if let Some(j) = &self.current_junction {
            let m = format!(
                "Limit is not supported in junction relationships yet. Junction relationship being limited: {j}"
            );
            return self.invalidated(m);
        }
        let mut ast = self.ast.clone();
        ast.limit = Some(limit as f64);
        self.with(ast, None)
    }

    /// Orders the rows by `field`, after any earlier `order_by`; the primary
    /// key breaks ties.
    pub fn order_by(self, field: impl Into<String>, direction: Direction) -> Self {
        let field = field.into();
        if let Some(j) = &self.current_junction {
            let m = format!(
                "Order by is not supported in junction relationships yet. Junction relationship being ordered: {j}"
            );
            return self.invalidated(m);
        }
        if let Some(group_by) = &self.ast.group_by
            && let Some(m) = not_group_column(&field, group_by)
        {
            return self.invalidated(m);
        }
        let mut ast = self.ast.clone();
        ast.order_by
            .get_or_insert_with(Vec::new)
            .push(OrderPart { field, direction });
        self.with(ast, None)
    }

    /// Includes the rows of `relationship`, as `sub` shapes them.
    pub fn related(self, relationship: &str, sub: impl FnOnce(AnyQuery) -> AnyQuery) -> Self {
        assert!(
            !relationship.starts_with(SUBQUERY_ALIAS_PREFIX),
            "Relationship names may not start with \"{SUBQUERY_ALIAS_PREFIX}\". That is a reserved prefix."
        );
        if let Some(m) = self.not_distinct("related") {
            return self.invalidated(m);
        }
        let hops = self.relationship_hops(relationship);
        let mut ast = self.ast.clone();
        let mut format = self.format.clone();
        match hops.as_slice() {
            [hop] => {
                let q = sub(self.base(
                    &hop.dest_schema,
                    relationship.to_owned(),
                    singular(hop),
                    None,
                ));
                if let Some(m) = q
                    .invalid
                    .clone()
                    .or_else(|| q.not_grouped_subquery("related"))
                {
                    return self.invalidated(m);
                }
                // The builder and the name mapper read a `related` under an
                // aggregate as a junction's destination, so a direct one
                // can't nest one.
                if q.format.aggregate.is_some() && q.ast.related.is_some() {
                    return self.invalidated(
                        "an aggregate relationship does not support related() on its subquery",
                    );
                }
                // A relationship's aggregate lives on its correlated
                // subquery, not on the subquery's AST, which would reduce
                // itself to a scalar too.
                let mut subquery = q.ast;
                subquery.aggregate = None;
                insert_related(
                    &mut ast.related,
                    CorrelatedSubquery {
                        correlation: correlation(hop),
                        subquery: Box::new(subquery),
                        system: Some(self.system),
                        hidden: None,
                        aggregate: q.format.aggregate.clone(),
                    },
                );
                format.set_relationship(relationship, q.format);
            }
            [first, second] => {
                let q = sub(self.base(
                    &second.dest_schema,
                    relationship.to_owned(),
                    singular(second),
                    Some(relationship),
                ));
                if let Some(m) = q
                    .invalid
                    .clone()
                    .or_else(|| q.not_grouped_subquery("related"))
                {
                    return self.invalidated(m);
                }
                if let Some(aggregate) = q.format.aggregate.clone() {
                    // An aggregate over a junction. A `where` on the
                    // destination is fine; nesting and bounds are not.
                    if q.ast.related.is_some() || q.ast.limit.is_some() || q.ast.start.is_some() {
                        return self.invalidated(
                            "an aggregate over a junction relationship does not yet support related/limit/start on the destination",
                        );
                    }
                    let mut dest = q.ast;
                    dest.aggregate = None;
                    let dest_csq = CorrelatedSubquery {
                        correlation: correlation(second),
                        subquery: Box::new(dest),
                        system: Some(self.system),
                        hidden: None,
                        aggregate: None,
                    };
                    let mut junction = Ast::new(first.dest_schema.clone());
                    junction.alias = Some(relationship.to_owned());
                    let is_count = aggregate.fn_ == AggregateFunction::Count;
                    if is_count {
                        // A count over the junction rows whose destination
                        // exists (where the server applies its read
                        // permissions), never reading the destination.
                        junction.where_ =
                            Some(normalize_condition(&Condition::CorrelatedSubquery {
                                related: dest_csq,
                                op: CorrelatedSubqueryOperator::Exists,
                                flip: None,
                                scalar: None,
                            }));
                    } else {
                        // sum, avg, min and max need the destination's
                        // field: the builder lifts it onto the junction row.
                        junction.related = Some(vec![dest_csq]);
                    }
                    insert_related(
                        &mut ast.related,
                        CorrelatedSubquery {
                            correlation: correlation(first),
                            subquery: Box::new(junction),
                            system: Some(self.system),
                            hidden: None,
                            aggregate: Some(if is_count {
                                Aggregate::new(AggregateFunction::Count)
                            } else {
                                aggregate
                            }),
                        },
                    );
                    format.set_relationship(relationship, q.format);
                } else {
                    let mut junction = Ast::new(first.dest_schema.clone());
                    junction.alias = Some(relationship.to_owned());
                    junction.related = Some(vec![CorrelatedSubquery {
                        correlation: correlation(second),
                        subquery: Box::new(q.ast),
                        system: Some(self.system),
                        hidden: None,
                        aggregate: None,
                    }]);
                    insert_related(
                        &mut ast.related,
                        CorrelatedSubquery {
                            correlation: correlation(first),
                            subquery: Box::new(junction),
                            system: Some(self.system),
                            hidden: Some(true),
                            aggregate: None,
                        },
                    );
                    format.set_relationship(relationship, q.format);
                }
            }
            _ => panic!("Invalid relationship {relationship}"),
        }
        self.with(ast, Some(format))
    }

    /// `#exists`: the condition `exists(relationship)` adds, and why the
    /// subquery is invalid, if it is.
    fn exists_condition(
        &self,
        relationship: &str,
        options: ExistsOptions,
        sub: impl FnOnce(AnyQuery) -> AnyQuery,
    ) -> (Condition, Option<String>) {
        let hops = self.relationship_hops(relationship);
        let exists = |related, scalar| Condition::CorrelatedSubquery {
            related,
            op: CorrelatedSubqueryOperator::Exists,
            flip: options.flip,
            scalar,
        };
        match hops.as_slice() {
            [hop] => {
                let q = sub(self.base(
                    &hop.dest_schema,
                    format!("{SUBQUERY_ALIAS_PREFIX}{relationship}"),
                    Format::default(),
                    None,
                ));
                let invalid = q.invalid.clone().or_else(|| q.not_exists_subquery());
                let related = CorrelatedSubquery {
                    correlation: correlation(hop),
                    subquery: Box::new(q.ast),
                    system: Some(self.system),
                    hidden: None,
                    aggregate: None,
                };
                (exists(related, options.scalar), invalid)
            }
            [first, second] => {
                let q = sub(self.base(
                    &second.dest_schema,
                    format!("{SUBQUERY_ALIAS_PREFIX}zhidden_{relationship}"),
                    Format::default(),
                    Some(relationship),
                ));
                let invalid = q.invalid.clone().or_else(|| q.not_exists_subquery());
                let inner = exists(
                    CorrelatedSubquery {
                        correlation: correlation(second),
                        subquery: Box::new(q.ast),
                        system: Some(self.system),
                        hidden: None,
                        aggregate: None,
                    },
                    options.scalar,
                );
                let mut junction = Ast::new(first.dest_schema.clone());
                junction.alias = Some(format!("{SUBQUERY_ALIAS_PREFIX}{relationship}"));
                junction.where_ = Some(normalize_condition(&inner));
                // The junction hop carries `flip` but not `scalar`, as in JS.
                let related = CorrelatedSubquery {
                    correlation: correlation(first),
                    subquery: Box::new(junction),
                    system: Some(self.system),
                    hidden: None,
                    aggregate: None,
                };
                (exists(related, None), invalid)
            }
            _ => panic!("Invalid relationship {relationship}"),
        }
    }

    // Aggregates and groupings.

    /// The number of rows: a query of one value rather than rows.
    pub fn count(self) -> Self {
        self.aggregate(AggregateFunction::Count, None, None)
    }

    /// The sum of `field`'s non-null values; null when there are none.
    pub fn sum(self, field: impl Into<String>) -> Self {
        self.aggregate(AggregateFunction::Sum, Some(field.into()), None)
    }

    /// The sum of the numbers at a JSON path in the `json` column `column`.
    pub fn sum_json(self, column: impl Into<String>, path: Vec<JsonPathSegment>) -> Self {
        self.aggregate(AggregateFunction::Sum, Some(column.into()), Some(path))
    }

    /// The average of `field`'s non-null values; null when there are none.
    pub fn avg(self, field: impl Into<String>) -> Self {
        self.aggregate(AggregateFunction::Avg, Some(field.into()), None)
    }

    /// Reduces the query to the average of the numbers at `path` in the JSON
    /// `column`.
    pub fn avg_json(self, column: impl Into<String>, path: Vec<JsonPathSegment>) -> Self {
        self.aggregate(AggregateFunction::Avg, Some(column.into()), Some(path))
    }

    /// The least of `field`'s non-null values; null when there are none.
    pub fn min(self, field: impl Into<String>) -> Self {
        self.aggregate(AggregateFunction::Min, Some(field.into()), None)
    }

    /// The least number or string at a JSON path; numbers sort first.
    pub fn min_json(self, column: impl Into<String>, path: Vec<JsonPathSegment>) -> Self {
        self.aggregate(AggregateFunction::Min, Some(column.into()), Some(path))
    }

    /// The greatest of `field`'s non-null values; null when there are none.
    pub fn max(self, field: impl Into<String>) -> Self {
        self.aggregate(AggregateFunction::Max, Some(field.into()), None)
    }

    /// Reduces the query to the greatest value at `path` in the JSON `column`.
    pub fn max_json(self, column: impl Into<String>, path: Vec<JsonPathSegment>) -> Self {
        self.aggregate(AggregateFunction::Max, Some(column.into()), Some(path))
    }

    /// `#aggregate`. On a top-level query the view holds the value; in a
    /// `related` subquery the relationship does.
    fn aggregate(
        self,
        fn_: AggregateFunction,
        field: Option<String>,
        path: Option<Vec<JsonPathSegment>>,
    ) -> Self {
        let name = fn_.as_str();
        if let Some(m) = self.not_aggregated(name) {
            return self.invalidated(m);
        }
        // A count of a distinct() counts its rows. A groupBy() has its
        // aggregate already.
        let distinct = self.ast.group_by.clone();
        if distinct.is_some()
            && (fn_ != AggregateFunction::Count || self.ast.aggregate.is_some())
            && let Some(m) = self.not_distinct(name)
        {
            return self.invalidated(m);
        }
        // The server runs a named query's own definition, so the aggregate
        // would never reach it.
        if let Some(c) = &self.custom_query_id {
            let m = format!(
                "{name}() cannot be applied to the named query \"{}\": put it in the query's definition",
                c.name
            );
            return self.invalidated(m);
        }
        if let (Some(path), Some(field)) = (&path, &field)
            && let Some(m) = invalid_json_path(field, path)
        {
            return self.invalidated(m);
        }
        let aggregate = match distinct {
            Some(columns) => Aggregate {
                distinct: Some(columns),
                ..Aggregate::new(fn_)
            },
            None => Aggregate {
                field,
                path,
                ..Aggregate::new(fn_)
            },
        };
        let mut ast = self.ast.clone();
        ast.group_by = None;
        ast.aggregate = Some(aggregate.clone());
        let mut format = self.format.clone();
        format.singular = true;
        format.aggregate = Some(aggregate);
        self.with(ast, Some(format))
    }

    /// The distinct combinations of `columns`' values, as `SELECT DISTINCT`:
    /// rows of those columns. `count()` after it counts the combinations.
    pub fn distinct(self, columns: Vec<String>) -> Self {
        if let Some(m) = self.not_groupable("distinct", &columns) {
            return self.invalidated(m);
        }
        let mut ast = self.ast.clone();
        ast.group_by = Some(columns);
        self.with(ast, None)
    }

    /// Groups the rows by `columns`, as `GROUP BY`; an aggregate of the
    /// result gives one row per group, with the value under the function's
    /// name.
    pub fn group_by(self, columns: Vec<String>) -> AnyGroupedQuery {
        let invalid = self.not_groupable("groupBy", &columns);
        AnyGroupedQuery {
            query: self,
            columns,
            invalid,
        }
    }

    /// `groupAggregate`: the rows of `groupBy(columns)` reduced by `fn_`.
    fn group_aggregate(
        self,
        columns: Vec<String>,
        fn_: AggregateFunction,
        field: Option<String>,
        path: Option<Vec<JsonPathSegment>>,
    ) -> Self {
        let name = fn_.as_str();
        if columns.iter().any(|c| c == name) {
            return self.invalidated(format!(
                "groupBy(): the column \"{name}\" has the same name as the {name}() value"
            ));
        }
        if let (Some(path), Some(field)) = (&path, &field)
            && let Some(m) = invalid_json_path(field, path)
        {
            return self.invalidated(m);
        }
        let mut ast = self.ast.clone();
        ast.group_by = Some(columns);
        ast.aggregate = Some(Aggregate {
            field,
            path,
            ..Aggregate::new(fn_)
        });
        self.with(ast, None)
    }

    /// `#assertNotAggregated`: a bound goes in the same AST fields before or
    /// after an aggregate, so it must come first.
    fn not_aggregated(&self, method: &str) -> Option<String> {
        let aggregate = self.format.aggregate.as_ref()?;
        Some(format!(
            "{method}() must come before {}(): it bounds the rows the aggregate reduces",
            aggregate.fn_.as_str()
        ))
    }

    /// `#assertNotDistinct`.
    fn not_distinct(&self, method: &str) -> Option<String> {
        self.ast.group_by.as_ref()?;
        let kind = if self.ast.aggregate.is_some() {
            "groupBy()"
        } else {
            "distinct()"
        };
        Some(format!("{method}() does not support {kind} yet"))
    }

    /// `#assertGroupable`.
    fn not_groupable(&self, method: &str, columns: &[String]) -> Option<String> {
        if let Some(m) = self
            .not_aggregated(method)
            .or_else(|| self.not_distinct(method))
        {
            return Some(m);
        }
        if let Some(c) = &self.custom_query_id {
            return Some(format!(
                "{method}() cannot be applied to the named query \"{}\": put it in the query's definition",
                c.name
            ));
        }
        if columns.is_empty() {
            return Some(format!("{method}() needs at least one column"));
        }
        let table_columns = self.schema.tables.get(&self.table).map(|t| &t.columns);
        for (i, column) in columns.iter().enumerate() {
            let Some(column_schema) = table_columns.and_then(|c| c.get(column)) else {
                return Some(format!(
                    "{method}(): \"{column}\" is not a column of \"{}\"",
                    self.table
                ));
            };
            if column_schema.type_ == ValueType::Json {
                return Some(format!(
                    "{method}() does not support the json column \"{column}\""
                ));
            }
            if columns.iter().position(|c| c == column) != Some(i) {
                return Some(format!("{method}(): \"{column}\" is named twice"));
            }
            if is_synthetic_column(column) {
                return Some(format!(
                    "{method}(): \"{column}\" is the name of a synthetic column, which a grouped row has too"
                ));
            }
        }
        if self.ast.limit.is_some() || self.ast.start.is_some() {
            return Some(format!(
                "{method}() does not support limit(), start() or one() yet"
            ));
        }
        if self.ast.related.is_some() {
            return Some(format!("{method}() does not support related() yet"));
        }
        for part in self.ast.order_by.iter().flatten() {
            if let Some(m) = not_group_column(&part.field, columns) {
                return Some(m);
            }
        }
        None
    }

    /// `assertSubqueryNotGrouped`.
    fn not_grouped_subquery(&self, method: &str) -> Option<String> {
        let distinct = self
            .ast
            .aggregate
            .as_ref()
            .is_some_and(|a| a.distinct.is_some());
        if self.ast.group_by.is_none() && !distinct {
            return None;
        }
        let kind = if self.ast.group_by.is_some() && self.ast.aggregate.is_some() {
            "groupBy()"
        } else {
            "distinct()"
        };
        Some(format!("{method}() does not support a {kind} subquery yet"))
    }

    fn not_exists_subquery(&self) -> Option<String> {
        if self.ast.aggregate.is_some() {
            return Some("exists() does not support an aggregate subquery".to_owned());
        }
        self.not_grouped_subquery("exists")
    }

    fn relationship_hops(&self, relationship: &str) -> Relationship {
        let Some(hops) = self.schema.relationship(&self.table, relationship) else {
            panic!("Invalid relationship {}.{relationship}", self.table);
        };
        for hop in hops {
            assert!(
                !hop.source_field.is_empty() && hop.source_field.len() == hop.dest_field.len(),
                "Invalid relationship {}.{relationship}",
                self.table
            );
        }
        hops.clone()
    }
}

/// A query grouped by columns, waiting for the aggregate that reduces each
/// group (`GroupedQuery`).
#[derive(Clone, Debug)]
pub struct AnyGroupedQuery {
    query: AnyQuery,
    columns: Vec<String>,
    invalid: Option<String>,
}

impl AnyGroupedQuery {
    fn reduce(
        self,
        fn_: AggregateFunction,
        field: Option<String>,
        path: Option<Vec<JsonPathSegment>>,
    ) -> AnyQuery {
        match self.invalid {
            Some(m) => self.query.invalidated(m),
            None => self.query.group_aggregate(self.columns, fn_, field, path),
        }
    }

    /// One row per group: its columns and `count`.
    pub fn count(self) -> AnyQuery {
        self.reduce(AggregateFunction::Count, None, None)
    }

    /// Each group's sum of `field`.
    pub fn sum(self, field: impl Into<String>) -> AnyQuery {
        self.reduce(AggregateFunction::Sum, Some(field.into()), None)
    }

    /// Each group's sum of the numbers at `path` in the JSON `column`.
    pub fn sum_json(self, column: impl Into<String>, path: Vec<JsonPathSegment>) -> AnyQuery {
        self.reduce(AggregateFunction::Sum, Some(column.into()), Some(path))
    }

    /// Each group's average of `field`.
    pub fn avg(self, field: impl Into<String>) -> AnyQuery {
        self.reduce(AggregateFunction::Avg, Some(field.into()), None)
    }

    /// Each group's average of the numbers at `path` in the JSON `column`.
    pub fn avg_json(self, column: impl Into<String>, path: Vec<JsonPathSegment>) -> AnyQuery {
        self.reduce(AggregateFunction::Avg, Some(column.into()), Some(path))
    }

    /// Each group's least value of `field`.
    pub fn min(self, field: impl Into<String>) -> AnyQuery {
        self.reduce(AggregateFunction::Min, Some(field.into()), None)
    }

    /// Each group's least value at `path` in the JSON `column`.
    pub fn min_json(self, column: impl Into<String>, path: Vec<JsonPathSegment>) -> AnyQuery {
        self.reduce(AggregateFunction::Min, Some(column.into()), Some(path))
    }

    /// Each group's greatest value of `field`.
    pub fn max(self, field: impl Into<String>) -> AnyQuery {
        self.reduce(AggregateFunction::Max, Some(field.into()), None)
    }

    /// Each group's greatest value at `path` in the JSON `column`.
    pub fn max_json(self, column: impl Into<String>, path: Vec<JsonPathSegment>) -> AnyQuery {
        self.reduce(AggregateFunction::Max, Some(column.into()), Some(path))
    }
}

/// Builds the conditions of a `where` (`ExpressionBuilder` in
/// `packages/zql/src/query/expression.ts`).
pub struct ExpressionBuilder<'a> {
    query: &'a AnyQuery,
    /// The first misuse JS's builder throws for, for `where`.
    invalid: RefCell<Option<String>>,
}

impl<'a> ExpressionBuilder<'a> {
    fn new(query: &'a AnyQuery) -> Self {
        ExpressionBuilder {
            query,
            invalid: RefCell::new(None),
        }
    }

    fn record(&self, message: String) {
        let mut invalid = self.invalid.borrow_mut();
        if invalid.is_none() {
            *invalid = Some(message);
        }
    }

    /// `field` compares to `value` by `op`.
    pub fn cmp(
        &self,
        field: impl Into<String>,
        op: SimpleOperator,
        value: impl Into<Value>,
    ) -> Condition {
        cmp_condition(field.into(), op, ValuePosition::Literal(value.into()))
    }

    /// `field` compares to a value of the context by `op`.
    pub fn cmp_param(
        &self,
        field: impl Into<String>,
        op: SimpleOperator,
        param: &StaticParameter,
    ) -> Condition {
        cmp_condition(field.into(), op, param.position())
    }

    /// A value inside the `json` column `column`, for [`Self::cmp_json`]:
    /// `path` is object keys and array indices, applied in order. At least
    /// one segment is needed, and an index must be at most
    /// [`MAX_JSON_PATH_INDEX`].
    pub fn json(&self, column: impl Into<String>, path: Vec<JsonPathSegment>) -> JsonPathReference {
        let column = column.into();
        if let Some(m) = invalid_json_path(&column, &path) {
            self.record(m);
        }
        JsonPathReference { column, path }
    }

    /// Compares the value at a JSON path with a value of the same JSON type;
    /// a value of another type never equals it. An `IN` list must hold
    /// values of one type.
    pub fn cmp_json(
        &self,
        reference: &JsonPathReference,
        op: SimpleOperator,
        value: impl Into<Value>,
    ) -> Condition {
        let value = value.into();
        if matches!(op, SimpleOperator::In | SimpleOperator::NotIn)
            && let Value::Array(list) = &value
            && !is_homogeneous(list)
        {
            self.record(format!(
                "{} against a JSON path requires a list of values of one type",
                op.as_str()
            ));
        }
        Condition::Simple {
            op,
            left: reference.position(),
            right: ValuePosition::Literal(value),
        }
    }

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

    /// Compares two literals, neither of them a column.
    pub fn cmp_lit(
        &self,
        left: impl Into<Value>,
        op: SimpleOperator,
        right: impl Into<Value>,
    ) -> Condition {
        Condition::Simple {
            op,
            left: ValuePosition::Literal(left.into()),
            right: ValuePosition::Literal(right.into()),
        }
    }

    /// Compares a value of the context with a literal.
    pub fn cmp_lit_param(
        &self,
        left: &StaticParameter,
        op: SimpleOperator,
        right: impl Into<Value>,
    ) -> Condition {
        Condition::Simple {
            op,
            left: left.position(),
            right: ValuePosition::Literal(right.into()),
        }
    }

    /// Rows that every condition matches.
    pub fn and(&self, conditions: Vec<Condition>) -> Condition {
        and_conditions(conditions)
    }

    /// Rows that any condition matches.
    pub fn or(&self, conditions: Vec<Condition>) -> Condition {
        or_conditions(conditions)
    }

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

    /// Whether a related row in `relationship` matches `sub`.
    pub fn exists(
        &self,
        relationship: &str,
        options: ExistsOptions,
        sub: impl FnOnce(AnyQuery) -> AnyQuery,
    ) -> Condition {
        let (condition, invalid) = self.query.exists_condition(relationship, options, sub);
        if let Some(m) = invalid {
            self.record(m);
        }
        condition
    }
}

/// A value inside a `json` column, made by [`ExpressionBuilder::json`]
/// (`ColumnRef` in `packages/zql/src/query/expression.ts`).
#[derive(Clone, Debug, PartialEq)]
pub struct JsonPathReference {
    /// The JSON column.
    pub column: String,
    /// The path in its value.
    pub path: Vec<JsonPathSegment>,
}

impl JsonPathReference {
    fn position(&self) -> ValuePosition {
        ValuePosition::Json {
            column: self.column.clone(),
            path: self.path.clone(),
        }
    }
}

/// `TRUE`: an empty AND.
pub const TRUE_CONDITION: Condition = Condition::And(Vec::new());
/// `FALSE`: an empty OR.
pub const FALSE_CONDITION: Condition = Condition::Or(Vec::new());

fn is_always_true(c: &Condition) -> bool {
    matches!(c, Condition::And(c) if c.is_empty())
}

fn is_always_false(c: &Condition) -> bool {
    matches!(c, Condition::Or(c) if c.is_empty())
}

fn cmp_condition(field: String, op: SimpleOperator, right: ValuePosition) -> Condition {
    Condition::Simple {
        op,
        left: ValuePosition::Column(field),
        right,
    }
}

/// `and(...)`: drops the always-true conditions; one left is itself.
fn and_conditions(conditions: Vec<Condition>) -> Condition {
    let mut expressions: Vec<Condition> = conditions
        .into_iter()
        .filter(|c| !is_always_true(c))
        .collect();
    if expressions.len() == 1 {
        return expressions.pop().expect("one");
    }
    if expressions.iter().any(is_always_false) {
        return FALSE_CONDITION;
    }
    Condition::And(expressions)
}

/// `or(...)`: drops the always-false conditions; one left is itself.
fn or_conditions(conditions: Vec<Condition>) -> Condition {
    let mut expressions: Vec<Condition> = conditions
        .into_iter()
        .filter(|c| !is_always_false(c))
        .collect();
    if expressions.len() == 1 {
        return expressions.pop().expect("one");
    }
    if expressions.iter().any(is_always_true) {
        return TRUE_CONDITION;
    }
    Condition::Or(expressions)
}

/// `not`: pushes the negation down to the leaves (De Morgan).
fn negate_condition(condition: Condition) -> Condition {
    match condition {
        Condition::And(c) => Condition::Or(c.into_iter().map(negate_condition).collect()),
        Condition::Or(c) => Condition::And(c.into_iter().map(negate_condition).collect()),
        Condition::CorrelatedSubquery {
            related,
            op,
            flip,
            scalar,
        } => Condition::CorrelatedSubquery {
            related,
            op: match op {
                CorrelatedSubqueryOperator::Exists => CorrelatedSubqueryOperator::NotExists,
                CorrelatedSubqueryOperator::NotExists => CorrelatedSubqueryOperator::Exists,
            },
            flip,
            scalar,
        },
        Condition::Simple { op, left, right } => Condition::Simple {
            op: op.negated(),
            left,
            right,
        },
    }
}

impl SimpleOperator {
    /// The operator that matches exactly the rows this one does not.
    pub fn negated(self) -> SimpleOperator {
        use SimpleOperator::*;
        match self {
            Eq => Ne,
            Ne => Eq,
            Lt => Ge,
            Gt => Le,
            Ge => Lt,
            Le => Gt,
            In => NotIn,
            NotIn => In,
            Like => NotLike,
            NotLike => Like,
            Ilike => NotIlike,
            NotIlike => Ilike,
            Is => IsNot,
            IsNot => Is,
        }
    }
}

/// `simplifyCondition` (`packages/zql/src/query/expression.ts`).
pub fn simplify_condition(c: Condition) -> Condition {
    let (is_and, conditions) = match c {
        Condition::And(c) => (true, c),
        Condition::Or(c) => (false, c),
        c => return c,
    };
    if conditions.len() == 1 {
        return simplify_condition(conditions.into_iter().next().expect("one"));
    }
    let mut flattened = Vec::with_capacity(conditions.len());
    for c in conditions.into_iter().map(simplify_condition) {
        match (is_and, c) {
            (true, Condition::And(inner)) | (false, Condition::Or(inner)) => {
                flattened.extend(inner)
            }
            (_, c) => flattened.push(c),
        }
    }
    if is_and {
        if flattened.iter().any(is_always_false) {
            return FALSE_CONDITION;
        }
        Condition::And(flattened)
    } else {
        if flattened.iter().any(is_always_true) {
            return TRUE_CONDITION;
        }
        Condition::Or(flattened)
    }
}

/// `insertRelated`: adds `subquery` to a normalized `related`, after the
/// entries it compares equal to.
fn insert_related(related: &mut Option<Vec<CorrelatedSubquery>>, subquery: CorrelatedSubquery) {
    let related = related.get_or_insert_with(Vec::new);
    let mut i = related.len();
    while i > 0 && cmp_related(&related[i - 1], &subquery).is_gt() {
        i -= 1;
    }
    related.insert(i, subquery);
}

fn singular(hop: &RelationshipHop) -> Format {
    Format {
        singular: hop.cardinality == Cardinality::One,
        ..Format::default()
    }
}

fn correlation(hop: &RelationshipHop) -> Correlation {
    Correlation {
        parent_field: hop.source_field.clone(),
        child_field: hop.dest_field.clone(),
    }
}

/// `#assertGroupColumn`.
fn not_group_column(column: &str, columns: &[String]) -> Option<String> {
    if columns.iter().any(|c| c == column) {
        return None;
    }
    let names: Vec<String> = columns
        .iter()
        .map(|c| json::stringify(&Value::String(c.clone())))
        .collect();
    Some(format!(
        "orderBy(\"{column}\"): the rows can only be ordered by the grouped columns ({})",
        names.join(", ")
    ))
}

/// `makeColumnRef`'s checks of a JSON path.
fn invalid_json_path(column: &str, path: &[JsonPathSegment]) -> Option<String> {
    if path.is_empty() {
        return Some(format!(
            "json('{column}') requires at least one path segment"
        ));
    }
    path.iter().find_map(|s| match s {
        JsonPathSegment::Index(i) if *i > MAX_JSON_PATH_INDEX => Some(format!(
            "Invalid JSON path segment {i} in json('{column}', ...): a numeric segment is an array index and must be a non-negative integer no larger than {MAX_JSON_PATH_INDEX}"
        )),
        _ => None,
    })
}

/// `isSyntheticColumn` (`packages/zql/src/ivm/aggregate.ts`): the columns
/// the aggregate operators add to a row.
fn is_synthetic_column(column: &str) -> bool {
    matches!(
        column,
        "_0_key" | "_0_value" | "_0_sum" | "_0_count" | "_0_rows"
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    /// `issue.labels` through the junction `issueLabel`, as in the zql test
    /// schema.
    fn schema() -> Arc<Schema> {
        let text = r#"{
          "tables": {
            "issue": {"name": "issue", "columns": {"id": {"type": "string"}}, "primaryKey": ["id"]},
            "issueLabel": {"name": "issueLabel", "columns": {"issueId": {"type": "string"}, "labelId": {"type": "string"}}, "primaryKey": ["issueId", "labelId"]},
            "label": {"name": "label", "columns": {"id": {"type": "string"}}, "primaryKey": ["id"]}
          },
          "relationships": {"issue": {"labels": [
            {"sourceField": ["id"], "destField": ["issueId"], "destSchema": "issueLabel", "cardinality": "many"},
            {"sourceField": ["labelId"], "destField": ["id"], "destSchema": "label", "cardinality": "many"}
          ]}}
        }"#;
        Arc::new(Schema::from_json(&json::parse(text).unwrap()).unwrap())
    }

    /// No vector reaches these: JS's generator never bounds or orders a
    /// junction's destination.
    #[test]
    fn junction_destinations_take_no_limit_or_order() {
        let q = AnyQuery::new(schema(), "issue", System::Client);
        let limited = q.clone().related("labels", |l| l.limit(1));
        assert_eq!(
            limited.invalid(),
            Some(
                "Limit is not supported in junction relationships yet. Junction relationship being limited: labels"
            )
        );
        let ordered = q
            .clone()
            .related("labels", |l| l.order_by("id", Direction::Asc).limit(1));
        assert_eq!(
            ordered.invalid(),
            Some(
                "Order by is not supported in junction relationships yet. Junction relationship being ordered: labels"
            )
        );
        // The step JS throws for is left out; the rest of the query stands.
        let unbound = q.clone().limit(-1).limit(2);
        assert_eq!(unbound.invalid(), Some("Limit must be non-negative"));
        assert_eq!(unbound.ast().limit, Some(2.0));
        assert_eq!(q.related("labels", |l| l).invalid(), None);
    }
}