The key features of an ORDBMS are:
- The ability to model easily extrapolated information within the data model itself, through use of user defined functions
- Extensibility with regard to types and functions, written in general purpose programming languages
Towards an Object-Relational AlgebraOne way to think of object-relational modelling is in the language of relational algebra. I haven't been able to find accepted notation for functions on individual tuples, so here I use f(relation) notation, to show that the function operates over the set of tuples in the relation.
Relational algebra as defined by Dr Codd is extremely important but it cannot solve certain classes of important queries and so SQL has gone beyond it. Important blind-spots include:
- Transitive operations at arbitrary depth. Transitive closure and "n next highest values" are both examples of this.
- Calculated data problems
We already know that elements of a tuple are functionally dependent on others if, and only if, for each value of the dependency, there is only one functionally dependent value. So if a is functionally dependent on b, for every b there is exactly one valid value of a. Functional dependency, as in algebra, is value-based, not expression-based.
I am choosing an algebraic-inspired notation for this, where f(R) is a function of relation R if, and only if, for every tuple in relation R, there is only one f(R).
f(R) is trivial if for every tuple (a, b, c) in relation R, f(R) returns a value or a tuple that is a subset of the tuple processed. So if for every tuple (a, b, c), if f(R) = a, or if f(R) = (a, c), then the function is trivial. Every trivial function also represents a trivial functional dependency within the tuple.
A function is relational if it can be expressed solely through relational algebra. All trivial functions can be expressed relationally (using π operations) and therefore are also relational. A relational function thus always specifies a functional data dependency in or between relations. Relational functions have the property of always denoting global functional dependencies.
A function is non-relational if it cannot be expressed solely through non-relational algebra, for example if it involves processing of the actual value of one or more of the tuple elements or their functional dependencies. If we have a relation (Emp) containing an attribute salary_per_wk, and annual_salary(Emp) = πsalary_per_wk * 52(Emp), then annual_salary is non-relational because it involves actual processing of data inside the tuple. Relational functions often can be expanded in relational operations but as far as relational operations, non-relational functions are black boxes and function very much like attributes of a relation.
For example id(R) = πid(R) and c(R) = πname(σid=id(R)(C)) are both relational functions, but only id(R) is trivial. c(R) represents essentially a join and subselect.
An example of a general operation in functional notation might be:
π age(R) (R). Similarly we can πname(R)(σage(R)=41(R))
Of course, we must be careful. Since age(R) is only locally functionally dependant, indexes are out of the question and we must be careful about specifying constraints. However defining a relation such that age(R) < 65 might prove problematic unless we are re-checking every day.
This would be similar to the following statement in PostgreSQL:
FROM employee r
WHERE r.age = 41;
where name and age are table methods. This allows us to store less information in the database (and hence with less duplication and chances for error) and extrapolate important information out of the data that is stored. It also allows us to store data in ways which are less traditional (nested structures etc) for the sole purpose of writing functions against it in that specific format and thus modelling constraints which cannot be modelled using more traditional structures (though as we have seen that poses significant gotchas and complexity costs).
Similarly recursive queries require recursive operators to work. I place a capital Greek Sigma (Σ) to signify recursion above the join operator. This is borrowed because it is the series designator elsewhere in mathematics. An optional maximum depth is specified as a subscript to the Σ. So Σ5 would indicate that the expression or join should be subject to no more than 5 iterations. In a recursive join, the join is repeated until the θ condition is no longer satisfied. The functions of path(R) and depth(R) are functionally dependent on the output of a recursive join, so Σ5 is identical to (σdepth(r)=5(Σ(...) r)). The choice of the Σ is also helpful because while Σ always returns an extended superset, σ always returns a subset.
Since path is functionally dependent on the output of a recursive join, we can prove transitive closure over a finite set using recursive self-joins, functions, and boolean operators. We can also express next highest N tuples results. Alternatively the Σ can be followed by parentheses to show that an entire expression should be repeated until it brings no further results into the set. In a Σ expression the set is divided into two subsets: previous and new. New results are those returned by the last iteration, and are the only ones processed for join conditions. On each iteration, the "new" tuples are moved into the previous set and the tuples which satisfied the join condition are moved into the "new" set.
I also use three other symbols for specifying order-dependent information. ω (omega) denotes a "window" order and partition in which aggregates can be applied to tuples in order, tuples can be removed from the beginning (α with a subscript for number of tuples to "skip") or truncated from the end (τ with a subscript for the number of tuples after which the set is to be truncated). These allow me to approach the problems which SQL can address but relational algebra cannot. An interesting property of ω is that the window order only is valid for some specific operations and is lost on any join or select operations. These operations have interesting properties as well but they are somewhat outside the scope of this posting. I will however note that it is usually cleaner to solve next N result issues with window ordering, tuple omission, and truncation than it is with recursion and aggregates.
Next: Towards a simple explanation of object-relational database systems.