TEST_F(OptimizePipeline, MixedMatchThenProjectPushedDown) {
    auto pipeline = Pipeline::parse(
        makeVector(fromjson("{$_internalUnpackBucket: { exclude: [], timeField: 'time', metaField: "
                            "'myMeta', bucketMaxSpanSeconds: 3600}}"),
                   fromjson("{$match: {myMeta: {$eq: 'abc'}, a: {$lte: 4}}}"),
                   fromjson("{$project: {x: 1}}")),
        getExpCtx());
    ASSERT_EQ(3u, pipeline->getSources().size());

    pipeline->optimizePipeline();

    // We can push down part of the $match and use dependency analysis on the end of the pipeline.
    auto stages = pipeline->writeExplainOps(ExplainOptions::Verbosity::kQueryPlanner);
    ASSERT_EQ(4u, stages.size());
    ASSERT_BSONOBJ_EQ(
        fromjson(
            "{$match: {$and: [{meta: {$eq: 'abc'}}, {'control.min.a': {$_internalExprLte: 4}}]}}"),
        stages[0].getDocument().toBson());
    ASSERT_BSONOBJ_EQ(fromjson("{$_internalUnpackBucket: { include: ['_id', 'a', 'x'], timeField: "
                               "'time', metaField: 'myMeta', bucketMaxSpanSeconds: 3600}}"),
                      stages[1].getDocument().toBson());
    ASSERT_BSONOBJ_EQ(fromjson("{$match: {a: {$lte: 4}}}"), stages[2].getDocument().toBson());
    ASSERT_BSONOBJ_EQ(fromjson("{$project: {_id: true, x: true}}"),
                      stages[3].getDocument().toBson());
}
TEST_F(OptimizePipeline, MixedMatchThenProjectPushedDown) {
    auto pipeline = Pipeline::parse(
        makeVector(fromjson("{$_internalUnpackBucket: { exclude: [], timeField: 'time', metaField: "
                            "'myMeta', bucketMaxSpanSeconds: 3600}}"),
                   fromjson("{$match: {myMeta: {$eq: 'abc'}, a: {$lte: 4}}}"),
                   fromjson("{$project: {x: 1}}")),
        getExpCtx());
    ASSERT_EQ(3u, pipeline->getSources().size());

    pipeline->optimizePipeline();

     
    auto stages = pipeline->writeExplainOps(ExplainOptions::Verbosity::kQueryPlanner);
    ASSERT_EQ(4u, stages.size());
    ASSERT_BSONOBJ_EQ(
        fromjson(
            "{$match: {$and: [{meta: {$eq: 'abc'}}, {'control.min.a': {$_internalExprLte: 4}}]}}"),
        stages[0].getDocument().toBson());
    ASSERT_BSONOBJ_EQ(fromjson("{$_internalUnpackBucket: { include: ['_id', 'a', 'x'], timeField: "
                               "'time', metaField: 'myMeta', bucketMaxSpanSeconds: 3600}}"),
                      stages[1].getDocument().toBson());
    ASSERT_BSONOBJ_EQ(fromjson("{$match: {a: {$lte: 4}}}"), stages[2].getDocument().toBson());
    ASSERT_BSONOBJ_EQ(fromjson("{$project: {_id: true, x: true}}"),
                      stages[3].getDocument().toBson());
}