If the preceding stages generate output in nondeterministic order and $setWindowFields has a default ‘partitionBy’ and ‘sortBy’ a non-existing field, $setWindowFields could generate inconsistent result every time the same pipeline is executed.
Due to the same reason, disabling/enabling query optimization could also cause $setWindowFields producing inconsistent result. For example in BF-20745, consider a pipeline [*$bucket + $setWindowFields]* which will be desugared to the following pipeline: [ $group + $sort ({“_id”: 1}) + $sort + $_internalSetWindowFields ]. The two adjacent $sort stages will be optimized to a single $sort by removing the first $sort. However, if the second $sort sorted a non-existing field, $_internalSetWindowFields would still get a non-deterministic order of input because $group does not order its output.
It seems not a legit $setWindowFields bug, but it’s failing in some certain agg-fuzzers, e.g. aggregation_wildcard_fuzzer and aggregation_optimization_fuzzer.