# TypeQL 3.0 Syntactic and Semantic Summary ## Overview TypeQL 3.0 is a strongly-typed, polymorphic query language for TypeDB that enables schema definition, data manipulation, and complex queries through a type-theoretic approach. This summary covers the key syntactic and semantic differences from TypeDB 2.0 and provides comprehensive guidance for TypeQL 3.0. ## Major Changes compared to TypeQL 2.0 ### Value Type Renaming - `long` → `integer` (all integer values now use `integer` type) ### Variables - **All variables**: Must start with `$` (no more `?` for value variables) - **Computed values**: Use `let` keyword for assignments for expressions or function calls ### Patterns - **Relation role player shorthand has changed**: We NO LONGER use `$relation (role: $player) isa relation-type;`. Instead, we use `$relation isa relation-type (role: $player)`. Any usage of `$var (role: $player)` forces `$var` to be a _type_, instead of an _instance_ in TypeQL 3.0 ### Schema Definition: Type Kinds - **No more predefined root types** - all types must be explicitly declared with their kind - **Syntax**: `entity person`, `relation friendship`, `attribute name` - **Subtyping**: Still supported with separate definition: `entity person, sub animal;` ### Updated Query Syntax #### Undefine and Delete Operations - **Type removal**: `undefine person;` (for entities, relations, attributes) - **Trait removal**: - `undefine owns name from person;` - `undefine relates best-friend from friendship;` - `undefine plays friendship:best-friend from person;` - **Instance deletion**: `delete $x;` - **Connection deletion**: - `delete has $name of $x` - `delete links (friend: $p) of $rel`; #### Query Clauses - **Variable filtering**: 'get' operation has been renamed 'select' #### New Fetch Syntax - **JSON-aligned formatting** with nested structures. Syntax describes the structure of JSON output. - **Subqueries**: Fetch subqueries with many answers are embedded using `[]`, while single-returning subqueries use `()` - **All attributes**: Getting all attributes can be done with `fetch { $person.* };`, or `fetch { "person-attributes": { $person.* } };`, since the .* operation expands into a dictionary, it must be wrapped in {} ### 4. Annotations Consolidation - **Abstract and regex**: Now grouped under `@` annotations - **Syntax**: `define entity abstract-person @abstract, owns email @regex(".*@.*");` ### 5. Cardinality Defaults - **Plays**: `@card(0..)` (unlimited) - default - **Owns/Relates**: `@card(0..1)` (up to one) - default - **Explicit cardinality**: `@card(0..)` required for unbounded cardinality ## Core Syntax Elements Comments always start from `#` in typeql to the end of the line, and there are no block comments. These are reserved identifiers. **Never use them as user-defined identifier, in any capitalization**: with, match, fetch, update, define, undefine, redefine, insert, put, delete, end, entity, relation, attribute, role, asc, desc, struct, fun, return, alias, sub, owns, as, plays, relates, iid, isa, links, has, is, or, not, try, in, true, false, of, from, first, last ### Schema Definition * use `define` to idempotently add to the schema * use `undefine` to remove elements from the schema * use `redefine` to replace parts of the schema ### Query Pipeline Structure TypeQL 3.0 uses pipeline-based queries with multiple stages: Pipeline stages can be composed in any order, except that `fetch` can only be the final stage. Stages can be chained: a `reduce` stage produces new bindings that can feed into another `match` stage (see "Chained Reduce" below). #### Match Stage - **Purpose**: Find data matching patterns - **Syntax**: `match ;` - **Example**: ```typeql match $user isa user, has username "user_0"; friendship (friend: $user, friend: $friend); ``` #### Insert Stage - **Purpose**: Add new data - **Syntax**: `insert ;` - **Example**: `insert $user has status "VIP";` #### Delete Stage - **Purpose**: Remove data - **Syntax**: `delete ;` #### Update Stage - **Purpose**: Replace existing data - **Syntax**: `update ;` #### Put Stage - **Purpose**: Insert or update data conditionally (upsert operation) - **Syntax**: `put ;` - **Behavior**: Creates new data if **the entire pattern** doesn't exist, otherwise returns the existing matching data - **Example**: ```typeql match $p isa person, has full-name "John Doe"; put $p has age 30; ``` #### Fetch Stage - **Purpose**: Retrieve and format data - **Syntax**: `fetch { };` - **Features**: - JSON-like nested structures - Subqueries with `match` and either terminated with `fetch` or `return` - Attribute access with `$var.*` to get all (wrapped in {}), `$var.` for a 0 or 1 cardinality attribute ownership, and `[ $var. ]` for higher cardinality attribute ownership #### Reduce Stage - **Purpose**: Aggregate and compute values - **Syntax**: `reduce = [groupby ];` - **Example**: `reduce $VIP-friend-count = count groupby $friend;` ##### Select Operator - **Purpose**: Specify which variables to return in query results - **Syntax**: `select $var1, $var2;` - **Behavior**: Filters output to only include specified variables - **Example**: ```typeql match $p isa person, has full-name $n, has age $a; select $n, $a; ``` ##### Sort Operator - **Purpose**: Order query results based on specified variables - **Syntax**: `sort $var [asc/desc] (, $var2 [asc/desc], ...);` - **Default**: Ascending order if not specified - **Example**: ```typeql match $p isa person, has full-name $n, has age $a; sort $a desc; ``` ##### Limit Operator - **Purpose**: Restrict the number of results returned - **Syntax**: `limit ;` - **Example**: ```typeql match $p isa person, has full-name $n; limit 2; ``` ##### Offset Operator - **Purpose**: Skip a specified number of results before returning remaining ones - **Syntax**: `offset ;` - **Example**: ```typeql match $p isa person, has full-name $n; offset 2; ``` ### Variable System - **Concept Variables**: `$variable-name` (represent instances/values) - **Value Variables**: `$variable-name` (all variables now use `$`) - **Computed Variables**: `let $computed = ;` ### Pattern Construction #### Basic Data Statements - **Type assertion**: `$x isa person;` - **Attribute ownership**: `$x has $name;` or `$x has name "John";` - **Anonymous Relation participation**: `friendship (friend: $x, friend: $y);` - **Variablized relation participation**: `$rel isa friendship, links (friend: $x, friend: $y);` - Shorthand form: `$rel isa friendship (friend: $x, friend: $y);` - **Value comparison operation**: `$x > 25;`. Comparing two variables will ignore attribute type if the variables hold attributes and not primitive values. - **Concept comparison**: use `is` to do exact equality between concepts, eg `$x is $y;`. This will _not_ ignore attribute type if the variables hold attributes. Be careful: when a relation type **relates** a role, you can query data instances with links player syntax as the linking variable: `$relation-instance links (role: $player)`. However, when a relation type (or any other type) **plays** a role, then the instances are queried with links player syntax with the variable **inside** the parenthesis: `$other-relation-instance links (role: $relation-instance)`. #### Basic schema statements - **Entity, relation or attribute type**: `entity $entity-type`; `relation $relation-type`; `attribute $attribute-type` - **Label**: `$type-variable label