Skip to content

Commit c2fb9aa

Browse files
authored
[PYG-426] 🐛 Sorting nested bug (#525)
# Description **Reviewer**: All code inside `examples/` is generated. This is the a small but urgent fix needed for AtlasAI and Impact. ## Bump - [x] Patch - [ ] Minor - [ ] Skip ## Changelog ### Fixed - [alpha] When calling the QueryExecutor.list(...) with sort and nested properties the results are now sorted.
1 parent d42997f commit c2fb9aa

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

cognite/pygen/_query/interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def _execute_list(
322322
for prop in factory.reverse_properties.values()
323323
if isinstance(prop.through.source, dm.ViewId)
324324
}
325-
builder.append(factory.root(filter, limit=limit))
325+
builder.append(factory.root(filter, limit=limit, sort=self._as_sort_list(sort)))
326326
for connection_id, connection in factory.connection_properties.items():
327327
builder.extend(factory.from_connection(connection_id, connection, reverse_views))
328328
executor = builder.build()

tests/test_integration/test_query_execution.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from cognite.client import CogniteClient
55
from cognite.client import data_modeling as dm
66
from cognite.client.data_classes import filters
7+
from cognite.client.data_classes.data_modeling import InstanceSort
78
from omni import OmniClient
89
from omni import data_classes as dc
910

@@ -84,6 +85,7 @@ def test_query_direct_relation(cognite_client: CogniteClient, omni_views: dict[s
8485

8586
def test_query_edge_outwards(cognite_client: CogniteClient, omni_views: dict[str, dm.View]) -> None:
8687
item_a = omni_views["ConnectionItemA"]
88+
view_A_id = item_a.as_id()
8789
item_b = omni_views["ConnectionItemB"]
8890
executor = QueryExecutor(cognite_client, views=[item_a, item_b], unpack_edges="include")
8991
properties: list[str | dict[str, Any]] = [
@@ -92,7 +94,12 @@ def test_query_edge_outwards(cognite_client: CogniteClient, omni_views: dict[str
9294
{"outwards": [{"node": ["name", "externalId"]}, "type"]},
9395
]
9496
flatten_props = {"name", "outwards", "externalId"}
95-
result = executor.list(item_a.as_id(), properties, limit=5)
97+
result = executor.list(
98+
view_A_id,
99+
properties,
100+
sort=InstanceSort(view_A_id.as_property_ref("name"), direction="descending", nulls_first=True),
101+
limit=5,
102+
)
96103
assert isinstance(result, list)
97104
assert len(result) > 0
98105
ill_formed_items = [item for item in result if not (set(item.keys()) <= flatten_props)]
@@ -106,6 +113,8 @@ def test_query_edge_outwards(cognite_client: CogniteClient, omni_views: dict[str
106113
]
107114
assert not ill_formed_subitems, f"Subitems with unexpected properties: {ill_formed_subitems}"
108115

116+
assert result == sorted(result, key=lambda x: (int("name" not in x), x.get("name", "")), reverse=True)
117+
109118

110119
def test_query_edge_outwards_skip_edge(cognite_client: CogniteClient, omni_views: dict[str, dm.View]) -> None:
111120
item_a = omni_views["ConnectionItemA"]

0 commit comments

Comments
 (0)