on 144_proxy_tupleable help sign in
project-m36/docs/tutd_tutorial.markdown
556 lines 27 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|text|arbitrary text|"The Old Man and the Sea"|
24|int|any integer|-4|
25|datetime|timestamp UTC|dateTimeFromEpochSeconds(1502304846)|
26|date|calendar date|fromGregorian(2017,05,30)|
27|double|floating point number|3.1459|
28|bool|boolean value|t|
29|bytestring|arbitrary-length string of bytes- input is base64-encoded|bytestring("dGVzdGRhdGE=")|
30|interval x|interval/range type for ints, doubles, datetimes, and dates|interval(3,5,f,f)|
32With regards to boolean values, be sure not to conflate ```t``` or ```f``` as a boolean value with ```true``` and ```false``` which are relation variables.
34The ```interval``` function last two arguments are boolean values indicating whether the interval is open at the start point and end point respectively.
36Project:M36 will complain loudly if the expected types do not match. Automatic type coercion does not exist.
38```
39TutorialD (master/main): :showexpr S:{more:=add(10,@sname)}
40ERR: AtomFunctionTypeError "add" 2 IntAtomType StringAtomType
41```
43The integer "10" cannot be added to the string sname value.
45### Using Relation Variables
47Relation Variables are named cells which reference relations within a transaction state. The relation can be replaced with another relation through assignment.
49Definition of a relation variable:
50```
51products :: {name char, age int}
52```
53This 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.
55Assignment to a relation variable:
56```
57products := relation{tuple{name "Mike",age 6},tuple{name "Sam",age 10}}
58```
59This expression assigns a relation containing two tuples to the previously-defined "products" relation.
61```tutd``` includes two relations by default:
62* ```true```: the relation with no attributes and a body of one empty-attributed tuple
63* ```false```: the relation with no attributes and a body of no tuples
65Chris 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:
66```
67products{}
68```
70### Date's Sample Relations
72The examples in this tutorial can be executed after loading Chris Date's sample relations using:
74```
75:importexample date
76```
78Each section assumes a fresh instance of the Date examples. The relation variables created in the script are:
80|Relation Variable Name|Description|
81|----------------------|-----------|
82|s|suppliers|
83|p|parts|
84|sp|contains mapping of which suppliers supply which parts|
86### Relational Expressions
88A relational expression combines relational operators to create a new relation. ```tutd``` can display the result of executing a relational expression with ":showexpr".
90```
91TutorialD (master/main): :showexpr p
92┌──────┬─────┬──┬─────┬──────┐
93│city │color│p#│pname│weight│
94├──────┼─────┼──┼─────┼──────┤
95│London│Red │P6│Cog │19 │
96│Paris │Blue │P5│Cam │12 │
97│London│Red │P1│Nut │12 │
98│London│Red │P4│Screw│14 │
99│Oslo │Blue │P3│Screw│17 │
100│Paris │Green│P2│Bolt │17 │
101└──────┴─────┴──┴─────┴──────┘
102```
104To show all relation variables in the current session, use ```:showrelvars```:
106```
107TutorialD (master/main): :showrelvars
108┌─────────────────────────────────────────────────┬──────────┐
109│attributes::relation {attribute::Text,type::Text}│name::Text│
110├─────────────────────────────────────────────────┼──────────┤
111│┌───────────────┬──────────┐ │"sp" │
112││attribute::Text│type::Text│ │ │
113│├───────────────┼──────────┤ │ │
114││"p#" │"Text" │ │ │
115││"qty" │"Int" │ │ │
116││"s#" │"Text" │ │ │
117│└───────────────┴──────────┘ │ │
118│┌───────────────┬──────────┐ │"true" │
119││attribute::Text│type::Text│ │ │
120│└───────────────┴──────────┘ │ │
121│┌───────────────┬──────────┐ │"p" │
122││attribute::Text│type::Text│ │ │
123│├───────────────┼──────────┤ │ │
124││"weight" │"Int" │ │ │
125││"p#" │"Text" │ │ │
126││"color" │"Text" │ │ │
127││"city" │"Text" │ │ │
128││"pname" │"Text" │ │ │
129│└───────────────┴──────────┘ │ │
130│┌───────────────┬──────────┐ │"s" │
131││attribute::Text│type::Text│ │ │
132│├───────────────┼──────────┤ │ │
133││"status" │"Int" │ │ │
134││"s#" │"Text" │ │ │
135││"sname" │"Text" │ │ │
136││"city" │"Text" │ │ │
137│└───────────────┴──────────┘ │ │
138│┌───────────────┬──────────┐ │"false" │
139││attribute::Text│type::Text│ │ │
140│└───────────────┴──────────┘ │ │
141└─────────────────────────────────────────────────┴──────────┘
142```
144### Using Relational Operators
146Relational operators generate new relations from existing relations. The operators form the closed algebra against relations.
148#### Rename
150The 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.
152```
153TutorialD (master/main): :showexpr s rename {city as town}
154┌──┬─────┬──────┬──────┐
155│s#│sname│status│town │
156├──┼─────┼──────┼──────┤
157│S3│Blake│30 │Paris │
158│S4│Clark│20 │London│
159│S5│Adams│30 │Athens│
160│S1│Smith│20 │London│
161│S2│Jones│10 │Paris │
162└──┴─────┴──────┴──────┘
163```
165#### Projection
167Projection 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.
169For example:
171```
172TutorialD (master/main): :showexpr p{color,city}
173┌──────┬─────┐
174│city │color│
175├──────┼─────┤
176│London│Red │
177│Paris │Blue │
178│Oslo │Blue │
179│Paris │Green│
180└──────┴─────┘
181```
183#### Restriction
185Restriction is applied using a "where" clause, much like in SQL.
187```
188TutorialD (master/main): :showexpr p where color="Blue" and city="Paris"
189┌─────┬─────┬──┬─────┬──────┐
190│city │color│p#│pname│weight│
191├─────┼─────┼──┼─────┼──────┤
192│Paris│Blue │P5│Cam │12 │
193└─────┴─────┴──┴─────┴──────┘
194```
196The 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:
198```
199TutorialD (master/main): :showexpr s where ^lt(@status,20)
200┌─────┬──┬─────┬──────┐
201│city │s#│sname│status│
202├─────┼──┼─────┼──────┤
203│Paris│S2│Jones│10 │
204└─────┴──┴─────┴──────┘
205```
207#### Join
209Joins 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
211```
212TutorialD (master/main): :showexpr s join sp
213┌──────┬──┬───┬──┬─────┬──────┐
214│city │p#│QTY│s#│sname│status│
215├──────┼──┼───┼──┼─────┼──────┤
216│London│P6│100│S1│Smith│20 │
217│London│P3│400│S1│Smith│20 │
218│London│P5│400│S4│Clark│20 │
219│London│P1│300│S1│Smith│20 │
220│Paris │P2│200│S3│Blake│30 │
221│Paris │P1│300│S2│Jones│10 │
222│London│P5│100│S1│Smith│20 │
223│London│P4│300│S4│Clark│20 │
224│Paris │P2│400│S2│Jones│10 │
225│London│P2│200│S1│Smith│20 │
226│London│P4│200│S1│Smith│20 │
227└──────┴──┴───┴──┴─────┴──────┘
228```
230#### Union
232The 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.
234```
235TutorialD (master/main): :showexpr S union S
236┌──────┬──┬─────┬──────┐
237│city │s#│sname│status│
238├──────┼──┼─────┼──────┤
239│Paris │S3│Blake│30 │
240│London│S4│Clark│20 │
241│Athens│S5│Adams│30 │
242│London│S1│Smith│20 │
243│Paris │S2│Jones│10 │
244└──────┴──┴─────┴──────┘
245```
247The union of any relation with itself is itself.
249#### Extend
251The 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.
253```
254TutorialD (master/main): :showexpr s:{status2:=add(10,@status)}
255┌──────┬──┬─────┬──────┬───────┐
256│city │s#│sname│status│status2│
257├──────┼──┼─────┼──────┼───────┤
258│Paris │S2│Jones│10 │20 │
259│Athens│S5│Adams│30 │40 │
260│Paris │S3│Blake│30 │40 │
261│London│S1│Smith│20 │30 │
262│London│S4│Clark│20 │30 │
263└──────┴──┴─────┴──────┴───────┘
264```
266The "@" is required in the above query in order to distinguish the attribute's name from a relation's name.
268Supported atom functions include:
270|Function|Purpose|
271|--------|-------|
272|add(int,int)|Return the sum of two integers.|
273|not(bool)|Invert a boolean expression.|
274|lt(int,int)|Returns boolean true atom if he first argument is less than the second argument.|
275|lte(int,int)|Returns boolean true atom if he first argument is less than or equal to the second argument.|
276|gt(int,int)|Returns boolean true atom if he first argument is greater than the second argument.|
277|gte(int,int)|Returns boolean true atom if he first argument is greater than or equal to the second argument.|
279#### Group
281The 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.
283Grouping 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".
285The group operator can also be used to emulate functionality similar to SQL's OUTER JOIN but without relying on overloading the meaning of NULL.
287```
288TutorialD (master/main): :showexpr s group ({sname,status,s#} as subrel)
289┌──────┬─────────────────┐
290│city │subrel │
291├──────┼─────────────────┤
292│Athens│┌──┬─────┬──────┐│
293│ ││s#│sname│status││
294│ │├──┼─────┼──────┤│
295│ ││S5│Adams│30 ││
296│ │└──┴─────┴──────┘│
297│London│┌──┬─────┬──────┐│
298│ ││s#│sname│status││
299│ │├──┼─────┼──────┤│
300│ ││S1│Smith│20 ││
301│ ││S4│Clark│20 ││
302│ │└──┴─────┴──────┘│
303│Paris │┌──┬─────┬──────┐│
304│ ││s#│sname│status││
305│ │├──┼─────┼──────┤│
306│ ││S3│Blake│30 ││
307│ ││S2│Jones│10 ││
308│ │└──┴─────┴──────┘│
309└──────┴─────────────────┘
310```
312#### Ungroup
314The unary ungroup operator "unwraps" subrelations in a relation-valued attribute. There is no equivalent in SQL.
316```
317TutorialD (master/main): :showexpr (s group ({sname,status,s#} as subrel)) ungroup subrel
318┌──────┬──┬─────┬──────┐
319│city │s#│sname│status│
320├──────┼──┼─────┼──────┤
321│Paris │S3│Blake│30 │
322│London│S4│Clark│20 │
323│Athens│S5│Adams│30 │
324│London│S1│Smith│20 │
325│Paris │S2│Jones│10 │
326└──────┴──┴─────┴──────┘
327```
329#### Minus
331The binary minus operator takes two relation variables of the same type and returns the tuples which appear in the first relation variable but not in the second relation.
333```
334TutorialD (master/main): x:=relation{tuple{name "Steve"},tuple{name "Bob"},tuple{name "Jim"},tuple{name "Bart"}}
335TutorialD (master/main): y:=relation{tuple{name "Jim"},tuple{name "Bart"}}
336TutorialD (master/main): :showexpr x minus y
337┌──────────┐
338│name::Text│
339├──────────┤
340│"Steve" │
341│"Bob" │
342└──────────┘
343```
346## State Operators
348While relational operators compose to relational expressions representing queries of the database, state operators change the state of the database.
350### Relation Variable State Changes
352#### Define and Assign
354The relation variable assignment mentioned above certainly changes the state of the database.
356```
357TutorialD (master/main): newrelvar:=relation{tuple{age 3}}
358TutorialD (master/main): :showexpr newrelvar
359┌───┐
360│age│
361├───┤
362│3 │
363└───┘
364```
366All 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.
368#### Undefine
370To signal that a relation variable should be forgotten, undefine it.
372```
373TutorialD (master/main): undefine s
374TutorialD (master/main): :showexpr s
375ERR: RelVarNotDefinedError "s"
376```
378#### Insert
380The 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.
382```
383TutorialD (master/main): insert s relation{tuple{city "Boston",s# "S10",sname "Gonzalez",status 10}}
384TutorialD (master/main): :showexpr s
385┌──────┬───┬────────┬──────┐
386│city │s# │sname │status│
387├──────┼───┼────────┼──────┤
388│Paris │S3 │Blake │30 │
389│London│S4 │Clark │20 │
390│Athens│S5 │Adams │30 │
391│London│S1 │Smith │20 │
392│Paris │S2 │Jones │10 │
393│Boston│S10│Gonzalez│10 │
394└──────┴───┴────────┴──────┘
395```
397This insertion operation is equivalent to:
399```
400s:=s union relation{tuple{city "Boston",s# "S10",sname "Gonzalez",status 10}}
401```
402#### Update
404The update operator accepts a relation argument and a predicate, optionally filters and creates new tuples based on the original relation.
406```
407TutorialD (master/main): update s where status=20 (sname:="Mr. Twenty")
408TutorialD (master/main): :showexpr s
409┌──────┬──┬──────────┬──────┐
410│city │s#│sname │status│
411├──────┼──┼──────────┼──────┤
412│Paris │S3│Blake │30 │
413│London│S1│Mr. Twenty│20 │
414│Athens│S5│Adams │30 │
415│Paris │S2│Jones │10 │
416│London│S4│Mr. Twenty│20 │
417└──────┴──┴──────────┴──────┘
418```
420This is logically equivalent to:
422```
423TutorialD (master/main): s:=(((s where status=20){city,s#,status}):{sname:="Mr. Twenty"}) union (s where not status=20)
424TutorialD (master/main): :showexpr s
425┌──────┬──┬──────────┬──────┐
426│city │s#│sname │status│
427├──────┼──┼──────────┼──────┤
428│Paris │S3│Blake │30 │
429│London│S1│Mr. Twenty│20 │
430│Athens│S5│Adams │30 │
431│Paris │S2│Jones │10 │
432│London│S4│Mr. Twenty│20 │
433└──────┴──┴──────────┴──────┘
434```
436This 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."
438#### Delete
440The 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.
442```
443TutorialD (master/main): delete sp where s#="S4"
444TutorialD (master/main): :showexpr sp
445┌──┬───┬──┐
446│p#│QTY│s#│
447├──┼───┼──┤
448│P3│400│S1│
449│P4│200│S1│
450│P2│400│S2│
451│P2│200│S1│
452│P6│100│S1│
453│P1│300│S1│
454│P2│200│S3│
455│P1│300│S2│
456│P5│100│S1│
457└──┴───┴──┘
458```
460This is logically equivalent to:
462```
463TutorialD (master/main): sp:=sp where not s#="S4"
464```
466Note the inverted predicate.
468## Constraints
470Database 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.
472Chris Date identified that all constraints can be represented as inclusion dependencies, but Project:M36 offers some convenience operators.
474#### Uniqueness Constraints
476Uniqueness 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.
478```
479TutorialD (master/main): key s_key_constraint {s#} s
480```
482This expression creates a constraint named "S_key_constraint" and ensure that the values of the "s#" attribute are unique in the "S" relational expression.
484#### Foreign Key Constraints
486Foreign 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.
488```
489TutorialD (master/main): foreign key s#_in_sp sp{s#} in s{s#}
490```
492This expression ensure that any sp{s#} must also appear as a value in s{s#}.
494#### Functional dependencies
496Unlike 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```.
498To create a functional dependency:
500```
501TutorialD (master/main): funcdep sname_status (sname) -> (status) s
502```
504In 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.
506#### Other Constraints
508All other constraints can be represented by a set of inclusion dependencies.
510```
511TutorialD (master/main): constraint s_status_less_than_50 (s where ^lt(50,@status)){} in false
512```
514This expression ensures that the relation variable's relation can never include a tuple with status greater than 50.
516Internally, *all* constraints are converted and stored as inclusion dependencies.
518## Aggregate Queries
520Aggregate 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.
522SQL 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.
524```
525TutorialD (master/main): :showexpr s group ({s#,sname,status} as subrel):{citycount:=count(@subrel)}
526┌──────┬─────────┬─────────────────┐
527│city │citycount│subrel │
528├──────┼─────────┼─────────────────┤
529│Paris │2 │┌──┬─────┬──────┐│
530│ │ ││s#│sname│status││
531│ │ │├──┼─────┼──────┤│
532│ │ ││S3│Blake│30 ││
533│ │ ││S2│Jones│10 ││
534│ │ │└──┴─────┴──────┘│
535│London│2 │┌──┬─────┬──────┐│
536│ │ ││s#│sname│status││
537│ │ │├──┼─────┼──────┤│
538│ │ ││S1│Smith│20 ││
539│ │ ││S4│Clark│20 ││
540│ │ │└──┴─────┴──────┘│
541│Athens│1 │┌──┬─────┬──────┐│
542│ │ ││s#│sname│status││
543│ │ │├──┼─────┼──────┤│
544│ │ ││S5│Adams│30 ││
545│ │ │└──┴─────┴──────┘│
546└──────┴─────────┴─────────────────┘
547```
549In 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.
551|Function Name|Description|
552|-------------|-----------|
553|count(relation{any tuple types})|return the number of tuples in each relation value|
554|sum(relation{int})|return the int sum of the relation's values|
555|max(relation{int})|return the maximum int from all the relation's values|
556|min(relation{int})|return the minimum int from all the relation's values|