Skip to content

Commit d54d3d0

Browse files
authored
Merge pull request #110865 from timsander1/master
update UDF guidance
2 parents 2de1857 + 8f41971 commit d54d3d0

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

articles/cosmos-db/sql-query-udfs.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
---
22
title: User-defined functions (UDFs) in Azure Cosmos DB
33
description: Learn about User-defined functions in Azure Cosmos DB.
4-
author: markjbrown
4+
author: timsander1
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 05/31/2019
8-
ms.author: mjbrown
9-
7+
ms.date: 04/09/2020
8+
ms.author: tisande
109
---
1110

1211
# User-defined functions (UDFs) in Azure Cosmos DB
1312

1413
The SQL API provides support for user-defined functions (UDFs). With scalar UDFs, you can pass in zero or many arguments and return a single argument result. The API checks each argument for being legal JSON values.
1514

16-
The API extends the SQL syntax to support custom application logic using UDFs. You can register UDFs with the SQL API, and reference them in SQL queries. In fact, the UDFs are exquisitely designed to call from queries. As a corollary, UDFs do not have access to the context object like other JavaScript types, such as stored procedures and triggers. Queries are read-only, and can run either on primary or secondary replicas. UDFs, unlike other JavaScript types, are designed to run on secondary replicas.
15+
## UDF use cases
1716

