@@ -119,11 +119,10 @@ def query(self, compiler, connection, lookup_name=None):
119
119
for col , i in subquery_compiler .column_indices .items ()
120
120
},
121
121
}
122
+ wrapping_result_pipeline = None
122
123
# The result must be a list of values. The output is compressed with an
123
124
# aggregation pipeline.
124
125
if lookup_name in ("in" , "range" ):
125
- if subquery .aggregation_pipeline is None :
126
- subquery .aggregation_pipeline = []
127
126
wrapping_result_pipeline = [
128
127
{
129
128
"$facet" : {
@@ -155,12 +154,49 @@ def query(self, compiler, connection, lookup_name=None):
155
154
}
156
155
},
157
156
]
157
+ if lookup_name == "overlap" :
158
+ wrapping_result_pipeline = [
159
+ {
160
+ "$facet" : {
161
+ "group" : [
162
+ {"$project" : {"tmp_name" : expr .as_mql (subquery_compiler , connection )}},
163
+ {
164
+ "$unwind" : "$tmp_name" ,
165
+ },
166
+ {
167
+ "$group" : {
168
+ "_id" : None ,
169
+ "tmp_name" : {"$addToSet" : "$tmp_name" },
170
+ }
171
+ },
172
+ ]
173
+ }
174
+ },
175
+ {
176
+ "$project" : {
177
+ field_name : {
178
+ "$ifNull" : [
179
+ {
180
+ "$getField" : {
181
+ "input" : {"$arrayElemAt" : ["$group" , 0 ]},
182
+ "field" : "tmp_name" ,
183
+ }
184
+ },
185
+ [],
186
+ ]
187
+ }
188
+ }
189
+ },
190
+ ]
191
+ if wrapping_result_pipeline :
158
192
# If the subquery is a combinator, wrap the result at the end of the
159
193
# combinator pipeline...
160
194
if subquery .query .combinator :
161
195
subquery .combinator_pipeline .extend (wrapping_result_pipeline )
162
196
# ... otherwise put at the end of subquery's pipeline.
163
197
else :
198
+ if subquery .aggregation_pipeline is None :
199
+ subquery .aggregation_pipeline = []
164
200
subquery .aggregation_pipeline .extend (wrapping_result_pipeline )
165
201
# Erase project_fields since the required value is projected above.
166
202
subquery .project_fields = None
0 commit comments