typedb / typeql

TypeQL: the power of programming, in your database
https://typedb.com
Mozilla Public License 2.0
218 stars 45 forks source link

Implement full fetch specification #369

Closed flyingsilverfin closed 3 weeks ago

flyingsilverfin commented 1 month ago

Usage and product changes

We refactor Fetch and Function behaviour, to allow any of the following Fetching patterns:

match
...
fetch {
    // fetch a matched attribute, value, or type. Represented as a attribute/value/type or null (if the variable is optional).
    "single variable": $a,   

    // attribute 'age' of $x. Must be `@card(0..1)` or `@card(1..1)`. Represented as an attribute or null.
    "single-card attributes": $x.age,

    // all attributes 'name' of $x. Can be any cardinality.
    "list higher-card attributes": [ $x.name ], 

    // inline-computed expression value. Represented as a value.
    "single value expression": $a + 1,  

    // an inline query with a 'return' block to select a *single* answer. Represented identically to a single variable or null.
    "single answer block": (  
        match
        $x has name $name;
        return first $name;
    ),

    // an inline query with a 'return' block to reduce to a *single* answer. Represented as a value or null
    "reduce answer block": ( 
        match
        $x has name $name;
        return count($name);
    ),

    // an inline query that returns a stream of lists/tuples. Represented as list of lists.
    "list positional return block": [  
        match
        $x has name $n,
            has age $a;
        return { $n, $a };
    ],

    // an inline query that returns stream of sub-documents. Represented as a list of objects.
    "list pipeline": [ 
        match
        $x has name $n,
            has age $a;
        fetch {
            "name": $n
        };
    ],

    // special syntax to fetch all attributes of a concept. Represented as an object, where keys are attribute type names and values are either lists (for >card(1)) or nullable values (for card(0..1) or card(1..1))
    "all attributes": { $x.* }
}

Implementation

typedb-bot commented 1 month ago

PR Review Checklist

Do not edit the content of this comment. The PR reviewer should simply update this comment by ticking each review item below, as they get completed.


Trivial Change

Code

Architecture