Skip to content

Commit ae1b8f7

Browse files
authored
Merge pull request #284051 from seesharprun/cosmos-computed-properties-data-explorer
Cosmos DB | Data Explorer steps for computed properties
2 parents d0a107e + caafd64 commit ae1b8f7

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

articles/cosmos-db/nosql/query/computed-properties.md

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.service: azure-cosmos-db
99
ms.subservice: nosql
1010
ms.topic: reference
1111
ms.devlang: nosql
12-
ms.date: 02/27/2024
12+
ms.date: 08/08/2024
1313
ms.custom: query-reference
1414
---
1515

@@ -59,21 +59,21 @@ The constraints on computed property names are:
5959

6060
### Query constraints
6161

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.
6363

6464
The limitations on computed property query definitions are:
6565

6666
- 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`.
6767
- Queries must use a VALUE clause in the projection.
6868
- 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.
7070
- Queries can't use any of the following clauses: WHERE, GROUP BY, ORDER BY, TOP, DISTINCT, OFFSET LIMIT, EXISTS, ALL, LAST, FIRST, and NONE.
7171
- Queries can't include a scalar subquery.
7272
- Aggregate functions, spatial functions, nondeterministic functions, and user defined functions (UDFs) aren't supported.
7373

7474
## Create computed properties
7575

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.
7777

7878
| | Supported version | Notes |
7979
| --- | --- | --- |
@@ -142,7 +142,7 @@ const { container: containerWithComputedProperty } = await database.containers.c
142142

143143
### [Python](#tab/python)
144144

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.
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.
146146

147147
```python
148148
computed_properties = [{'name': "cp_lower", 'query': "SELECT VALUE LOWER(c.db_group) FROM c"},
@@ -152,14 +152,14 @@ computed_properties = [{'name': "cp_lower", 'query': "SELECT VALUE LOWER(c.db_gr
152152
container_with_computed_props = db.create_container_if_not_exists(
153153
"myContainer", PartitionKey(path="/pk"), computed_properties=computed_properties)
154154
```
155+
155156
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:
156157

157158
```python
158159
queried_items = list(
159160
container_with_computed_props.query_items(query='Select * from c Where c.cp_power = 25', partition_key="test"))
160161
```
161162

162-
163163
---
164164

165165
Here's an example of how to update computed properties on an existing container:
@@ -218,20 +218,44 @@ console.log("Container definition is undefined.");
218218
```
219219

220220
### [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.
222223

223224
---
224225

225226
> [!TIP]
226227
> 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.
227228
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+
228252
## Use computed properties in queries
229253

230254
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.
231255

232256
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.
233257

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":::
235259

236260
Here's an example of an item:
237261

@@ -359,14 +383,14 @@ ORDER BY
359383

360384
## Index computed properties
361385

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.
363387

364388
There are a few considerations for indexing computed properties, including:
365389

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 indexed property must also be dropped
370394

371395
> [!NOTE]
372396
> 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:
379403
>
380404
> If you want to delete a computed property, you'll first need to remove it from the index policy.
381405
382-
383406
### Add a single index for computed properties
384407

385408
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
437460

438461
## Understand request unit consumption
439462

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.
441464

442465
## Related content
443466

22.9 KB
Loading

0 commit comments

Comments
 (0)