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. .Schema for the following examples

Details
#!test[schema, commit]
define
  entity content owns id;
  entity page sub content,
    owns page-id,
    owns name;
  entity profile sub page,
    owns username;
  entity user sub profile,
    owns phone,
    owns karma;

  attribute id value string;
  attribute page-id sub id;
  attribute username sub page-id;
  attribute name value string;
  attribute phone value string;
  attribute karma value double;

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.

#!test[schema, commit]
define
  fun user_phones($user: user) -> { phone }:
    match
      $user has phone $phone;
    return { $phone };
#!test[schema, commit]
define
  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:

#!test[read]
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.

#!test[schema, commit]
define
  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:

#!test[read]
match
  let $user, $phone, $phone-value in all_users_and_phones();