Skip to content

Commit 08bfda9

Browse files
committed
docs(InstanceSort): add docstring with examples and index-alignment tip
1 parent b395b02 commit 08bfda9

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

cognite/client/data_classes/data_modeling/instances.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,59 @@ class InstancesApply:
13441344

13451345

13461346
class InstanceSort(DataModelingSort):
1347+
"""Sort order for an instance query.
1348+
1349+
Args:
1350+
property (list[str] | tuple[str, str] | tuple[str, str, str]): The property to sort by, given as a path, e.g.
1351+
``("mySpace", "myView/v1", "myProperty")`` or ``["node", "externalId"]``.
1352+
direction (Literal['ascending', 'descending']): Sort direction. Case-insensitive. Defaults to ``"ascending"``.
1353+
nulls_first (bool | None): Where to place ``null`` values. Defaults to ``None`` (auto). See tip below.
1354+
1355+
Tip:
1356+
For the backend database to use an index when sorting nullable properties, the ``nulls_first`` setting
1357+
must match the sort direction:
1358+
1359+
- ``ascending`` → nulls last (``nulls_first=False``)
1360+
- ``descending`` → nulls first (``nulls_first=True``)
1361+
1362+
When ``nulls_first=None`` (the default), the correct value is chosen automatically. Passing the
1363+
opposite combination is still accepted and sent to the API as-is, but may trigger a warning
1364+
for API endpoints that support index utilization if an unsupported combination is used.
1365+
1366+
Examples:
1367+
1368+
Sort by a view property ascending (default):
1369+
1370+
>>> from cognite.client.data_classes.data_modeling import InstanceSort
1371+
>>> sort = InstanceSort(("mySpace", "myView/v1", "myProperty"))
1372+
1373+
Can also use a ViewId to simplify the property path:
1374+
1375+
>>> from cognite.client.data_classes.data_modeling import ViewId
1376+
>>> view_id = ViewId("mySpace", "myView", "v1")
1377+
>>> sort = InstanceSort(view_id.as_property_ref("myProperty"))
1378+
1379+
Sort descending:
1380+
1381+
>>> sort = InstanceSort(
1382+
... view_id.as_property_ref("myProperty"),
1383+
... direction="descending",
1384+
... )
1385+
1386+
Sort by a base property:
1387+
1388+
>>> sort = InstanceSort(["node", "externalId"], direction="ascending")
1389+
1390+
Force a specific null placement (first/last). A UserWarning will fire at relevant API call
1391+
sites when this conflicts with index alignment:
1392+
1393+
>>> sort = InstanceSort(
1394+
... ("mySpace", "myView/v1", "myProperty"),
1395+
... direction="descending",
1396+
... nulls_first=True,
1397+
... )
1398+
"""
1399+
13471400
def __init__(
13481401
self,
13491402
property: list[str] | tuple[str, str] | tuple[str, str, str],

0 commit comments

Comments
 (0)