on 94_atomfunctionatoms help sign in
project-m36/docs/tutd_tutorial.markdown
533 lines 26 kB history raw
1# TutorialD Tutorial for Project:M36
3TutorialD is an logic language designed by Chris Date for interacting with any relational algebra engine. Project:M36 supports this language for the purposes of learning about the relational algebra. TutorialD is not recommended for use in production systems.
5It is presumed that the audience for this tutorial has at least some minimal background with an SQL-based DBMS.
7### Starting the TutorialD Interpreter
9From the project-m36 source directory, execute ```cabal run tutd``` to start the interactive TutorialD interpreter. You will be greeted by the interactive prompt:
11```
12TutorialD (master/main):
13```
15"master" refers to the name of the branch to which the current transaction will be potentially committing.
17### Types
19TutorialD is strongly-typed. The basic built-in types are:
21|Type Name|Explanation|Example|
22|---------|-----------|-------|
23|char|arbitrary text|"The Old Man and the Sea"|
24|int|any integer|-4|
25|datetime|timestamp UTC|"2015-02-02 04:05:02"::datetime|
26|double|floating point number|3.1459|
27|bool|boolean value|t|
28|bytestring|arbitrary-length string of bytes- input is base64-encoded|"dGVzdGRhdGE="::bytestring|
30With regards to boolean values, be sure not to conflate ```t``` or ```f``` as a boolean value with ```true``` and ```false``` which are relation variables.
32Project:M36 will complain loudly if the expected types do not match. Automatic type coercion does not exist.
34```
35TutorialD (master/main): :showexpr S:{more:=add(10,@sname)}
36ERR: AtomFunctionTypeError "add" 2 IntAtomType StringAtomType
37```
39The integer "10" cannot be added to the string sname value.
41### Using Relation Variables
43Relation Variables are named cells which reference relations within a transaction state. The relation can be replaced with another relation through assignment.
45Definition of a relation variable:
46```
47products :: {name char, age int}
48```
49This expression creates a new relation variable in the current context which can refer to any relation with the header of "name" of type "char" and "age" of type int.
51Assignment to a relation variable:
52```
53products := relation{tuple{name "Mike",age 6},tuple{name "Sam",age 10}}
54```
55This expression assigns a relation containing two tuples to the previously-defined "products" relation.
57```tutd``` includes two relations by default:
58* ```true```: the relation with no attributes and a body of one empty-attributed tuple
59* ```false```: the relation with no attributes and a body of no tuples
61Chris Date refers to these relations as "TABLE_DUM" and "TABLE_DEE" but such arbitrary names are not so recognizable. ```true``` and ```false``` can be easily remembered as the answer to the question: "Does this relation have any tuples?" which can be formulated in TutorialD as the projection of a relation asking for no attributes:
62```
63products{}
64```
66### Date's Sample Relations
68The examples in this tutorial can be executed after loading Chris Date's sample relations using:
70```
71:importexample date
72```
74Each section assumes a fresh instance of the Date examples. The relation variables created in the script are:
76|Relation Variable Name|Description|
77|----------------------|-----------|
78|s|suppliers|
79|p|parts|
80|sp|contains mapping of which suppliers supply which parts|
82### Relational Expressions
84A relational expression combines relational operators to create a new relation. ```tutd``` can display the result of executing a relational expression with ":showexpr".
86```
87TutorialD (master/main): :showexpr p
88┌──────┬─────┬──┬─────┬──────┐
89│city │color│p#│pname│weight│
90├──────┼─────┼──┼─────┼──────┤
91│London│Red │P6│Cog │19 │
92│Paris │Blue │P5│Cam │12 │
93│London│Red │P1│Nut │12 │
94│London│Red │P4│Screw│14 │
95│Oslo │Blue │P3│Screw│17 │
96│Paris │Green│P2│Bolt │17 │
97└──────┴─────┴──┴─────┴──────┘
98```
100To show all relation variables in the current session, use ```:showrelvars```:
102```
103TutorialD (master/main): :showrelvars
104┌─────────────────────────────────────────────────┬──────────┐
105│attributes::relation {attribute::Text,type::Text}│name::Text│
106├─────────────────────────────────────────────────┼──────────┤
107│┌───────────────┬──────────┐ │"sp" │
108││attribute::Text│type::Text│ │ │
109│├───────────────┼──────────┤ │ │
110││"p#" │"Text" │ │ │
111││"qty" │"Int" │ │ │
112││"s#" │"Text" │ │ │
113│└───────────────┴──────────┘ │ │
114│┌───────────────┬──────────┐ │"true" │
115││attribute::Text│type::Text│ │ │
116│└───────────────┴──────────┘ │ │
117│┌───────────────┬──────────┐ │"p" │
118││attribute::Text│type::Text│ │ │
119│├───────────────┼──────────┤ │ │
120││"weight" │"Int" │ │ │
121││"p#" │"Text" │ │ │
122││"color" │"Text" │ │ │
123││"city" │"Text" │ │ │
124││"pname" │"Text" │ │ │
125│└───────────────┴──────────┘ │ │
126│┌───────────────┬──────────┐ │"s" │
127││attribute::Text│type::Text│ │ │
128│├───────────────┼──────────┤ │ │
129││"status" │"Int" │ │ │
130││"s#" │"Text" │ │ │
131││"sname" │"Text" │ │ │
132││"city" │"Text" │ │ │
133│└───────────────┴──────────┘ │ │
134│┌───────────────┬──────────┐ │"false" │
135││attribute::Text│type::Text│ │ │
136│└───────────────┴──────────┘ │ │
137└─────────────────────────────────────────────────┴──────────┘
138```
140### Using Relational Operators
142Relational operators generate new relations from existing relations. The operators form the closed algebra against relations.
144#### Rename
146The unary rename operator outputs a new relation with chosen attributes renamed. In SQL, the equivalent is to use "AS" in the column name list of SELECTS.
148```
149TutorialD (master/main): :showexpr s rename {city as town}
150┌──┬─────┬──────┬──────┐
151│s#│sname│status│town │
152├──┼─────┼──────┼──────┤
153│S3│Blake│30 │Paris │
154│S4│Clark│20 │London│
155│S5│Adams│30 │Athens│
156│S1│Smith│20 │London│
157│S2│Jones│10 │Paris │
158└──┴─────┴──────┴──────┘
159```
161#### Projection
163Projection is applied by adding curly braces and attribute names following a relational expression. Projection in SQL is applied by adding a list of column names to a SELECT statement.
165For example:
167```
168TutorialD (master/main): :showexpr p{color,city}
169┌──────┬─────┐
170│city │color│
171├──────┼─────┤
172│London│Red │
173│Paris │Blue │
174│Oslo │Blue │
175│Paris │Green│
176└──────┴─────┘
177```
179#### Restriction
181Restriction is applied using a "where" clause, much like in SQL.
183```
184TutorialD (master/main): :showexpr p where color="Blue" and city="Paris"
185┌─────┬─────┬──┬─────┬──────┐
186│city │color│p#│pname│weight│
187├─────┼─────┼──┼─────┼──────┤
188│Paris│Blue │P5│Cam │12 │
189└─────┴─────┴──┴─────┴──────┘
190```
192The restriction predicate can be built from "and", "not", and "or". Boolean atom functions can also appear in a restriction as long as they are preceded by "^". For example:
194```
195TutorialD (master/main): :showexpr s where ^lt(@status,20)
196┌─────┬──┬─────┬──────┐
197│city │s#│sname│status│
198├─────┼──┼─────┼──────┤
199│Paris│S2│Jones│10 │
200└─────┴──┴─────┴──────┘
201```
203#### Join
205Joins are binary operators and are applied with the "join" keyword. The equivalent in SQL would be a "NATURAL JOIN". To join attributes which are not identically-named, use the rename operator
207```
208TutorialD (master/main): :showexpr s join sp
209┌──────┬──┬───┬──┬─────┬──────┐
210│city │p#│QTY│s#│sname│status│
211├──────┼──┼───┼──┼─────┼──────┤
212│London│P6│100│S1│Smith│20 │
213│London│P3│400│S1│Smith│20 │
214│London│P5│400│S4│Clark│20 │
215│London│P1│300│S1│Smith│20 │
216│Paris │P2│200│S3│Blake│30 │
217│Paris │P1│300│S2│Jones│10 │
218│London│P5│100│S1│Smith│20 │
219│London│P4│300│S4│Clark│20 │
220│Paris │P2│400│S2│Jones│10 │
221│London│P2│200│S1│Smith│20 │
222│London│P4│200│S1│Smith│20 │
223└──────┴──┴───┴──┴─────┴──────┘
224```
226#### Union
228The binary union operator merges tuples from both relations if-and-only-if the attributes of both relations are identical. The SQL equivalent is the "UNION" operator. Unlike SQL, however, duplicate tuples are not tolerated, so there is no "UNION ALL" equivalent in the relational algebra.
230```
231TutorialD (master/main): :showexpr S union S
232┌──────┬──┬─────┬──────┐
233│city │s#│sname│status│
234├──────┼──┼─────┼──────┤
235│Paris │S3│Blake│30 │
236│London│S4│Clark│20 │
237│Athens│S5│Adams│30 │
238│London│S1│Smith│20 │
239│Paris │S2│Jones│10 │
240└──────┴──┴─────┴──────┘
241```
243The union of any relation with itself is itself.
245#### Extend
247The extend unary operator adds an attribute to a relation's header and body and is represented by a colon ":". The equivalent in SQL is a subselect in a SELECT column list. Typically, extend is used to add information derived from relation.
249```
250TutorialD (master/main): :showexpr s:{status2:=add(10,@status)}
251┌──────┬──┬─────┬──────┬───────┐
252│city │s#│sname│status│status2│
253├──────┼──┼─────┼──────┼───────┤
254│Paris │S2│Jones│10 │20 │
255│Athens│S5│Adams│30 │40 │
256│Paris │S3│Blake│30 │40 │
257│London│S1│Smith│20 │30 │
258│London│S4│Clark│20 │30 │
259└──────┴──┴─────┴──────┴───────┘
260```
262The "@" is required in the above query in order to distinguish the attribute's name from a relation's name.
264Supported atom functions include:
266|Function|Purpose|
267|--------|-------|
268|add(int,int)|Return the sum of two integers.|
269|not(bool)|Invert a boolean expression.|
270|lt(int,int)|Returns boolean true atom if he first argument is less than the second argument.|
271|lte(int,int)|Returns boolean true atom if he first argument is less than or equal to the second argument.|
272|gt(int,int)|Returns boolean true atom if he first argument is greater than the second argument.|
273|gte(int,int)|Returns boolean true atom if he first argument is greater than or equal to the second argument.|
275#### Group
277The unary group operator groups the argument attributes into subrelations for each ungrouped set of attributes. SQL does not support tables as values, though SQL does support a notion of grouping by equal values. However, SQL's "GROUP BY" relies on tuple ordering in a table. A relations' tuples are never ordered.
279Grouping is useful for aggregate operations (see below) or for summarizing data against a set of attributes; for example, "display all employees grouped by boss name".
281```
282TutorialD (master/main): :showexpr s group ({sname,status,s#} as subrel)
283┌──────┬─────────────────┐
284│city │subrel │
285├──────┼─────────────────┤
286│Athens│┌──┬─────┬──────┐│
287│ ││s#│sname│status││
288│ │├──┼─────┼──────┤│
289│ ││S5│Adams│30 ││
290│ │└──┴─────┴──────┘│
291│London│┌──┬─────┬──────┐│
292│ ││s#│sname│status││
293│ │├──┼─────┼──────┤│
294│ ││S1│Smith│20 ││
295│ ││S4│Clark│20 ││
296│ │└──┴─────┴──────┘│
297│Paris │┌──┬─────┬──────┐│
298│ ││s#│sname│status││
299│ │├──┼─────┼──────┤│
300│ ││S3│Blake│30 ││
301│ ││S2│Jones│10 ││
302│ │└──┴─────┴──────┘│
303└──────┴─────────────────┘
304```
306#### Ungroup
308The unary ungroup operator "unwraps" subrelations in a relation-valued attribute. There is no equivalent in SQL.
310```
311TutorialD (master/main): :showexpr (s group ({sname,status,s#} as subrel)) ungroup subrel
312┌──────┬──┬─────┬──────┐
313│city │s#│sname│status│
314├──────┼──┼─────┼──────┤
315│Paris │S3│Blake│30 │
316│London│S4│Clark│20 │
317│Athens│S5│Adams│30 │
318│London│S1│Smith│20 │
319│Paris │S2│Jones│10 │
320└──────┴──┴─────┴──────┘
321```
323## State Operators
325While relational operators compose to relational expressions representing queries of the database, state operators change the state of the database.
327### Relation Variable State Changes
329#### Define and Assign
331The relation variable assignment mentioned above certainly changes the state of the database.
333```
334TutorialD (master/main): newrelvar:=relation{tuple{age 3}}
335TutorialD (master/main): :showexpr newrelvar
336┌───┐
337│age│
338├───┤
339│3 │
340└───┘
341```
343All further operators are convenience operators and could be implemented with simple assignment. SQL has no equivalent to relational assignment; instead, one must issue "CREATE TABLE" commands and insert rows into the resultant tables.
345#### Undefine
347To signal that a relation variable should be forgotten, undefine it.
349```
350TutorialD (master/main): undefine s
351TutorialD (master/main): :showexpr s
352ERR: RelVarNotDefinedError "s"
353```
355#### Insert
357The insert operators accepts a relation variable and a relation of the same type (a relation having an identical header), and replaces the relation referenced by the relation variable with the union of the previous relation value and the argument's relation value.
359```
360TutorialD (master/main): insert s relation{tuple{city "Boston",s# "S10",sname "Gonzalez",status 10}}
361TutorialD (master/main): :showexpr s
362┌──────┬───┬────────┬──────┐
363│city │s# │sname │status│
364├──────┼───┼────────┼──────┤
365│Paris │S3 │Blake │30 │
366│London│S4 │Clark │20 │
367│Athens│S5 │Adams │30 │
368│London│S1 │Smith │20 │
369│Paris │S2 │Jones │10 │
370│Boston│S10│Gonzalez│10 │
371└──────┴───┴────────┴──────┘
372```
374This insertion operation is equivalent to:
376```
377s:=s union relation{tuple{city "Boston",s# "S10",sname "Gonzalez",status 10}}
378```
379#### Update
381The update operator accepts a relation argument and a predicate, optionally filters and creates new tuples based on the original relation.
383```
384TutorialD (master/main): update s where status=20 (sname:="Mr. Twenty")
385TutorialD (master/main): :showexpr s
386┌──────┬──┬──────────┬──────┐
387│city │s#│sname │status│
388├──────┼──┼──────────┼──────┤
389│Paris │S3│Blake │30 │
390│London│S1│Mr. Twenty│20 │
391│Athens│S5│Adams │30 │
392│Paris │S2│Jones │10 │
393│London│S4│Mr. Twenty│20 │
394└──────┴──┴──────────┴──────┘
395```
397This is logically equivalent to:
399```
400TutorialD (master/main): s:=(((s where status=20){city,s#,status}):{sname:="Mr. Twenty"}) union (s where not status=20)
401TutorialD (master/main): :showexpr s
402┌──────┬──┬──────────┬──────┐
403│city │s#│sname │status│
404├──────┼──┼──────────┼──────┤
405│Paris │S3│Blake │30 │
406│London│S1│Mr. Twenty│20 │
407│Athens│S5│Adams │30 │
408│Paris │S2│Jones │10 │
409│London│S4│Mr. Twenty│20 │
410└──────┴──┴──────────┴──────┘
411```
413This query means: "Drop the sname attribute from S filtered where status equals 20, extend the resultant relation by sname with value 'Mr. Twenty', then union the resultant relation with S filtered by status not equaling 20, and assign that to S."
415#### Delete
417The delete operator accepts a relation argument and a filtering predicate, creates a new relation with only the tuples which do not match the predicate, and stores the result in the relation variable argument.
419```
420TutorialD (master/main): delete sp where s#="S4"
421TutorialD (master/main): :showexpr sp
422┌──┬───┬──┐
423│p#│QTY│s#│
424├──┼───┼──┤
425│P3│400│S1│
426│P4│200│S1│
427│P2│400│S2│
428│P2│200│S1│
429│P6│100│S1│
430│P1│300│S1│
431│P2│200│S3│
432│P1│300│S2│
433│P5│100│S1│
434└──┴───┴──┘
435```
437This is logically equivalent to:
439```
440TutorialD (master/main): sp:=sp where not s#="S4"
441```
443Note the inverted predicate.
445## Constraints
447Database constraints are user-specified relational algebra predicates which must be true at all times. If a database expression attempts to violate a database constraint, then the DBMS must reject the expression and ensure that the previous, non-violating state remains.
449Chris Date identified that all constraints can be represented as inclusion dependencies, but Project:M36 offers some convenience operators.
451#### Uniqueness Constraints
453Uniqueness constraints ensure that a set of attributes' values are unique throughout a relational expression. For example, it is useful to ensure that all products' identifiers are unique. The equivalent in SQL is the UNIQUE expression found in table creation.
455```
456TutorialD (master/main): key s_key_constraint {s#} s
457```
459This expression creates a constraint named "S_key_constraint" and ensure that the values of the "s#" attribute are unique in the "S" relational expression.
461#### Foreign Key Constraints
463Foreign keys are constraints which require that certain values appearing in one relation variable must appear in another. The equivalent in SQL is the FOREIGN KEY() construction in table creation.
465```
466TutorialD (master/main): foreign key s#_in_sp sp{s#} in s{s#}
467```
469This expression ensure that any sp{s#} must also appear as a value in s{s#}.
471#### Functional dependencies
473Unlike SQL databases, Project:M36 includes first-class support for functional dependencies. A functional dependency for an attribute set ```A``` to attribute set ```B``` ensures that for every unique appearance of a value for ```A``` there is only one possible value for ```B```.
475To create a functional dependency:
477```
478TutorialD (master/main): funcdep sname_status (sname) -> (status) s
479```
481In this example, the functional dependency is named "sname_status" and creates a constraint which ensures that the "sname" value is functionally determines "status" value in the relation variable "s", though the final argument can be any relational expression. Multiple attribute names are separated by a comma.
483#### Other Constraints
485All other constraints can be represented by a set of inclusion dependencies.
487```
488TutorialD (master/main): constraint s_status_less_than_50 (s where ^lt(50,@status)){} in false
489```
491This expression ensures that the relation variable's relation can never include a tuple with status greater than 50.
493Internally, *all* constraints are converted and stored as inclusion dependencies.
495## Aggregate Queries
497Aggregate queries in TutorialD are able to provide much more information than SQL queries due to the support for relation-valued attributes. For example, a single query can return aggregate results alongside its constituent tuples.
499SQL supports aggregate queries using aggregate functions (SUM(), COUNT(), AVERAGE(), etc.) and GROUP BY while the aggregate functions in the relational algebra simply accept relation values as arguments.
501```
502TutorialD (master/main): :showexpr s group ({s#,sname,status} as subrel):{citycount:=count(@subrel)}
503┌──────┬─────────┬─────────────────┐
504│city │citycount│subrel │
505├──────┼─────────┼─────────────────┤
506│Paris │2 │┌──┬─────┬──────┐│
507│ │ ││s#│sname│status││
508│ │ │├──┼─────┼──────┤│
509│ │ ││S3│Blake│30 ││
510│ │ ││S2│Jones│10 ││
511│ │ │└──┴─────┴──────┘│
512│London│2 │┌──┬─────┬──────┐│
513│ │ ││s#│sname│status││
514│ │ │├──┼─────┼──────┤│
515│ │ ││S1│Smith│20 ││
516│ │ ││S4│Clark│20 ││
517│ │ │└──┴─────┴──────┘│
518│Athens│1 │┌──┬─────┬──────┐│
519│ │ ││s#│sname│status││
520│ │ │├──┼─────┼──────┤│
521│ │ ││S5│Adams│30 ││
522│ │ │└──┴─────┴──────┘│
523└──────┴─────────┴─────────────────┘
524```
526In this query result, we can simultaneously answer how many suppliers are located in each city and which suppliers they are. The expression is constructed by first grouping to isolate the city, then extending the query to add a attribute containing the tuple count for each subrelation.
528|Function Name|Description|
529|-------------|-----------|
530|count(relation{any tuple types})|return the number of tuples in each relation value|
531|sum(relation{int})|return the int sum of the relation's values|
532|max(relation{int})|return the maximum int from all the relation's values|
533|min(relation{int})|return the minimum int from all the relation's values|