@@ -369,6 +369,80 @@ def test_contains_subquery(self):
369369 self .objs [1 :3 ],
370370 )
371371
372+ def test_contained_by_subquery (self ):
373+ IntegerArrayModel .objects .create (field = [2 , 3 ])
374+ inner_qs = IntegerArrayModel .objects .values_list ("field" , flat = True )
375+ self .assertSequenceEqual (
376+ NullableIntegerArrayModel .objects .filter (field__contained_by = inner_qs [:1 ]),
377+ self .objs [1 :3 ],
378+ )
379+ IntegerArrayModel .objects .create (field = [2 ])
380+ inner_qs = IntegerArrayModel .objects .filter (field__contained_by = OuterRef ("field" ))
381+ # fails with "both operands of $setIsSubset must be arrays. Second
382+ # argument is of type: null"
383+ self .assertSequenceEqual (
384+ NullableIntegerArrayModel .objects .filter (Exists (inner_qs )),
385+ self .objs [1 :3 ],
386+ )
387+
388+ # [
389+ # {
390+ # "$lookup": {
391+ # "as": "__subquery0",
392+ # "from": "model_fields__integerarraymodel",
393+ # "let": {"parent__field__0": "$field"},
394+ # "pipeline": [
395+ # {
396+ # "$match": {
397+ # "$expr": {
398+ # "$and": [
399+ # {"$ne": ["$field", None]},
400+ # {"$ne": ["$$parent__field__0", None]},
401+ # {"$setIsSubset": ["$field", "$$parent__field__0"]},
402+ # ]
403+ # }
404+ # }
405+ # },
406+ # {"$project": {"a": {"$literal": 1}}},
407+ # {"$limit": 1},
408+ # ],
409+ # }
410+ # },
411+ # {
412+ # "$set": {
413+ # "__subquery0": {
414+ # "$cond": {
415+ # "if": {
416+ # "$or": [
417+ # {"$eq": [{"$type": "$__subquery0"}, "missing"]},
418+ # {"$eq": [{"$size": "$__subquery0"}, 0]},
419+ # ]
420+ # },
421+ # "then": {},
422+ # "else": {"$arrayElemAt": ["$__subquery0", 0]},
423+ # }
424+ # }
425+ # }
426+ # },
427+ # {
428+ # "$match": {
429+ # "$expr": {
430+ # "$eq": [
431+ # {
432+ # "$not": {
433+ # "$or": [
434+ # {"$eq": [{"$type": "$__subquery0.a"}, "missing"]},
435+ # {"$eq": ["$__subquery0.a", None]},
436+ # ]
437+ # }
438+ # },
439+ # True,
440+ # ]
441+ # }
442+ # }
443+ # },
444+ # ]
445+
372446 def test_contains_including_expression (self ):
373447 self .assertSequenceEqual (
374448 NullableIntegerArrayModel .objects .filter (
0 commit comments