jstests/aggregation/extras/window_function_helpers.js
/**
 * Runs the given 'accum' as both a window function and its equivalent accumulator in $group across
 * various combinations of partitioning and window bounds. Asserts that the results are consistent.
 *
 * Note that this function assumes that the documents in 'coll' were initialized using the
 * seedWithTickerData() method above.
 */
Runs the given 'accum' as both a window function and its equivalent accumulator in $group across
various combinations of partitioning and window bounds. Asserts that the results are consistent.

Note that this function assumes that the documents in 'coll' were initialized using the
seedWithTickerData() method above.
Runs the given ' accum ' as both a window function and its equivalent accumulator in $group across various combinations of partitioning and window bounds . Asserts that the results are consistent . Note that this function assumes that the documents in ' coll ' were initialized using the seedWithTickerData () method above .
function testAccumAgainstGroup(coll, accum, onNoResults = null, accumArgs = "$price") {
    const accumSpec = {[accum]: accumArgs};
    forEachPartitionCase(function(partition) {
        documentBounds.forEach(function(bounds, index) {
            jsTestLog("Testing accumulator " + tojson(accumSpec) + " against " + tojson(partition) +
                      " partition and [" + bounds + "] bounds");
            let outputSpec = {window: {documents: bounds}};
            Object.assign(outputSpec, accumSpec);
            const pipeline = [
                {
                    $setWindowFields:
                        {partitionBy: partition, sortBy: {_id: 1}, output: {res: outputSpec}},
                },
            ];
            const wfResults = coll.aggregate(pipeline, {allowDiskUse: true}).toArray();
            for (let index = 0; index < wfResults.length; index++) {
                const wfRes = wfResults[index];
                let indexInPartition = (partition === null) ? index : wfRes.partIndex;
                let groupRes;
                if (partition == null) {
                    groupRes = computeAsGroup({
                        coll: coll,
                        partitionKey: {},
                        accumSpec: accumSpec,
                        bounds: bounds,
                        indexInPartition: indexInPartition,
                        defaultValue: onNoResults,
                    });
                } else {
                    groupRes = computeAsGroup({
                        coll: coll,
                        partitionKey: {ticker: wfRes.ticker},
                        accumSpec: accumSpec,
                        bounds: bounds,
                        indexInPartition: indexInPartition,
                        defaultValue: onNoResults,
                    });
                }
                assertResultsEqual(wfRes, index, groupRes, accum);
            }
            assertExplainResult(
                coll.explain("executionStats").aggregate(pipeline, {allowDiskUse: true}));
            jsTestLog("Done");
        });
        forEachDocumentBoundsCombo(function(arrayOfBounds) {
            jsTestLog("Testing accumulator " + tojson(accumSpec) +
                      " against multiple bounds: " + tojson(arrayOfBounds));
            let baseSpec = {
                partitionBy: partition,
                sortBy: {_id: 1},
            };
            let outputFields = {};
            arrayOfBounds.forEach(function(bounds, index) {
                let outputSpec = {window: {documents: bounds}};
                Object.assign(outputSpec, accumSpec);
                outputFields["res" + index] = outputSpec;
            });
            let specWithOutput = Object.merge(baseSpec, {output: outputFields});
            const wfResults =
                coll.aggregate([{$setWindowFields: specWithOutput}], {allowDiskUse: true})
                    .toArray();
            assert.gt(wfResults.length, 0);
            jsTestLog("Done");
        });
    });
}
function testAccumAgainstGroup ( coll , accum , onNoResults = null , accumArgs = "$price" ) { const accumSpec = { [ accum ] : accumArgs } ; forEachPartitionCase ( function ( partition ) { documentBounds . forEach ( function ( bounds , index ) { jsTestLog ( "Testing accumulator " + tojson ( accumSpec ) + " against " + tojson ( partition ) + " partition and [" + bounds + "] bounds" ) ; let outputSpec = { window : { documents : bounds } } ; Object . assign ( outputSpec , accumSpec ) ; const pipeline = [ { $setWindowFields : { partitionBy : partition , sortBy : { _id : 1 } , output : { res : outputSpec } } , } , ] ; const wfResults = coll . aggregate ( pipeline , { allowDiskUse : true } ) . toArray ( ) ; for ( let index = 0 ; index < wfResults . length ; index ++ ) { const wfRes = wfResults [ index ] ; let indexInPartition = ( partition === null ) ? index : wfRes . partIndex ; let groupRes ; if ( partition == null ) { groupRes = computeAsGroup ( { coll : coll , partitionKey : { } , accumSpec : accumSpec , bounds : bounds , indexInPartition : indexInPartition , defaultValue : onNoResults , } ) ; } else { groupRes = computeAsGroup ( { coll : coll , partitionKey : { ticker : wfRes . ticker } , accumSpec : accumSpec , bounds : bounds , indexInPartition : indexInPartition , defaultValue : onNoResults , } ) ; } assertResultsEqual ( wfRes , index , groupRes , accum ) ; } assertExplainResult ( coll . explain ( "executionStats" ) . aggregate ( pipeline , { allowDiskUse : true } ) ) ; jsTestLog ( "Done" ) ; } ) ; forEachDocumentBoundsCombo ( function ( arrayOfBounds ) { jsTestLog ( "Testing accumulator " + tojson ( accumSpec ) + " against multiple bounds: " + tojson ( arrayOfBounds ) ) ; let baseSpec = { partitionBy : partition , sortBy : { _id : 1 } , } ; let outputFields = { } ; arrayOfBounds . forEach ( function ( bounds , index ) { let outputSpec = { window : { documents : bounds } } ; Object . assign ( outputSpec , accumSpec ) ; outputFields [ "res" + index ] = outputSpec ; } ) ; let specWithOutput = Object . merge ( baseSpec , { output : outputFields } ) ; const wfResults = coll . aggregate ( [ { $setWindowFields : specWithOutput } ] , { allowDiskUse : true } ) . toArray ( ) ; assert . gt ( wfResults . length , 0 ) ; jsTestLog ( "Done" ) ; } ) ; } ) ; }
Runs the given 'accum' as both a window function and its equivalent accumulator in $group across
 various combinations of partitioning and window bounds.
Runs the given ' accum ' as both a window function and its equivalent accumulator in $group across various combinations of partitioning and window bounds .
// Run the same pipeline with explain verbosity "executionStats" and verify that the // reported metrics are sensible. // To get additional coverage, specifically regarding the expiration policy, test // combinations of various window types in the same $setWindowFields stage. This is more of // a fuzz test so no need to check results.