-
Notifications
You must be signed in to change notification settings - Fork 62
feat: Add CompoundMeasureValueFilter #1304
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
Merged
+202
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,9 @@ | |
| from gooddata_api_client.models import ( | ||
| ComparisonMeasureValueFilterComparisonMeasureValueFilter as ComparisonMeasureValueFilterBody, | ||
| ) | ||
| from gooddata_api_client.models import ( | ||
| CompoundMeasureValueFilterCompoundMeasureValueFilter as CompoundMeasureValueFilterBody, | ||
| ) | ||
| from gooddata_api_client.models import NegativeAttributeFilterNegativeAttributeFilter as NegativeAttributeFilterBody | ||
| from gooddata_api_client.models import PositiveAttributeFilterPositiveAttributeFilter as PositiveAttributeFilterBody | ||
| from gooddata_api_client.models import RangeMeasureValueFilterRangeMeasureValueFilter as RangeMeasureValueFilterBody | ||
|
|
@@ -483,6 +486,105 @@ def description(self, labels: dict[str, str], format_locale: Optional[str] = Non | |
| ) | ||
|
|
||
|
|
||
| @attrs.define(frozen=True, slots=True) | ||
| class MetricValueComparisonCondition: | ||
| operator: str | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using StrEnum or Literal. I suspect that the values will not change. |
||
| value: Union[int, float] | ||
|
|
||
| def as_api_model(self) -> afm_models.MeasureValueCondition: | ||
| comparison = afm_models.ComparisonConditionComparison( | ||
| operator=self.operator, | ||
| value=float(self.value), | ||
| _check_type=False, | ||
| ) | ||
| return afm_models.MeasureValueCondition(comparison=comparison, _check_type=False) | ||
|
|
||
| def description(self) -> str: | ||
| return f"{_METRIC_VALUE_FILTER_OPERATOR_LABEL.get(self.operator, self.operator)} {float(self.value)}" | ||
|
|
||
|
|
||
| @attrs.define(frozen=True, slots=True) | ||
| class MetricValueRangeCondition: | ||
| operator: str | ||
| from_value: Union[int, float] | ||
| to_value: Union[int, float] | ||
|
|
||
| def as_api_model(self) -> afm_models.MeasureValueCondition: | ||
| range_body = afm_models.RangeConditionRange( | ||
| _from=float(self.from_value), | ||
| operator=self.operator, | ||
| to=float(self.to_value), | ||
| _check_type=False, | ||
| ) | ||
| return afm_models.MeasureValueCondition(range=range_body, _check_type=False) | ||
|
|
||
| def description(self) -> str: | ||
| not_between = "not" if self.operator == "NOT_BETWEEN" else "" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on usage in the example below, maybe "not " (missing space)? |
||
| return f"{not_between}between {float(self.from_value)} - {float(self.to_value)}" | ||
|
|
||
|
|
||
| MetricValueCondition = Union[MetricValueComparisonCondition, MetricValueRangeCondition] | ||
|
|
||
|
|
||
| class CompoundMetricValueFilter(Filter): | ||
| """ | ||
| Compound measure value filter. | ||
|
|
||
| Semantics match backend `CompoundMeasureValueFilter`: multiple conditions combined with OR logic. | ||
|
|
||
| Note: | ||
| - If `conditions` is empty, the filter is a noop (all rows are returned). | ||
| - `treat_nulls_as` is applied at the filter level (same for all conditions). | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| metric: Union[ObjId, str, Metric], | ||
| conditions: list[MetricValueCondition], | ||
| treat_nulls_as: Union[float, None] = None, | ||
| ) -> None: | ||
| super().__init__() | ||
| self._metric = _extract_id_or_local_id(metric) | ||
| self._conditions = conditions | ||
| self._treat_nulls_as = treat_nulls_as | ||
|
|
||
| @property | ||
| def metric(self) -> Union[ObjId, str]: | ||
| return self._metric | ||
|
|
||
| @property | ||
| def conditions(self) -> list[MetricValueCondition]: | ||
| return self._conditions | ||
|
|
||
| @property | ||
| def treat_nulls_as(self) -> Union[float, None]: | ||
| return self._treat_nulls_as | ||
|
|
||
| def is_noop(self) -> bool: | ||
| return len(self.conditions) == 0 | ||
|
|
||
| def as_api_model(self) -> afm_models.CompoundMeasureValueFilter: | ||
| measure = _to_identifier(self._metric) | ||
|
|
||
| kwargs: dict[str, Any] = dict( | ||
| measure=measure, | ||
| conditions=[c.as_api_model() for c in self.conditions], | ||
| _check_type=False, | ||
| ) | ||
| if self.treat_nulls_as is not None: | ||
| kwargs["treat_null_values_as"] = self.treat_nulls_as | ||
|
|
||
| body = CompoundMeasureValueFilterBody(**kwargs) | ||
| return afm_models.CompoundMeasureValueFilter(body, _check_type=False) | ||
|
|
||
| def description(self, labels: dict[str, str], format_locale: Optional[str] = None) -> str: | ||
| metric_id = self.metric.id if isinstance(self.metric, ObjId) else self.metric | ||
| if not self.conditions: | ||
| return f"{labels.get(metric_id, metric_id)}: All" | ||
| conditions_str = " OR ".join([c.description() for c in self.conditions]) | ||
| return f"{labels.get(metric_id, metric_id)}: {conditions_str}" | ||
|
|
||
|
|
||
| _RANKING_OPERATORS = {"TOP", "BOTTOM"} | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/gooddata-sdk/tests/compute_model/test_compound_metric_value_filter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # (C) 2026 GoodData Corporation | ||
| from __future__ import annotations | ||
|
|
||
| from gooddata_sdk import ( | ||
| CompoundMetricValueFilter, | ||
| MetricValueComparisonCondition, | ||
| MetricValueRangeCondition, | ||
| ) | ||
| from gooddata_sdk.compute.model.base import ObjId | ||
|
|
||
|
|
||
| def test_compound_metric_value_filter_to_api_model(): | ||
| f = CompoundMetricValueFilter( | ||
| metric="local_id1", | ||
| conditions=[ | ||
| MetricValueComparisonCondition(operator="GREATER_THAN", value=10), | ||
| MetricValueRangeCondition(operator="BETWEEN", from_value=2, to_value=3), | ||
| ], | ||
| treat_nulls_as=0, | ||
| ) | ||
|
|
||
| assert f.is_noop() is False | ||
| assert f.as_api_model().to_dict() == { | ||
| "compound_measure_value_filter": { | ||
| "conditions": [ | ||
| {"comparison": {"operator": "GREATER_THAN", "value": 10.0}}, | ||
| {"range": {"_from": 2.0, "operator": "BETWEEN", "to": 3.0}}, | ||
| ], | ||
| "measure": {"local_identifier": "local_id1"}, | ||
| "treat_null_values_as": 0, | ||
| } | ||
| } | ||
|
|
||
|
|
||
| def test_compound_metric_value_filter_noop_when_no_conditions(): | ||
| f = CompoundMetricValueFilter(metric=ObjId(type="metric", id="metric.id"), conditions=[]) | ||
| assert f.is_noop() is True |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplification:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I get this one, this would change the behaviour or not?
For non-compound filters, f becomes {} and the code would then hit f["measure"] (or similar) and fail with a KeyError, instead of cleanly falling through to the other filter types.
It also hides bugs: defaulting to {} makes “missing key” look like “present but empty”.