Skip to content

Commit

Permalink
Add documentation for using sets as arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnwpark committed Jan 8, 2025
1 parent e246a6d commit b18d257
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/datamodel/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,48 @@ This function accepts a :eql:type:`str` as an argument and produces a
.. _ref_datamodel_functions_modifying:

Sets as arguments
^^^^^^^^^^^^^^^^^

Calling a user-defined function on a set will always apply it as
:ref:`*element-wise* <_ref_reference_cardinality_functions_operators>`.

.. code-block:: sdl
function magnitude(x: float64) -> float64
using (
math::sqrt(sum(x * x))
);
.. code-block:: edgeql-repl
db> select magnitude({3, 4});
{3, 4}
In order to pass in multiple arguments at once, arguments should be packed into
arrays:

.. code-block:: sdl
function magnitude(xs: array<float64>) -> float64
using (
with x := array_unpack(xs)
select math::sqrt(sum(x * x))
);
.. code-block:: edgeql-repl
db> select magnitude([3, 4]);
{5}
Multiple packed arrays can be passed into such a function, which will then be
applied element-wise.

.. code-block:: edgeql-repl
db> select magnitude({[3, 4], [5, 12]});
{5, 13}
Modifying Functions
^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -93,6 +135,18 @@ then the following queries are valid:
db> select add_user('May', <datetime>'2024-12-11T12:00:00-07:00') {name, joined_at};
{default::User {name: 'May', joined_at: <datetime>'2024-12-11T12:00:00Z'}}
In order to insert or update a multi parameter, the desired arguments should be
aggregated into an array as described above:

.. code-block:: sdl
function add_user(name: str, nicknames: array<str>) -> User
using (
insert User {
name := name,
nicknames := array_unpack(nicknames),
}
);
.. list-table::
:class: seealso
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/edgeql/cardinality.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ instance, when assigning to a ``required multi`` link, the value being
assigned in question *must* have a cardinality of ``One`` or ``AtLeastOne``
(as empty sets are not permitted).

.. _ref_reference_cardinality_functions_operators:

Functions and operators
-----------------------

Expand Down
2 changes: 2 additions & 0 deletions docs/reference/sdl/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ This declaration defines a new constraint with the following options:
argument as a *whole set*, as opposed to being called on the input
product element-by-element.

User defined functions can not use ``set of`` arguments.

The ``optional`` qualifier indicates that the function will be called
if the argument is an empty set. The default behavior is to return
an empty set if the argument is not marked as ``optional``.
Expand Down

0 comments on commit b18d257

Please sign in to comment.