Chapter 09Algebra of Binding Tables
This chapter introduces the algebra of binding tables, which forms the intermediate value domain on which query blocks and statements operate. We treat binding tables as bags of records and present selection, projection, joins, unwinding, and subquery composition as operations that transform them. The algebra is formulated with explicit bag semantics, three-valued logic, and ordering metadata, in a manner that aligns with the GQL standard’s treatment of optional pattern matching, correlation, and primitive result statements.
Classical bag-query algebras provide background on duplicates, aggregation, nesting, and expressive power (Grumbach and Milo 1996; Libkin and Wong 1997). They serve here only as mathematical background; binding-table descriptors, ordering metadata, preferred column names, and the operator rules remain governed by GQL.
9.1 Selection and Projection
Let be a binding table with . The formulas below act on this bag projection; each operator separately preserves or updates the ordered/unordered and preferred-column metadata of the complete binding-table descriptor. Projecting actions are modeled as functions on multisets of records:
- Selection: — the submultiset of records satisfying predicate (evaluated in GQL types, with null handling according to the comparison semantics and the three-valued truth tables from Chapter 8).
- Projection: — each input record occurrence contributes once to a new record with fields named taking values ; multiplicities of equal projected records are summed.
- Natural join: — join based on name compatibility and equal values of shared fields; the bag multiplicity is consistent with the product of multiplicities of the paired records.
- Ordering: ordering metadata may be included in the descriptor of a binding table, distinguishing ordered from unordered tables; it does not affect the underlying bag semantics but determines the order in which records are emitted.2
For example, let have the two rows
Then retains only , and projecting it onto title yields the singleton row .
9.2 Joins and OPTIONAL (Left-Outer Join)
Compatibility and merge. Mappings are naturally compatible, written , iff their fields with the same name have equal values in the value-level sense used by the GQL definition of natural joinability. This is not an evaluation of the three-valued comparison predicate; in particular, two common fields that both contain the null value have equal values. Their merge is the union partial map after any required common-type coercion and is well-defined when .
Binding-table semantics of OPTIONAL MATCH. Let be the current table, let be a pattern, and let be its graph pattern binding table with an explicit or implicit YIELD clause. In ISO/IEC 39075, OPTIONAL MATCH is specified via a translation to an OPTIONAL CALL whose RETURN list contains only the new output bindings. The call is evaluated separately for every occurrence of an incoming record, so the correlation columns must not be projected away before the optional expansion.
More generally, let be a correlated binding-table transformer and let be the columns in its declared output record type that are not columns of . For every occurrence of in , put
where the singleton is an incoming table occurrence and the final parentheses denote projection. Define the correlated optional expansion by
Thus, with , . This preserves correlation with each incoming record and fills all statically declared new fields with exactly when that record has no matching extension.
Bag join. For bags of mappings, define the (natural) bag join
Guarded join (ON-condition). Given a Boolean guard , the guarded join is
where is evaluated over . Note that or in discards the pair (no match), keeps it. If evaluates to for some pair, the join is undefined (signals a dynamic exception).
Left outer join. Let be the set of variables in the declared record type of , including when is empty. Let project a merged mapping to the left variables , let denote bag difference on these left projections, and let map each remaining left mapping to , where binds every right-only variable in to . For and guard , the left outer join is the bag
Denotational semantics of OPTIONAL MATCH. For a basic pattern and an optional pattern with internal guard (e.g., a WHERE-predicate inside the optional clause), denotation is
where includes the internal guard . A separate FILTER WHERE after OPTIONAL is applied by to the outer-join result (hence -sensitive); a WHERE belonging to the optional graph pattern remains inside .
OPTIONAL Block Denotation For a block consisting of a basic pattern , an optional pattern with guard , a separate trailing filter , and a projection , let denote the corresponding record projection, including evaluation of its expressions with the null-and-error semantics of Chapter 8 and preservation of bag multiplicities. Then
9.3 FOR and List Unnesting
FOR on a list is modeled by . For each occurrence of a record , if the field contains the list , the operator produces the records
Under bag semantics, each generated record inherits the multiplicity of ; thus, multiplicities are multiplied when identical output records arise from different input occurrences or list positions. If evaluates to , no output record is generated for .
9.4 Subqueries and Scope
- : (possibly empty) conjunction of graph/path patterns with a WHERE-predicate producing a base binding table as in Section 7.1.
- : a sequence of algebraic operators over binding tables (, , , , , , , ).
- : a final projection (RETURN-style) specifying visible columns and expressions to emit.
The grouping, ordering, and slicing stages are defined in Chapters 12 and 13. For a store state , let denote the base binding table obtained by evaluating in the working graph and correlation environment of ; for empty , it is the unit binding table. The denotation of a block is
Example (query-block decomposition). For the second query in Section 4.4, which computes total_late_fee, let denote its two path patterns together with the two ISBN conditions in their WHERE clause. In the abstraction above,
Thus the graph-pattern WHERE belongs to ; LET and the subsequent FILTER WHERE determine , while RETURN determines .
Linear-query pipelining. ISO/IEC 39075 does not use a WITH clause as a query-block boundary. Instead, the incoming and outgoing working record and working table types are threaded from each simple query statement to the next in a linear query statement. Projection and renaming are performed by the applicable result statement, and only the columns of its outgoing working table type are visible to a following statement.
Subqueries. A subquery evaluates to a binding table in the current . When the subquery is correlated, references to names from the enclosing query are resolved in the current outer-row environment. The resulting binding table is consumed according to the surrounding evaluation context, as specified below for relational, scalar, and row-producing subqueries.
Environments and scopes. A static environment maps names (variables, parameters, functions, and relevant catalog objects) to denotations. A dynamic environment provides the current graph and the current outer-row mapping during correlated evaluation.
Uncorrelated subquery. A subquery that contains no reference to an outer binding is independent of the outer row. Its result is consumed according to its surrounding context; the additional static restrictions for a value query expression are stated below under scalar subqueries.
Correlated subquery. A subquery referencing outer attributes is a function of the outer row:
The EXISTS context consumes the resulting bag as follows:
Row-producing subqueries. In the FROM/pattern position, a subquery contributes its bag of rows to the surrounding algebra just like a base pattern.
Dynamic-context notation. In the formulas below, abbreviates the corresponding components of ; function and procedure registries continue to be resolved through the static environment . The outer mapping may be empty.
Variable hygiene. Each subquery has its own local variable namespace disjoint from outer variables by -renaming, except for explicitly correlated references to outer names, which are resolved in . Shadowing of names by aliases is disallowed unless explicitly stated.
Relational subqueries. A subquery used by an EXISTS predicate is evaluated as a bag in environment . Here is the current left-row mapping.
(never ); errors from inside the subquery propagate to the enclosing expression.
Scalar subqueries. A scalar subquery is a value query expression . Its nested query has a single result column, and its emitting RETURN is statically required either to use LIMIT 1 or to contain an aggregate without GROUP BY; hence its result has at most one record. Formally, with :
Subquery in OPTIONAL. When is correlated and appears on the right of OPTIONAL, the correlation uses the left-row mapping at the point of join; each yields independently, composing via as above.
9.5 Whole-Query Denotation
A full read-only query is a pipeline of blocks
Each block is evaluated with respect to the fixed store state and transforms the binding table produced by its predecessor. Let , and, for , define
The denotation of the complete query is therefore
Equivalently,
Example (linear-query pipeline). The linear-composition query in Line 14 instantiates this recurrence with four successive transformations. Let denote the first MATCH graph pattern together with its last-name WHERE predicate and its implicit YIELD clause, let denote the corresponding graph pattern binding table for the second MATCH, and let
be the predicate of the subsequent FILTER WHERE. At the level of the abstraction above, , where
Here denotes ascending ordering by the projected due_date field. Consequently, .
An updating statement is modeled separately because its evaluation may change the store state. Given an input binding table , its denotation has the form
where is the resulting store state and is either a result binding table or an omitted result. When no preceding query blocks supply bindings to the update, is typically the unit binding table.
Soundness checks and algebraic laws (selected) Let denote bag-equivalence of binding tables (same records with same multiplicities, ignoring order). The following laws are intended only when every relevant expression evaluation yields the same value and raises no dynamic exception:
- .
- when mentions only .
- is associative and commutative up to field renaming and multiplicity algebra.
- .
9.6 Composite Queries and Set Operators
A composite query combines the results of one or more linear query pipelines using set-like operators UNION, EXCEPT, INTERSECT and the fallback operator OTHERWISE. At the semantic level, a composite query expression denotes a binding table obtained by composing the binding tables produced by its constituent linear queries.
Syntactic shape. The grammar fragment
composite-query ::= query-part { query-conj query-part }
introduces a left-associative composition of query-parts. Each query-part is a linear-query. A query-conj is one of UNION, EXCEPT, INTERSECT, or OTHERWISE, with the set operators optionally qualified by a set quantifier ALL or DISTINCT. In line with the GQL standard, the following constraints are imposed:
- If a set operator (UNION, EXCEPT, INTERSECT) is specified without an explicit set quantifier, the quantifier DISTINCT is implicit.
- Within a single composite-query, all occurrences of query-conj must be the same query conjunction: the same operator and, for a set operator, the same ALL/DISTINCT choice after applying the implicit DISTINCT. Thus neither mixed operators such as UNION ... EXCEPT ... nor mixed quantifiers such as UNION ALL ... UNION DISTINCT ... are permitted.
Thus, each well-formed composite query expression has the form
for a fixed query conjunction , including its fixed set quantifier when applicable, and , and is interpreted as left-associative:
Typing and column combination. Each linear-query is a (possibly focused) linear query statement in the sense of the standard. We assume that evaluation of a linear query produces a binding table whose bag projection is
where is a record type describing the columns and their value types, and is a finite multiset of records of type .
Let be the query parts of a composite query, , and (these are the results of the immediately contained linear queries). Typing of the composite query is governed by the following constraints, mirroring Subclause 14.2:
- All must be column name-equal: they have the same set of column names.
- The must be column-combinable, in the sense of the type-combination relation: for each column name, the corresponding value types in admit a combined type, possibly a dynamic union type.
- Let be the combined record type obtained from . The declared type of the composite query is the material binding table type with column type .
Operationally, each operand can be coerced to the common type before applying the set operator, using the same coercion and dynamic-union rules as for column-combinable record types. When the operator is one of , , , , , the standard additionally requires that no column in has a dynamic union type that mixes incomparable value types; otherwise, the composite query fails static validation. In these cases, every column acts as a grouping key, and the general grouping rules apply to the composite query.
Bag-based semantics of set operators. We now describe the algebraic semantics in terms of the binding-table algebra. Recall that a binding table is the descriptor-bearing object in fixed in Section 6.1. The equations below determine its bag projection ; the ordered/unordered indicator and preferred column-name sequence remain components of the complete result descriptor and are determined by the corresponding operation. Bag union is written , with multiplicities added element-wise.
For a binary composite expression of the form , where and denote, respectively, the left and right query part, with set operator
and quantifier (with DISTINCT as the default), let and , with and after coercion to the common type . For any record of type , write and for its multiplicities in the left and right operand (with ).
-
UNION ALL.
-
This is the bag analog of GQL UNION ALL: duplicates from both operands are preserved, and their multiplicities added.
-
UNION (distinct).
-
The result is the set-union of the supports of the two bags:
Equivalently, is obtained by applying the DISTINCT operator to .
-
EXCEPT ALL.
-
Intuitively, duplicates of in the right operand cancel duplicates in the left operand, but never below zero.
-
EXCEPT (distinct).
-
Equivalently, one can first apply DISTINCT to both operands and then form their EXCEPT ALL bag difference.
-
INTERSECT ALL.
-
The result is bag intersection:
Each duplicate appears as many times as it is present in both operands.
-
INTERSECT (distinct).
-
The result is set intersection:
Again, this can be seen as INTERSECT ALL followed by DISTINCT.
For any of the distinct variants, and for EXCEPT ALL and INTERSECT ALL, if a record of the left operand and a record of the right operand are not comparable values, evaluation raises the data exception “values not comparable” (22G04) instead of producing a binding table.
Fallback semantics of OTHERWISE. The operator OTHERWISE is a fallback query conjunction rather than a set operator. For a binary composite
where and denote the left and right operands, respectively, let and , with
where is their common combined type. Then
For a longer chain
the result is the binding table produced by the leftmost query operand whose result is non-empty, or the empty binding table of the combined type if all operands produce empty results. The same column-combination discipline as for the set operators applies; no duplicate elimination or bag arithmetic is performed. This describes result selection, not short-circuit evaluation: the General Rules for a composite query expression obtain the results of its query parts before choosing between them, so an operand is not skipped merely because an earlier result is non-empty.
Folding over multiple query parts. Let a composite query be written, after enforcing the uniform-operator rule, as
and let be the binary operation on binding tables induced by the chosen operator and quantifier (one of the six set operations, or the OTHERWISE fallback). Then the denotation of the composite query is the left fold
This algebraic account is extensionally equivalent to the stepwise binding table construction described for the composite query expression in the GQL standard and provides a compact description of how composite queries act on the intermediate binding tables produced by linear query statements.