Skip to content

Commit

Permalink
Fix ordering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasG0 committed Jan 8, 2025
1 parent 24270c2 commit f5a8a5f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
41 changes: 19 additions & 22 deletions backend/infrahub/core/query/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,6 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
self.order_by = []

self.return_labels = ["n.uuid", "rb.branch", f"{db.get_id_function_name()}(rb) as rb_id"]
where_clause_elements = []

branch_filter, branch_params = self.branch.get_query_filter_path(
at=self.at, branch_agnostic=self.branch_agnostic
Expand Down Expand Up @@ -911,18 +910,15 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
""" % {"branch_filter": branch_filter, "node_kind": self.schema.kind}
self.add_to_query(topquery)

use_simple = False
if self.has_filter_by_id and self.filters:
use_simple = True
where_clause_elements.append("n.uuid = $uuid")
self.params["uuid"] = self.filters["id"]
elif not self.has_filters and not self.schema.order_by:
use_simple = True
if self.order is None or self.order.disable is not True:
self.order_by = ["n.uuid"]
if use_simple:
if where_clause_elements:
self.add_to_query(" AND " + " AND ".join(where_clause_elements))
self.add_to_query(" AND n.uuid = $uuid")
return

disable_order = not self.schema.order_by or (self.order is not None and self.order.disable)
if not self.has_filters and disable_order:
# Always order by uuid to guarantee pagination.
self.order_by = ["n.uuid"]
return

if self.filters and "ids" in self.filters:
Expand All @@ -934,11 +930,21 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
await self._add_node_filter_attributes(
db=db, field_attribute_requirements=field_attribute_requirements, branch_filter=branch_filter
)
should_order = self.schema.order_by and (self.order is None or self.order.disable is not True)
if should_order:

if not disable_order:
await self._add_node_order_attributes(
db=db, field_attribute_requirements=field_attribute_requirements, branch_filter=branch_filter
)
for far in field_attribute_requirements:
if not far.is_order:
continue
if far.supports_profile:
self.order_by.append(far.final_value_query_variable)
continue
self.order_by.append(far.node_value_query_variable)

# Always order by uuid to guarantee pagination.
self.order_by.append("n.uuid")

if use_profiles:
await self._add_profiles_per_node_query(db=db, branch_filter=branch_filter)
Expand All @@ -948,15 +954,6 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
await self._add_profile_rollups(field_attribute_requirements=field_attribute_requirements)

self._add_final_filter(field_attribute_requirements=field_attribute_requirements)
if should_order:
for far in field_attribute_requirements:
if not far.is_order:
continue
if far.supports_profile:
self.order_by.append(far.final_value_query_variable)
continue
self.order_by.append(far.node_value_query_variable)
self.order_by.append("n.uuid")

async def _add_node_filter_attributes(
self,
Expand Down
1 change: 0 additions & 1 deletion backend/tests/query_benchmark/test_node_get_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ async def init_and_execute():
ordering=ordering,
)
res = await query.execute(db=db_profiling_queries)
print(f"{len(res.get_node_ids())=}")
return res

nb_cars = 10_000
Expand Down

0 comments on commit f5a8a5f

Please sign in to comment.