-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update docs to cover modifying functions. #8106
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc60ce2
Update docs to cover modifying functions.
dnwpark d01cf1f
Add link to volatility.
dnwpark e246a6d
Update docs/datamodel/functions.rst
dnwpark b18d257
Add documentation for using sets as arguments.
dnwpark 6fe34ef
Fix references.
dnwpark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -32,6 +32,121 @@ This function accepts a :eql:type:`str` as an argument and produces a | |||||
test> select exclamation({'Hello', 'World'}); | ||||||
{'Hello!', 'World!'} | ||||||
|
||||||
.. _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 | ||||||
^^^^^^^^^^^^^^^^^^^ | ||||||
|
||||||
.. versionadded:: 6.0 | ||||||
|
||||||
User-defined functions can contain DML (i.e., | ||||||
:ref:`insert <ref_eql_insert>`, :ref:`update <ref_eql_update>`, | ||||||
:ref:`delete <ref_eql_delete>`) to make changes to existing data. These | ||||||
functions have a :ref:`modifying <_ref_reference_volatility>` volatility. | ||||||
|
||||||
.. code-block:: sdl | ||||||
|
||||||
function add_user(name: str) -> User | ||||||
using ( | ||||||
insert User { | ||||||
name := name, | ||||||
joined_at := std::datetime_current(), | ||||||
} | ||||||
); | ||||||
|
||||||
.. code-block:: edgeql-repl | ||||||
|
||||||
db> select add_user('Jan') {name, joined_at}; | ||||||
{default::User {name: 'Jan', joined_at: <datetime>'2024-12-11T11:49:47Z'}} | ||||||
|
||||||
Unlike other functions, the arguments of modifying functions **must** have a | ||||||
:ref:`cardinality <_ref_reference_cardinality>` of ``One``. | ||||||
|
||||||
.. code-block:: edgeql-repl | ||||||
|
||||||
db> select add_user({'Feb','Mar'}); | ||||||
edgedb error: QueryError: possibly more than one element passed into | ||||||
modifying function | ||||||
db> select add_user(<str>{}); | ||||||
edgedb error: QueryError: possibly an empty set passed as non-optional | ||||||
argument into modifying function | ||||||
|
||||||
Optional arguments can still accept empty sets. For example, if ``add_user`` | ||||||
was defined as: | ||||||
|
||||||
.. code-block:: sdl | ||||||
|
||||||
function add_user(name: str, joined_at: optional datetime) -> User | ||||||
using ( | ||||||
insert User { | ||||||
name := name, | ||||||
joined_at := joined_at ?? std::datetime_current(), | ||||||
} | ||||||
); | ||||||
|
||||||
then the following queries are valid: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not entirely sure that's customary, since it's technically the continuation of the previous sentence. |
||||||
|
||||||
.. code-block:: edgeql-repl | ||||||
|
||||||
db> select add_user('Apr', <datetime>{}) {name, joined_at}; | ||||||
{default::User {name: 'Apr', joined_at: <datetime>'2024-12-11T11:50:51Z'}} | ||||||
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 | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Practical question: if some property that you're trying to insert or update is
multi
, does that mean you'll have to pass it as an array and unpack it? Maybe we should show this as an example?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a more general section on passing in sets as arguments as well.