Officially out now: The TypeDB 3.0 Roadmap >>

OR (Disjunction)

A disjunction of patterns requires at least one of its patterns to be true. A disjunction is itself a pattern.

Syntax

A disjunction block of two or more pattern is of the form:

Syntax
"{" <pattern> "}" or "{" <pattern> "}" [ or "{" <pattern> "}" ... ] ;

where each <pattern> can be any valid pattern. Note that at least one variable in the <pattern> must be bound outside the disjunction.

Behavior

Disjunctions can be used in a match clause or a condition of a rule to create alternative constraints. Matching one of the branches is enough for the solution to be returned.

Disjunction of statements

For this example, use a database with the IAM schema and sample data loaded.

One of the uses of a disjunction is to retrieve instances of different types. As a simple and one of the most common examples, let’s retrieve all data from a database by using a disjunction with all three root types:

Data retrieval example
match
$s isa $t;
{$t type entity;} or {$t type relation;} or {$t type attribute;};
get $s;

The above query returns all data instances from a database as stateful objects.

Disjunction of patterns

Let’s retrieve entities that can be of either person or file entity type and fetch their attributes, either a full-name or a path respectively:

Disjunction example
match
$x isa $type;
{$type type person; $x has full-name $name;} or {$type type file; $x has path $name;};
fetch
$name;
See example output
Output example (partial)
{ "name": { "value": "Kevin Morrison", "type": { "label": "full-name", "root": "attribute", "value_type": "string" } } }
{ "name": { "value": "Masako Holley", "type": { "label": "full-name", "root": "attribute", "value_type": "string" } } }
{ "name": { "value": "Pearle Goodman", "type": { "label": "full-name", "root": "attribute", "value_type": "string" } } }
{ "name": { "value": "axidw.java", "type": { "label": "path", "root": "attribute", "value_type": "string" } } }
{ "name": { "value": "budget_2021-08-01.xlsx", "type": { "label": "path", "root": "attribute", "value_type": "string" } } }
{ "name": { "value": "README.md", "type": { "label": "path", "root": "attribute", "value_type": "string" } } }

Disjunction in rules

For this example, use a database with the IAM schema and sample data loaded.

Disjunction can be used in a rule condition in the same way as it can be used in a match clause.

Let’s create a rule to add the validity attribute with the value true to a permission if at least one of the following conditions is met:

  • the permission has review-date attribute with value bigger than 25th of December 2022;

  • the permission has no review-date attribute.

Disjunction in a rule example
define
rule permission-validity: when {
    $permission isa permission;
    { $permission has review-date $date;
      $date > 2022-12-25T00:00:00.000; }
    or
    { not { $permission has review-date $date; }; };
} then {
    $permission has validity true;
};

Learn more

Learn more about conjunction in TypeQL.

Learn more about negation in TypeQL.

Learn more about statements in TypeQL patterns.