18-
The following example registers a UDF under an item container in the Cosmos database. The example creates a UDF whose name is `REGEX_MATCH`. It accepts two JSON string values, `input` and `pattern`, and checks if the first matches the pattern specified in the second using JavaScript's `string.match()` function.
17+
The API extends the SQL syntax to support custom application logic using UDFs. You can register UDFs with the SQL API, and reference them in SQL queries. Unlike stored procedures and triggers, UDFs are read-only.
18+
19+
Using UDFs, you can extend Azure Cosmos DB's query language. UDFs are a great way to express complex business logic in a query's projection.
20+
21+
However, we recommending avoiding UDFs when:
22+
23+
- An equivalent [system function](sql-query-system-functions.md) already exists in Azure Cosmos DB. System functions will always use fewer RU's than the equivalent UDF.
24+
- The UDF is the only filter in the `WHERE` clause of your query. UDF's do not utilize the index so evaluating the UDF will require loading documents. Combining additional filter predicates that use the index, in combination with a UDF, in the `WHERE` clause will reduce the number of documents processed by the UDF.
25+
26+
If you must use the same UDF multiple times in a query, you should reference the UDF in a [subquery](sql-query-subquery.md#evaluate-once-and-reference-many-times), allowing you to use a JOIN expression to evaluate the UDF once but reference it many times.
1927

2028
## Examples
2129

30+
The following example registers a UDF under an item container in the Cosmos database. The example creates a UDF whose name is `REGEX_MATCH`. It accepts two JSON string values, `input` and `pattern`, and checks if the first matches the pattern specified in the second using JavaScript's `string.match()` function.
31+
2232
```javascript
2333
UserDefinedFunction regexMatchUdf = new UserDefinedFunction
2434
{

articles/cosmos-db/stored-procedures-triggers-udfs.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: Work with stored procedures, triggers, and UDFs in Azure Cosmos DB
33
description: This article introduces the concepts such as stored procedures, triggers, and user-defined functions in Azure Cosmos DB.
4-
author: markjbrown
4+
author: timsander1
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 08/01/2019
8-
ms.author: mjbrown
7+
ms.date: 04/09/2020
8+
ms.author: tisande
99
ms.reviewer: sngun
1010

1111
---
@@ -65,7 +65,7 @@ Stored procedures and triggers are always executed on the primary replica of an
6565

6666
All Azure Cosmos DB operations must complete within the specified timeout duration. This constraint applies to JavaScript functions - stored procedures, triggers, and user-defined functions. If an operation does not complete within that time limit, the transaction is rolled back.
6767

68-
You can either ensure that your JavaScript functions finish within the time limit or implement a continuation-based model to batch/resume execution. In order to simplify development of stored procedures and triggers to handle time limits, all functions under the Azure Cosmos container (for example, create, read, update, and delete of items) return a boolean value that represents whether that operation will complete. If this value is false, it is an indication that the procedure must wrap up execution because the script is consuming more time or provisioned throughput than the configured value. Operations queued prior to the first unaccepted store operation are guaranteed to complete if the stored procedure completes in time and does not queue any more requests. Thus, operations should be queued one at a time by using JavaScript’s callback convention to manage the script’s control flow. Because scripts are executed in a server-side environment, they are strictly governed. Scripts that repeatedly violate execution boundaries may be marked inactive and can't be executed, and they should be recreated to honor the execution boundaries.
68+
You can either ensure that your JavaScript functions finish within the time limit or implement a continuation-based model to batch/resume execution. In order to simplify development of stored procedures and triggers to handle time limits, all functions under the Azure Cosmos container (for example, create, read, update, and delete of items) return a boolean value that represents whether that operation will complete. If this value is false, it is an indication that the procedure must wrap up execution because the script is consuming more time or provisioned throughput than the configured value. Operations queued prior to the first unaccepted store operation are guaranteed to complete if the stored procedure completes in time and does not queue any more requests. Thus, operations should be queued one at a time by using JavaScript's callback convention to manage the script's control flow. Because scripts are executed in a server-side environment, they are strictly governed. Scripts that repeatedly violate execution boundaries may be marked inactive and can't be executed, and they should be recreated to honor the execution boundaries.
6969

7070
JavaScript functions are also subject to [provisioned throughput capacity](request-units.md). JavaScript functions could potentially end up using a large number of request units within a short time and may be rate-limited if the provisioned throughput capacity limit is reached. It is important to note that scripts consume additional throughput in addition to the throughput spent executing database operations, although these database operations are slightly less expensive than executing the same operations from the client.
7171

@@ -79,14 +79,14 @@ Azure Cosmos DB provides triggers that can be invoked by performing an operation
7979

8080
### Post-triggers
8181

82-
Similar to pre-triggers, post-triggers, are also associated with an operation on an Azure Cosmos item and they dont require any input parameters. They run *after* the operation has completed and have access to the response message that is sent to the client. For examples, see [How to write triggers](how-to-write-stored-procedures-triggers-udfs.md#triggers) article.
82+
Similar to pre-triggers, post-triggers, are also associated with an operation on an Azure Cosmos item and they don't require any input parameters. They run *after* the operation has completed and have access to the response message that is sent to the client. For examples, see [How to write triggers](how-to-write-stored-procedures-triggers-udfs.md#triggers) article.
8383

8484
> [!NOTE]
8585
> Registered triggers don't run automatically when their corresponding operations (create / delete / replace / update) happen. They have to be explicitly called when executing these operations. To learn more, see [how to run triggers](how-to-use-stored-procedures-triggers-udfs.md#pre-triggers) article.
8686
8787
## <a id="udfs"></a>User-defined functions
8888

89-
User-defined functions (UDFs) are used to extend the SQL API query language syntax and implement custom business logic easily. They can be called only within queries. UDFs do not have access to the context object and are meant to be used as compute only JavaScript. Therefore, UDFs can be run on secondary replicas. For examples, see [How to write user-defined functions](how-to-write-stored-procedures-triggers-udfs.md#udfs) article.
89+
[User-defined functions](sql-query-udfs.md) (UDFs) are used to extend the SQL API query language syntax and implement custom business logic easily. They can be called only within queries. UDFs do not have access to the context object and are meant to be used as compute only JavaScript. Therefore, UDFs can be run on secondary replicas. For examples, see [How to write user-defined functions](how-to-write-stored-procedures-triggers-udfs.md#udfs) article.
9090

9191
## <a id="jsqueryapi"></a>JavaScript language-integrated query API
9292

0 commit comments

Comments
 (0)