jstests/core/wildcard_index_distinct_scan.js
/**
 * Runs a single wildcard distinct scan test. If 'expectedPath' is non-null, verifies that there
 * is an indexed solution that uses the $** index with the given path string. If 'expectedPath'
 * is null, verifies that no indexed solution was found.
 */
Runs a single wildcard distinct scan test. If 'expectedPath' is non-null, verifies that there
is an indexed solution that uses the $** index with the given path string. If 'expectedPath'
is null, verifies that no indexed solution was found.
Runs a single wildcard distinct scan test . If ' expectedPath ' is non - null verifies that there is an indexed solution that uses the $ ** index with the given path string . If ' expectedPath ' is null verifies that no indexed solution was found .
function assertWildcardDistinctScan(
    {distinctKey, query, pathProjection, expectedScanType, expectedResults, expectedPath}) {
    assert.commandWorked(coll.dropIndexes());
    let winningPlan = getWinningPlan(coll.explain().distinct(distinctKey, query).queryPlanner);
    assert(planHasStage(coll.getDB(), winningPlan, "COLLSCAN"));
    assertArrayEq(coll.distinct(distinctKey, query), expectedResults);
    const options = (pathProjection ? {wildcardProjection: pathProjection} : {});
    assert.commandWorked(coll.createIndex({"$**": 1}, options));
    const fetchIsExpected = (expectedScanType !== "DISTINCT_SCAN");
    winningPlan = getWinningPlan(coll.explain().distinct(distinctKey, query).queryPlanner);
    if (!expectedPath) {
        assert(planHasStage(coll.getDB(), winningPlan, "COLLSCAN"));
        assert.eq(expectedScanType, "COLLSCAN");
        return;
    }
    assertArrayEq(coll.distinct(distinctKey, query), expectedResults);
    assert.eq(planHasStage(coll.getDB(), winningPlan, "FETCH"), fetchIsExpected);
    assert(planHasStage(coll.getDB(), winningPlan, expectedScanType));
    assert.docEq({$_path: 1, [expectedPath]: 1},
                 getPlanStages(winningPlan, expectedScanType).shift().keyPattern);
}
function assertWildcardDistinctScan ( { distinctKey , query , pathProjection , expectedScanType , expectedResults , expectedPath } ) { assert . commandWorked ( coll . dropIndexes ( ) ) ; let winningPlan = getWinningPlan ( coll . explain ( ) . distinct ( distinctKey , query ) . queryPlanner ) ; assert ( planHasStage ( coll . getDB ( ) , winningPlan , "COLLSCAN" ) ) ; assertArrayEq ( coll . distinct ( distinctKey , query ) , expectedResults ) ; const options = ( pathProjection ? { wildcardProjection : pathProjection } : { } ) ; assert . commandWorked ( coll . createIndex ( { "$**" : 1 } , options ) ) ; const fetchIsExpected = ( expectedScanType !== "DISTINCT_SCAN" ) ; winningPlan = getWinningPlan ( coll . explain ( ) . distinct ( distinctKey , query ) . queryPlanner ) ; if ( ! expectedPath ) { assert ( planHasStage ( coll . getDB ( ) , winningPlan , "COLLSCAN" ) ) ; assert . eq ( expectedScanType , "COLLSCAN" ) ; return ; } assertArrayEq ( coll . distinct ( distinctKey , query ) , expectedResults ) ; assert . eq ( planHasStage ( coll . getDB ( ) , winningPlan , "FETCH" ) , fetchIsExpected ) ; assert ( planHasStage ( coll . getDB ( ) , winningPlan , expectedScanType ) ) ; assert . docEq ( { $_path : 1 , [ expectedPath ] : 1 } , getPlanStages ( winningPlan , expectedScanType ) . shift ( ) . keyPattern ) ; }
Runs a single wildcard distinct scan test.
Runs a single wildcard distinct scan test .
// Drop all indexes before running the test. This allows us to perform the distinct with a // COLLSCAN at first, to confirm that the results are as expected. // Confirm that the distinct runs with a COLLSCAN. // Run the distinct and confirm that it produces the expected results. // Build a wildcard index on the collection and re-run the test. // We expect the following outcomes for a 'distinct' that attempts to use a $** index: // - No query: COLLSCAN. // - Query for object value on distinct field: COLLSCAN. // - Query for non-object value on non-multikey distinct field: DISTINCT_SCAN. // - Query for non-object value on multikey distinct field: IXSCAN with FETCH. // - Query for non-object value on field other than the distinct field: IXSCAN with FETCH. // Explain the query, and determine whether an indexed solution is available. If // 'expectedPath' is null, then we do not expect the $** index to provide a plan. // Confirm that the $** distinct scan produces the expected results. // Confirm that the $** plan adheres to 'fetchIsExpected' and 'expectedScanType'.