Skip to content

feat(InstanceSort): auto-align nulls_first for index utilization, warn on mismatch#2613

Open
haakonvt wants to merge 8 commits into
masterfrom
auto-instance-sort-nulls-first
Open

feat(InstanceSort): auto-align nulls_first for index utilization, warn on mismatch#2613
haakonvt wants to merge 8 commits into
masterfrom
auto-instance-sort-nulls-first

Conversation

@haakonvt
Copy link
Copy Markdown
Contributor

@haakonvt haakonvt commented May 8, 2026

Summary

  • InstanceSort now validates direction at construction time (case-insensitive, raises ValueError on bad input)
  • nulls_first=None (default) is resolved lazily at PostgreSQL-backed call sites (list, query, sync, subscribe) via the new _apply_postgres_defaults_or_maybe_warn() method, keeping the wire format clean for Elasticsearch-backed endpoints (search, aggregate)
  • A UserWarning is emitted when an explicit nulls_first value conflicts with index alignment for the given direction
  • New is_index_aligned property for inspecting alignment without side effects
  • Query classes (Query, QuerySync) gain _iter_sorts() traversal and _prepare_sorts() to apply defaults across all nested sorts in one call

Note 2 reviewer:

Validation and auto-conversion from None to True/False was initially put on InstanceSort itself, but there is an existing None-check on instances.search(...) raising ValueError if set to non-null, so that's why this grew a bit out of proportions and had to be added to all "API call sites hitting postgres"

@haakonvt haakonvt requested review from a team as code owners May 8, 2026 09:52
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces automatic nulls_first derivation for InstanceSort to optimize PostgreSQL index utilization and adds UserWarning alerts for non-aligned sort configurations across query, sync, and subscribe operations. Feedback indicates that always setting a boolean for nulls_first causes a regression in instances.search(), which explicitly disallows the parameter and will now raise a ValueError.

Comment thread cognite/client/data_classes/data_modeling/instances.py Outdated
@haakonvt haakonvt marked this pull request as draft May 8, 2026 11:04
@haakonvt haakonvt changed the title feat(InstanceSort): auto-derive nulls_first and warn on index-misaligned sorts DRAFT feat(InstanceSort): auto-derive nulls_first and warn on index-misaligned sorts May 8, 2026
@haakonvt haakonvt force-pushed the auto-instance-sort-nulls-first branch from 63a94ff to 12103ea Compare May 8, 2026 19:48
@haakonvt haakonvt changed the title DRAFT feat(InstanceSort): auto-derive nulls_first and warn on index-misaligned sorts feat(InstanceSort): auto-align nulls_first for index utilization, warn on mismatch May 8, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 8, 2026

Codecov Report

❌ Patch coverage is 99.39759% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.06%. Comparing base (ed1ce44) to head (835cba8).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
...ite/client/data_classes/data_modeling/instances.py 95.65% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2613      +/-   ##
==========================================
+ Coverage   93.02%   93.06%   +0.04%     
==========================================
  Files         486      486              
  Lines       49462    49634     +172     
==========================================
+ Hits        46010    46192     +182     
+ Misses       3452     3442      -10     
Files with missing lines Coverage Δ
cognite/client/_api/data_modeling/instances.py 90.65% <100.00%> (+3.43%) ⬆️
...ognite/client/_sync_api/data_modeling/instances.py 99.00% <ø> (ø)
cognite/client/data_classes/data_modeling/query.py 93.77% <100.00%> (+0.33%) ⬆️
...st_data_classes/test_data_models/test_instances.py 100.00% <100.00%> (ø)
...ite/client/data_classes/data_modeling/instances.py 91.23% <95.65%> (+0.09%) ⬆️

... and 12 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@haakonvt haakonvt marked this pull request as ready for review May 8, 2026 20:00
@haakonvt
Copy link
Copy Markdown
Contributor Author

haakonvt commented May 8, 2026

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces automatic PostgreSQL index alignment for instance sorting in data modeling. It enhances the InstanceSort class to automatically set or validate the nulls_first parameter based on the sort direction, ensuring optimal database performance. Additionally, it implements a traversal mechanism within the Query class to apply these defaults across all nested sort configurations. I have no feedback to provide.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces automatic PostgreSQL index alignment for InstanceSort by resolving nulls_first based on the sort direction and providing warnings for misaligned configurations. It includes updates to the instances API to prepare sorts before execution and adds corresponding unit tests. Feedback was provided regarding the mutation of InstanceSort objects in the _apply_postgres_defaults_or_maybe_warn method, which may cause compatibility issues with Elasticsearch-based search operations if the sort object is reused.

Comment on lines +1433 to +1440
def _apply_postgres_defaults_or_maybe_warn(self) -> None:
"""Resolve nulls_first for PostgreSQL index alignment, warning if the explicit value is misaligned.

When nulls_first is None, sets it to the index-compatible value. When explicitly set but
misaligned, emits a UserWarning.
"""
if self.nulls_first is None:
self.nulls_first = self.direction == "descending"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The _apply_postgres_defaults_or_maybe_warn method mutates the InstanceSort object by assigning a value to self.nulls_first when it is None. This side effect can cause subsequent calls to instances.search() to fail with a ValueError if the same InstanceSort object is reused, because search() (which uses Elasticsearch) explicitly prohibits non-null nulls_first values (see cognite/client/_api/data_modeling/instances.py:1311).

To avoid this, consider resolving the default value during the payload construction phase in the API client, or providing a way to get the resolved value without modifying the instance state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout, will consider

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gemini-code-assist I've adressed this now, please do a re-review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for addressing the concerns regarding object mutation and the potential side effects on instances.search(). The implementation of _get_query_with_defaults_applied() using deepcopy and the targeted application of defaults across query traversals effectively resolves the issue while maintaining the integrity of the original InstanceSort objects. The addition of comprehensive unit tests covering these scenarios is also appreciated.

Comment thread cognite/client/data_classes/data_modeling/instances.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants