@@ -39,6 +39,9 @@ class BaseAggregation(ABC):
39
39
Base class representing an Aggregation operation in Datastore
40
40
"""
41
41
42
+ def __init__ (self , alias = None ):
43
+ self .alias = alias
44
+
42
45
@abc .abstractmethod
43
46
def _to_pb (self ):
44
47
"""
@@ -59,7 +62,7 @@ class CountAggregation(BaseAggregation):
59
62
"""
60
63
61
64
def __init__ (self , alias = None ):
62
- self . alias = alias
65
+ super ( CountAggregation , self ). __init__ ( alias = alias )
63
66
64
67
def _to_pb (self ):
65
68
"""
@@ -71,6 +74,60 @@ def _to_pb(self):
71
74
return aggregation_pb
72
75
73
76
77
+ class SumAggregation (BaseAggregation ):
78
+ """
79
+ Representation of a "Sum" aggregation query.
80
+
81
+ :type property_ref: str
82
+ :param property_ref: The property_ref for the aggregation.
83
+
84
+ :type value: int
85
+ :param value: The resulting value from the aggregation.
86
+
87
+ """
88
+
89
+ def __init__ (self , property_ref , alias = None ):
90
+ self .property_ref = property_ref
91
+ super (SumAggregation , self ).__init__ (alias = alias )
92
+
93
+ def _to_pb (self ):
94
+ """
95
+ Convert this instance to the protobuf representation
96
+ """
97
+ aggregation_pb = query_pb2 .AggregationQuery .Aggregation ()
98
+ aggregation_pb .sum = query_pb2 .AggregationQuery .Aggregation .Sum ()
99
+ aggregation_pb .sum .property .name = self .property_ref
100
+ aggregation_pb .alias = self .alias
101
+ return aggregation_pb
102
+
103
+
104
+ class AvgAggregation (BaseAggregation ):
105
+ """
106
+ Representation of a "Avg" aggregation query.
107
+
108
+ :type property_ref: str
109
+ :param property_ref: The property_ref for the aggregation.
110
+
111
+ :type value: int
112
+ :param value: The resulting value from the aggregation.
113
+
114
+ """
115
+
116
+ def __init__ (self , property_ref , alias = None ):
117
+ self .property_ref = property_ref
118
+ super (AvgAggregation , self ).__init__ (alias = alias )
119
+
120
+ def _to_pb (self ):
121
+ """
122
+ Convert this instance to the protobuf representation
123
+ """
124
+ aggregation_pb = query_pb2 .AggregationQuery .Aggregation ()
125
+ aggregation_pb .avg = query_pb2 .AggregationQuery .Aggregation .Avg ()
126
+ aggregation_pb .avg .property .name = self .property_ref
127
+ aggregation_pb .alias = self .alias
128
+ return aggregation_pb
129
+
130
+
74
131
class AggregationResult (object ):
75
132
"""
76
133
A class representing result from Aggregation Query
@@ -154,6 +211,28 @@ def count(self, alias=None):
154
211
self ._aggregations .append (count_aggregation )
155
212
return self
156
213
214
+ def sum (self , property_ref , alias = None ):
215
+ """
216
+ Adds a sum over the nested query
217
+
218
+ :type property_ref: str
219
+ :param property_ref: The property_ref for the sum
220
+ """
221
+ sum_aggregation = SumAggregation (property_ref = property_ref , alias = alias )
222
+ self ._aggregations .append (sum_aggregation )
223
+ return self
224
+
225
+ def avg (self , property_ref , alias = None ):
226
+ """
227
+ Adds a avg over the nested query
228
+
229
+ :type property_ref: str
230
+ :param property_ref: The property_ref for the sum
231
+ """
232
+ avg_aggregation = AvgAggregation (property_ref = property_ref , alias = alias )
233
+ self ._aggregations .append (avg_aggregation )
234
+ return self
235
+
157
236
def add_aggregation (self , aggregation ):
158
237
"""
159
238
Adds an aggregation operation to the nested query
@@ -327,8 +406,7 @@ def _build_protobuf(self):
327
406
"""
328
407
pb = self ._aggregation_query ._to_pb ()
329
408
if self ._limit is not None and self ._limit > 0 :
330
- for aggregation in pb .aggregations :
331
- aggregation .count .up_to = self ._limit
409
+ pb .nested_query .limit = self ._limit
332
410
return pb
333
411
334
412
def _process_query_results (self , response_pb ):
@@ -438,5 +516,8 @@ def _item_to_aggregation_result(iterator, pb):
438
516
:rtype: :class:`google.cloud.datastore.aggregation.AggregationResult`
439
517
:returns: The list of AggregationResults
440
518
"""
441
- results = [AggregationResult (alias = k , value = pb [k ].integer_value ) for k in pb .keys ()]
519
+ results = [
520
+ AggregationResult (alias = k , value = pb [k ].integer_value or pb [k ].double_value )
521
+ for k in pb .keys ()
522
+ ]
442
523
return results
0 commit comments