Officially out now: The TypeDB 3.0 Roadmap >>

With stage

The with stage in a data pipeline allows the local declaration of function, which can be used as part of the pipeline.

Syntax

with <function-declaration>
... # continue pipeline

where <function-declaration> is a function declaration.

Behavior

  • The with stages in a data pipeline must precede any other kind of stage (together, all with stages are referred to as the pipeline preamble)

  • with stages must be followed by other data pipeline stages, i.e. with stages by themselves do not consitute a valid data pipeline.

  • Function declarations in the preamble of a data pipeline are only valid within that pipeline (declared functions may be called in match and fetch stages).

Validation

The usual function validation applies to the function declaration in a with clause.

Examples

  1. Find users with more friends than a specific user and return their usernames

    with fun friend_count($user: user) -> long:
      match friendship($user, $x)
      return count;
    match
      $user1 isa user, has username "<name>";
      $user2 isa user, has username $name;
      friend_count($user1) < friend_count($user2);
    select $name;
  2. Return all “popular” users in the database

    with fun friend_count($user: user) -> long:
      match friendship($user, $x)
      return count;
    with fun popular_users() -> { user }:
      match
        $x isa user;
        friend_count($x) > 500;
      return { $x };
    match
      let $y in popular_users();
      $y has username $pop_name;
    select $pop_name;