typedb icon indicating copy to clipboard operation
typedb copied to clipboard

Support unions

Open cxdorn opened this issue 1 year ago • 0 comments

Problem to Solve

TypeQL is missing a union keyword. This is a basic feature of, e.g., SQL, Cypher, and other query languages. From a practical perspective, the union keyword could remove code duplication for users and speed up the execution of complex queries by allowing for the re-use of work. From a theoretical perspective, the union keyword corresponds to union types which are fundamental to type theory.

Current Workaround

Currently, a union query can be modeled by running multiple separate queries and then manually concatenating the results.

Proposed Solution

The simplest solution would be to add the union keyword as syntactic sugar for running multiple queries. (However, this is not the most efficient solution as it doesn't re-use work.) That is, running the query:

match
    $x isa person;
    $x has name "John Doe";
    { ($x, $y) isa friendship; } union { ($x,$z) isa rivalry; };

would be equivalent to the two queries run together:

match
    $x isa person;
    $x has name "John Doe";
    ($x, $y) isa friendship;
match
    $x isa person;
    $x has name "John Doe";
    ($x, $z) isa rivalry;

In words, in this union query we want to return the union of all friends and all rivals of a person with name John Doe. Notably, the variables $y and $z are only referred to from inside the two union branches but would still be returned.

Note: from the above example it may look like we could rewrite union queries with the or keyword instead ... while this is true for the given example, it's not true in general.

From an implementation point of view, one would need to decide what is the best format to return union queries results (two concept maps? one concept map with null's?).

Additional Information

Some rules that union satisfies:

  • union distributes over comma:
A, { B } union { C } = { A, B } union { A, C }
  • Associativity:
{ A } union { { B } union { C } } = { { A } union  { B } } union { C }

cxdorn avatar Aug 22 '23 11:08 cxdorn