Skip to content

Commit f9adb5d

Browse files
committed
update UDF guidance
1 parent ebb2984 commit f9adb5d

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,31 @@ description: Learn about User-defined functions in Azure Cosmos DB.
44
author: markjbrown
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 05/31/2019
7+
ms.date: 04/09/2020
88
ms.author: mjbrown
9-
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 in the `WHERE` clause of your query. UDF's do not utilize the index so evaluating the UDF will require loading documents.
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
{

0 commit comments

Comments
 (0)