on 144_proxy_tupleable help sign in
project-m36/docs/transgraphrelationalexpr.markdown
61 lines 4.6 kB history raw
1# Trans-Graph Relational Expressions
3## Introduction
5A standard relational expression queries the state of the database at a single point in time. This point-in-time is represented by an atomic transaction.
7In contrast, a trans-graph relational expression can refer to any transaction's state in the entire history of the database. For example, a trans-graph relational expression could be used to determine how a relation variable has changed over time.
9## Motivation
11Common SQL DBMSs contend all database changes (INSERTs, UPDATEs, DELETEs) on a single state. The DBMS is responsible for simulating serialized access to this ever-changing state. Access to previous states is impossible- the only state visible is the one which is available when the client initiated the transaction.
13In constrast, Project:M36 allows queries against current *and* past transactions. This is useful for learning how a database has changed over time. One essential use for this feature is for "auditing", whereby the database can be queried to explain how, when, and by whom certain changes were made.
15## TutorialD Usage
17In practice, trans-graph relational expressions are very similar to normal relational expressions except that the database context for trans-graph relational expressions is **not** implied. This means that any reference to data in the database context must be marked with a transaction marker. This includes data such as relation variables and atom functions.
19In ```tutd```, here are two equivalent queries (assuming that ```s``` and ```sp``` are relation variables which have been committed to the ```master``` branch):
20```
21TutorialD (master/main): :showexpr s join sp
22```
23```
24TutorialD (master/main): :showtransgraphexpr s@master join sp@master
25```
27Note that the only difference is that the second expression explicitly references which transaction the relation variable is referencing. In the first relational expression, this marker is implicit because the query can only reference the current in-flight transaction.
29Naturally, the trans-graph relational expression is most useful when querying across multiple transactions. This can be accomplished by specifying the transaction id (UUID) or using head name backtracking which is intentionally similar to the git traversal syntax.
31```
32TutorialD (master/main): :showtransgraphexpr s@master~ join sp@master
33```
35The transaction marker ```master~``` indicates that we wish to reference the first parent of the transaction at the head of the "master" branch. Then, we wish to join it with the relation variable ```sp``` at the head transaction of the master branch. Thusly, we can join two relations from two different transactions.
37Since transactions can have multiple parents due to merge commits, the ```^x``` syntax can be used to specify the ```x```-th parent of the referenced transaction.
39These markers can be freely mixed to traverse the graph from a head, as in the following example:
41```
42TutorialD (master/main): :showtransgraphexpr s@improveheadlights~^2
43```
45This query asks to return relation variable ```s``` at the transaction context from the second parent of the first parent of the head of the "improveheadlights" branch.
47## Reference
49Note that relational expressions can query the current in-flight state (called the "disconnected transaction" because it is not committed), while the trans-graph relational expression can only query against committed transactions.
51Note that the caret operator relies on a notion of parent transaction ordering (the n-th parent transaction of a merge transaction). While this ordering is stable, it is arbitrary, so it is up to the user to verify that the correct branch is traversed.
53The same ```@``` syntax can be used with relation variables, atom functions and type constructors.
55Other than the following markers, trans-graph relational expressions share identical syntax with relational expressions.
57| Syntax | Example | Meaning |
58|--------|---------|---------|
59| <uuid> | ```rv@dc385ccc-387e-407d-b65f-e3681cf221a9``` | The relation variable ```rv``` at the state of transaction referred to by explicit unique identifier.|
60| ^[<n-th parent>] | ```rv@addcustomer^2``` | The relation variable ```rv``` at the state of the second parent transaction of the head transaction of the branch ```addcustomer```. |
61| ~[<steps>] | ```rv@addcustomer~3``` | The relation variable ```rv``` at the state of the great-grandparent of the head transaction of branch ```addcustomer```. The tilde operator always traverses the "first" parent transaction of any merge transaction.|