TypeDB 3.0 is live! Get started for free.

Stream functions

Stream functions are function types which return zero or more output rows.

Syntax

Stream functions can be identified by their return type containing curly braces {}.

Scalar stream function return format
{ <return-type> }
Tuple stream function return format
{ <return-type-0>, ..., <return-type-N> }

Usage

Stream functions are defined and called like any other function. To retrieve the results of the called function, considering that the number of results for a single call is undefined, use the keyword in.

Scalar functions

Stream scalar functions are used to return multiple results for one expression: a variable or a function call. The following examples illustrate simple usages of functions for retrieving a stream of type s and a stream of integer s.

  fun user_phones($user: user) -> { phone }:
    match
      $user has phone $phone;
    return { $phone };
  fun add_streamed($x: integer, $y: integer) -> { integer }:
    match
      let $z = $x + $y;
    return { $z };

To retrieve the results of such functions calls, the following read query can be used:

match
  $user isa user;
  let $phone in user_phones($user);

Tuple functions

It is possible to add another dimension to the returned type and enumerate a number of results returned from a function using commas. Tuples can contain any combinations of scalar returned values.

  fun all_users_and_phones() -> { user, phone, string }:
    match
      $user isa user, has phone $phone;
      let $phone-value = $phone;
    return { $user, $phone, $phone-value };

To retrieve the results of such functions calls, the following read query can be used:

match
  let $user, $phone, $phone-value in all_users_and_phones();