-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDeeDoc.html
More file actions
2322 lines (2198 loc) · 211 KB
/
DeeDoc.html
File metadata and controls
2322 lines (2198 loc) · 211 KB
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
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
<title>Dee</title>
<meta name="author" content="Greg Gaughan" />
<meta name="copyright" content="Copyright (C) 2007 Greg Gaughan" />
<meta name="date" content="22/05/2007" />
<style type="text/css">
/*
:Author: David Goodger
:Contact: goodger@users.sourceforge.net
:Date: $Date: 2005-12-18 01:56:14 +0100 (Sun, 18 Dec 2005) $
:Revision: $Revision: 4224 $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/
/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
border: 0 }
table.borderless td, table.borderless th {
/* Override padding for "table.docutils td" with "! important".
The right padding separates the table cells. */
padding: 0 0.5em 0 0 ! important }
.first {
/* Override more specific margin styles with "! important". */
margin-top: 0 ! important }
.last, .with-subtitle {
margin-bottom: 0 ! important }
.hidden {
display: none }
a.toc-backref {
text-decoration: none ;
color: black }
blockquote.epigraph {
margin: 2em 5em ; }
dl.docutils dd {
margin-bottom: 0.5em }
/* Uncomment (and remove this text!) to get bold-faced definition list terms
dl.docutils dt {
font-weight: bold }
*/
div.abstract {
margin: 2em 5em }
div.abstract p.topic-title {
font-weight: bold ;
text-align: center }
div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
margin: 2em ;
border: medium outset ;
padding: 1em }
div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
font-weight: bold ;
font-family: sans-serif }
div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
color: red ;
font-weight: bold ;
font-family: sans-serif }
/* Uncomment (and remove this text!) to get reduced vertical space in
compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
margin-bottom: 0.5em }
div.compound .compound-last, div.compound .compound-middle {
margin-top: 0.5em }
*/
div.dedication {
margin: 2em 5em ;
text-align: center ;
font-style: italic }
div.dedication p.topic-title {
font-weight: bold ;
font-style: normal }
div.figure {
margin-left: 2em ;
margin-right: 2em }
div.footer, div.header {
clear: both;
font-size: smaller }
div.line-block {
display: block ;
margin-top: 1em ;
margin-bottom: 1em }
div.line-block div.line-block {
margin-top: 0 ;
margin-bottom: 0 ;
margin-left: 1.5em }
div.sidebar {
margin-left: 1em ;
border: medium outset ;
padding: 1em ;
background-color: #ffffee ;
width: 40% ;
float: right ;
clear: right }
div.sidebar p.rubric {
font-family: sans-serif ;
font-size: medium }
div.system-messages {
margin: 5em }
div.system-messages h1 {
color: red }
div.system-message {
border: medium outset ;
padding: 1em }
div.system-message p.system-message-title {
color: red ;
font-weight: bold }
div.topic {
margin: 2em }
h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
margin-top: 0.4em }
h1.title {
text-align: center }
h2.subtitle {
text-align: center }
hr.docutils {
width: 75% }
img.align-left {
clear: left }
img.align-right {
clear: right }
ol.simple, ul.simple {
margin-bottom: 1em }
ol.arabic {
list-style: decimal }
ol.loweralpha {
list-style: lower-alpha }
ol.upperalpha {
list-style: upper-alpha }
ol.lowerroman {
list-style: lower-roman }
ol.upperroman {
list-style: upper-roman }
p.attribution {
text-align: right ;
margin-left: 50% }
p.caption {
font-style: italic }
p.credits {
font-style: italic ;
font-size: smaller }
p.label {
white-space: nowrap }
p.rubric {
font-weight: bold ;
font-size: larger ;
color: maroon ;
text-align: center }
p.sidebar-title {
font-family: sans-serif ;
font-weight: bold ;
font-size: larger }
p.sidebar-subtitle {
font-family: sans-serif ;
font-weight: bold }
p.topic-title {
font-weight: bold }
pre.address {
margin-bottom: 0 ;
margin-top: 0 ;
font-family: serif ;
font-size: 100% }
pre.literal-block, pre.doctest-block {
margin-left: 2em ;
margin-right: 2em ;
background-color: #eeeeee }
span.classifier {
font-family: sans-serif ;
font-style: oblique }
span.classifier-delimiter {
font-family: sans-serif ;
font-weight: bold }
span.interpreted {
font-family: sans-serif }
span.option {
white-space: nowrap }
span.pre {
white-space: pre }
span.problematic {
color: red }
span.section-subtitle {
/* font-size relative to parent (h1..h6 element) */
font-size: 80% }
table.citation {
border-left: solid 1px gray;
margin-left: 1px }
table.docinfo {
margin: 2em 4em }
table.docutils {
margin-top: 0.5em ;
margin-bottom: 0.5em }
table.footnote {
border-left: solid 1px black;
margin-left: 1px }
table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
padding-left: 0.5em ;
padding-right: 0.5em ;
vertical-align: top }
table.docutils th.field-name, table.docinfo th.docinfo-name {
font-weight: bold ;
text-align: left ;
white-space: nowrap ;
padding-left: 0 }
h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
font-size: 100% }
tt.docutils {
background-color: #eeeeee }
ul.auto-toc {
list-style-type: none }
.highlight { background: #f8f8f8; margin-left: 2em ; margin-right: 2em}
.highlight .c { color: #008800; font-style: italic } /* Comment */
.highlight .err { border: 1px solid #FF0000 } /* Error */
.highlight .k { color: #AA22FF; font-weight: bold } /* Keyword */
.highlight .o { color: #666666 } /* Operator */
.highlight .cm { color: #008800; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #008800 } /* Comment.Preproc */
.highlight .c1 { color: #008800; font-style: italic } /* Comment.Single */
.highlight .cs { color: #008800; font-weight: bold } /* Comment.Special */
.highlight .gd { color: #A00000 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #FF0000 } /* Generic.Error */
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.highlight .gi { color: #00A000 } /* Generic.Inserted */
.highlight .go { color: #808080 } /* Generic.Output */
.highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.highlight .gt { color: #0040D0 } /* Generic.Traceback */
.highlight .kc { color: #AA22FF; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #AA22FF; font-weight: bold } /* Keyword.Declaration */
.highlight .kp { color: #AA22FF } /* Keyword.Pseudo */
.highlight .kr { color: #AA22FF; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #AA22FF; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #666666 } /* Literal.Number */
.highlight .s { color: #BB4444 } /* Literal.String */
.highlight .na { color: #BB4444 } /* Name.Attribute */
.highlight .nb { color: #AA22FF } /* Name.Builtin */
.highlight .nc { color: #0000FF } /* Name.Class */
.highlight .no { color: #880000 } /* Name.Constant */
.highlight .nd { color: #AA22FF } /* Name.Decorator */
.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */
.highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #00A000 } /* Name.Function */
.highlight .nl { color: #A0A000 } /* Name.Label */
.highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #B8860B } /* Name.Variable */
.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.highlight .mf { color: #666666 } /* Literal.Number.Float */
.highlight .mh { color: #666666 } /* Literal.Number.Hex */
.highlight .mi { color: #666666 } /* Literal.Number.Integer */
.highlight .mo { color: #666666 } /* Literal.Number.Oct */
.highlight .sb { color: #BB4444 } /* Literal.String.Backtick */
.highlight .sc { color: #BB4444 } /* Literal.String.Char */
.highlight .sd { color: #BB4444; font-style: italic } /* Literal.String.Doc */
.highlight .s2 { color: #BB4444 } /* Literal.String.Double */
.highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
.highlight .sh { color: #BB4444 } /* Literal.String.Heredoc */
.highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
.highlight .sx { color: #008000 } /* Literal.String.Other */
.highlight .sr { color: #BB6688 } /* Literal.String.Regex */
.highlight .s1 { color: #BB4444 } /* Literal.String.Single */
.highlight .ss { color: #B8860B } /* Literal.String.Symbol */
.highlight .bp { color: #AA22FF } /* Name.Builtin.Pseudo */
.highlight .vc { color: #B8860B } /* Name.Variable.Class */
.highlight .vg { color: #B8860B } /* Name.Variable.Global */
.highlight .vi { color: #B8860B } /* Name.Variable.Instance */
.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */
</style>
</head>
<body>
<div class="document" id="dee">
<h1 class="title"><a class="reference external" href="http://www.quicksort.co.uk">Dee</a></h1>
<h2 class="subtitle" id="makes-python-relational">makes <a class="reference external" href="http://www.python.org/">Python</a> relational</h2>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Greg Gaughan</td></tr>
<tr><th class="docinfo-name">Copyright:</th>
<td>Copyright (C) 2007 Greg Gaughan</td></tr>
<tr class="field"><th class="docinfo-name">Licence:</th><td class="field-body">GPL (see Licence.txt for details)</td>
</tr>
<tr><th class="docinfo-name">Contact:</th>
<td><a class="first last reference external" href="mailto:feedback%40quicksort.co.uk">feedback<span>@</span>quicksort<span>.</span>co<span>.</span>uk</a></td></tr>
<tr><th class="docinfo-name">Date:</th>
<td>22/05/2007</td></tr>
</tbody>
</table>
<!-- todo:
update css
style sheet! (same as web?)
add .. sidebar:: Sidebar Title
:subtitle: Optional Sidebar Subtitle
add image(s)! -->
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#introduction" id="id5">Introduction</a></li>
<li><a class="reference internal" href="#mind-the-gap" id="id6">Mind the Gap</a></li>
<li><a class="reference internal" href="#enough-of-the-shenanigans" id="id7">Enough of the Shenanigans!</a></li>
<li><a class="reference internal" href="#a-bit-of-history" id="id8">A Bit of History</a></li>
<li><a class="reference internal" href="#where-we-re-coming-from" id="id9">Where We're Coming From</a></li>
<li><a class="reference internal" href="#where-we-re-going" id="id10">Where We're Going</a></li>
<li><a class="reference internal" href="#basics" id="id11">Basics</a><ul>
<li><a class="reference internal" href="#tuples" id="id12">Tuples</a></li>
<li><a class="reference internal" href="#relations" id="id13">Relations</a><ul>
<li><a class="reference internal" href="#the-interpretation-of-a-relation" id="id14">The Interpretation of a Relation</a></li>
</ul>
</li>
<li><a class="reference internal" href="#function-based-relations" id="id15">Function-based Relations</a></li>
<li><a class="reference internal" href="#relation-valued-attributes" id="id16">Relation-Valued Attributes</a></li>
<li><a class="reference internal" href="#predefined-relations" id="id17">Predefined Relations</a><ul>
<li><a class="reference internal" href="#dum" id="id18">DUM</a></li>
<li><a class="reference internal" href="#id3" id="id19">DEE</a></li>
</ul>
</li>
<li><a class="reference internal" href="#relation-constraints" id="id20">Relation Constraints</a></li>
</ul>
</li>
<li><a class="reference internal" href="#lambda" id="id21">Lambda</a></li>
<li><a class="reference internal" href="#relations-to-tuples" id="id22">Relations to Tuples</a><ul>
<li><a class="reference internal" href="#fromtuple" id="id23">fromTuple</a></li>
<li><a class="reference internal" href="#totuple" id="id24">toTuple</a></li>
<li><a class="reference internal" href="#fromtuplelist" id="id25">fromTupleList</a></li>
<li><a class="reference internal" href="#totuplelist" id="id26">toTupleList</a></li>
</ul>
</li>
<li><a class="reference internal" href="#relational-comparisons" id="id27">Relational Comparisons</a><ul>
<li><a class="reference internal" href="#equality" id="id28">Equality (==)</a></li>
<li><a class="reference internal" href="#inequality-not" id="id29">Inequality (!=, not ... ==)</a></li>
<li><a class="reference internal" href="#proper-subset" id="id30">Proper Subset (<)</a></li>
<li><a class="reference internal" href="#subset" id="id31">Subset (<=)</a></li>
<li><a class="reference internal" href="#proper-superset" id="id32">Proper Superset (>)</a></li>
<li><a class="reference internal" href="#superset" id="id33">Superset (>=)</a></li>
<li><a class="reference internal" href="#membership-in" id="id34">Membership (in)</a></li>
</ul>
</li>
<li><a class="reference internal" href="#relational-operators" id="id35">Relational Operators</a><ul>
<li><a class="reference internal" href="#projection-project-remove" id="id36">Projection (project, remove)</a></li>
<li><a class="reference internal" href="#rename-rename" id="id37">Rename (rename)</a></li>
<li><a class="reference internal" href="#restriction-where" id="id38">Restriction (where)</a></li>
<li><a class="reference internal" href="#natural-join-times-intersection" id="id39">Natural Join, Times, Intersection (&)</a><ul>
<li><a class="reference internal" href="#natural-join-some-attributes-in-common" id="id40">Natural Join - Some attributes in common</a></li>
<li><a class="reference internal" href="#times-cartesian-join-no-attributes-in-common" id="id41">Times (Cartesian Join) - No attributes in common</a></li>
<li><a class="reference internal" href="#intersection-all-attributes-in-common" id="id42">Intersection - All attributes in common</a></li>
</ul>
</li>
<li><a class="reference internal" href="#or-union" id="id43">Or, Union (|)</a></li>
<li><a class="reference internal" href="#difference" id="id44">Difference (-)</a></li>
<li><a class="reference internal" href="#extension-extend" id="id45">Extension (extend)</a></li>
</ul>
</li>
<li><a class="reference internal" href="#aggregate-operators" id="id46">Aggregate Operators</a><ul>
<li><a class="reference internal" href="#count-len" id="id47">COUNT (len)</a></li>
<li><a class="reference internal" href="#sum" id="id48">SUM</a></li>
<li><a class="reference internal" href="#avg" id="id49">AVG</a></li>
<li><a class="reference internal" href="#min" id="id50">MIN</a></li>
<li><a class="reference internal" href="#max" id="id51">MAX</a></li>
<li><a class="reference internal" href="#all" id="id52">ALL</a></li>
<li><a class="reference internal" href="#any" id="id53">ANY</a></li>
<li><a class="reference internal" href="#is-empty" id="id54">IS_EMPTY</a></li>
</ul>
</li>
<li><a class="reference internal" href="#some-not-so-basic-but-very-useful-operators" id="id55">Some Not-so-basic but Very Useful Operators</a><ul>
<li><a class="reference internal" href="#generation-generate" id="id56">Generation (GENERATE)</a></li>
<li><a class="reference internal" href="#divide-divide-simple" id="id57">Divide (DIVIDE_SIMPLE)</a></li>
<li><a class="reference internal" href="#summarization-summarize" id="id58">Summarization (SUMMARIZE)</a></li>
<li><a class="reference internal" href="#grouping-and-ungrouping-group-ungroup" id="id59">Grouping and Ungrouping (GROUP, UNGROUP)</a></li>
<li><a class="reference internal" href="#wrapping-and-unwrapping-wrap-unwrap" id="id60">Wrapping and Unwrapping (WRAP, UNWRAP)</a></li>
<li><a class="reference internal" href="#semijoin-semijoin-matching" id="id61">Semijoin (SEMIJOIN, MATCHING)</a></li>
<li><a class="reference internal" href="#semidifference-semiminus-not-matching" id="id62">Semidifference (SEMIMINUS, NOT_MATCHING)</a></li>
<li><a class="reference internal" href="#composition-compose" id="id63">Composition (COMPOSE)</a></li>
<li><a class="reference internal" href="#transitive-closure-tclose" id="id64">Transitive Closure (TCLOSE)</a></li>
<li><a class="reference internal" href="#quota-quota" id="id65">Quota (QUOTA)</a></li>
<li><a class="reference internal" href="#insert-insert" id="id66">Insert (insert, |=)</a></li>
<li><a class="reference internal" href="#delete-delete" id="id67">Delete (delete, -=)</a></li>
<li><a class="reference internal" href="#update-update" id="id68">Update (update)</a></li>
</ul>
</li>
<li><a class="reference internal" href="#user-definable-operators" id="id69">User-definable Operators</a></li>
<li><a class="reference internal" href="#databases" id="id70">Databases</a></li>
<li><a class="reference internal" href="#clusters" id="id71">Clusters</a></li>
<li><a class="reference internal" href="#transactions" id="id72">Transactions</a></li>
<li><a class="reference internal" href="#front-ends" id="id73">Front-ends</a></li>
<li><a class="reference internal" href="#future-work" id="id74">Future Work</a><ul>
<li><a class="reference internal" href="#constraint-checking" id="id75">Constraint Checking</a></li>
<li><a class="reference internal" href="#constraint-inference" id="id76">Constraint Inference</a></li>
<li><a class="reference internal" href="#catalog-maintenance" id="id77">Catalog Maintenance</a></li>
<li><a class="reference internal" href="#storage" id="id78">Storage</a></li>
<li><a class="reference internal" href="#concurrency" id="id79">Concurrency</a></li>
<li><a class="reference internal" href="#attribute-values" id="id80">Attribute Values</a></li>
<li><a class="reference internal" href="#api" id="id81">API</a></li>
<li><a class="reference internal" href="#virtual-relation-variables" id="id82">Virtual Relation Variables</a></li>
<li><a class="reference internal" href="#security" id="id83">Security</a></li>
<li><a class="reference internal" href="#sql-veneer" id="id84">SQL Veneer</a></li>
<li><a class="reference internal" href="#optimisation" id="id85">Optimisation</a></li>
<li><a class="reference internal" href="#surface-more-abstractions" id="id86">Surface More Abstractions</a></li>
<li><a class="reference internal" href="#et-cetera" id="id87">Et Cetera</a></li>
</ul>
</li>
<li><a class="reference internal" href="#colophon" id="id88">Colophon</a></li>
</ul>
</div>
<div class="section" id="introduction">
<h1><a class="toc-backref" href="#id5">Introduction</a></h1>
<p>Inspired by Date and Darwen's <a class="reference external" href="http://www.amazon.co.uk/Databases-Types-Relational-Model-C-J/dp/0321399420/sr=1-2/qid=1169757255/ref=sr_1_2/203-3128636-8071105?ie=UTF8&s=books">Databases, Types and the Relational Model (The Third Manifesto)</a>, we're putting forward an implementation of a truly relational language using <a class="reference external" href="http://www.python.org/">Python</a>. We will address two problems:</p>
<blockquote>
<ol class="arabic simple">
<li>The impedance mismatch between programming languages and databases</li>
<li>The weakness and syntactic awkwardness of SQL</li>
</ol>
</blockquote>
</div>
<div class="section" id="mind-the-gap">
<h1><a class="toc-backref" href="#id6">Mind the Gap</a></h1>
<p>Most of today's programs handle data in one way or another and often this data is stored in some kind of relational database. To read and modify this data, a program must bridge the gap between its representation and the one used by the dialect of SQL that the database provides. This bridge typically comprises a database API that sends queries as text strings, often accompanied by some kind of table-to-object mapper that has to coerce data and relationships in both directions, usually with elaborate layers of abstraction in an effort to keep the two sides loosely coupled.</p>
<blockquote>
<p>"Yet by obscuring the true data source these solutions end up throwing away the most compelling feature of relational databases; the ability for the data to be queried."</p>
<p class="attribution">—Microsoft, DLinq .NET Language-Integrated Query for Relational Data, May 2006</p>
</blockquote>
<div class="sidebar">
<p class="first sidebar-title">"It was Codd's very great insight that a database could be thought of as a set of relations, that a relation in turn could be thought of as a set of propositions (assumed by convention to be true), and hence that all of the apparatus of formal logic could be directly applied to the problem of database access and related problems."</p>
<p class="last">-- C. J. Date, <a class="reference external" href="http://www.amazon.co.uk/Database-Relational-Model-Retrospective-Historical/dp/0201612941/sr=1-9/qid=1169757016/ref=sr_1_9/203-3128636-8071105?ie=UTF8&s=books">The Database Relational Model</a>, Addison-Wesley, April 2000</p>
</div>
<p>This approach not only adds complexity and increases the need for data transformations but, most importantly, it destroys the significant advantages provided by the relational model of data. The relational model is built upon predicate logic which brings the power of formal reasoning to data: it is the only sound foundation available.</p>
</div>
<div class="section" id="enough-of-the-shenanigans">
<h1><a class="toc-backref" href="#id7">Enough of the Shenanigans!</a></h1>
<p>A number of approaches and frameworks have been proposed to span the gap between the two systems; most never question why there are two systems in the first place.</p>
<p>Microsoft's forthcoming LINQ to SQL (formerly DLinq) is a major attempt to bring SQL closer into the program than before, but will still keep the database sub-language and all that it entails.</p>
<blockquote>
<p>"It is no wonder that applications expected to bridge this gap are difficult to build and maintain. It would certainly simplify the equation to get rid of one side or the other. Yet relational databases provide critical infrastructure for long-term storage and query processing, and modern programming languages are indispensable for agile development and rich computation."</p>
<p class="attribution">—Microsoft, DLinq .NET Language-Integrated Query for Relational Data, May 2006</p>
</blockquote>
<p>The solution to the problem is not to get rid of one side or the other, nor to have one side overlap the other, but to merge the two sides into one: supersede SQL (the COBOL of database languages) with a true relational programming language, one that is computationally complete, and then the gap disappears. Our solution uses one of the most effective, expressive and readable languages available, <a class="reference external" href="http://www.python.org/">Python</a>, and extends it with relations and a sound relational algebra.</p>
</div>
<div class="section" id="a-bit-of-history">
<h1><a class="toc-backref" href="#id8">A Bit of History</a></h1>
<p>Since its inception in 1969 by E. F. Codd, the relational model has been the foundation for nearly all databases. It replaced earlier network and hierarchical ad-hoc approaches to data storage by being as simple as it needed to be, but no simpler. It was so powerful it allowed users to ask for what they wanted to find, rather than specify how they might find it.</p>
<p>Over the decades, SQL has become the de-facto language for relational databases, but SQL misses many of the benefits of relational technology. In recent years, partly due to SQL's weaknesses and partly due to minimalistic and stagnant implementations, the database has become merely a storage engine fronted by layers of drivers, mappers, hierarchical markups and frameworks which make flexible querying both complex and distant from the application code.</p>
</div>
<div class="section" id="where-we-re-coming-from">
<h1><a class="toc-backref" href="#id9">Where We're Coming From</a></h1>
<p>Having implemented a comprehensive, standards-compliant SQL server, <a class="reference external" href="http://www.thinksql.co.uk">ThinkSQL</a>, we did some further research into the history of SQL's dominance in the marketplace and its quirky syntax. We found a far superior alternative in the form of <strong>D</strong> <a class="footnote-reference" href="#id4" id="id2">[1]</a>, a generic name for any relational language that conforms to <a class="reference external" href="http://www.amazon.co.uk/Databases-Types-Relational-Model-C-J/dp/0321399420/sr=1-2/qid=1169757255/ref=sr_1_2/203-3128636-8071105?ie=UTF8&s=books">The Third Manifesto</a>. We’ve implemented such a language, <a class="reference external" href="http://www.quicksort.co.uk">Dee</a>, as an extension to <a class="reference external" href="http://www.python.org/">Python</a>.</p>
<p>The relational algebra and most of the ideas underlying <a class="reference external" href="http://www.quicksort.co.uk">Dee</a> come from Date and Darwen's <a class="reference external" href="http://www.amazon.co.uk/Databases-Types-Relational-Model-C-J/dp/0321399420/sr=1-2/qid=1169757255/ref=sr_1_2/203-3128636-8071105?ie=UTF8&s=books">Databases, Types and the Relational Model (The Third Manifesto)</a>. An introduction into the ideas behind it can be found in <a class="reference external" href="http://www.amazon.co.uk/Database-Depth-Relational-Model-Practitioners/dp/0596100124/sr=1-2/qid=1169757016/ref=sr_1_2/203-3128636-8071105?ie=UTF8&s=books">Databases in Depth</a> and many related links and reference materials are on <a class="reference external" href="http://www.thethirdmanifesto.com">The Third Manifesto website</a>.</p>
<p>The current version of <a class="reference external" href="http://www.quicksort.co.uk">Dee</a> is an initial release to gain feedback regarding the approach. We chose <a class="reference external" href="http://www.python.org/">Python</a> because its interpreted style, dynamic typing and built-in sets and dictionaries make it ideal for interacting with data; plus any language that allows you to do the following sorts of things has got to be good:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">45</span><span class="p">,</span> <span class="mi">90</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="go">45 90</span>
<span class="gp">>>> </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">y</span><span class="p">,</span> <span class="n">x</span> <span class="c">#swapping values without the usual temporary variable!</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="go">90 45</span>
<span class="gp">>>> </span><span class="mi">70</span> <span class="o"><</span> <span class="n">x</span> <span class="o"><</span> <span class="mi">120</span>
<span class="go">True</span>
</pre></div>
<p>See <a class="reference external" href="http://www.python.org/doc/essays/ppt/acm-ws/sld011.htm">Why Use Python?</a> for more information on the advantages of the language. A guide to the Python language can be found in <a class="reference external" href="http://www.network-theory.co.uk/python/intro/">An Introduction to Python</a>. We do assume you are familiar with Python in what follows.</p>
</div>
<div class="section" id="where-we-re-going">
<h1><a class="toc-backref" href="#id10">Where We're Going</a></h1>
<p>The current release is an initial proposal, intended to encourage feedback. We have many ideas for future versions to make it more deployable. See the <a class="reference internal" href="#future-work">Future Work</a> section below for more details.</p>
</div>
<div class="section" id="basics">
<h1><a class="toc-backref" href="#id11">Basics</a></h1>
<p>To start using <a class="reference external" href="http://www.quicksort.co.uk">Dee</a> from within the <a class="reference external" href="http://www.python.org/">Python</a> interpreter or from a <a class="reference external" href="http://www.python.org/">Python</a> program, first import the module. (For demonstrating we import everything but it's recommended that you only import the features you need.)</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">from</span> <span class="nn">Dee</span> <span class="k">import</span> <span class="o">*</span>
</pre></div>
<div class="section" id="tuples">
<h2><a class="toc-backref" href="#id12">Tuples</a></h2>
<p>A Tuple is a set of attribute/value pairs. A Tuple can be represented by a <a class="reference external" href="http://www.python.org/">Python</a> dictionary, e.g.</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="p">{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">'S1'</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">:</span><span class="s">'Anne'</span><span class="p">}</span>
<span class="go">{'StudentId': 'S1', 'Name': 'Anne'}</span>
</pre></div>
<p>and the attributes and values can be extracted using the standard <a class="reference external" href="http://www.python.org/">Python</a> syntax, e.g.</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">t1</span> <span class="o">=</span> <span class="p">{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">'S1'</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">:</span><span class="s">'Anne'</span><span class="p">}</span>
<span class="gp">>>> </span><span class="n">t1</span><span class="p">[</span><span class="s">"StudentId"</span><span class="p">]</span>
<span class="go">'S1'</span>
<span class="gp">>>> </span><span class="s">"Name"</span> <span class="ow">in</span> <span class="n">t1</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">t1</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="go">['StudentId', 'Name']</span>
</pre></div>
<p>A more powerful way is to use the Tuple class which allows a slightly simpler syntax for denoting attribute values. To specify a Tuple:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">t1</span> <span class="o">=</span> <span class="n">Tuple</span><span class="p">(</span><span class="n">StudentId</span><span class="o">=</span><span class="s">'S1'</span><span class="p">,</span> <span class="n">Name</span><span class="o">=</span><span class="s">'Anne'</span><span class="p">)</span>
</pre></div>
<p>and then the attributes values can be extracted in the same way as the <a class="reference external" href="http://www.python.org/">Python</a> dictionary but also using the dot notation without the quotes, e.g.</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">t1</span><span class="p">[</span><span class="s">"Name"</span><span class="p">]</span>
<span class="go">'Anne'</span>
<span class="gp">>>> </span><span class="n">t1</span><span class="o">.</span><span class="n">Name</span>
<span class="go">'Anne'</span>
</pre></div>
<p>The Tuple class also provides a number of useful methods, such as project and remove, for manipulating relational tuples.</p>
<p>Attribute values are dynamically typed in the usual <a class="reference external" href="http://www.python.org/">Python</a> way and they must be of the same type for every tuple in a given relation. Currently, the types can be anything that can be <a class="reference external" href="http://docs.python.org/lib/module-pickle.html">pickled</a>.</p>
</div>
<div class="section" id="relations">
<h2><a class="toc-backref" href="#id13">Relations</a></h2>
<p>A Relation comprises a heading and a body. The heading is a set of attribute name/type pairs. The body is a set of tuples. Each tuple in the body comprises a value for every attribute in the heading. To specify a relation literal, pass the heading as a list of attribute names followed by the body as a list of tuple literals, e.g.:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">Relation</span><span class="p">([</span><span class="s">"StudentId"</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">],</span>
<span class="gp">... </span> <span class="p">[{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">'S1'</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">:</span><span class="s">'Anne'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">'S2'</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">:</span><span class="s">'Boris'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">'S3'</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">:</span><span class="s">'Cindy'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">'S4'</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">:</span><span class="s">'Devinder'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">'S5'</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">:</span><span class="s">'Boris'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">])</span>
<span class="go">+-----------+----------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+==========+</span>
<span class="go">| S1 | Anne |</span>
<span class="go">| S2 | Boris |</span>
<span class="go">| S3 | Cindy |</span>
<span class="go">| S4 | Devinder |</span>
<span class="go">| S5 | Boris |</span>
<span class="go">+-----------+----------+</span>
</pre></div>
<p>Note:</p>
<ul class="simple">
<li>there is no order to the heading attributes (they are a set)</li>
<li>nor is there any order to the tuples in the body (they are a set)</li>
<li>there is no duplication in the heading attribute names (they are a set)</li>
<li>nor is there any duplication in the tuples in the body (they are a set)</li>
</ul>
<p>Also, we will try to use the term <strong>relation variable</strong> when we mean a variable that refers to a Relation, and just <strong>relation</strong> (or relation value) to mean the value of the relation. This is an important distinction. The value of a relation never changes, just like the value 5 never changes.</p>
<p>To assign a relation value to a relation variable, use the standard <a class="reference external" href="http://www.python.org/">Python</a> syntax, e.g.</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">IS_CALLED</span> <span class="o">=</span> <span class="n">Relation</span><span class="p">([</span><span class="s">"StudentId"</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">],</span>
<span class="gp">... </span> <span class="p">[{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">'S1'</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">:</span><span class="s">'Anne'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">'S2'</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">:</span><span class="s">'Boris'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">'S3'</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">:</span><span class="s">'Cindy'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">'S4'</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">:</span><span class="s">'Devinder'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">'S5'</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">:</span><span class="s">'Boris'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">])</span>
</pre></div>
<p>An alternative way to define a relation is to use the Tuple class to define the body:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">IS_CALLED</span> <span class="o">=</span> <span class="n">Relation</span><span class="p">([</span><span class="s">"StudentId"</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">],</span>
<span class="gp">... </span> <span class="p">[</span><span class="n">Tuple</span><span class="p">(</span><span class="n">StudentId</span><span class="o">=</span><span class="s">'S1'</span><span class="p">,</span> <span class="n">Name</span><span class="o">=</span><span class="s">'Anne'</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">Tuple</span><span class="p">(</span><span class="n">StudentId</span><span class="o">=</span><span class="s">'S2'</span><span class="p">,</span> <span class="n">Name</span><span class="o">=</span><span class="s">'Boris'</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">Tuple</span><span class="p">(</span><span class="n">StudentId</span><span class="o">=</span><span class="s">'S3'</span><span class="p">,</span> <span class="n">Name</span><span class="o">=</span><span class="s">'Cindy'</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">Tuple</span><span class="p">(</span><span class="n">StudentId</span><span class="o">=</span><span class="s">'S4'</span><span class="p">,</span> <span class="n">Name</span><span class="o">=</span><span class="s">'Devinder'</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">Tuple</span><span class="p">(</span><span class="n">StudentId</span><span class="o">=</span><span class="s">'S5'</span><span class="p">,</span> <span class="n">Name</span><span class="o">=</span><span class="s">'Boris'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">])</span>
</pre></div>
<p>or alteratively, a more concise option is available which relies on the order of the body attributes matching the order of the heading:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">IS_CALLED</span> <span class="o">=</span> <span class="n">Relation</span><span class="p">([</span><span class="s">"StudentId"</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">],</span>
<span class="gp">... </span> <span class="p">[(</span><span class="s">'S1'</span><span class="p">,</span> <span class="s">'Anne'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S2'</span><span class="p">,</span> <span class="s">'Boris'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S3'</span><span class="p">,</span> <span class="s">'Cindy'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S4'</span><span class="p">,</span> <span class="s">'Devinder'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S5'</span><span class="p">,</span> <span class="s">'Boris'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">])</span>
</pre></div>
<p>(Note that <a class="reference external" href="http://www.python.org/">Python</a> allows an additional comma after the last item in a list, which can simplify copy/paste operations. Also a <a class="reference external" href="http://www.python.org/">Python</a> tuple with a single value must have a comma after the value to distinguish it from a value in parentheses, e.g. <tt class="docutils literal"><span class="pre">(7,)</span></tt> rather than <tt class="docutils literal"><span class="pre">(7)</span></tt>)</p>
<p>There are a number of ways to display a relation:</p>
<ol class="arabic simple">
<li>Print it as a string (i.e. using its <tt class="docutils literal"><span class="pre">__str__</span></tt> method), e.g.</li>
</ol>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED</span>
<span class="go">+-----------+----------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+==========+</span>
<span class="go">| S1 | Anne |</span>
<span class="go">| S2 | Boris |</span>
<span class="go">| S3 | Cindy |</span>
<span class="go">| S4 | Devinder |</span>
<span class="go">| S5 | Boris |</span>
<span class="go">+-----------+----------+</span>
</pre></div>
<ol class="arabic simple" start="2">
<li>Print a literal representation (one of possibly many variations) (i.e. using its <tt class="docutils literal"><span class="pre">__repr__</span></tt> method), e.g.</li>
</ol>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="sb">`IS_CALLED`</span> <span class="c">#or just: >>> IS_CALLED</span>
<span class="go">Relation(('StudentId', 'Name'),</span>
<span class="go">[Tuple(StudentId='S1', Name='Anne'), Tuple(StudentId='S2', Name='Boris'), Tuple(StudentId='S3', Name='Cindy'), Tuple(StudentId='S4', Name='Devinder'), Tuple(StudentId='S5', Name='Boris')],</span>
<span class="go">{'PK':(Key, None)})</span>
</pre></div>
<p>Note: this literal can itself be evaluated using <a class="reference external" href="http://www.python.org/">Python</a>'s <tt class="docutils literal"><span class="pre">eval()</span></tt> function to retrieve the relation's value, e.g.</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="nb">eval</span><span class="p">(</span><span class="sb">`IS_CALLED`</span><span class="p">)</span>
<span class="go">+-----------+----------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+==========+</span>
<span class="go">| S1 | Anne |</span>
<span class="go">| S2 | Boris |</span>
<span class="go">| S3 | Cindy |</span>
<span class="go">| S4 | Devinder |</span>
<span class="go">| S5 | Boris |</span>
<span class="go">+-----------+----------+</span>
<span class="gp">>>> </span><span class="n">r2</span><span class="o">=</span><span class="nb">eval</span><span class="p">(</span><span class="sb">`IS_CALLED`</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">r2</span>
<span class="go">+-----------+----------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+==========+</span>
<span class="go">| S1 | Anne |</span>
<span class="go">| S2 | Boris |</span>
<span class="go">| S3 | Cindy |</span>
<span class="go">| S4 | Devinder |</span>
<span class="go">| S5 | Boris |</span>
<span class="go">+-----------+----------+</span>
</pre></div>
<ol class="arabic simple" start="3">
<li>Print it rendered as an HTML table, e.g.</li>
</ol>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED</span><span class="o">.</span><span class="n">renderHTML</span><span class="p">()</span>
<span class="go"><table><thead><th><em>StudentId</em></th><th><em>Name</em></th></thead><tbody><tr><td>S1</td><td>Anne</td></tr><tr><td>S2</td><td>Boris</td></tr><tr><td>S3</td><td>Cindy</td></tr><tr><td>S4</td><td>Devinder</td></tr><tr><td>S5</td><td>Boris</td></tr></tbody></table></span>
</pre></div>
<p>Which in a browser becomes:</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="52%" />
<col width="48%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">StudentId</th>
<th class="head">Name</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>S1</td>
<td>Anne</td>
</tr>
<tr><td>S2</td>
<td>Boris</td>
</tr>
<tr><td>S3</td>
<td>Cindy</td>
</tr>
<tr><td>S4</td>
<td>Devinder</td>
</tr>
<tr><td>S5</td>
<td>Boris</td>
</tr>
</tbody>
</table>
</blockquote>
<p>The heading of a relation can be retrieved via its <tt class="docutils literal"><span class="pre">heading</span></tt> method, which returns the attribute names as a <a class="reference external" href="http://www.python.org/">Python</a> set, e.g.</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED</span><span class="o">.</span><span class="n">heading</span><span class="p">()</span>
<span class="go">set(['StudentId', 'Name'])</span>
</pre></div>
<div class="section" id="the-interpretation-of-a-relation">
<h3><a class="toc-backref" href="#id14">The Interpretation of a Relation</a></h3>
<p>Given a relation such as the one denoted by IS_CALLED above, we should take the meaning of it to be as follows:</p>
<ul class="simple">
<li>The heading supplies the parameters for the <strong>predicate</strong>, e.g. StudentId and Name are the parameters for the IS_CALLED predicate.</li>
<li>The tuple <tt class="docutils literal"><span class="pre">Tuple(StudentId='S3',</span> <span class="pre">Name='Cindy')</span></tt> is an <em>instantiation</em> of that predicate. It is a <strong>proposition</strong> where the argument values 'S3' and 'Cindy' are substituted for the parameters. This states that student S3 is called Cindy.</li>
<li>Each tuple in the relation is a <em>true</em> instantiation.</li>
<li>Any tuple not in the relation is a <em>false</em> instantiation.</li>
</ul>
</div>
</div>
<div class="section" id="function-based-relations">
<h2><a class="toc-backref" href="#id15">Function-based Relations</a></h2>
<p>Instead of defining the value of a relation variable once when it is assigned, we can refer to a function to provide the relation. The function can then return different values at different times. One important kind of relation variable that refers to a function for its data is a virtual (or derived) relation variable. A <strong>virtual</strong> relation variable refers to a function that returns a relational expression. All other relational variables are <strong>base</strong> relation variables. To specify a virtual relation variable we first need to define a function to provide the data by returning a relational expression. For example (ignore the relational expression syntax for now, we'll cover the details of that later):</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">vIS_CALLED_caps</span><span class="p">():</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">IS_CALLED</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s">'NameCaps'</span><span class="p">],</span> <span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="p">{</span><span class="s">'NameCaps'</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Name</span><span class="o">.</span><span class="n">upper</span><span class="p">()})</span><span class="o">.</span><span class="n">remove</span><span class="p">([</span><span class="s">'Name'</span><span class="p">])</span>
</pre></div>
<p>Then pass the heading as a list of attribute names followed by the body as a function reference, e.g.</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">IS_CALLED_caps</span> <span class="o">=</span> <span class="n">Relation</span><span class="p">([</span><span class="s">"StudentId"</span><span class="p">,</span> <span class="s">"NameCaps"</span><span class="p">],</span> <span class="n">vIS_CALLED_caps</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED_caps</span>
<span class="go">+-----------+----------+</span>
<span class="go">| StudentId | NameCaps |</span>
<span class="go">+===========+==========+</span>
<span class="go">| S1 | ANNE |</span>
<span class="go">| S2 | BORIS |</span>
<span class="go">| S3 | CINDY |</span>
<span class="go">| S4 | DEVINDER |</span>
<span class="go">| S5 | BORIS |</span>
<span class="go">+-----------+----------+</span>
</pre></div>
<p>Such virtual relation variables' values will then vary as the underlying base relation variables vary. These virtual relation variables are called views in SQL.</p>
</div>
<div class="section" id="relation-valued-attributes">
<h2><a class="toc-backref" href="#id16">Relation-Valued Attributes</a></h2>
<p>An attribute value can itself be a relation. Such attributes are known as relation-valued attributes or RVAs. There are a number of relational operators (actually macros) that use such nested relations. For example, <tt class="docutils literal"><span class="pre">GROUP</span></tt>, which takes a relation and a set of attribute names together with a new attribute name and returns a relation with the set of attributes as a nested relation, 1 per unique value of the non-grouped attributes:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">GROUP</span><span class="p">(</span><span class="n">IS_CALLED</span><span class="p">,</span> <span class="p">[</span><span class="s">'StudentId'</span><span class="p">],</span> <span class="s">'StudentIds'</span><span class="p">)</span>
<span class="go">+----------+---------------+</span>
<span class="go">| Name | StudentIds |</span>
<span class="go">+==========+===============+</span>
<span class="go">| Anne | +-----------+ |</span>
<span class="go">| | | StudentId | |</span>
<span class="go">| | +===========+ |</span>
<span class="go">| | | S1 | |</span>
<span class="go">| | +-----------+ |</span>
<span class="go">| Boris | +-----------+ |</span>
<span class="go">| | | StudentId | |</span>
<span class="go">| | +===========+ |</span>
<span class="go">| | | S2 | |</span>
<span class="go">| | | S5 | |</span>
<span class="go">| | +-----------+ |</span>
<span class="go">| Cindy | +-----------+ |</span>
<span class="go">| | | StudentId | |</span>
<span class="go">| | +===========+ |</span>
<span class="go">| | | S3 | |</span>
<span class="go">| | +-----------+ |</span>
<span class="go">| Devinder | +-----------+ |</span>
<span class="go">| | | StudentId | |</span>
<span class="go">| | +===========+ |</span>
<span class="go">| | | S4 | |</span>
<span class="go">| | +-----------+ |</span>
<span class="go">+----------+---------------+</span>
</pre></div>
</div>
<div class="section" id="predefined-relations">
<h2><a class="toc-backref" href="#id17">Predefined Relations</a></h2>
<p>There are two interesting relations that are useful for defining some fundamental relational operators in <a class="reference external" href="http://www.quicksort.co.uk">Dee</a>. We introduce them here.</p>
<div class="section" id="dum">
<h3><a class="toc-backref" href="#id18">DUM</a></h3>
<p>This is the relation that has no attributes and no tuples. It plays the role of False. It is difficult to display:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">DUM</span>
<span class="go">+</span>
<span class="go">|</span>
<span class="go">+</span>
<span class="go">+</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">DUM</span><span class="o">.</span><span class="n">renderHTML</span><span class="p">()</span>
<span class="go"><table><thead></thead><tbody></tbody></table></span>
</pre></div>
<p>It is also called TABLE_DUM and FALSE.</p>
</div>
<div class="section" id="id3">
<h3><a class="toc-backref" href="#id19">DEE</a></h3>
<p>This is the relation that has no attributes and a single tuple. It plays the role of True. It is difficult to display:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">DEE</span>
<span class="go">+</span>
<span class="go">|</span>
<span class="go">+</span>
<span class="go">|</span>
<span class="go">+</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">DEE</span><span class="o">.</span><span class="n">renderHTML</span><span class="p">()</span>
<span class="go"><table><thead></thead><tbody><tr></tr></tbody></table></span>
</pre></div>
<p>It is also called TABLE_DEE and TRUE.</p>
</div>
</div>
<div class="section" id="relation-constraints">
<h2><a class="toc-backref" href="#id20">Relation Constraints</a></h2>
<p>A Relation (function-based or not) can also take an extra parameter in its constructor to specify a set of constraints. This takes the form of a <a class="reference external" href="http://www.python.org/">Python</a> dictionary where each key gives the constraint name and each value is a pair of constraint-function, parameters. For example, to specify that the "StudentId" attribute is a candidate key for the above relation we could say:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">IS_CALLED</span> <span class="o">=</span> <span class="n">Relation</span><span class="p">([</span><span class="s">"StudentId"</span><span class="p">,</span> <span class="s">"Name"</span><span class="p">],</span>
<span class="gp">... </span> <span class="p">[(</span><span class="s">'S1'</span><span class="p">,</span> <span class="s">'Anne'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S2'</span><span class="p">,</span> <span class="s">'Boris'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S3'</span><span class="p">,</span> <span class="s">'Cindy'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S4'</span><span class="p">,</span> <span class="s">'Devinder'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S5'</span><span class="p">,</span> <span class="s">'Boris'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">],</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">'PK'</span><span class="p">:(</span><span class="n">Key</span><span class="p">,</span> <span class="p">[</span><span class="s">"StudentId"</span><span class="p">])}</span>
<span class="gp">... </span> <span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED</span>
<span class="go">+-----------+----------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+----------+</span>
<span class="go">| S1 | Anne |</span>
<span class="go">| S2 | Boris |</span>
<span class="go">| S3 | Cindy |</span>
<span class="go">| S4 | Devinder |</span>
<span class="go">| S5 | Boris |</span>
<span class="go">+-----------+----------+</span>
</pre></div>
<p>Here, <tt class="docutils literal"><span class="pre">Key</span></tt> is a pre-defined constraint type (actually a function wrapper that creates a function) that takes a list of attributes to enforce the constraint. A constraint function can return True or False and is called whenever the relation is assigned a new value. If no candidate key is specified for a relation, one is assumed comprising all the attributes in the relation (this is displayed in representations as <tt class="docutils literal"><span class="pre">{'PK':(Key,</span> <span class="pre">None)}</span></tt>). As another example:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">COURSE</span> <span class="o">=</span> <span class="n">Relation</span><span class="p">([</span><span class="s">"CourseId"</span><span class="p">,</span> <span class="s">"Title"</span><span class="p">],</span>
<span class="gp">... </span> <span class="p">[(</span><span class="s">'C1'</span><span class="p">,</span> <span class="s">'Database'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'C2'</span><span class="p">,</span> <span class="s">'HCI'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'C3'</span><span class="p">,</span> <span class="s">'Op Systems'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'C4'</span><span class="p">,</span> <span class="s">'Programming'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">],</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">'PK'</span><span class="p">:(</span><span class="n">Key</span><span class="p">,</span> <span class="p">[</span><span class="s">"CourseId"</span><span class="p">])}</span>
<span class="gp">... </span> <span class="p">)</span>
</pre></div>
<p>Another pre-defined constraint (function wrapper) is <tt class="docutils literal"><span class="pre">ForeignKey</span></tt>. It takes a relation name and a mapping of foreign key attributes to candidate key attributes as parameters, e.g.:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">IS_ENROLLED_ON</span> <span class="o">=</span> <span class="n">Relation</span><span class="p">([</span><span class="s">"StudentId"</span><span class="p">,</span> <span class="s">"CourseId"</span><span class="p">],</span>
<span class="gp">... </span> <span class="p">[(</span><span class="s">'S1'</span><span class="p">,</span> <span class="s">'C1'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S1'</span><span class="p">,</span> <span class="s">'C2'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S2'</span><span class="p">,</span> <span class="s">'C1'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S3'</span><span class="p">,</span> <span class="s">'C3'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S4'</span><span class="p">,</span> <span class="s">'C1'</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">],</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">'FKS'</span><span class="p">:(</span><span class="n">ForeignKey</span><span class="p">,</span> <span class="p">(</span><span class="s">'IS_CALLED'</span><span class="p">,</span> <span class="p">{</span><span class="s">"StudentId"</span><span class="p">:</span><span class="s">"StudentId"</span><span class="p">})),</span>
<span class="gp">... </span> <span class="s">'FKC'</span><span class="p">:(</span><span class="n">ForeignKey</span><span class="p">,</span> <span class="p">(</span><span class="s">'COURSE'</span><span class="p">,</span> <span class="p">{</span><span class="s">"CourseId"</span><span class="p">:</span><span class="s">"CourseId"</span><span class="p">}))}</span>
<span class="gp">... </span> <span class="p">)</span>
</pre></div>
<p>Here, two foreign keys are declared to ensure referential integrity between this relation and the relations referred to by IS_CALLED and COURSE.</p>
</div>
</div>
<div class="section" id="lambda">
<h1><a class="toc-backref" href="#id21">Lambda</a></h1>
<p>In a number of places we need to pass expressions, e.g. restrictions (where clauses). <a class="reference external" href="http://www.python.org/">Python</a> has a built-in way of defining such expressions with anonymous functions using the <tt class="docutils literal"><span class="pre">lambda</span></tt> keyword. So an example restriction for the above IS_CALLED relation could be:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Name</span> <span class="o">==</span> <span class="s">'Boris'</span><span class="p">)</span>
<span class="go">+-----------+-------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+=======+</span>
<span class="go">| S2 | Boris |</span>
<span class="go">| S5 | Boris |</span>
<span class="go">+-----------+-------+</span>
</pre></div>
<p>In this example, the lambda expression is passed to the relation's <tt class="docutils literal"><span class="pre">where</span></tt> function and the expression introduces a range variable, <tt class="docutils literal"><span class="pre">t</span></tt>, which will stand for each Tuple in the relation. The expression itself, the part after the colon, tests whether the Name attribute of each tuple is equal to 'Boris': if it is then the tuple is included in the result. Any <a class="reference external" href="http://www.python.org/">Python</a> expression can be passed this way. So here, complex boolean expressions including boolean operators and function calls can be built, e.g.</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">'B'</span><span class="p">)</span> <span class="ow">and</span> <span class="n">t</span><span class="o">.</span><span class="n">StudentId</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s">'5'</span><span class="p">))</span>
<span class="go">+-----------+-------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+=======+</span>
<span class="go">| S5 | Boris |</span>
<span class="go">+-----------+-------+</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="s">'A'</span> <span class="o"><</span> <span class="n">t</span><span class="o">.</span><span class="n">Name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o"><</span> <span class="s">'D'</span><span class="p">)</span>
<span class="go">+-----------+-------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+=======+</span>
<span class="go">| S2 | Boris |</span>
<span class="go">| S3 | Cindy |</span>
<span class="go">| S5 | Boris |</span>
<span class="go">+-----------+-------+</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">t</span><span class="p">[</span><span class="s">"Name"</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">'B'</span><span class="p">))</span>
<span class="go">+-----------+-------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+=======+</span>
<span class="go">| S2 | Boris |</span>
<span class="go">| S5 | Boris |</span>
<span class="go">+-----------+-------+</span>
</pre></div>
<p>Of course, simple boolean expressions can also be used, e.g.</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="bp">True</span><span class="p">)</span>
<span class="go">+-----------+----------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+==========+</span>
<span class="go">| S1 | Anne |</span>
<span class="go">| S2 | Boris |</span>
<span class="go">| S3 | Cindy |</span>
<span class="go">| S4 | Devinder |</span>
<span class="go">| S5 | Boris |</span>
<span class="go">+-----------+----------+</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="bp">False</span><span class="p">)</span>
<span class="go">+-----------+------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+======+</span>
<span class="go">+-----------+------+</span>
</pre></div>
<p>It's perhaps worth noting that the where function is really just shorthand for a natural join. Take the first example:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Name</span> <span class="o">==</span> <span class="s">'Boris'</span><span class="p">)</span>
<span class="go">+-----------+-------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+=======+</span>
<span class="go">| S2 | Boris |</span>
<span class="go">| S5 | Boris |</span>
<span class="go">+-----------+-------+</span>
</pre></div>
<p>This relational calculus based where clause can be rephrased using the relational algebra's <tt class="docutils literal"><span class="pre">AND</span></tt> operator (in this case acting as the natural join):</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED</span> <span class="o">&</span> <span class="n">Relation</span><span class="p">([</span><span class="s">"Name"</span><span class="p">],</span> <span class="p">[(</span><span class="s">'Boris'</span><span class="p">,)])</span>
<span class="go">+-----------+-------+</span>
<span class="go">| StudentId | Name |</span>
<span class="go">+===========+=======+</span>
<span class="go">| S2 | Boris |</span>
<span class="go">| S5 | Boris |</span>
<span class="go">+-----------+-------+</span>
</pre></div>
<p>Many of the relational methods provided are in fact macros implemented using only a few fundamental relational operators, such as <tt class="docutils literal"><span class="pre">AND</span></tt>.</p>
<p>Another place lambda expressions can be used is when defining virtual relation variables. For example the earlier example:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">vIS_CALLED_caps</span><span class="p">():</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">IS_CALLED</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s">'NameCaps'</span><span class="p">],</span> <span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="p">{</span><span class="s">'NameCaps'</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Name</span><span class="o">.</span><span class="n">upper</span><span class="p">()})</span><span class="o">.</span><span class="n">remove</span><span class="p">([</span><span class="s">'Name'</span><span class="p">])</span>
<span class="gp">>>> </span><span class="n">IS_CALLED_caps</span> <span class="o">=</span> <span class="n">Relation</span><span class="p">([</span><span class="s">"StudentId"</span><span class="p">,</span> <span class="s">"NameCaps"</span><span class="p">],</span> <span class="n">vIS_CALLED_caps</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED_caps</span>
<span class="go">+-----------+----------+</span>
<span class="go">| StudentId | NameCaps |</span>
<span class="go">+===========+==========+</span>
<span class="go">| S1 | ANNE |</span>
<span class="go">| S2 | BORIS |</span>
<span class="go">| S3 | CINDY |</span>
<span class="go">| S4 | DEVINDER |</span>
<span class="go">| S5 | BORIS |</span>
<span class="go">+-----------+----------+</span>
</pre></div>
<p>Could be re-coded using lambda in a more concise way as:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">IS_CALLED_caps</span> <span class="o">=</span> <span class="n">Relation</span><span class="p">([</span><span class="s">"StudentId"</span><span class="p">,</span> <span class="s">"NameCaps"</span><span class="p">],</span>
<span class="gp">... </span> <span class="k">lambda</span><span class="p">:</span> <span class="n">IS_CALLED</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s">"NameCaps"</span><span class="p">],</span> <span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="p">{</span>
<span class="gp">... </span> <span class="s">"NameCaps"</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Name</span><span class="o">.</span><span class="n">upper</span><span class="p">()})</span><span class="o">.</span><span class="n">remove</span><span class="p">([</span><span class="s">"Name"</span><span class="p">]))</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">IS_CALLED_caps</span>
<span class="go">+-----------+----------+</span>
<span class="go">| StudentId | NameCaps |</span>
<span class="go">+===========+==========+</span>
<span class="go">| S1 | ANNE |</span>
<span class="go">| S2 | BORIS |</span>
<span class="go">| S3 | CINDY |</span>
<span class="go">| S4 | DEVINDER |</span>
<span class="go">| S5 | BORIS |</span>
<span class="go">+-----------+----------+</span>
</pre></div>
<p>Lambda expressions can also be used as general constraints. On relations, another pre-defined constraint is <tt class="docutils literal"><span class="pre">Constraint</span></tt>. This takes a function that must evaluate to True for the constraint to hold, e.g.:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">EXAM_MARK</span> <span class="o">=</span> <span class="n">Relation</span><span class="p">([</span><span class="s">"StudentId"</span><span class="p">,</span> <span class="s">"CourseId"</span><span class="p">,</span> <span class="s">"Mark"</span><span class="p">],</span>
<span class="gp">... </span> <span class="p">[(</span><span class="s">'S1'</span><span class="p">,</span> <span class="s">'C1'</span><span class="p">,</span> <span class="mi">85</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S1'</span><span class="p">,</span> <span class="s">'C2'</span><span class="p">,</span> <span class="mi">49</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S2'</span><span class="p">,</span> <span class="s">'C1'</span><span class="p">,</span> <span class="mi">49</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S3'</span><span class="p">,</span> <span class="s">'C3'</span><span class="p">,</span> <span class="mi">66</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s">'S4'</span><span class="p">,</span> <span class="s">'C1'</span><span class="p">,</span> <span class="mi">93</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">],</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">'PK'</span><span class="p">:(</span><span class="n">Key</span><span class="p">,</span> <span class="p">[</span><span class="s">"StudentId"</span><span class="p">,</span> <span class="s">"CourseId"</span><span class="p">]),</span>
<span class="gp">... </span> <span class="s">'MarkRange'</span><span class="p">:</span> <span class="p">(</span><span class="n">Constraint</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">r</span><span class="p">:</span> <span class="n">ALL</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="mi">0</span> <span class="o"><=</span> <span class="n">t</span><span class="o">.</span><span class="n">Mark</span> <span class="o"><=</span> <span class="mi">100</span><span class="p">))}</span>
<span class="gp">... </span> <span class="p">)</span>
</pre></div>
<p>Here, the 'MarkRange' Constraint uses the <tt class="docutils literal"><span class="pre">ALL</span></tt> relational operator (discussed below) to ensure that all Marks in this relation are between 0 and 100. Note the Constraint works at the relation level and its range variable is <tt class="docutils literal"><span class="pre">r</span></tt> in the example. Useful operators at this level are <tt class="docutils literal"><span class="pre">ALL</span></tt>, <tt class="docutils literal"><span class="pre">ANY</span></tt>, <tt class="docutils literal"><span class="pre">IS_EMPTY</span></tt>, and the relational comparison operators discussed below, because they all take relations and return a boolean result.</p>
</div>
<div class="section" id="relations-to-tuples">
<h1><a class="toc-backref" href="#id22">Relations to Tuples</a></h1>
<p>Here are some conversion functions to map between relations and tuples:</p>
<div class="section" id="fromtuple">
<h2><a class="toc-backref" href="#id23">fromTuple</a></h2>
<p>This static method returns a relation from a tuple:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">r1</span> <span class="o">=</span> <span class="n">Relation</span><span class="o">.</span><span class="n">fromTuple</span><span class="p">({</span><span class="s">'CourseId'</span><span class="p">:</span><span class="s">'C1'</span><span class="p">,</span> <span class="s">'Title'</span><span class="p">:</span><span class="s">'Database'</span><span class="p">})</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">r1</span>
<span class="go">+----------+----------+</span>