-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrelational.smli
1731 lines (1526 loc) · 51.7 KB
/
relational.smli
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
(*
* Licensed to Julian Hyde under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Julian Hyde licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*)
Sys.set ("lineWidth", 78);
> val it = () : unit
Sys.set ("printDepth", 6);
> val it = () : unit
Sys.set ("printLength", 64);
> val it = () : unit
Sys.set ("stringDepth", ~1);
> val it = () : unit
useSilently "scott.smli";
> [opening scott.smli]
> val it = () : unit
let val emp0 = {id = 100, name = "Fred", deptno = 10} in #id emp0 end;
> val it = 100 : int
val emp0 = {id = 100, name = "Fred", deptno = 10};
> val emp0 = {deptno=10,id=100,name="Fred"} : {deptno:int, id:int, name:string}
val emp1 = {id = 101, name = "Velma", deptno = 20};
> val emp1 = {deptno=20,id=101,name="Velma"} : {deptno:int, id:int, name:string}
val emp2 = {id = 102, name = "Shaggy", deptno = 30};
> val emp2 = {deptno=30,id=102,name="Shaggy"}
> : {deptno:int, id:int, name:string}
val emp3 = {id = 103, name = "Scooby", deptno = 30};
> val emp3 = {deptno=30,id=103,name="Scooby"}
> : {deptno:int, id:int, name:string}
val emps = [emp0, emp1, emp2, emp3];
> val emps =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
val emps =
let
val emp0 = {id = 100, name = "Fred", deptno = 10}
and emp1 = {id = 101, name = "Velma", deptno = 20}
and emp2 = {id = 102, name = "Shaggy", deptno = 30}
and emp3 = {id = 103, name = "Scooby", deptno = 30}
in
[emp0, emp1, emp2, emp3]
end;
> val emps =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
val depts =
[{deptno = 10, name = "Sales"},
{deptno = 20, name = "HR"},
{deptno = 30, name = "Engineering"},
{deptno = 40, name = "Support"}];
> val depts =
> [{deptno=10,name="Sales"},{deptno=20,name="HR"},
> {deptno=30,name="Engineering"},{deptno=40,name="Support"}]
> : {deptno:int, name:string} list
from e in emps yield e;
> val it =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
from e in emps yield #id e;
> val it = [100,101,102,103] : int list
from e in emps yield (#id e) - 100;
> val it = [0,1,2,3] : int list
from e in emps yield e.id - 100;
> val it = [0,1,2,3] : int list
from e in emps yield #deptno e;
> val it = [10,20,30,30] : int list
from e in emps yield e.deptno;
> val it = [10,20,30,30] : int list
from e in emps yield {deptno = #deptno e, one = 1};
> val it =
> [{deptno=10,one=1},{deptno=20,one=1},{deptno=30,one=1},{deptno=30,one=1}]
> : {deptno:int, one:int} list
from e in emps yield {deptno = e.deptno, one = 1};
> val it =
> [{deptno=10,one=1},{deptno=20,one=1},{deptno=30,one=1},{deptno=30,one=1}]
> : {deptno:int, one:int} list
from e in emps yield {e.deptno, one = 1};
> val it =
> [{deptno=10,one=1},{deptno=20,one=1},{deptno=30,one=1},{deptno=30,one=1}]
> : {deptno:int, one:int} list
from e in emps yield {x = e.deptno} where x > 10 yield {y = x} where y < 30;
> val it = [20] : int list
from e in emps yield {x = e.deptno} where x > 10 yield {x = x} where x < 30;
> val it = [20] : int list
from e in emps yield ((#id e) + (#deptno e));
> val it = [110,121,132,133] : int list
from e in emps yield (e.id + e.deptno);
> val it = [110,121,132,133] : int list
from e2 in (from e in emps yield #deptno e) yield e2 + 1;
> val it = [11,21,31,31] : int list
from e2 in (from e in emps yield e.deptno) yield e2 + 1;
> val it = [11,21,31,31] : int list
from e in emps where #deptno e = 30 yield #name e;
> val it = ["Shaggy","Scooby"] : string list
from e in emps where false yield e.deptno;
> val it = [] : int list
(*) 'yield' uses 'case' to deconstruct a tuple
from p in [(1, 2), (3, 5)]
yield case p of (x, y) => {x, y};
> val it = [{x=1,y=2},{x=3,y=5}] : {x:int, y:int} list
(*) 'yield' uses 'case' to deconstruct a record
from p in [{x = 1, y = 2}, {x = 3, y = 5}]
yield case p of {x=x, y=y} => (x, y);
> val it = [(1,2),(3,5)] : (int * int) list
(*) 'yield' uses 'case' to deconstruct a record using shorthand syntax
from p in [{x = 1, y = 2},{x = 3, y = 5}]
yield case p of {x, y} => (x, y);
> val it = [(1,2),(3,5)] : (int * int) list
from e in emps
yield {x = e.id + e.deptno, y = e.id - e.deptno}
yield x + y;
> val it = [200,202,204,206] : int list
from e in emps
where e.deptno < 30
yield {x = e.id + e.deptno, y = e.id - e.deptno}
where x > 120
yield x + y;
> val it = [202] : int list
from e in emps
yield {e.deptno, e.name}
order deptno desc;
> val it =
> [{deptno=30,name="Shaggy"},{deptno=30,name="Scooby"},
> {deptno=20,name="Velma"},{deptno=10,name="Fred"}]
> : {deptno:int, name:string} list
from e in emps
yield {e.deptno}
order deptno desc
yield {deptno};
> val it = [{deptno=30},{deptno=30},{deptno=20},{deptno=10}] : {deptno:int} list
from e in emps
yield {e.deptno}
order deptno desc;
> val it = [30,30,20,10] : int list
from e in emps
yield {e.deptno}
order deptno desc
skip 1;
> val it = [30,20,10] : int list
from e in emps
yield {e.deptno}
order deptno desc
skip 1
take 2;
> val it = [30,20] : int list
from e in emps
yield {e.deptno}
order deptno desc
take 2;
> val it = [30,30] : int list
(*) Pass 'take' and 'skip' via function arguments
let
fun earlyEmps n =
from e in emps
yield {e.id, e.deptno}
order id
skip n - 2
take n
in
(earlyEmps 2, earlyEmps 3)
end;
> val it =
> ([{deptno=10,id=100},{deptno=20,id=101}],
> [{deptno=20,id=101},{deptno=30,id=102},{deptno=30,id=103}])
> : {deptno:int, id:int} list * {deptno:int, id:int} list
(*) 'yield' followed by 'order'
from e in emps
yield {e.deptno, x = e.deptno, e.name}
order deptno desc, name;
> val it =
> [{deptno=30,name="Scooby",x=30},{deptno=30,name="Shaggy",x=30},
> {deptno=20,name="Velma",x=20},{deptno=10,name="Fred",x=10}]
> : {deptno:int, name:string, x:int} list
(*) 'yield' followed by 'order', then 'yield', then 'order'
from e in emps
yield {e.deptno, x = e.deptno, e.name}
order deptno, name desc
yield {name = x, x = name, z = x + 1}
order z desc;
> val it =
> [{name=30,x="Shaggy",z=31},{name=30,x="Scooby",z=31},
> {name=20,x="Velma",z=21},{name=10,x="Fred",z=11}]
> : {name:int, x:string, z:int} list
(*) singleton record 'yield' followed by 'where'
from e in emps
yield {d = e.deptno}
where d > 10;
> val it = [20,30,30] : int list
(*) singleton record 'yield' followed by 'where' followed by 'yield'
from e in emps
yield {d = e.deptno}
where d > 10
yield {d = d};
> val it = [{d=20},{d=30},{d=30}] : {d:int} list
(*) singleton record 'yield' followed by singleton 'group'
from e in emps
yield {d = e.deptno}
group d
order d;
> val it = [10,20,30] : int list
(*) singleton record 'yield' followed by 'distinct'
from e in emps
yield {d = e.deptno}
distinct
order d;
> val it = [10,20,30] : int list
(*) singleton record 'yield' followed by 'group'
from e in emps
yield {d = e.deptno}
group d compute c = count
order d;
> val it = [{c=1,d=10},{c=1,d=20},{c=2,d=30}] : {c:int, d:int} list
(*) singleton record 'yield' followed by 'order'
from e in emps
yield {d = e.deptno}
order d desc;
> val it = [30,30,20,10] : int list
(*) singleton record 'yield' followed by 'order' then singleton 'yield'
from e in emps
yield {d = e.deptno}
order d desc
yield {d = d};
> val it = [{d=30},{d=30},{d=20},{d=10}] : {d:int} list
(*) singleton record 'yield' followed by 'order' then 'yield'
from e in emps
yield {d = e.deptno}
order d desc
yield {tens = d / 10, units = d mod 10};
> val it = [{tens=3,units=0},{tens=3,units=0},{tens=2,units=0},{tens=1,units=0}]
> : {tens:int, units:int} list
(*) record whose only field is a record with only one field
from x in [{a = {a = 1}}, {a = {a = 2}}]
yield {b = x.a};
> val it = [{b={a=1}},{b={a=2}}] : {b:{a:int}} list
from x in [{a = {a = 1}}, {a = {a = 2}}]
yield {b = x.a.a};
> val it = [{b=1},{b=2}] : {b:int} list
from x in [{a = {a = 1}}, {a = {a = 2}}]
yield {x.a};
> val it = [{a={a=1}},{a={a=2}}] : {a:{a:int}} list
from i in [1, 2, 3]
yield {i = {i = i}};
> val it = [{i={i=1}},{i={i=2}},{i={i=3}}] : {i:{i:int}} list
from e in emps
yield
let
val x = e.id + e.deptno
and y = e.id - e.deptno
in
x + y
end;
> val it = [200,202,204,206] : int list
(*) 'distinct' is equivalent to 'group' with all fields
(*) Query 1, using `distinct`
from e in scott.emp
yield {e.job, e.deptno}
distinct;
> val it =
> [{deptno=10,job="MANAGER"},{deptno=10,job="PRESIDENT"},
> {deptno=20,job="CLERK"},{deptno=30,job="MANAGER"},{deptno=20,job="ANALYST"},
> {deptno=30,job="SALESMAN"},{deptno=30,job="CLERK"},
> {deptno=20,job="MANAGER"},{deptno=10,job="CLERK"}]
> : {deptno:int, job:string} list
(*) Query 2, using `group` with no aggregate functions, equivalent to query 1
from e in scott.emp
yield {e.job, e.deptno}
group job, deptno;
> val it =
> [{deptno=10,job="MANAGER"},{deptno=10,job="PRESIDENT"},
> {deptno=20,job="CLERK"},{deptno=30,job="MANAGER"},{deptno=20,job="ANALYST"},
> {deptno=30,job="SALESMAN"},{deptno=30,job="CLERK"},
> {deptno=20,job="MANAGER"},{deptno=10,job="CLERK"}]
> : {deptno:int, job:string} list
(*) Function defined inside query
from e in emps
where e.deptno < 30
yield
let
fun p1 x = x + 1
in
p1 e.id
end;
> val it = [101,102] : int list
fun range i j =
if i >= j then [] else i :: (range (i + 1) j);
> val range = fn : int -> int -> int list
range 0 5;
> val it = [0,1,2,3,4] : int list
from i in range 0 5 where i mod 2 = 1 yield i;
> val it = [1,3] : int list
from i in range 0 5 where i mod 2 = 1 yield i;
> val it = [1,3] : int list
(*) missing yield
from i in range 0 5 where i mod 2 = 1;
> val it = [1,3] : int list
from e in emps where e.deptno = 30 yield e.id;
> val it = [102,103] : int list
(*) cartesian product
from e in emps, e2 in emps yield e.name ^ "-" ^ e2.name;
> val it =
> ["Fred-Fred","Fred-Velma","Fred-Shaggy","Fred-Scooby","Velma-Fred",
> "Velma-Velma","Velma-Shaggy","Velma-Scooby","Shaggy-Fred","Shaggy-Velma",
> "Shaggy-Shaggy","Shaggy-Scooby","Scooby-Fred","Scooby-Velma",
> "Scooby-Shaggy","Scooby-Scooby"] : string list
(*) cartesian product, missing yield
from d in depts, i in range 0 5;
> val it =
> [{d={deptno=10,name="Sales"},i=0},{d={deptno=10,name="Sales"},i=1},
> {d={deptno=10,name="Sales"},i=2},{d={deptno=10,name="Sales"},i=3},
> {d={deptno=10,name="Sales"},i=4},{d={deptno=20,name="HR"},i=0},
> {d={deptno=20,name="HR"},i=1},{d={deptno=20,name="HR"},i=2},
> {d={deptno=20,name="HR"},i=3},{d={deptno=20,name="HR"},i=4},
> {d={deptno=30,name="Engineering"},i=0},
> {d={deptno=30,name="Engineering"},i=1},
> {d={deptno=30,name="Engineering"},i=2},
> {d={deptno=30,name="Engineering"},i=3},
> {d={deptno=30,name="Engineering"},i=4},{d={deptno=40,name="Support"},i=0},
> {d={deptno=40,name="Support"},i=1},{d={deptno=40,name="Support"},i=2},
> {d={deptno=40,name="Support"},i=3},{d={deptno=40,name="Support"},i=4}]
> : {d:{deptno:int, name:string}, i:int} list
(*) 3-way comma join
from x in ["a", "b"],
y in ["c", "d"],
z in ["e", "f"];
> val it =
> [{x="a",y="c",z="e"},{x="a",y="c",z="f"},{x="a",y="d",z="e"},
> {x="a",y="d",z="f"},{x="b",y="c",z="e"},{x="b",y="c",z="f"},
> {x="b",y="d",z="e"},{x="b",y="d",z="f"}]
> : {x:string, y:string, z:string} list
(*) same, using 'join'
from x in ["a", "b"]
join y in ["c", "d"]
join z in ["e", "f"];
> val it =
> [{x="a",y="c",z="e"},{x="a",y="c",z="f"},{x="a",y="d",z="e"},
> {x="a",y="d",z="f"},{x="b",y="c",z="e"},{x="b",y="c",z="f"},
> {x="b",y="d",z="e"},{x="b",y="d",z="f"}]
> : {x:string, y:string, z:string} list
(*) 'join' with '='
from x in [1, 2]
join y = 3;
> val it = [{x=1,y=3},{x=2,y=3}] : {x:int, y:int} list
(*) 'join' with '=' and 'on'
from x in [1, 2]
join y = 3 on y = x + 1;
> val it = [{x=2,y=3}] : {x:int, y:int} list
(*) 'join' with '=' and 'on false'
from x in [1, 2]
join y = 3 on false;
> val it = [] : {x:int, y:int} list
(*) join
from e in emps, d in depts
where e.deptno = d.deptno
yield {id = e.id, deptno = e.deptno, ename = e.name, dname = d.name};
> val it =
> [{deptno=10,dname="Sales",ename="Fred",id=100},
> {deptno=20,dname="HR",ename="Velma",id=101},
> {deptno=30,dname="Engineering",ename="Shaggy",id=102},
> {deptno=30,dname="Engineering",ename="Scooby",id=103}]
> : {deptno:int, dname:string, ename:string, id:int} list
(*) as above, using abbreviated record syntax
from e in emps, d in depts
where e.deptno = d.deptno
yield {e.id, e.deptno, ename = e.name, dname = d.name};
> val it =
> [{deptno=10,dname="Sales",ename="Fred",id=100},
> {deptno=20,dname="HR",ename="Velma",id=101},
> {deptno=30,dname="Engineering",ename="Shaggy",id=102},
> {deptno=30,dname="Engineering",ename="Scooby",id=103}]
> : {deptno:int, dname:string, ename:string, id:int} list
(*) join, no yield
from e in emps, d in depts;
> val it =
> [{d={deptno=10,name="Sales"},e={deptno=10,id=100,name="Fred"}},
> {d={deptno=20,name="HR"},e={deptno=10,id=100,name="Fred"}},
> {d={deptno=30,name="Engineering"},e={deptno=10,id=100,name="Fred"}},
> {d={deptno=40,name="Support"},e={deptno=10,id=100,name="Fred"}},
> {d={deptno=10,name="Sales"},e={deptno=20,id=101,name="Velma"}},
> {d={deptno=20,name="HR"},e={deptno=20,id=101,name="Velma"}},
> {d={deptno=30,name="Engineering"},e={deptno=20,id=101,name="Velma"}},
> {d={deptno=40,name="Support"},e={deptno=20,id=101,name="Velma"}},
> {d={deptno=10,name="Sales"},e={deptno=30,id=102,name="Shaggy"}},
> {d={deptno=20,name="HR"},e={deptno=30,id=102,name="Shaggy"}},
> {d={deptno=30,name="Engineering"},e={deptno=30,id=102,name="Shaggy"}},
> {d={deptno=40,name="Support"},e={deptno=30,id=102,name="Shaggy"}},
> {d={deptno=10,name="Sales"},e={deptno=30,id=103,name="Scooby"}},
> {d={deptno=20,name="HR"},e={deptno=30,id=103,name="Scooby"}},
> {d={deptno=30,name="Engineering"},e={deptno=30,id=103,name="Scooby"}},
> {d={deptno=40,name="Support"},e={deptno=30,id=103,name="Scooby"}}]
> : {d:{deptno:int, name:string}, e:{deptno:int, id:int, name:string}} list
(*) join where neither variable is referenced
from e in emps, d in depts
yield 0;
> val it = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] : int list
(*) join where right variable is not referenced
from e in emps, d in depts
yield e.id;
> val it = [100,100,100,100,101,101,101,101,102,102,102,102,103,103,103,103]
> : int list
(*) join where left variable is not referenced
from e in emps, d in depts
yield d.deptno;
> val it = [10,20,30,40,10,20,30,40,10,20,30,40,10,20,30,40] : int list
(*) join group where neither variable is referenced
from e in emps, d in depts
group compute count = sum of 1;
> val it = [16] : int list
(*) as above, without 'of'
from e in emps, d in depts
group compute count = count;
> val it = [16] : int list
(*) join group where right variable is not referenced
from e in emps, d in depts
group e.deptno compute count = sum of 1
order deptno;
> val it = [{count=4,deptno=10},{count=4,deptno=20},{count=8,deptno=30}]
> : {count:int, deptno:int} list
(*) join with intervening 'where'
(*) we can't write ', d in depts' after 'where'
from e in emps
where e.name elem ["Shaggy", "Fred"]
join d in depts
where e.deptno = d.deptno;
> val it =
> [{d={deptno=10,name="Sales"},e={deptno=10,id=100,name="Fred"}},
> {d={deptno=30,name="Engineering"},e={deptno=30,id=102,name="Shaggy"}}]
> : {d:{deptno:int, name:string}, e:{deptno:int, id:int, name:string}} list
(*) 'where' then 'join on'
from x in [0,3,6,9,12]
where x > 1
join y in [0,2,4,6,8,10,12] on x = y;
> val it = [{x=6,y=6},{x=12,y=12}] : {x:int, y:int} list
(*) join with intervening 'group'
(* TODO: resolve ambiguity
from e in emps
group e.deptno compute count
join d in depts
where deptno = d.deptno;
(*) as previous, using 'on' rather than 'where'
from e in emps
group e.deptno compute count
join d in depts on deptno = d.deptno;
*)
(*) 'join' followed by 'distinct'
from e in emps
join d in depts on e.deptno = d.deptno
distinct;
> val it =
> [{d={deptno=10,name="Sales"},e={deptno=10,id=100,name="Fred"}},
> {d={deptno=20,name="HR"},e={deptno=20,id=101,name="Velma"}},
> {d={deptno=30,name="Engineering"},e={deptno=30,id=102,name="Shaggy"}},
> {d={deptno=30,name="Engineering"},e={deptno=30,id=103,name="Scooby"}}]
> : {d:{deptno:int, name:string}, e:{deptno:int, id:int, name:string}} list
(*) exists (defining the "exists" function ourselves)
(*) and correlated sub-query
let
fun exists [] = false
| exists (hd :: tl) = true
in
from e in emps
where exists (from d in depts
where d.deptno = e.deptno
andalso d.name = "Engineering")
yield e.name
end;
> val it = ["Shaggy","Scooby"] : string list
(*) in (defining the "in_" function ourselves)
let
fun in_ e [] = false
| in_ e (h :: t) = e = h orelse (in_ e t)
in
from e in emps
where in_ e.deptno (from d in depts
where d.name = "Engineering"
yield d.deptno)
yield e.name
end;
> val it = ["Shaggy","Scooby"] : string list
(*) elem (equivalent to SQL's IN)
from e in emps
where e.deptno elem (from d in depts
where d.name = "Engineering"
yield d.deptno)
yield e.name;
> val it = ["Shaggy","Scooby"] : string list
(*) notelem (equivalent to SQL's NOT IN, also to 'not ... elem')
from e in emps
where e.deptno notelem (from d in depts
where d.name = "Engineering"
yield d.deptno)
yield e.name;
> val it = ["Fred","Velma"] : string list
(*) equivalent to previous
from e in emps
where not (e.deptno elem (from d in depts
where d.name = "Engineering"
yield d.deptno))
yield e.name;
> val it = ["Fred","Velma"] : string list
(*) equivalent to previous
from e in emps
where e.deptno elem (from d in depts
where d.name = "Engineering"
yield d.deptno) = false
yield e.name;
> val it = ["Fred","Velma"] : string list
(*) union (matches SQL's UNION ALL)
(from e in emps yield e.deptno)
union
(from d in depts yield d.deptno);
> val it = [10,20,30,30,10,20,30,40] : int list
(*) simulate SQL's UNION DISTINCT
from deptno in (
(from e in emps yield e.deptno)
union
(from d in depts yield d.deptno))
group deptno
order deptno;
> val it = [10,20,30,40] : int list
(*) except
(from d in depts yield d.deptno)
except
(from e in emps yield e.deptno);
> val it = [40] : int list
(*) simulate SQL's EXCEPT DISTINCT
fun exceptDistinct l1 l2 =
from v in l1 except l2
group v;
> val exceptDistinct = fn : 'a list -> 'a list -> 'a list
exceptDistinct (from e in emps yield e.deptno)
(from d in depts where d.deptno <> 20 yield d.deptno);
> val it = [20] : int list
(*) simulate SQL's EXCEPT ALL
fun exceptAll l1 l2 =
from e in (
from e in
(from v in l1 yield {v, c = 1})
union
(from v in l2 yield {v, c = ~1})
group e.v compute c = sum of e.c
where c > 0),
r in (
let
fun units 0 = []
| units n = () :: (units (n - 1))
in
units e.c
end)
yield e.v;
> val exceptAll = fn : 'a list -> 'a list -> 'a list
exceptAll (from e in emps yield e.deptno)
(from d in depts yield d.deptno);
> val it = [30] : int list
(*) intersect
(from e in emps yield e.deptno)
intersect
(from d in depts yield d.deptno);
> val it = [10,20,30,30] : int list
(*) simulate SQL's INTERSECT DISTINCT
fun intersectDistinct l1 l2 =
from v in l1 intersect l2
group v;
> val intersectDistinct = fn : 'a list -> 'a list -> 'a list
from d in intersectDistinct (from e in emps yield e.deptno)
(from d in depts yield d.deptno)
order d;
> val it = [10,20,30] : int list
(*) simulate SQL's INTERSECT ALL
fun intersectAll l1 l2 =
from e in (
from e in
(from v in l1 group v compute c = count)
union
(from v in l2 group v compute c = count)
group e.v compute c = min of e.c, c2 = count
where c2 = 2
yield {v, c}),
r in (
let
fun units 0 = []
| units n = () :: (units (n - 1))
in
units e.c
end)
yield e.v;
> val intersectAll = fn : 'a list -> 'a list -> 'a list
from d in intersectAll (from e in emps yield e.deptno)
(from d in depts yield d.deptno)
order d;
> val it = [10,20,30] : int list
(*) union followed by group
from x in (from e in emps yield e.deptno)
union (from d in depts yield d.deptno)
group x compute c = count
order c, x;
> val it = [{c=1,x=40},{c=2,x=10},{c=2,x=20},{c=3,x=30}] : {c:int, x:int} list
(*) except followed by group
from x in (from e in emps yield e.deptno)
except (from d in depts yield d.deptno)
group x compute c = count
order c, x;
> val it = [] : {c:int, x:int} list
(*) intersect followed by group
from x in (from e in emps yield e.deptno)
intersect (from d in depts yield d.deptno)
group x compute c = count
order c, x;
> val it = [{c=1,x=10},{c=1,x=20},{c=2,x=30}] : {c:int, x:int} list
(*) foldl function (built into SML)
let
fun foldl f start [] = start
| foldl f start (hd :: tl) = foldl f (f (start, hd)) tl
in
foldl (fn (x, y) => x + y) 0 [2,3,4]
end;
> val it = 9 : int
(*) "group by" via higher-order functions
(*
let
fun foldl f start [] = start
| foldl f start (hd :: tl) = foldl f (f (start, hd)) tl;
fun map f [] = []
| map f (hd :: tl) = (f hd) :: (map f tl);
fun computeAgg (extractor, folder) list =
foldl folder (map extractor list);
fun aggregate aggFns list =
map (computeAgg list) aggFns;
fun sum (x, y) = x + y;
in
aggregate [(fn {id=id1,name=name1,deptno=deptno1} => id1, sum)] emps
end;
*)
(*) Basic 'group'
from e in emps
group deptno = e.deptno
compute sum = sum of e.id,
count = count
order deptno;
> val it =
> [{count=1,deptno=10,sum=100},{count=1,deptno=20,sum=101},
> {count=2,deptno=30,sum=205}] : {count:int, deptno:int, sum:int} list
(*) As previous, without the implied "deptno =" in "group",
(*) and "sum =" and "count =" in "compute".
from e in emps
group e.deptno
compute sum of e.id,
count
order deptno;
> val it =
> [{count=1,deptno=10,sum=100},{count=1,deptno=20,sum=101},
> {count=2,deptno=30,sum=205}] : {count:int, deptno:int, sum:int} list
(*) 'group' with no aggregates
from e in emps
group deptno = e.deptno
order deptno;
> val it = [10,20,30] : int list
from e in emps
group e.deptno
order deptno;
> val it = [10,20,30] : int list
(*) composite 'group' with no aggregates
from e in emps
group e.deptno, idMod2 = e.id mod 2
order deptno;
> val it =
> [{deptno=10,idMod2=0},{deptno=20,idMod2=1},{deptno=30,idMod2=0},
> {deptno=30,idMod2=1}] : {deptno:int, idMod2:int} list
(*) 'group' with empty key produces one output row
from e in emps
group compute count, sid = sum of e.id;
> val it = [{count=4,sid=406}] : {count:int, sid:int} list
(*) 'group' with empty key produces one output row even if input is empty
from e in emps
where false
group compute count;
> val it = [0] : int list
(*) 'group' with empty key, empty input, no aggregate functions
from e in emps
where false
group;
> val it = [()] : unit list
(*) 'group' with 'where' and complex argument to 'sum'
from e in emps
where e.deptno < 30
group deptno = e.deptno
compute sumId = sum of e.id,
sumIdPlusDeptno = sum of e.id + e.deptno
order deptno;
> val it =
> [{deptno=10,sumId=100,sumIdPlusDeptno=110},
> {deptno=20,sumId=101,sumIdPlusDeptno=121}]
> : {deptno:int, sumId:int, sumIdPlusDeptno:int} list
(*) 'group' with 'exists' as an aggregate function
from e in emps
group e.deptno
compute sumId = sum of e.id,
existsId = exists of e.id,
existsStar = exists
order deptno;
> val it =
> [{deptno=10,existsId=true,existsStar=true,sumId=100},
> {deptno=20,existsId=true,existsStar=true,sumId=101},
> {deptno=30,existsId=true,existsStar=true,sumId=205}]
> : {deptno:int, existsId:bool, existsStar:bool, sumId:int} list
(*) 'group' with record key
(*) (useful if we want to refer to 'e' later in the pipeline)
from e in emps
group e = {e.deptno, odd = e.id mod 2 = 1} compute c = count
yield {e.deptno, c1 = c + 1}
order deptno;
> val it = [{c1=2,deptno=10},{c1=2,deptno=20},{c1=2,deptno=30},{c1=2,deptno=30}]
> : {c1:int, deptno:int} list
(*) 'group' with join
from e in emps, d in depts
where e.deptno = d.deptno
group e.deptno, ename = e.name, dname = d.name
compute sumId = sum of e.id
order ename;
> val it =
> [{deptno=10,dname="Sales",ename="Fred",sumId=100},
> {deptno=30,dname="Engineering",ename="Scooby",sumId=103},
> {deptno=30,dname="Engineering",ename="Shaggy",sumId=102},
> {deptno=20,dname="HR",ename="Velma",sumId=101}]
> : {deptno:int, dname:string, ename:string, sumId:int} list
(*) 'group' that yields record
from e in emps, d in depts
where e.deptno = d.deptno
group d;
> val it =
> [{deptno=20,name="HR"},{deptno=30,name="Engineering"},
> {deptno=10,name="Sales"}] : {deptno:int, name:string} list
(*) Yield a variable whose value is a record.
from e in emps, d in depts
where e.deptno = d.deptno
yield e;
> val it =
> [{deptno=10,id=100,name="Fred"},{deptno=20,id=101,name="Velma"},
> {deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]
> : {deptno:int, id:int, name:string} list
(*) Yield a record containing a pair of variables whose values are records.
from e in emps, d in depts
where e.deptno = d.deptno
yield {e, d};
> val it =
> [{d={deptno=10,name="Sales"},e={deptno=10,id=100,name="Fred"}},
> {d={deptno=20,name="HR"},e={deptno=20,id=101,name="Velma"}},
> {d={deptno=30,name="Engineering"},e={deptno=30,id=102,name="Shaggy"}},
> {d={deptno=30,name="Engineering"},e={deptno=30,id=103,name="Scooby"}}]
> : {d:{deptno:int, name:string}, e:{deptno:int, id:int, name:string}} list
(*) empty 'group'
from e in emps
group compute sumId = sum of e.id;
> val it = [406] : int list
(*) 'group' with aggregate function that references the key
from (k, v) in [(1, 5), (1, 6), (2, 7), (2, 10)]
group k compute x = (fn vs => k + (sum vs)) of v;
> val it = [{k=1,x=12},{k=2,x=19}] : {k:int, x:int} list
from (x, y, z) in [("a", "p", "e"), ("m", "a", "n"), ("a", "l", "e"), ("a", "w", "e")]
group x compute z = (fn ys => concat (x :: ys)) of y;
> val it = [{x="a",z="aplw"},{x="m",z="ma"}] : {x:string, z:string} list
(*) similar, but with composite key
from (x, y, z) in [("a", "p", "e"), ("m", "a", "n"), ("a", "l", "e"), ("a", "w", "e")]
group x, z compute a = (fn ys => x ^ ":" ^ z ^ ":" ^ (concat ys)) of y;
> val it = [{a="m:n:a",x="m",z="n"},{a="a:e:plw",x="a",z="e"}]
> : {a:string, x:string, z:string} list
(*) similar, but aggregate does not reference key
from (x, y, z) in [("a", "p", "e"), ("m", "a", "n"), ("a", "l", "e"), ("a", "w", "e")]
group x compute z = (fn ys => concat ys) of y;
> val it = [{x="a",z="plw"},{x="m",z="a"}] : {x:string, z:string} list
(*) equivalent to previous
from (x, y, z) in [("a", "p", "e"), ("m", "a", "n"), ("a", "l", "e"), ("a", "w", "e")]
group x compute z = concat of y;
> val it = [{x="a",z="plw"},{x="m",z="a"}] : {x:string, z:string} list
(*) user-defined aggregate function
let
fun siz [] = 0
| siz (ht :: tl) = 1 + (siz tl)
in
from e in emps
group deptno = e.deptno compute size = siz of e.id
order deptno
end;
> val it = [{deptno=10,size=1},{deptno=20,size=1},{deptno=30,size=2}]
> : {deptno:int, size:int} list
(*) as previous, but 'e' rather than 'e.id'
let
fun siz [] = 0
| siz (ht :: tl) = 1 + (siz tl)
in
from e in emps
group deptno = e.deptno compute size = siz of e
order deptno
end;
> val it = [{deptno=10,size=1},{deptno=20,size=1},{deptno=30,size=2}]
> : {deptno:int, size:int} list
(*) user-defined aggregate function #3
let
fun my_sum [] = 0
| my_sum (head :: tail) = head + (my_sum tail)
in
from e in emps
group e.deptno compute my_sum of e.id
order deptno
end;
> val it =
> [{deptno=10,my_sum=100},{deptno=20,my_sum=101},{deptno=30,my_sum=205}]
> : {deptno:int, my_sum:int} list
(*) Identity aggregate function (equivalent to SQL's COLLECT)
let
fun id x = x
in
from e in emps
group e.deptno compute rows = id of e
order deptno
end;
> val it =
> [{deptno=10,rows=[{deptno=10,id=100,name="Fred"}]},
> {deptno=20,rows=[{deptno=20,id=101,name="Velma"}]},
> {deptno=30,
> rows=[{deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]}]
> : {deptno:int, rows:{deptno:int, id:int, name:string} list} list
(*) Identity aggregate function, without 'of'
let
fun id x = x
in
from e in emps
group e.deptno compute rows = id
order deptno
end;
> val it =
> [{deptno=10,rows=[{deptno=10,id=100,name="Fred"}]},
> {deptno=20,rows=[{deptno=20,id=101,name="Velma"}]},
> {deptno=30,
> rows=[{deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]}]
> : {deptno:int, rows:{deptno:int, id:int, name:string} list} list
(*) Identity aggregate function, using lambda
from e in emps
group e.deptno compute rows = (fn x => x)
order deptno;
> val it =
> [{deptno=10,rows=[{deptno=10,id=100,name="Fred"}]},
> {deptno=20,rows=[{deptno=20,id=101,name="Velma"}]},
> {deptno=30,
> rows=[{deptno=30,id=102,name="Shaggy"},{deptno=30,id=103,name="Scooby"}]}]
> : {deptno:int, rows:{deptno:int, id:int, name:string} list} list
(*) Identity aggregate function with multiple input variables
from e in emps, d in depts
where e.deptno = d.deptno
group e.deptno compute rows = (fn x => x)
order deptno;
> val it =
> [
> {deptno=10,
> rows=[{d={deptno=10,name="Sales"},e={deptno=10,id=100,name="Fred"}}]},
> {deptno=20,
> rows=[{d={deptno=20,name="HR"},e={deptno=20,id=101,name="Velma"}}]},
> {deptno=30,