Skip to content

Commit b18d257

Browse files
committed
Add documentation for using sets as arguments.
1 parent e246a6d commit b18d257

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

docs/datamodel/functions.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,48 @@ This function accepts a :eql:type:`str` as an argument and produces a
3434
3535
.. _ref_datamodel_functions_modifying:
3636

37+
Sets as arguments
38+
^^^^^^^^^^^^^^^^^
39+
40+
Calling a user-defined function on a set will always apply it as
41+
:ref:`*element-wise* <_ref_reference_cardinality_functions_operators>`.
42+
43+
.. code-block:: sdl
44+
45+
function magnitude(x: float64) -> float64
46+
using (
47+
math::sqrt(sum(x * x))
48+
);
49+
50+
.. code-block:: edgeql-repl
51+
52+
db> select magnitude({3, 4});
53+
{3, 4}
54+
55+
In order to pass in multiple arguments at once, arguments should be packed into
56+
arrays:
57+
58+
.. code-block:: sdl
59+
60+
function magnitude(xs: array<float64>) -> float64
61+
using (
62+
with x := array_unpack(xs)
63+
select math::sqrt(sum(x * x))
64+
);
65+
66+
.. code-block:: edgeql-repl
67+
68+
db> select magnitude([3, 4]);
69+
{5}
70+
71+
Multiple packed arrays can be passed into such a function, which will then be
72+
applied element-wise.
73+
74+
.. code-block:: edgeql-repl
75+
76+
db> select magnitude({[3, 4], [5, 12]});
77+
{5, 13}
78+
3779
Modifying Functions
3880
^^^^^^^^^^^^^^^^^^^
3981

@@ -93,6 +135,18 @@ then the following queries are valid:
93135
db> select add_user('May', <datetime>'2024-12-11T12:00:00-07:00') {name, joined_at};
94136
{default::User {name: 'May', joined_at: <datetime>'2024-12-11T12:00:00Z'}}
95137
138+
In order to insert or update a multi parameter, the desired arguments should be
139+
aggregated into an array as described above:
140+
141+
.. code-block:: sdl
142+
143+
function add_user(name: str, nicknames: array<str>) -> User
144+
using (
145+
insert User {
146+
name := name,
147+
nicknames := array_unpack(nicknames),
148+
}
149+
);
96150
97151
.. list-table::
98152
:class: seealso

docs/reference/edgeql/cardinality.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ instance, when assigning to a ``required multi`` link, the value being
2222
assigned in question *must* have a cardinality of ``One`` or ``AtLeastOne``
2323
(as empty sets are not permitted).
2424

25+
.. _ref_reference_cardinality_functions_operators:
26+
2527
Functions and operators
2628
-----------------------
2729

docs/reference/sdl/functions.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ This declaration defines a new constraint with the following options:
101101
argument as a *whole set*, as opposed to being called on the input
102102
product element-by-element.
103103

104+
User defined functions can not use ``set of`` arguments.
105+
104106
The ``optional`` qualifier indicates that the function will be called
105107
if the argument is an empty set. The default behavior is to return
106108
an empty set if the argument is not marked as ``optional``.

0 commit comments

Comments
 (0)