You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/nosql/query/computed-properties.md
+38-15Lines changed: 38 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ ms.service: azure-cosmos-db
9
9
ms.subservice: nosql
10
10
ms.topic: reference
11
11
ms.devlang: nosql
12
-
ms.date: 02/27/2024
12
+
ms.date: 08/08/2024
13
13
ms.custom: query-reference
14
14
---
15
15
@@ -59,21 +59,21 @@ The constraints on computed property names are:
59
59
60
60
### Query constraints
61
61
62
-
Queries in the computed property definition must be valid syntactically and semantically, otherwise the create or update operation fails. Queries should evaluate to a deterministic value for all items in a container. Queries may evaluate to undefined or null for some items, and computed properties with undefined or null values behave the same as persisted properties with undefined or null values when used in queries.
62
+
Queries in the computed property definition must be valid syntactically and semantically, otherwise the create or update operation fails. Queries should evaluate to a deterministic value for all items in a container. Queries might evaluate to undefined or null for some items, and computed properties with undefined or null values behave the same as persisted properties with undefined or null values when used in queries.
63
63
64
64
The limitations on computed property query definitions are:
65
65
66
66
- Queries must specify a FROM clause that represents the root item reference. Examples of supported FROM clauses are: `FROM c`, `FROM root c`, and `FROM MyContainer c`.
67
67
- Queries must use a VALUE clause in the projection.
68
68
- Queries can't include a JOIN.
69
-
- Queries can't use non-deterministic Scalar expressions. Examples of non-deterministic scalar expressions are: GetCurrentDateTime, GetCurrentTimeStamp, GetCurrentTicks, and RAND.
69
+
- Queries can't use nondeterministic Scalar expressions. Examples of nondeterministic scalar expressions are: GetCurrentDateTime, GetCurrentTimeStamp, GetCurrentTicks, and RAND.
70
70
- Queries can't use any of the following clauses: WHERE, GROUP BY, ORDER BY, TOP, DISTINCT, OFFSET LIMIT, EXISTS, ALL, LAST, FIRST, and NONE.
71
71
- Queries can't include a scalar subquery.
72
72
- Aggregate functions, spatial functions, nondeterministic functions, and user defined functions (UDFs) aren't supported.
73
73
74
74
## Create computed properties
75
75
76
-
After the computed properties are created, you can execute queries that reference the properties by using any method, including all SDKs and Azure Data Explorer in the Azure portal.
76
+
After the computed properties are created, you can execute queries that reference the properties by using any method, including all software development kits (SDKs) and Azure Data Explorer in the Azure portal.
You can define multiple computed properties in a list and then add them to the container properties. Python SDK currently doesn't support computed properties on existing containers.
145
+
You can define multiple computed properties in a list and then add them to the container properties. Python SDK currently doesn't support computed properties on existing containers.
146
146
147
147
```python
148
148
computed_properties = [{'name': "cp_lower", 'query': "SELECT VALUE LOWER(c.db_group) FROM c"},
Computed properties can be used like any other property in queries. For example, you can use the computed property `cp_lower` in a query like this:
156
157
157
158
```python
158
159
queried_items =list(
159
160
container_with_computed_props.query_items(query='Select * from c Where c.cp_power = 25', partition_key="test"))
160
161
```
161
162
162
-
163
163
---
164
164
165
165
Here's an example of how to update computed properties on an existing container:
@@ -218,20 +218,44 @@ console.log("Container definition is undefined.");
218
218
```
219
219
220
220
### [Python](#tab/python)
221
-
Updating computed properties on an existing container is not supported in Python SDK. You can only define computed properties when creating a new container. This is a work in progress currently.
221
+
222
+
Updating computed properties on an existing container isn't supported in Python SDK. You can only define computed properties when creating a new container.
222
223
223
224
---
224
225
225
226
> [!TIP]
226
227
> Every time you update container properties, the old values are overwritten. If you have existing computed properties and want to add new ones, be sure that you add both new and existing computed properties to the collection.
227
228
229
+
### Create computed properties by using the Data Explorer
230
+
231
+
You can use the Data Explorer to create a computed property for a container.
232
+
233
+
1. Open your existing container in the Data Explorer.
234
+
235
+
1. Navigate to the **Settings** section for your container. Then, navigate to the **Computed Properties* subsection.
236
+
237
+
1. Edit the computed properties definition JSON for your container. In this example, this JSON is used to define a computed property to split the `SKU` string for a retail product using the `-` delimiter.
238
+
239
+
```json
240
+
[
241
+
{
242
+
"name": "cp_splitSku",
243
+
"query": "SELECT VALUE StringSplit(p.sku, \"-\") FROM products p"
244
+
}
245
+
]
246
+
```
247
+
248
+
:::image type="content" source="media/computed-properties/data-explorer-editor.png" alt-text="Screenshot of the computed properties JSON editor in the Data Explorer interface.":::
249
+
250
+
1. **Save** the computed property.
251
+
228
252
## Use computed properties in queries
229
253
230
254
Computed properties can be referenced in queries the same way that persisted properties are referenced. Values for computed properties that aren't indexed are evaluated during runtime by using the computed property definition. If a computed property is indexed, the index is used the same way that it's used for persisted properties, and the computed property is evaluated on an as-needed basis. We recommend that you [add indexes on your computed properties](#index-computed-properties) for the best cost and performance.
231
255
232
256
The following examples use the quickstart products dataset that's available in [Data Explorer](../../data-explorer.md) in the Azure portal. To get started, select **Launch the quick start** and load the dataset in a new container.
233
257
234
-
:::image type="content" source="./media/computed-properties/data-explorer-quickstart-data.png" alt-text="Screenshot that shows how to begin the quickstart to load a sample dataset in the Azure portal." border="false":::
258
+
:::image type="content" source="./media/computed-properties/data-explorer-home.png" alt-text="Screenshot illustrating how to load a sample data set into a database and container." border="false":::
235
259
236
260
Here's an example of an item:
237
261
@@ -359,14 +383,14 @@ ORDER BY
359
383
360
384
## Index computed properties
361
385
362
-
Computed properties aren't indexed by default and aren't covered by wildcard paths in the [indexing policy](../../index-policy.md). You can add single or composite indexes on computed properties in the indexing policy the same way you would add indexes on persisted properties. We recommend that you add relevant indexes to all computed properties. We recommend these indexes because they're beneficial in increasing performance and reducing RUs when they're indexed. When computed properties are indexed, actual values are evaluated during item write operations to generate and persist index terms.
386
+
Computed properties aren't indexed by default and aren't covered by wildcard paths in the [indexing policy](../../index-policy.md). You can add single or composite indexes on computed properties in the indexing policy the same way you would add indexes on persisted properties. We recommend that you add relevant indexes to all computed properties. We recommend these indexes because they're beneficial in increasing performance and reducing request units (RUs). When computed properties are indexed, actual values are evaluated during item write operations to generate and persist index terms.
363
387
364
388
There are a few considerations for indexing computed properties, including:
365
389
366
-
- Computed properties can be specified in included paths, excluded paths, and composite index paths.
367
-
- Computed properties can't have a spatial index defined on them.
368
-
- Wildcard paths under the computed property path work like they do for regular properties.
369
-
-If you're removing a computed property that has been indexed, all indexes on that property must also be dropped.
390
+
- Computed properties can be specified in included paths, excluded paths, and composite index paths
391
+
- Computed properties can't have a spatial index defined on them
392
+
- Wildcard paths under the computed property path work like they do for regular properties
393
+
-Related indexes on a removed and indexedproperty must also be dropped
370
394
371
395
> [!NOTE]
372
396
> All computed properties are defined at the top level of the item. The path is always `/<computed property name>`.
@@ -379,7 +403,6 @@ There are a few considerations for indexing computed properties, including:
379
403
>
380
404
> If you want to delete a computed property, you'll first need to remove it from the index policy.
381
405
382
-
383
406
### Add a single index for computed properties
384
407
385
408
To add a single index for a computed property named `cp_myComputedProperty`:
@@ -437,7 +460,7 @@ To add a composite index on two properties in which, one is computed as `cp_myCo
437
460
438
461
## Understand request unit consumption
439
462
440
-
Adding computed properties to a container doesn't consume RUs. Write operations on containers that have computed properties defined might have a slight RU increase. If a computed property is indexed, RUs on write operations increase to reflect the costs for indexing and evaluation of the computed property.
463
+
Adding computed properties to a container doesn't consume RUs. Write operations on containers that have computed properties defined might have a slight RU increase. If a computed property is indexed, RUs on write operations increase to reflect the costs for indexing and evaluation of the computed property.
0 commit comments