unreql 0.2.1

Well documented and easy to use RethinkDB Rust Driver
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
use ql2::term::TermType;
use serde::Serialize;
use unreql_macros::create_cmd;

use crate::{
    cmd::{
        args::{Arg, ManyArgs, OneAndSecondOptionalArg, Opt},
        options::{FoldOptions, GroupOptions, Index},
    },
    Command,
};

create_cmd!(
    /// Takes a stream and partitions it into multiple groups based on the fields
    /// or functions provided.
    ///
    /// With the `multi` flag single documents can be assigned to multiple groups,
    /// similar to the behavior of [multi-indexes](https://rethinkdb.com/docs/secondary-indexes/javascript).
    /// When `multi` is `true` and the grouping value is an array, documents will
    /// be placed in each group that corresponds to the elements of the array.
    /// If the array is empty the row will be ignored.
    ///
    /// Suppose that the table games has the following data:
    ///
    /// ```json
    /// [
    ///     {"id": 2, "player": "Bob", "points": 15, "type": "ranked"},
    ///     {"id": 5, "player": "Alice", "points": 7, "type": "free"},
    ///     {"id": 11, "player": "Bob", "points": 10, "type": "free"},
    ///     {"id": 12, "player": "Alice", "points": 2, "type": "free"}
    /// ]
    /// ```
    ///
    /// ## Example
    /// Group games by player.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("games").group("player").run(conn)
    /// # })
    /// ```
    ///
    /// Result:
    ///
    /// ```json
    /// [
    ///     {
    ///         "group": "Alice",
    ///         "values": [
    ///             {"id": 5, "player": "Alice", "points": 7, "type": "free"},
    ///             {"id": 12, "player": "Alice", "points": 2, "type": "free"}
    ///         ]
    ///     },
    ///     {
    ///         "group": "Bob",
    ///         "values": [
    ///             {"id": 2, "player": "Bob", "points": 15, "type": "ranked"},
    ///             {"id": 11, "player": "Bob", "points": 10, "type": "free"}
    ///         ]
    ///     }
    /// ]
    /// ```
    ///
    /// Commands chained after `group` will be called on each of these grouped
    /// sub-streams, producing grouped data.
    ///
    /// ## Example
    /// What is each player’s best game?
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("games").group("player").max("points").run(conn)
    /// # })
    /// ```
    ///
    /// Result:
    ///
    /// ```json
    /// [
    ///     {
    ///         "group": "Alice",
    ///         "values": {"id": 5, "player": "Alice", "points": 7, "type": "free"}
    ///     },
    ///     {
    ///         "group": "Bob",
    ///         "values": {"id": 2, "player": "Bob", "points": 15, "type": "ranked"}
    ///     }
    /// ]
    /// ```
    ///
    /// Commands chained onto grouped data will operate on each grouped datum,
    /// producing more grouped data.
    ///
    /// ## Example
    /// What is the maximum number of points scored by each player?
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("games").group("player").max("points").g("points").run(conn)
    /// # })
    /// ```
    ///
    /// Result:
    ///
    /// ```json
    /// [
    ///     {
    ///         "group": "Alice",
    ///         "values": 7
    ///     },
    ///     {
    ///         "group": "Bob",
    ///         "values": 15
    ///     }
    /// ]
    /// ```
    ///
    /// You can also group by more than one field.
    ///
    /// ## Example
    /// What is the maximum number of points scored by each player for
    /// each game type?
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("games")
    ///   .group(r.args(("player", "type")))
    ///   .max("points")
    ///   .g("points")
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// Result:
    ///
    /// ```json
    /// [
    ///     {
    ///         "group": ["Alice", "free"],
    ///         "values": 7
    ///     },
    ///     {
    ///         "group": ["Bob", "free"],
    ///         "values": 10
    ///     },
    ///     {
    ///         "group": ["Bob", "ranked"],
    ///         "values": 15
    ///     }
    /// ]
    /// ```
    ///
    /// You can also group by a function.
    ///
    /// ## Example
    /// What is the maximum number of points scored by each player for each game type?
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("games")
    ///   .group(func!(|game| {
    ///     game.pluck(r.args(("player", "type")))
    ///   }))
    ///   .max("points")
    ///   .g("points")
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// Result:
    ///
    /// ```json
    /// [
    ///     {
    ///         "group": {"player": "Alice", "type": "free"},
    ///         "values": 7
    ///     },
    ///     {
    ///         "group": {"player": "Bob", "type": "free"},
    ///         "values": 10
    ///     },
    ///     {
    ///         "group": {"player": "Bob", "type": "ranked"},
    ///         "values": 15
    ///     }
    /// ]
    /// ```
    ///
    /// Using a function, you can also group by date on a ReQL [date field](https://rethinkdb.com/docs/dates-and-times/javascript/).
    ///
    /// ## Example
    /// How many matches have been played this year by month?
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("matches")
    ///   .group([r.row().g("date").year(), r.row().g("date").month()])
    ///   .count(())
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// Result:
    ///
    /// ```json
    /// [
    ///     {
    ///         "group": [2014, 2],
    ///         "values": 2
    ///     },
    ///     {
    ///         "group": [2014, 3],
    ///         "values": 2
    ///     },
    ///     {
    ///         "group": [2014, 4],
    ///         "values": 1
    ///     },
    ///     {
    ///         "group": [2014, 5],
    ///         "values": 3
    ///     }
    /// ]
    /// ```
    ///
    /// You can also group on an index (primary key or secondary).
    ///
    /// ## Example
    /// What is the maximum number of points scored by game type?
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("games")
    ///   .group(r.index("type"))
    ///   .max("points")
    ///   .g("points")
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// Result:
    ///
    /// ```json
    /// [
    ///     {
    ///         "group": "free",
    ///         "values": 10
    ///     },
    ///     {
    ///         "group": "ranked",
    ///         "values": 15
    ///     }
    /// ]
    /// ```
    ///
    /// See more details on the [javascript api documentation](https://rethinkdb.com/api/javascript/group#organizing-by-value-with-multi).
    ///
    /// # More Examples
    ///
    /// ## Example
    /// What is the maximum number of points scored by each player in free games?
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("games")
    ///   .filter(r.row().g("type").eq("free"))
    ///   .group("player")
    ///   .max("points")
    ///   .g("points")
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// Result:
    ///
    /// ```json
    /// [
    ///     {
    ///         "group": "Alice",
    ///         "values": 7
    ///     },
    ///     {
    ///         "group": "Bob",
    ///         "values": 10
    ///     }
    /// ]
    /// ```
    ///
    /// ## Example
    /// What is each player’s highest even and odd score?
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("games")
    ///   .group(r.args((
    ///     "name",
    ///     func!(|game| game.g("points").mod_(2))
    ///   )))
    ///   .max("points")
    ///   .g("points")
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// Result:
    ///
    /// ```json
    /// [
    ///     {
    ///         "group": ["Alice", 1],
    ///         "values": 7
    ///     },
    ///     {
    ///         "group": ["Bob", 0],
    ///         "values": 10
    ///     },
    ///     {
    ///         "group": ["Bob", 1],
    ///         "values": 15
    ///     }
    /// ]
    /// ```
    ///
    /// # Related commands
    /// - [ungroup](Self::ungroup)
    /// - [map](Self::map)
    /// - [reduce](Self::reduce)
    /// - [count](Self::count)
    /// - [sum](Self::sum)
    /// - [avg](Self::avg)
    /// - [min](Self::min)
    /// - [max](Self::max)
    group(field_or_function: ManyArgs<GroupOptions>)
);

