jstests/aggregation/sources/setWindowFields/window_functions_on_timeseries_coll.js
/**
 * Runs the given 'pipeline' and asserts that the explain behavior is as expected and the pipeline
 * returns correct results.
 *
 * The 'expectedOpts' object contains:
 * - bucketFilter:  The predicate that is mapped onto the bucket-level, otherwise 'null' if no
 *                  bucket-level filtering is expected.
 * - bucketSort:    The sort key that is expected to be pushed down, otherwise 'null'.
 * - windowSort:    The sort key that is generated by the $setWindowFields desugaring and is not
 *                  able to be pushed down.
 * - inExcludeSpec: The include or exclude list for the $_internalUnpackBucket.
 */
Runs the given 'pipeline' and asserts that the explain behavior is as expected and the pipeline
returns correct results.

The 'expectedOpts' object contains:
- bucketFilter:  The predicate that is mapped onto the bucket-level, otherwise 'null' if no
bucket-level filtering is expected.
- bucketSort:    The sort key that is expected to be pushed down, otherwise 'null'.
- windowSort:    The sort key that is generated by the $setWindowFields desugaring and is not
able to be pushed down.
- inExcludeSpec: The include or exclude list for the $_internalUnpackBucket.
Runs the given ' pipeline ' and asserts that the explain behavior is as expected and the pipeline returns correct results . The ' expectedOpts ' object contains : - bucketFilter : The predicate that is mapped onto the bucket - level otherwise ' null ' if no bucket - level filtering is expected . - bucketSort : The sort key that is expected to be pushed down otherwise ' null ' . - windowSort : The sort key that is generated by the $setWindowFields desugaring and is not able to be pushed down . - inExcludeSpec : The include or exclude list for the $_internalUnpackBucket .
function assertExplainBehaviorAndCorrectResults(pipeline, expectedOpts, expectedResults) {
    const explain = coll.explain().aggregate(pipeline);
    const winningPlan = explain.stages[0].$cursor.queryPlanner.winningPlan;
    assert.neq(null, winningPlan);
    if (expectedOpts.bucketFilter) {
        assert.eq(expectedOpts.bucketFilter, winningPlan.filter);
    }
    if (expectedOpts.bucketSort) {
        assert.eq("SORT", winningPlan.stage);
        assert.eq(expectedOpts.bucketSort, winningPlan.sortPattern);
        assert.eq(null, getAggPlanStage(explain, "$sort"));
    } else if (expectedOpts.windowSort) {
        const sort = getAggPlanStage(explain, "$sort").$sort;
        assert.neq(null, sort);
        assert.eq(expectedOpts.windowSort, sort.sortKey);
    }
    const unpackBucket = getAggPlanStage(explain, "$_internalUnpackBucket").$_internalUnpackBucket;
    assert.neq(null, unpackBucket);
    if (expectedOpts.inExcludeSpec.hasOwnProperty("include")) {
        assert.sameMembers(expectedOpts.inExcludeSpec.include, unpackBucket.include);
    } else {
        assert.sameMembers(expectedOpts.inExcludeSpec.exclude, unpackBucket.exclude);
    }
    assert(arrayEq(expectedResults, coll.aggregate(pipeline).toArray()));
}
function assertExplainBehaviorAndCorrectResults ( pipeline , expectedOpts , expectedResults ) { const explain = coll . explain ( ) . aggregate ( pipeline ) ; const winningPlan = explain . stages [ 0 ] . $cursor . queryPlanner . winningPlan ; assert . neq ( null , winningPlan ) ; if ( expectedOpts . bucketFilter ) { assert . eq ( expectedOpts . bucketFilter , winningPlan . filter ) ; } if ( expectedOpts . bucketSort ) { assert . eq ( "SORT" , winningPlan . stage ) ; assert . eq ( expectedOpts . bucketSort , winningPlan . sortPattern ) ; assert . eq ( null , getAggPlanStage ( explain , "$sort" ) ) ; } else if ( expectedOpts . windowSort ) { const sort = getAggPlanStage ( explain , "$sort" ) . $sort ; assert . neq ( null , sort ) ; assert . eq ( expectedOpts . windowSort , sort . sortKey ) ; } const unpackBucket = getAggPlanStage ( explain , "$_internalUnpackBucket" ) . $_internalUnpackBucket ; assert . neq ( null , unpackBucket ) ; if ( expectedOpts . inExcludeSpec . hasOwnProperty ( "include" ) ) { assert . sameMembers ( expectedOpts . inExcludeSpec . include , unpackBucket . include ) ; } else { assert . sameMembers ( expectedOpts . inExcludeSpec . exclude , unpackBucket . exclude ) ; } assert ( arrayEq ( expectedResults , coll . aggregate ( pipeline ) . toArray ( ) ) ) ; }
Runs the given 'pipeline' and asserts that the explain behavior is as expected and the pipeline
 returns correct results.
Runs the given ' pipeline ' and asserts that the explain behavior is as expected and the pipeline returns correct results .