=pod =encoding utf8 =head1 NAME Muldis::D::Ext::Relation - Muldis D extension adding more generic relational operators =head1 VERSION This document is Muldis::D::Ext::Relation version 0.27.0. =head1 PREFACE This document is part of the Muldis D language specification, whose root document is L; you should read that root document before you read this one, which provides subservient details. =head1 DESCRIPTION Muldis D has a mandatory core set of system-defined (eternally available) entities, which is referred to as the I or the I; they are the minimal entities that all Muldis D implementations need to provide; they are mutually self-describing and are used to bootstrap the language; any entities outside the core, called I, are non-mandatory and are defined in terms of the core or each other, but the reverse isn't true. This current C document describes the system-defined I, which consists of many generic relational operators (for generic relations), adding to the minimum few defined in the language core. This current document does not describe the polymorphic operators that all types, or some types including core types, have defined over them; said operators are defined once for all types in L. I =head1 SYSTEM-DEFINED GENERIC SINGLE INPUT RELATION FUNCTIONS These functions are applicable to mainly relation types, but are generic in that they typically work with any relation types. Each C parameter is optional and defaults to the zero-attribute tuple if no explicit argument is given to it. =over =item C This function results in the degree of its argument (that is, the count of attributes it has). =item C This function results in C iff its argument has zero tuples, and C otherwise. Note that if you are using a C to represent a sparse data item, analagously to a SQL nullable context, then testing the C with C is analagous to testing a SQL nullable with C. =item C This function is exactly the same as C except that it results in the opposite boolean value when given the same argument. And following the analogy with C, C is analagous to SQL's C. =item C This function results in the empty relation of the same heading of its argument, that is having the same degree and attribute names; it has zero tuples. =item C This function results in the power set of its argument. The result is a C whose sole attribute is C-typed (its type is nominally the same as that of the argument) and which has a tuple for every distinct subset of tuples in the argument. The cardinality of the result is equal to 2 raised to the power of the cardinality of the argument (which may easily lead to a very large result, so use this function with care). Note that the N-ary relational union of the power set of some relation is that relation; the N-ary intersection of any power set is the empty relation. =item C This function results in the transitive closure of its argument. The argument is a binary relation whose attributes are both of the same type, and the result is a relation having the same heading and a body which is a superset of the argument's tuples. Assuming that the argument represents all of the node pairs in a directed graph that have an arc between them, and so each argument tuple represents an arc, C will determine all of the node pairs in that graph which have a path between them (a recursive operation), so each tuple of the result represents a path. The result is a superset since all arcs are also complete paths. The C function is intended to support recursive queries, such as in connection with the "part explosion problem" (the problem of finding all components, at all levels, of some specified part). =item C This function is a generic reduction operator that recursively takes each pair of tuples in C and applies an argument-specified tuple value-resulting function (which is both commutative and associative) to the pair until just one input tuple is left, which is the result. The function to apply is named in the C argument, and that function must have 3 arguments named C, C, C; the last parameter is curried with the same-named argument of C, and the first 2 parameters are the 2 input tuples for an invocation. If C has zero tuples, then C results in the tuple given in C. I may be changed to take a function name rather than a value, for consistency with C.> This function will fail|warn if the |declared headings of C and C aren't compatible. =item C This function is exactly the same as C except that it does not take an C argument, and it results in a C of what is otherwise the result type, and that result has zero elements if the argument has zero elements. =item C This function provides a convenient one-place generalization of per-tuple transformations that otherwise might require the chaining of up to a half-dozen other operators like restriction, extension, and rename. This function results in a relation each of whose tuples is the result of applying, to each of the tuples of its C argument, the C-resulting function named in its C argument when the latter function is curried by its C argument. There is no restriction on what attributes the result tuple of C may have (except that all tuples from C must have compatible headings); this tuple from C would completely replace the original tuple from C. The result relation has a cardinality that is the same as that of C, unless the result of C was redundant tuples, in which case the result has appropriately fewer tuples. As a trivial case, if C is defined to unconditionally result in the same tuple as its own C argument, then this function results simply in C; or, if C is defined to have a static result, then this function's result will have just 0..1 tuples. =back =head1 SYSTEM-DEFINED GENERIC MULTIPLE INPUT RELATION FUNCTIONS These functions are applicable to mainly relation types, but are generic in that they typically work with any relation types. =over =item C This function is exactly the same as C except that it results in C if its 2 arguments are equal. =item C This function is exactly the same as C except that it results in the opposite boolean value when given the same arguments. =item C This function results in the relational exclusion/exclusive-or of the N element values of its argument; it is a reduction operator that recursively takes each pair of input values and relationally excludes (which is both commutative and associative) them together until just one is left, which is the result. The result relation has the same heading as all of its inputs, and its body contains every tuple that is in just an odd number of the input relations. Matters concerning a C with zero values are as per C; this function will fail when given such, and the per-distinct-heading identity value for relational exclusion is the same as for relational union. Note that this operation is also legitimately known as I. =item C This function results in the relational composition of its 2 arguments. It is conceptually a short-hand for first doing an ordinary relational join between its 2 arguments, and then performing a relational projection on all of the attributes that only one of the arguments has; that is, the result has all of and just the attributes that were not involved in matching the tuples of the 2 arguments. This function will fail|warn any time that C would fail|warn on the same 2 input relations. =item C This function is a short-hand for first taking a (natural inner) C of its C and C arguments, and then taking a C on all of the attributes that only the C argument had, such that the attribute resulting from the group has the name C. The result has 1 tuple for every tuple of C where at least 1 matching tuple exists in C. This function will fail if C is the same name as any source attribute that wasn't grouped. This function is a convenient tool for gathering both parent and child records from a database using a single query while avoiding duplication of the parent record values. =back =head1 SYSTEM-DEFINED RELATIONAL RANKING AND QUOTA FUNCTIONS These additional functions are specific to supporting ranking and quotas. Each C parameter is optional and defaults to the zero-attribute tuple if no explicit argument is given to it. =over =item C This function results in the relational extension of its C argument by a single C-typed attribute whose name is provided by the C argument, where the value of the new attribute for each tuple is the rank of that tuple as determined by the (total) C function named in the C argument when the latter function is curried by the C argument. The C function compares tuples, with each invocation of it getting a C tuple as each its C and C arguments. The new attribute of C's result has a value of zero for its ranked-first tuple, and each further consecutive ranked tuple has the next larger integer value. Note that C provides the functionality of SQL's "RANK" feature but that the result of C is always a total ordering (as per a (total) C function) and so there is no "dense" / "not dense" distinction (however a partial ordering can be implemented over it). See also the C function, which is the same as C but that it wraps the source tuples rather than just adding an attribute to them. =item C This function results in the relational restriction of its C argument as determined by first ranking its tuples as per C function (using C and C) and then keeping just those tuples whose rank is within the inclusive range specified by the C and C arguments (C's extra attribute is not kept). The C function implements a certain kind of quota query where all the result tuples are consecutive in their ranks. This function will fail if C is before C. It is valid for C or C to be greater than the maximum rank of the source tuples; in the first case, the result has zero tuples; in the second case, the result has all remaining tuples starting at C. If C has any tuples and C matches the rank of a source tuple, then the result will always have at least 1 tuple. Note that C provides the functionality of SQL's "LIMIT/OFFSET" feature in combination with "ORDER BY" but that the result tuples of C do not remain ordered (but see C for an alternative). =back =head1 SYSTEM-DEFINED RELATIONAL SUBSTITUTION FUNCTIONS These additional functions are specific to supporting substitutions. Each C<\w*assuming> parameter is optional and defaults to the zero-attribute tuple if no explicit argument is given to it. =over =item C This function is similar to C except that it substitutes values of existing relation attributes rather than adding new attributes. The result relation has the same heading as C. The result tuple of the function named in C must have a heading that is a subset of the heading of C; corresponding values resulting from the function named in C will replace the values of the tuples of C. The result relation has a cardinality that is the same as that of C, unless the result of any substitutions was redundant tuples, in which case the result has appropriately fewer tuples. As a trivial case, if C is defined to unconditionally result in either the degree-zero tuple or in the same tuple as its own C argument, then this function results simply in C; or, if C is defined to have a static result and it replaces all attributes, then this function's result will have just 0..1 tuples. =item C This function is a simpler-syntax alternative to C in the typical scenario where every tuple of a relation, given in the C argument, is updated with identical values for the same attributes; the new attribute values are given in the C argument. =item C This function is like C except that it only transforms a subset of the tuples of C rather than all of them. It is a short-hand for first separating the tuples of C into 2 groups where those passed by a relational restriction (defined by C and C) are then transformed (defined by C and C), then the result of the substitution is unioned with the un-transformed group. See also the C function, which is a simpler-syntax alternative for C in its typical usage where restrictions are composed simply of anded or ored tests for attribute value equality. =item C This function is to C what C is to C. See also the C function. =item C This function is like C except that the subset of the tuples of C to be transformed is determined by those matched by a semijoin with C rather than those that pass a generic relational restriction. =item C This function is to C what C is to C. =back =head1 SYSTEM-DEFINED RELATIONAL OUTER-JOIN FUNCTIONS These additional functions are specific to supporting outer-joins. =over =item C This function is the same as C except that it results in a half-outer natural join rather than an inner natural join; every tuple of C has exactly 1 corresponding tuple in the result, but where there were no matching C tuples, the result attribute named by C contains zero tuples rather than 1+. =item C This function results in a plain half-outer natural join of its C and C arguments where all the result attributes that come from just C are C-typed; for result tuples from matched source tuples, each C attribute value is a C; for result tuples from non-matched C tuples, each C attribute value is a C. The C function is Muldis D's answer to the SQL LEFT OUTER JOIN where SQL NULL is implicitly used in result rows that were a non-match. =item C This function is the same as C but that the filler tuple is the default value of the tuple data type whose name is given in the C argument. This function is a short-hand for invoking C with the result from invoking C. =item C This function is the same as C but that C-sourced result attributes are not converted to C; rather, for result tuples from non-matches, the missing values are provided explicitly from the C argument, which is a tuple whose heading matches the projection of C's attributes that aren't in common with C, and whose body is the literal values to use for those missing attribute values. This function gets its name in that conceptually the result tuples from non-matches are the result of performing a relational cross-product between the un-matched C tuples and the single C tuple. This function could alternately be named I. =item C This function is the same as C but that the result tuples from non-matches are the result of performing a relational extension on the un-matched C tuples such that each said result tuple is determined by applying the function named in C to each said C tuple when the named function is curried using the C argument. The C parameter is optional and defaults to the zero-attribute tuple if no explicit argument is given to it. =back =head1 SYSTEM-DEFINED GENERIC RELVAR UPDATERS =over =item C This update operator is a short-hand for first invoking the C function such that it has 2 input relations from C's 2 arguments, and then assigning the result of that function to C. =item C This update operator is a short-hand for first invoking the C function with the same arguments, and then assigning the result of that function to C. This updater is analagous to the general case of the unconditional SQL "UPDATE" statement. =item C This update operator is a short-hand for first invoking the C function with the same arguments, and then assigning the result of that function to C. =item C This update operator is a short-hand for first invoking the C function with the same arguments, and then assigning the result of that function to C. This updater is analagous to the general case of the conditional SQL "UPDATE" statement. =item C This update operator is a short-hand for first invoking the C function with the same arguments, and then assigning the result of that function to C. =item C This updater is analagous to the common case of the conditional SQL "UPDATE" statement where the criteria is simply a set of and-ed and or-ed value equality tests. This update operator is a short-hand for first invoking the C function with the same arguments, and then assigning the result of that function to C. =item C This update operator is a short-hand for first invoking the C function with the same arguments, and then assigning the result of that function to C. =back =head1 SEE ALSO Go to L for the majority of distribution-internal references, and L for the majority of distribution-external references. =head1 AUTHOR Darren Duncan (C) =head1 LICENSE AND COPYRIGHT This file is part of the formal specification of the Muldis D language. Muldis D is Copyright © 2002-2008, Darren Duncan. See the LICENSE AND COPYRIGHT of L for details. =head1 TRADEMARK POLICY The TRADEMARK POLICY in L applies to this file too. =head1 ACKNOWLEDGEMENTS The ACKNOWLEDGEMENTS in L apply to this file too. =cut