create_cmd!(
    /// Takes a grouped stream or grouped data and turns it into an array of
    /// objects representing the groups. Any commands chained after `ungroup`
    /// will operate on this array, rather than operating on each group
    /// individually. This is useful if you want to e.g. order the groups by
    /// the value of their reduction.
    ///
    /// The format of the array returned by `ungroup` is the same as the default
    /// native format of grouped data in the javascript driver and data explorer.
    ///
    /// Suppose that the table `games` has the following data:
    ///
    /// ```json
    /// [
    ///     {"id": 2, "player": "Bob", "points": 15, "type": "ranked"},
    ///     {"id": 5, "player": "Alice", "points": 7, "type": "free"},
    ///     {"id": 11, "player": "Bob", "points": 10, "type": "free"},
    ///     {"id": 12, "player": "Alice", "points": 2, "type": "free"}
    /// ]
    /// ```
    ///
    /// ## Example
    /// What is the maximum number of points scored by each player, with the highest scorers first?
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("games")
    ///   .group("player")
    ///   .max("points")
    ///   .g("points")
    ///   .ungroup()
    ///   .order_by(r.desc("reduction"))
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// Result:
    ///
    /// ```json
    /// [
    ///     {
    ///         "group": "Bob",
    ///         "reduction": 15
    ///     },
    ///     {
    ///         "group": "Alice",
    ///         "reduction": 7
    ///     }
    /// ]
    /// ```
    ///
    /// ## Example
    /// Select one random player and all their games.
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("games").group("player").ungroup().sample(1).run(conn)
    /// # })
    /// ```
    ///
    /// Result:
    ///
    /// ```json
    /// [
    ///     {
    ///         "group": "Bob",
    ///         "reduction": 15
    ///     },
    ///     {
    ///         "group": "Alice",
    ///         "reduction": 7
    ///     }
    /// ]
    /// ```
    ///
    /// *Note* that if you didn’t call `ungroup`, you would instead select one
    /// random game from each player:
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("games").group("player").sample(1).run(conn)
    /// # })
    /// ```
    ///
    /// Result: (Note this is a JSON representation of a List<GroupedResult>; see the group documentation for more details.)
    ///
    /// ```json
    /// [
    ///     {
    ///         "group": "Alice",
    ///         "values": [
    ///             {"id": 5, "player": "Alice", "points": 7, "type": "free"}
    ///         ]
    ///     },
    ///     {
    ///         "group": "Bob",
    ///         "values": [
    ///             {"id": 11, "player": "Bob", "points": 10, "type": "free"}
    ///         ]
    ///     }
    /// ]
    /// ```
    ///
    /// ## Example
    /// Finding the arithmetic mode of an array of values:
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.expr([1,2,2,2,3,3])
    ///   .group(r.row())
    ///   .count(())
    ///   .ungroup()
    ///   .order_by("reduction")
    ///   .nth(-1)
    ///   .g("group")
    ///   .run(conn)
    /// // Result: 2
    /// # })
    /// ```
    ///
    /// ## Example
    /// Types!
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// // Returns "GROUPED_STREAM"
    /// r.table("games").group("player").type_of().run(conn)
    /// # });
    ///
    /// # unreql::example(|r, conn| {
    /// // Returns "ARRAY"
    /// r.table("games").group("player").ungroup().type_of().run(conn)
    /// # });
    ///
    /// # unreql::example(|r, conn| {
    /// // Returns "GROUPED_DATA"
    /// r.table("games").group("player").avg("points").run(conn)
    /// # });
    ///
    /// # unreql::example(|r, conn| {
    /// // Returns "ARRAY"
    /// r.table("games").group("player").avg("points").ungroup().run(conn)
    /// # });
    /// ```
    ///
    /// # Related commands
    /// - [group](Self::group)
    ungroup,
);

