@@ -82,75 +82,6 @@ class Desc(SortDirection):
82
82
DIRSTRING = "DESC"
83
83
84
84
85
- class Group :
86
- """
87
- This object automatically created in the `AggregateRequest.group_by()`
88
- """
89
-
90
- def __init__ (self , fields , reducers ):
91
- if not reducers :
92
- raise ValueError ("Need at least one reducer" )
93
-
94
- fields = [fields ] if isinstance (fields , str ) else fields
95
- reducers = [reducers ] if isinstance (reducers , Reducer ) else reducers
96
-
97
- self .fields = fields
98
- self .reducers = reducers
99
- self .limit = Limit ()
100
-
101
- def build_args (self ):
102
- ret = ["GROUPBY" , str (len (self .fields ))]
103
- ret .extend (self .fields )
104
- for reducer in self .reducers :
105
- ret += ["REDUCE" , reducer .NAME , str (len (reducer .args ))]
106
- ret .extend (reducer .args )
107
- if reducer ._alias is not None :
108
- ret += ["AS" , reducer ._alias ]
109
- return ret
110
-
111
-
112
- class Projection :
113
- """
114
- This object automatically created in the `AggregateRequest.apply()`
115
- """
116
-
117
- def __init__ (self , projector , alias = None ):
118
- self .alias = alias
119
- self .projector = projector
120
-
121
- def build_args (self ):
122
- ret = ["APPLY" , self .projector ]
123
- if self .alias is not None :
124
- ret += ["AS" , self .alias ]
125
-
126
- return ret
127
-
128
-
129
- class SortBy :
130
- """
131
- This object automatically created in the `AggregateRequest.sort_by()`
132
- """
133
-
134
- def __init__ (self , fields , max = 0 ):
135
- self .fields = fields
136
- self .max = max
137
-
138
- def build_args (self ):
139
- fields_args = []
140
- for f in self .fields :
141
- if isinstance (f , SortDirection ):
142
- fields_args += [f .field , f .DIRSTRING ]
143
- else :
144
- fields_args += [f ]
145
-
146
- ret = ["SORTBY" , str (len (fields_args ))]
147
- ret .extend (fields_args )
148
- if self .max > 0 :
149
- ret += ["MAX" , str (self .max )]
150
-
151
- return ret
152
-
153
-
154
85
class AggregateRequest :
155
86
"""
156
87
Aggregation request which can be passed to `Client.aggregate`.
@@ -202,9 +133,17 @@ def group_by(self, fields, *reducers):
202
133
- **reducers**: One or more reducers. Reducers may be found in the
203
134
`aggregation` module.
204
135
"""
205
- group = Group (fields , reducers )
206
- self . _aggregateplan . extend ( group . build_args ())
136
+ fields = [ fields ] if isinstance (fields , str ) else fields
137
+ reducers = [ reducers ] if isinstance ( reducers , Reducer ) else reducers
207
138
139
+ ret = ["GROUPBY" , str (len (fields )), * fields ]
140
+ for reducer in reducers :
141
+ ret += ["REDUCE" , reducer .NAME , str (len (reducer .args ))]
142
+ ret .extend (reducer .args )
143
+ if reducer ._alias is not None :
144
+ ret += ["AS" , reducer ._alias ]
145
+
146
+ self ._aggregateplan .extend (ret )
208
147
return self
209
148
210
149
def apply (self , ** kwexpr ):
@@ -218,8 +157,10 @@ def apply(self, **kwexpr):
218
157
expression itself, for example `apply(square_root="sqrt(@foo)")`
219
158
"""
220
159
for alias , expr in kwexpr .items ():
221
- projection = Projection (expr , alias )
222
- self ._aggregateplan .extend (projection .build_args ())
160
+ ret = ["APPLY" , expr ]
161
+ if alias is not None :
162
+ ret += ["AS" , alias ]
163
+ self ._aggregateplan .extend (ret )
223
164
224
165
return self
225
166
@@ -265,8 +206,7 @@ def limit(self, offset, num):
265
206
`sort_by()` instead.
266
207
267
208
"""
268
- limit = Limit (offset , num )
269
- self ._limit = limit
209
+ self ._limit = Limit (offset , num )
270
210
return self
271
211
272
212
def sort_by (self , * fields , ** kwargs ):
@@ -300,10 +240,20 @@ def sort_by(self, *fields, **kwargs):
300
240
if isinstance (fields , (str , SortDirection )):
301
241
fields = [fields ]
302
242
243
+ fields_args = []
244
+ for f in fields :
245
+ if isinstance (f , SortDirection ):
246
+ fields_args += [f .field , f .DIRSTRING ]
247
+ else :
248
+ fields_args += [f ]
249
+
250
+ ret = ["SORTBY" , str (len (fields_args ))]
251
+ ret .extend (fields_args )
303
252
max = kwargs .get ("max" , 0 )
304
- sortby = SortBy (fields , max )
253
+ if max > 0 :
254
+ ret += ["MAX" , str (max )]
305
255
306
- self ._aggregateplan .extend (sortby . build_args () )
256
+ self ._aggregateplan .extend (ret )
307
257
return self
308
258
309
259
def filter (self , expressions ):
0 commit comments