jstests/aggregation/extras/window_function_helpers.js
/**
 * Helper to calculate the correct limit based on the bounds given.
 */
Helper to calculate the correct limit based on the bounds given.
Helper to calculate the correct limit based on the bounds given .
function calculateLimit(lowerBound, upperBound, indexInPartition) {
    let limitValueToUse = "unbounded";
    if (upperBound === "current") {
        if (lowerBound === "unbounded") {
            limitValueToUse = indexInPartition + 1;
        } else if (lowerBound === "current") {
            limitValueToUse = 1;
        } else {
            limitValueToUse = Math.abs(lowerBound) + 1;
        }
    } else if (upperBound !== "unbounded") {
        if (lowerBound < 0) {
            if (Math.abs(lowerBound) > indexInPartition) {
                limitValueToUse =
                    upperBound <= 0 ? indexInPartition : indexInPartition + upperBound + 1;
            } else {
                limitValueToUse = Math.abs(lowerBound) + upperBound + 1;
            }
        } else {
            if (lowerBound === "unbounded") {
                limitValueToUse = indexInPartition + upperBound + 1;
            } else if (lowerBound === "current") {
                limitValueToUse = upperBound + 1;
            } else {
                limitValueToUse = upperBound - lowerBound + 1;
            }
        }
    }
    return limitValueToUse;
}
function calculateLimit ( lowerBound , upperBound , indexInPartition ) { let limitValueToUse = "unbounded" ; if ( upperBound === "current" ) { if ( lowerBound === "unbounded" ) { limitValueToUse = indexInPartition + 1 ; } else if ( lowerBound === "current" ) { limitValueToUse = 1 ; } else { limitValueToUse = Math . abs ( lowerBound ) + 1 ; } } else if ( upperBound !== "unbounded" ) { if ( lowerBound < 0 ) { if ( Math . abs ( lowerBound ) > indexInPartition ) { limitValueToUse = upperBound <= 0 ? indexInPartition : indexInPartition + upperBound + 1 ; } else { limitValueToUse = Math . abs ( lowerBound ) + upperBound + 1 ; } } else { if ( lowerBound === "unbounded" ) { limitValueToUse = indexInPartition + upperBound + 1 ; } else if ( lowerBound === "current" ) { limitValueToUse = upperBound + 1 ; } else { limitValueToUse = upperBound - lowerBound + 1 ; } } } return limitValueToUse ; }
Helper to calculate the correct limit based on the bounds given.
Helper to calculate the correct limit based on the bounds given .
// Keep unbounded as is to not add a limit stage at all later. // If we don't have a full window in this partition yet. // Either take all documents we've seen if our right bound is also negative, or only // do look ahead. // Only base upper limit on look ahead. // Sliding window uses both bounds for limit.