create_cmd!(
    /// Produce a single value from a sequence through repeated application
    /// of a reduction function.
    ///
    /// The reduction function can be called on:
    ///
    /// - two elements of the sequence
    /// - one element of the sequence and one result of a previous reduction
    /// - two results of previous reductions
    ///
    /// The reduction function can be called on the results of two previous
    /// reductions because the `reduce` command is distributed and parallelized
    /// across shards and CPU cores. A common mistaken when using the `reduce`
    /// command is to suppose that the reduction is executed from left to right.
    /// Read the [map-reduce in RethinkDB](https://rethinkdb.com/docs/map-reduce/) article to see an example.
    ///
    /// If the sequence is empty, the server will produce a `ReqlRuntimeError`
    /// that can be caught with `default`.
    /// If the sequence has only one element, the first element will be returned.
    ///
    /// ## Example
    /// Return the number of documents in the table posts.
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("posts")
    ///   .map(func!(|doc| r.expr(1)))
    ///   .reduce(func!(|left, right| left.add(right)))
    ///   .default(0)
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// A shorter way to execute this query is to use count.
    ///
    /// ## Example
    /// Suppose that each `post` has a field `comments` that is an array
    /// of comments. Return the number of comments for all posts.
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("posts")
    ///   .map(func!(|doc| doc.g("comments").count(())))
    ///   .reduce(func!(|left, right| left.add(right)))
    ///   .default(0)
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Suppose that each `post` has a field `comments` that is an array of
    /// comments. Return the maximum number comments per post.
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("posts")
    ///   .map(func!(|doc| doc.g("comments").count(())))
    ///   .reduce(func!(|left, right| left.add(right)))
    ///   .default(0)
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Suppose that each `post` has a field `comments` that is an array
    /// of comments. Return the maximum number comments per post.
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("posts")
    ///   .map(func!(|doc| doc.g("comments").count(())))
    ///   .reduce(func!(|left, right|
    ///     r.branch(
    ///       left.clone().gt(right.clone()),
    ///       left,
    ///       right
    ///     )
    ///   ))
    ///   .default(0)
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// A shorter way to execute this query is to use max.
    ///
    /// # Related commands
    /// - [group](Self::group)
    /// - [map](Self::map)
    /// - [concat_map](Self::concat_map)
    /// - [sum](Self::sum)
    /// - [avg](Self::avg)
    /// - [min](Self::min)
    /// - [max](Self::max)
    only_command,
    reduce(function: OneAndSecondOptionalArg<()>)
);

