NAME
Muldis::D::Ext::Relation - Muldis D extension adding more generic relational operators
VERSION
This document is Muldis::D::Ext::Relation version 0.27.0.
PREFACE
This document is part of the Muldis D language specification, whose root document is Muldis::D; you should read that root document before you read this one, which provides subservient details.
DESCRIPTION
Muldis D has a mandatory core set of system-defined (eternally available) entities, which is referred to as the Muldis D core or the core; 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 Muldis D extensions, are non-mandatory and are defined in terms of the core or each other, but the reverse isn't true.
This current Relation
document describes the system-defined Muldis D Relation Extension, 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 Muldis::D::Core.
This documentation is pending.
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 assuming
parameter is optional and defaults to the zero-attribute tuple if no explicit argument is given to it.
function sys.std.Relation.degree result UInt params { topic(Relation) }
-
This function results in the degree of its argument (that is, the count of attributes it has).
function sys.std.Relation.is_empty result Bool params { topic(Relation) }
-
This function results in
Bool:true
iff its argument has zero tuples, andBool:false
otherwise. Note that if you are using aMaybe
to represent a sparse data item, analagously to a SQL nullable context, then testing theMaybe
withis_empty
is analagous to testing a SQL nullable withis null
. function sys.std.Relation.is_not_empty result Bool params { topic(Relation) }
-
This function is exactly the same as
sys.std.Relation.is_empty
except that it results in the opposite boolean value when given the same argument. And following the analogy withis_empty
,is_not_empty
is analagous to SQL'sis not null
. function sys.std.Relation.empty result Relation params { topic(Relation) }
-
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.
function sys.std.Relation.power_set result set_of.Relation params { topic(Relation) }
-
This function results in the power set of its argument. The result is a
Set
whose sole attribute isRelation
-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. function sys.std.Relation.transitive_closure result Relation params { topic(Relation) }
-
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,
transitive_closure
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. Thetransitive_closure
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). function sys.std.Relation.reduction result Tuple params { topic(Relation), func(Cat.FuncRef), assuming(QuasiTuple)?, identity(Tuple) }
-
This function is a generic reduction operator that recursively takes each pair of tuples in
topic
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 thefunc
argument, and that function must have 3 arguments namedv1
,v2
,assuming
; the last parameter is curried with the same-named argument ofreduction
, and the first 2 parameters are the 2 input tuples for an invocation. Iftopic
has zero tuples, thenreduction
results in the tuple given inidentity
. Note thatidentity
may be changed to take a function name rather than a value, for consistency withfunc
. This function will fail|warn if the |declared headings ofidentity
andtopic
aren't compatible. function sys.std.Relation.maybe_reduction result maybe_of.Tuple params { topic(Relation), func(Cat.FuncRef), assuming(QuasiTuple)? }
-
This function is exactly the same as
sys.std.Relation.reduction
except that it does not take anassuming
argument, and it results in aMaybe
of what is otherwise the result type, and that result has zero elements if the argument has zero elements. function sys.std.Relation.map result Relation params { topic(Relation), func(Cat.FuncRef), assuming(QuasiTuple)? }
-
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
topic
argument, theTuple
-resulting function named in itsfunc
argument when the latter function is curried by itsassuming
argument. There is no restriction on what attributes the result tuple offunc
may have (except that all tuples fromfunc
must have compatible headings); this tuple fromfunc
would completely replace the original tuple fromtopic
. The result relation has a cardinality that is the same as that oftopic
, unless the result offunc
was redundant tuples, in which case the result has appropriately fewer tuples. As a trivial case, iffunc
is defined to unconditionally result in the same tuple as its owntopic
argument, then this function results simply intopic
; or, iffunc
is defined to have a static result, then this function's result will have just 0..1 tuples.
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.
function sys.std.Relation.is_proper_subset result Bool params { look_in(Relation), look_for(Relation) }
-
This function is exactly the same as
sys.std.Core.Relation.is_subset
except that it results inBool:false
if its 2 arguments are equal. function sys.std.Relation.is_not_proper_subset result Bool params { look_in(Relation), look_for(Relation) }
-
This function is exactly the same as
sys.std.Relation.is_proper_subset
except that it results in the opposite boolean value when given the same arguments. function sys.std.Relation.exclusion result Relation params { topic(bag_of.Relation) }
-
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
topic
with zero values are as persys.std.Core.Relation.union
; 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 symmetric difference. function sys.std.Relation.composition result Relation params { r1(Relation), r2(Relation) }
-
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
join
would fail|warn on the same 2 input relations. function sys.std.Relation.join_with_group result Relation params { primary(Relation), secondary(Relation), group_attr(Cat.Name) }
-
This function is a short-hand for first taking a (natural inner)
join
of itsprimary
andsecondary
arguments, and then taking agroup
on all of the attributes that only thesecondary
argument had, such that the attribute resulting from the group has the namegroup_attr
. The result has 1 tuple for every tuple ofprimary
where at least 1 matching tuple exists insecondary
. This function will fail ifgroup_attr
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.
SYSTEM-DEFINED RELATIONAL RANKING AND QUOTA FUNCTIONS
These additional functions are specific to supporting ranking and quotas. Each ord_assuming
parameter is optional and defaults to the zero-attribute tuple if no explicit argument is given to it.
function sys.std.Relation.rank result Relation params { topic(Relation), name(Cat.Name), ord_func(Cat.FuncRef), ord_assuming(QuasiTuple)? }
-
This function results in the relational extension of its
topic
argument by a singleUInt
-typed attribute whose name is provided by thename
argument, where the value of the new attribute for each tuple is the rank of that tuple as determined by the (total)order_determination
function named in theord_func
argument when the latter function is curried by theord_assuming
argument. Theorder_determination
function compares tuples, with each invocation of it getting atopic
tuple as each itstopic
andother
arguments. The new attribute ofrank
'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 thatrank
provides the functionality of SQL's "RANK" feature but that the result ofrank
is always a total ordering (as per a (total)order_determination
function) and so there is no "dense" / "not dense" distinction (however a partial ordering can be implemented over it). See also thesys.std.Array.Array_from_wrap
function, which is the same assys.std.Relation.rank
but that it wraps the source tuples rather than just adding an attribute to them. function sys.std.Relation.limit result Relation params { topic(Relation), ord_func(Cat.FuncRef), ord_assuming(QuasiTuple)?, min_rank(UInt), max_rank(UInt) }
-
This function results in the relational restriction of its
topic
argument as determined by first ranking its tuples as perrank
function (usingord_func
andord_assuming
) and then keeping just those tuples whose rank is within the inclusive range specified by themin_rank
andmax_rank
arguments (rank
's extra attribute is not kept). Thelimit
function implements a certain kind of quota query where all the result tuples are consecutive in their ranks. This function will fail ifmax_rank
is beforemin_rank
. It is valid formin_rank
ormax_rank
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 atmin_rank
. Iftopic
has any tuples andmin_rank
matches the rank of a source tuple, then the result will always have at least 1 tuple. Note thatlimit
provides the functionality of SQL's "LIMIT/OFFSET" feature in combination with "ORDER BY" but that the result tuples oflimit
do not remain ordered (but seesys.std.Array.limit_of_Array_from_wrap
for an alternative).
SYSTEM-DEFINED RELATIONAL SUBSTITUTION FUNCTIONS
These additional functions are specific to supporting substitutions. Each \w*assuming
parameter is optional and defaults to the zero-attribute tuple if no explicit argument is given to it.
function sys.std.Relation.substitution result Relation params { topic(Relation), func(Cat.FuncRef), assuming(QuasiTuple)? }
-
This function is similar to
extension
except that it substitutes values of existing relation attributes rather than adding new attributes. The result relation has the same heading astopic
. The result tuple of the function named infunc
must have a heading that is a subset of the heading oftopic
; corresponding values resulting from the function named infunc
will replace the values of the tuples oftopic
. The result relation has a cardinality that is the same as that oftopic
, unless the result of any substitutions was redundant tuples, in which case the result has appropriately fewer tuples. As a trivial case, iffunc
is defined to unconditionally result in either the degree-zero tuple or in the same tuple as its owntopic
argument, then this function results simply intopic
; or, iffunc
is defined to have a static result and it replaces all attributes, then this function's result will have just 0..1 tuples. function sys.std.Relation.static_substitution result Relation params { topic(Relation), attrs(Tuple) }
-
This function is a simpler-syntax alternative to
sys.std.Relation.substitution
in the typical scenario where every tuple of a relation, given in thetopic
argument, is updated with identical values for the same attributes; the new attribute values are given in theattrs
argument. function sys.std.Relation.substitution_in_restriction result Relation params { topic(Relation), restr_func(Cat.FuncRef), restr_assuming(QuasiTuple)?, subst_func(Cat.FuncRef), subst_assuming(QuasiTuple)? }
-
This function is like
substitution
except that it only transforms a subset of the tuples oftopic
rather than all of them. It is a short-hand for first separating the tuples oftopic
into 2 groups where those passed by a relational restriction (defined byrestr_func
andrestr_assuming
) are then transformed (defined bysubst_func
andsubst_assuming
), then the result of the substitution is unioned with the un-transformed group. See also thesubstitution_in_semijoin
function, which is a simpler-syntax alternative forsubstitution_in_restriction
in its typical usage where restrictions are composed simply of anded or ored tests for attribute value equality. function sys.std.Relation.static_substitution_in_restriction result Relation params { topic(Relation), restr_func(Cat.FuncRef), restr_assuming(QuasiTuple)?, subst(Tuple) }
-
This function is to
sys.std.Relation.substitution_in_restriction
whatsys.std.Relation.static_substitution
is tosys.std.Relation.substitution
. See also thestatic_substitution_in_semijoin
function. function sys.std.Relation.substitution_in_semijoin result Relation params { topic(Relation), restr(Relation), subst_func(Cat.FuncRef), subst_assuming(QuasiTuple)? }
-
This function is like
substitution_in_restriction
except that the subset of the tuples oftopic
to be transformed is determined by those matched by a semijoin withrestr
rather than those that pass a generic relational restriction. function sys.std.Relation.static_substitution_in_semijoin result Relation params { topic(Relation), restr(Relation), subst(Tuple) }
-
This function is to
sys.std.Relation.substitution_in_semijoin
whatsys.std.Relation.static_substitution
is tosys.std.Relation.substitution
.
SYSTEM-DEFINED RELATIONAL OUTER-JOIN FUNCTIONS
These additional functions are specific to supporting outer-joins.
function sys.std.Relation.outer_join_with_group result Relation params { primary(Relation), secondary(Relation), group_attr(Cat.Name) }
-
This function is the same as
sys.std.Relation.join_with_group
except that it results in a half-outer natural join rather than an inner natural join; every tuple ofprimary
has exactly 1 corresponding tuple in the result, but where there were no matchingsecondary
tuples, the result attribute named bygroup_attr
contains zero tuples rather than 1+. function sys.std.Relation.outer_join_with_maybes result Relation params { primary(Relation), secondary(Relation) }
-
This function results in a plain half-outer natural join of its
primary
andsecondary
arguments where all the result attributes that come from justsecondary
areMaybe
-typed; for result tuples from matched source tuples, eachsecondary
attribute value is aSingle
; for result tuples from non-matchedprimary
tuples, eachsecondary
attribute value is aNothing
. Theouter_join_with_maybes
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. function sys.std.Relation.outer_join_with_defaults result Relation params { primary(Relation), secondary(Relation), filler(Cat.TypeRef) }
-
This function is the same as
sys.std.Relation.outer_join_with_product
but that the filler tuple is the default value of the tuple data type whose name is given in thefiller
argument. This function is a short-hand for invokingouter_join_with_product
with the result from invokingsys.std.Core.Universal.default
. function sys.std.Relation.outer_join_with_product result Relation params { primary(Relation), secondary(Relation), filler(Tuple) }
-
This function is the same as
sys.std.Relation.outer_join_with_maybes
but thatsecondary
-sourced result attributes are not converted toMaybe
; rather, for result tuples from non-matches, the missing values are provided explicitly from thefiller
argument, which is a tuple whose heading matches the projection ofsecondary
's attributes that aren't in common withprimary
, 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-matchedprimary
tuples and the singlefiller
tuple. This function could alternately be named outer_join_with_static_extension. function sys.std.Relation.outer_join_with_extension result Relation params { primary(Relation), secondary(Relation), exten_func(Cat.FuncRef), exten_assuming(QuasiTuple)? }
-
This function is the same as
sys.std.Relation.outer_join_with_product
but that the result tuples from non-matches are the result of performing a relational extension on the un-matchedprimary
tuples such that each said result tuple is determined by applying the function named inexten_func
to each saidprimary
tuple when the named function is curried using theexten_assuming
argument. Theexten_assuming
parameter is optional and defaults to the zero-attribute tuple if no explicit argument is given to it.
SYSTEM-DEFINED GENERIC RELVAR UPDATERS
updater sys.std.Relation.assign_exclusion update { topic(Relation) } read { other(Relation) }
-
This update operator is a short-hand for first invoking the
sys.std.Relation.exclusion
function such that it has 2 input relations fromassign_exclusion
's 2 arguments, and then assigning the result of that function totopic
. updater sys.std.Relation.assign_substitution update { topic(Relation) } read { func(Cat.FuncRef), assuming(QuasiTuple)? }
-
This update operator is a short-hand for first invoking the
sys.std.Core.Relation.substitution
function with the same arguments, and then assigning the result of that function totopic
. This updater is analagous to the general case of the unconditional SQL "UPDATE" statement. updater sys.std.Relation.assign_static_substitution update { topic(Relation) } read { attrs(Tuple) }
-
This update operator is a short-hand for first invoking the
sys.std.Core.Relation.static_substitution
function with the same arguments, and then assigning the result of that function totopic
. updater sys.std.Relation.assign_substitution_in_restriction update { topic(Relation) } read { restr_func(Cat.FuncRef), restr_assuming(QuasiTuple)?, subst_func(Cat.FuncRef), subst_assuming(QuasiTuple)? }
-
This update operator is a short-hand for first invoking the
sys.std.Core.Relation.substitution_in_restriction
function with the same arguments, and then assigning the result of that function totopic
. This updater is analagous to the general case of the conditional SQL "UPDATE" statement. updater sys.std.Relation.assign_static_substitution_in_restriction update { topic(Relation) } read { restr_func(Cat.FuncRef), restr_assuming(QuasiTuple)?, subst(Tuple) }
-
This update operator is a short-hand for first invoking the
sys.std.Core.Relation.static_substitution_in_restriction
function with the same arguments, and then assigning the result of that function totopic
. updater sys.std.Relation.assign_substitution_in_semijoin update { topic(Relation) } read { restr(Relation), subst_func(Cat.FuncRef), subst_assuming(QuasiTuple)? }
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
sys.std.Core.Relation.substitution_in_semijoin
function with the same arguments, and then assigning the result of that function totopic
. updater sys.std.Relation.assign_static_substitution_in_semijoin update { topic(Relation) } read { restr(Relation), subst(Tuple) }
-
This update operator is a short-hand for first invoking the
sys.std.Core.Relation.static_substitution_in_semijoin
function with the same arguments, and then assigning the result of that function totopic
.
SEE ALSO
Go to Muldis::D for the majority of distribution-internal references, and Muldis::D::SeeAlso for the majority of distribution-external references.
AUTHOR
Darren Duncan (perl@DarrenDuncan.net
)
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 Muldis::D for details.
TRADEMARK POLICY
The TRADEMARK POLICY in Muldis::D applies to this file too.
ACKNOWLEDGEMENTS
The ACKNOWLEDGEMENTS in Muldis::D apply to this file too.