Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PYG-350] Aggregate when filter return empty #2124

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Changes are grouped as follows
- Support for the `/simulators/models` and `/simulators/models/revisions` API endpoints.
- Support for the `/simulators` and `/simulators/integration` API endpoints.

## [7.73.5] - 2025-02-24
### Fixed
- An issue with `client.data_modeling.instances.aggregates(..., filter=my_filter)` no longer raises a `KeyError` if you
have a filter that returns no results.

## [7.73.4] - 2025-02-24
### Fixed
- An issue with `DatapointsAPI.retrieve_latest` and usage of `instance_id` when using `ignore_unknown_ids=True`
Expand Down
2 changes: 1 addition & 1 deletion cognite/client/_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations

__version__ = "7.73.4"
__version__ = "7.73.5"

__api_subversion__ = "20230101"
22 changes: 10 additions & 12 deletions cognite/client/data_classes/aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ def _load(cls, resource: dict[str, Any], cognite_client: CogniteClient | None =
aggregate = resource["aggregate"]

if aggregate == "avg":
deserialized: Any = AvgValue(property=resource["property"], value=resource["value"])
deserialized: Any = AvgValue(property=resource["property"], value=resource.get("value"))
elif aggregate == "count":
deserialized = CountValue(property=resource["property"], value=resource["value"])
deserialized = CountValue(property=resource["property"], value=resource.get("value"))
elif aggregate == "max":
deserialized = MaxValue(property=resource["property"], value=resource["value"])
deserialized = MaxValue(property=resource["property"], value=resource.get("value"))
elif aggregate == "min":
deserialized = MinValue(property=resource["property"], value=resource["value"])
deserialized = MinValue(property=resource["property"], value=resource.get("value"))
elif aggregate == "sum":
deserialized = SumValue(property=resource["property"], value=resource["value"])
deserialized = SumValue(property=resource["property"], value=resource.get("value"))
elif aggregate == "histogram":
deserialized = HistogramValue(
property=resource["property"], interval=resource["interval"], buckets=resource["buckets"]
Expand All @@ -149,21 +149,19 @@ def _load(cls, resource: dict[str, Any], cognite_client: CogniteClient | None =
return cast(Self, deserialized)

def dump(self, camel_case: bool = True) -> dict[str, Any]:
output = {"aggregate": self._aggregate, "property": self.property}
if camel_case:
output = convert_all_keys_recursive(output)
return output
return {"aggregate": self._aggregate, "property": self.property}

Choose a reason for hiding this comment

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

This change isn't described by your PR description. Is it accidental, or deliberately but undocumented?



@dataclass
class AggregatedNumberedValue(AggregatedValue, ABC):
_aggregate: ClassVar[str] = "number"

value: float
value: float | None

def dump(self, camel_case: bool = True) -> dict[str, Any]:
output = super().dump(camel_case)
output["value"] = self.value
if self.value is not None:
output["value"] = self.value
return output


Expand All @@ -177,7 +175,7 @@ class AvgValue(AggregatedNumberedValue):
@dataclass
class CountValue(AggregatedNumberedValue):
_aggregate: ClassVar[str] = "count"
value: int
value: int | None


@final
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "cognite-sdk"

version = "7.73.4"
version = "7.73.5"

description = "Cognite Python SDK"
readme = "README.md"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,8 @@ def test_aggregate_multiple(self, cognite_client: CogniteClient, movie_view: Vie
avg_value = next((item for item in result if isinstance(item, aggregations.AvgValue)), None)
assert max_value is not None
assert avg_value is not None
assert isinstance(max_value.value, float)
assert isinstance(avg_value.value, float)
assert max_value.value > avg_value.value

def test_aggregate_count_persons(self, cognite_client: CogniteClient, person_view: View) -> None:
Expand All @@ -836,6 +838,7 @@ def test_aggregate_count_persons(self, cognite_client: CogniteClient, person_vie
limit=10,
)
assert count.property == "name"
assert isinstance(count.value, int)
assert count.value > 0, "Add at least one person to the view to run this test"

def test_aggregate_invalid_view_id(self, cognite_client: CogniteClient) -> None:
Expand Down Expand Up @@ -1004,6 +1007,7 @@ def test_aggregate_in_units(

assert aggregated
assert len(aggregated) == 1
assert isinstance(aggregated[0].value, float)
assert math.isclose(aggregated[0].value, 1.1 * 1e5)

def test_query_in_units(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ def aggregated_value_load_and_dump_equals_data() -> Iterator[ParameterSet]:
},
id="histogram",
)

yield pytest.param({"aggregate": "count", "property": "instances", "value": 5}, id="count")
yield pytest.param({"aggregate": "max", "property": "instances", "value": 5}, id="max")
yield pytest.param({"aggregate": "min", "property": "instances", "value": 5}, id="min")
yield pytest.param({"aggregate": "sum", "property": "instances", "value": 5}, id="sum")
yield pytest.param({"aggregate": "avg", "property": "instances", "value": 5}, id="avg")
yield pytest.param({"aggregate": "count", "property": "instances"}, id="count empty")
yield pytest.param({"aggregate": "max", "property": "instances"}, id="max empty")
yield pytest.param({"aggregate": "min", "property": "instances"}, id="min empty")
yield pytest.param({"aggregate": "sum", "property": "instances"}, id="sum empty")
yield pytest.param({"aggregate": "avg", "property": "height"}, id="avg empty")


@pytest.mark.parametrize("raw_data", list(aggregated_value_load_and_dump_equals_data()))
Expand Down