create_cmd!(
    /// Produce a single value from a sequence through repeated application
    /// of a reduction function.
    ///
    /// See [reduce](Command::reduce) in Command.
    only_root,
    reduce(sequence: Serialize, function: Serialize)
);

create_cmd!(
    /// Apply a function to a sequence in order, maintaining state via an
    /// accumulator. The `fold` command returns either a single value or a new
    /// sequence.
    ///
    /// In its first form, `fold` operates like [reduce](Command::reduce),
    /// returning a value by applying a combining function to each element
    /// in a sequence. The combining function takes two parameters: the
    /// previous reduction result (the accumulator) and the current element.
    /// However, fold has the following differences from reduce:
    ///
    /// - it is guaranteed to proceed through the sequence from first element
    ///   to last.
    /// - it passes an initial base value to the function with the first
    ///   element in place of the previous reduction result.
    ///
    /// `combiningFunction(accumulator | base, element) → newAccumulator`
    ///
    /// In its second form, `fold` operates like [concat_map](Command::concat_map),
    /// returning a new sequence rather than a single value. When an `emit`
    /// function is provided, `fold` will:
    ///
    /// - proceed through the sequence in order and take an initial base value,
    ///   as above.
    /// - for each element in the sequence, call both the combining function
    ///   and a separate emitting function. The emitting function takes three
    ///   parameters: the previous reduction result (the accumulator),
    ///   the current element, and the output from the combining function
    ///   (the new value of the accumulator).
    ///
    /// If provided, the emitting function must return a list.
    ///
    /// `emit(previousAccumulator, element, accumulator) → array`
    ///
    /// A `final_emit` function may also be provided, which will be called at
    /// the end of the sequence. It takes a single parameter: the result of the
    /// last reduction through the iteration (the accumulator), or the original
    /// base value if the input sequence was empty. This function must return
    /// a list, which will be appended to `fold`’s output stream.
    ///
    /// `final_emit(accumulator | base) → array`
    ///
    /// ## Example
    /// Concatenate words from a list.
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("words")
    ///   .order_by("id")
    ///   .fold("", func!(|acc, word| {
    ///     acc.clone().add(r.branch(acc.eq(""), "", ", ")).add(word)
    ///   }), ())
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// (This example could be implemented with `reduce`, but `fold` will
    /// preserve the order when `words` is a RethinkDB table or other stream,
    /// which is not guaranteed with `reduce`.)
    ///
    /// ## Example
    /// Return every other row in a table.
    ///
    /// ```
    /// # use unreql::{func, rjson};
    /// # use unreql::cmd::options::FoldOptions;
    /// # unreql::example(|r, conn| {
    /// r.table("even_things")
    ///   .order_by("id")
    ///   .fold(
    ///     0,
    ///     func!(|acc, row| {
    ///       acc.add(1)
    ///     }),
    ///     FoldOptions::new().emit(func!(|acc, row, new_acc| {
    ///       r.branch(new_acc.mod_(2).eq(0), [row], rjson!([]))
    ///     }))
    ///   )
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// The first function increments the accumulator each time it’s called,
    /// starting at `0`; the second function, the emitting function, alternates
    /// between returning a single-item list containing the current row or an
    /// empty list. The `fold` command will return a concatenated list of each
    /// emitted value.
    ///
    /// ## Example
    /// Compute a five-day running average for a weight tracker.
    ///
    /// ```
    /// # use unreql::{func, rjson};
    /// # use unreql::cmd::options::FoldOptions;
    /// # unreql::example(|r, conn| {
    /// r.table("tracker")
    ///   .filter(rjson!({"name": "bob"}))
    ///   .order_by("date")
    ///   .g("weight")
    ///   .fold(
    ///     rjson!([]),
    ///     func!(|acc, row| {
    ///       r.expr([row]).add(acc).limit(5)
    ///     }),
    ///     FoldOptions::new().emit(func!(|acc, row, new_acc| {
    ///       r.branch(new_acc.clone().count(()).eq(5), [new_acc.avg(())], rjson!([]))
    ///     }))
    ///   )
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// # Related commands
    /// - [reduce](Self::reduce)
    /// - [concat_map](Self::concat_map)
    fold(base: Serialize, function: Serialize, opt: Opt<FoldOptions>)
);

create_cmd!(
    /// Counts the number of elements in a sequence or key/value pairs in an
    /// object, or returns the size of a string or binary object.
    ///
    /// When `count` is called on a sequence with a predicate value or function,
    /// it returns the number of elements in the sequence equal to that value
    /// or where the function returns `true`. On a `binary` object, `count`
    /// returns the size of the object in bytes; on strings, `count` returns
    /// the string’s length. This is determined by counting the number of
    /// Unicode codepoints in the string, counting combining codepoints
    /// separately.
    ///
    /// ## Example
    /// Count the number of users.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("users").count(()).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Count the number of 18 year old users.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("users").g("age").count(18).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Count the number of users over 18.
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("users").g("age").count(func!(|age| age.gt(18))).run(conn)
    /// # })
    /// ```
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("users").count(func!(|user| user.g("age").gt(18))).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Return the length of a Unicode string.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.expr("こんにちは").count(()).run(conn)
    /// // Return: 5
    /// # })
    /// ```
    ///
    /// ## Example
    /// Return the length of an array.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.expr(["0","1","2"]).count(()).run(conn)
    /// // Return: 3
    /// # })
    /// ```
    ///
    /// # Related commands
    /// - [map](Self::map)
    /// - [reduce](Self::reduce)
    /// - [sum](Self::sum)
    /// - [avg](Self::avg)
    /// - [min](Self::min)
    /// - [max](Self::max)
    /// - [group](Self::group)
    count(value: ManyArgs<()>)
);

