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

feat: Add support for Sum and Avg aggregation query #437

Merged
merged 21 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
90 changes: 86 additions & 4 deletions google/cloud/datastore/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class BaseAggregation(ABC):
Base class representing an Aggregation operation in Datastore
"""

def __init__(self, alias=None):
self.alias = alias

@abc.abstractmethod
def _to_pb(self):
"""
Expand All @@ -59,7 +62,7 @@ class CountAggregation(BaseAggregation):
"""

def __init__(self, alias=None):
self.alias = alias
super(CountAggregation, self).__init__(alias=alias)

def _to_pb(self):
"""
Expand All @@ -71,6 +74,61 @@ def _to_pb(self):
return aggregation_pb


class SumAggregation(BaseAggregation):
"""
Representation of a "Sum" aggregation query.

:type property_ref: str
:param property_ref: The property_ref for the aggregation.

:type value: int
:param value: The resulting value from the aggregation.

"""

def __init__(self, property_ref, alias=None):
self.property_ref = property_ref
super(SumAggregation, self).__init__(alias=alias)

def _to_pb(self):
"""
Convert this instance to the protobuf representation
"""
aggregation_pb = query_pb2.AggregationQuery.Aggregation()
aggregation_pb.alias = self.alias
aggregation_pb.sum = query_pb2.AggregationQuery.Aggregation.Sum()
aggregation_pb.sum.property.name = self.property_ref
aggregation_pb.alias = self.alias
Copy link
Contributor

Choose a reason for hiding this comment

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

is this duplicated? (see line 98)

Copy link
Contributor

Choose a reason for hiding this comment

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

good catch!

return aggregation_pb


class AvgAggregation(BaseAggregation):
"""
Representation of a "Avg" aggregation query.

:type property_ref: str
:param property_ref: The property_ref for the aggregation.

:type value: int
:param value: The resulting value from the aggregation.

"""

def __init__(self, property_ref, alias=None):
self.property_ref = property_ref
super(AvgAggregation, self).__init__(alias=alias)

def _to_pb(self):
"""
Convert this instance to the protobuf representation
"""
aggregation_pb = query_pb2.AggregationQuery.Aggregation()
aggregation_pb.avg = query_pb2.AggregationQuery.Aggregation.Avg()
aggregation_pb.avg.property.name = self.property_ref
aggregation_pb.alias = self.alias
return aggregation_pb


class AggregationResult(object):
"""
A class representing result from Aggregation Query
Expand Down Expand Up @@ -154,6 +212,28 @@ def count(self, alias=None):
self._aggregations.append(count_aggregation)
return self

def sum(self, property_ref, alias=None):
"""
Adds a sum over the nested query

:type property_ref: str
:param property_ref: The property_ref for the sum
"""
sum_aggregation = SumAggregation(property_ref=property_ref, alias=alias)
self._aggregations.append(sum_aggregation)
return self

def avg(self, property_ref, alias=None):
"""
Adds a avg over the nested query

:type property_ref: str
:param property_ref: The property_ref for the sum
"""
avg_aggregation = AvgAggregation(property_ref=property_ref, alias=alias)
self._aggregations.append(avg_aggregation)
return self

def add_aggregation(self, aggregation):
"""
Adds an aggregation operation to the nested query
Expand Down Expand Up @@ -327,8 +407,7 @@ def _build_protobuf(self):
"""
pb = self._aggregation_query._to_pb()
if self._limit is not None and self._limit > 0:
for aggregation in pb.aggregations:
aggregation.count.up_to = self._limit
pb.nested_query.limit = self._limit
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
return pb

def _process_query_results(self, response_pb):
Expand Down Expand Up @@ -438,5 +517,8 @@ def _item_to_aggregation_result(iterator, pb):
:rtype: :class:`google.cloud.datastore.aggregation.AggregationResult`
:returns: The list of AggregationResults
"""
results = [AggregationResult(alias=k, value=pb[k].integer_value) for k in pb.keys()]
results = [
AggregationResult(alias=k, value=pb[k].integer_value or pb[k].double_value)
for k in pb.keys()
]
return results
2 changes: 1 addition & 1 deletion tests/system/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ indexes:
- name: family
- name: appearances


- kind: Character
ancestor: yes
properties:
- name: family
- name: appearances

Loading