create_cmd!(
    /// Sums all the elements of a sequence.
    ///
    /// If called with a field name, sums all the values of that field in the
    /// sequence, skipping elements of the sequence that lack that field. If
    /// called with a function, calls that function on every element of the
    /// sequence and sums the results, skipping elements of the sequence where
    /// that function returns `null` or a non-existence error.
    ///
    /// Returns `0` when called on an empty sequence.
    ///
    /// ## Example
    /// What’s 3 + 5 + 7?
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.expr([3, 5, 7]).sum(()).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// How many points have been scored across all games?
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("games").sum("points").run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// How many points have been scored across all games, counting bonus points?
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("games").sum(func!(|game| {
    ///   game.clone().g("points").add(game.g("bonus_points"))
    /// })).run(conn)
    /// # })
    /// ```
    ///
    /// # Related commands
    /// - [map](Self::map)
    /// - [reduce](Self::reduce)
    /// - [count](Self::count)
    /// - [avg](Self::avg)
    /// - [min](Self::min)
    /// - [max](Self::max)
    /// - [group](Self::group)
    only_command,
    sum(field_or_function: Serialize)
);

create_cmd!(
    /// Sums all the elements of a sequence.
    ///
    /// See [sum](Command::sum) in Command.
    only_root,
    sum(sequence: Serialize, field: Serialize)
);

create_cmd!(
    /// Averages all the elements of a sequence.
    ///
    /// If called with a field name, averages all the values of that field in
    /// the sequence, skipping elements of the sequence that lack that field.
    /// If called with a function, calls that function on every element of the
    /// sequence and averages the results, skipping elements of the sequence
    /// where that function returns `null` or a non-existence error.
    ///
    /// Produces a non-existence error when called on an empty sequence. You
    /// can handle this case with `default`.
    ///
    /// ## Example
    /// What’s the average of 3, 5, and 7?
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.expr([3, 5, 7]).avg(()).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// What’s the average number of points scored in a game?
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("games").avg("points").run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// What’s the average number of points scored in a game, counting bonus
    /// points?
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("games").avg(func!(|game| {
    ///   game.clone().g("points").add(game.g("bonus_points"))
    /// })).run(conn)
    /// # })
    /// ```
    ///
    /// # Related commands
    /// - [map](Self::map)
    /// - [reduce](Self::reduce)
    /// - [count](Self::count)
    /// - [sum](Self::sum)
    /// - [min](Self::min)
    /// - [max](Self::max)
    /// - [group](Self::group)
    only_command,
    avg(field_or_function: Serialize)
);

create_cmd!(
    /// Averages all the elements of a sequence.
    ///
    /// See [avg](Command::avg) in Command.
    only_root,
    avg(sequence: Serialize, field: Serialize)
);

create_cmd!(
    /// Finds the minimum element of a sequence.
    ///
    /// The `min` command can be called with:
    ///
    /// - a `field name`, to return the element of the sequence with the
    ///   smallest value in that field;
    /// - an `index` (the primary key or a secondary index), to return the
    ///   element of the sequence with the smallest value in that index;
    /// - a `function`, to apply the function to every element within the
    ///   sequence and return the element which returns the smallest value
    ///   from the function, ignoring any elements where the function
    ///   produces a non-existence error.
    ///
    /// For more information on RethinkDB’s sorting order, read the section in
    /// [ReQL data types](https://rethinkdb.com/docs/data-types/#sorting-order).
    ///
    /// Calling `min` on an empty sequence will throw a non-existence error;
    /// this can be handled using the `default` command.
    ///
    /// ## Example
    /// Return the minimum value in the list `[3, 5, 7]`.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.expr([3, 5, 7]).min(()).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Return the user who has scored the fewest points.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("users").min("points").run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// The same as above, but using a secondary index on the points field.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("users").min(r.index("points")).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Return the user who has scored the fewest points, adding in bonus points
    /// from a separate field using a function.
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("users").min(func!(|user| {
    ///   user.clone().g("points").add(user.g("bonusPoints"))
    /// })).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Return the smallest number of points any user has ever scored. This
    /// returns the value of that `points` field, not a document.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("users").min("points").g("points").run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Return the user who has scored the fewest points, but add a default
    /// `null` return value to prevent an error if no user has ever scored points.
    ///
    /// ```
    /// # use unreql::rjson;
    /// # unreql::example(|r, conn| {
    /// r.table("users").min("points").default(rjson!(null)).run(conn)
    /// # })
    /// ```
    ///
    /// # Related commands
    /// - [map](Self::map)
    /// - [reduce](Self::reduce)
    /// - [count](Self::count)
    /// - [sum](Self::sum)
    /// - [avg](Self::avg)
    /// - [max](Self::max)
    /// - [group](Self::group)
    only_command,
    min(field_or_function: Arg<Index>)
);

create_cmd!(
    /// Finds the minimum element of a sequence.
    ///
    /// See [min](Command::min) in Command.
    only_root,
    min(sequence: Serialize, field: Arg<Index>)
);

create_cmd!(
    /// Finds the maximum element of a sequence.
    ///
    /// The `max` command can be called with:
    ///
    /// - a `field name`, to return the element of the sequence with the
    ///   largest value in that field;
    /// - an `index` (the primary key or a secondary index), to return the
    ///   element of the sequence with the largest value in that index;
    /// - a `function`, to apply the function to every element within the
    ///   sequence and return the element which returns the largest value
    ///   from the function, ignoring any elements where the function
    ///   produces a non-existence error.
    ///
    /// For more information on RethinkDB’s sorting order, read the section in
    /// [ReQL data types](https://rethinkdb.com/docs/data-types/#sorting-order).
    ///
    /// Calling `max` on an empty sequence will throw a non-existence error;
    /// this can be handled using the `default` command.
    ///
    /// ## Example
    /// Return the maximum value in the list `[3, 5, 7]`.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.expr([3, 5, 7]).max(()).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Return the user who has scored the most points.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("users").max("points").run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// The same as above, but using a secondary index on the points field.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("users").max(r.index("points")).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Return the user who has scored the most points, adding in bonus points
    /// from a separate field using a function.
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("users").max(func!(|user| {
    ///   user.clone().g("points").add(user.g("bonusPoints"))
    /// })).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Return the highest number of points any user has ever scored. This
    /// returns the value of that `points` field, not a document.
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("users").max("points").g("points").run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Return the user who has scored the most points, but add a default
    /// `null` return value to prevent an error if no user has ever scored points.
    ///
    /// ```
    /// # use unreql::rjson;
    /// # unreql::example(|r, conn| {
    /// r.table("users").max("points").default(rjson!(null)).run(conn)
    /// # })
    /// ```
    ///
    /// # Related commands
    /// - [map](Self::map)
    /// - [reduce](Self::reduce)
    /// - [count](Self::count)
    /// - [sum](Self::sum)
    /// - [avg](Self::avg)
    /// - [min](Self::min)
    /// - [group](Self::group)
    only_command,
    max(field_or_function: Arg<Index>)
);

create_cmd!(
    /// Finds the maximum element of a sequence.
    ///
    /// See [max](Command::max) in Command.
    only_root,
    max(sequence: Serialize, field: Arg<Index>)
);

create_cmd!(
    /// Removes duplicates from elements in a sequence.
    ///
    /// The `distinct` command can be called on any sequence or table with an index.
    ///
    /// *Note*. While `distinct` can be called on a table without an index, the only
    /// effect will be to convert the table into a stream; the content of
    /// the stream will not be affected.
    ///
    /// ## Example
    /// Which unique villains have been vanquished by Marvel heroes?
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("marvel")
    ///   .concat_map(func!(|hero| hero.g("villainList")))
    ///   .distinct(())
    ///   .run(conn)
    /// # })
    /// ```
    ///
    /// ## Example: Topics in a table of messages have a secondary index on them,
    /// and more than one message can have the same topic. What are the unique
    /// topics in the table?
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("messages").distinct(r.index("topics")).run(conn)
    /// # })
    /// ```
    ///
    /// The above structure is functionally identical to:
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("messages").g("topics").distinct(()).run(conn)
    /// # })
    /// ```
    ///
    /// However, the first form (passing the index as an argument to `distinct`)
    /// is faster, and won’t run into array limit issues since it’s returning
    /// a stream.
    ///
    /// # Related commands
    /// - [map](Self::map)
    /// - [concat_map](Self::concat_map)
    /// - [group](Self::group)
    distinct,
    ManyArgs<Index>
);

create_cmd!(
    /// When called with values, returns `true` if a sequence contains all the
    /// specified values.
    ///
    /// When called with predicate functions, returns `true` if for each predicate
    /// there exists at least one element of the stream where that predicate
    /// returns `true`.
    ///
    /// Values and predicates may be mixed freely in the argument list.
    ///
    /// ## Example
    /// Has Iron Man ever fought Superman?
    ///
    /// ```
    /// # unreql::example(|r, conn| {
    /// r.table("marvel").get("ironman").g("opponents").contains("superman").run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Has Iron Man ever defeated Superman in battle?
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("marvel").get("ironman").g("battles").contains(func!(|battle| {
    ///   battle.clone().g("winner").eq("ironman").and(battle.g("loser").eq("superman"))
    /// })).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Return all heroes who have fought both Loki and the Hulk.
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("marvel").filter(func!(|hero| {
    ///   hero.g("opponents").contains(r.args(("loki", "hulk")))
    /// })).run(conn)
    /// # })
    /// ```
    ///
    /// ## Example
    /// Use `contains` with a predicate function to simulate an `or`. Return the
    /// Marvel superheroes who live in Detroit, Chicago or Hoboken.
    ///
    /// ```
    /// # use unreql::func;
    /// # unreql::example(|r, conn| {
    /// r.table("marvel").filter(func!(|hero| {
    ///   r.expr(["Detroit", "Chicago", "Hoboken"]).contains(hero.g("city"))
    /// })).run(conn)
    /// # })
    /// ```
    contains(value: ManyArgs<()>)
);