(function() {
|
'use strict';
|
|
const st = new ShardingTest({shards: 2, shardOptions: {setParameter: "featureFlagSBEGroupPushdown=true"}});
|
|
const db = st.getDB("sharding");
|
const dbAtShard0 = st.shard0.getDB("sharding");
|
const dbAtShard1 = st.shard1.getDB("sharding");
|
|
assert(
|
assert.commandWorked(dbAtShard0.adminCommand({getParameter: 1, featureFlagSBEGroupPushdown: 1}))
|
.featureFlagSBEGroupPushdown.value);
|
assert(
|
assert.commandWorked(dbAtShard1.adminCommand({getParameter: 1, featureFlagSBEGroupPushdown: 1}))
|
.featureFlagSBEGroupPushdown.value);
|
|
assert.commandWorked(st.s0.adminCommand({enableSharding: db.getName()}));
|
|
let runShardedGroupOnBothEngine = (coll, pipeline) => {
|
// Turns to the classic engine at the shard before figuring out its result.
|
assert.commandWorked(
|
dbAtShard0.adminCommand({setParameter: 1, internalQueryForceClassicEngine: true}));
|
assert.commandWorked(
|
dbAtShard1.adminCommand({setParameter: 1, internalQueryForceClassicEngine: true}));
|
|
// Collects the classic engine's result as the expected result, executing the pipeline at the
|
// mongos.
|
const classicalRes =
|
coll.runCommand({aggregate: coll.getName(), pipeline: pipeline, cursor: {}})
|
.cursor.firstBatch;
|
|
jsTestLog("classic result ---");
|
jsTestLog(classicalRes);
|
|
// Turns to the SBE engine at the shard.
|
assert.commandWorked(
|
dbAtShard0.adminCommand({setParameter: 1, internalQueryForceClassicEngine: false}));
|
assert.commandWorked(
|
dbAtShard1.adminCommand({setParameter: 1, internalQueryForceClassicEngine: false}));
|
|
const sbeRes = coll.runCommand({aggregate: coll.getName(), pipeline: pipeline, cursor: {}})
|
.cursor.firstBatch;
|
|
jsTestLog("SBE result ---");
|
jsTestLog(sbeRes);
|
};
|
|
let prepareCollection = coll => {
|
coll.drop();
|
|
// Makes sure that the collection is sharded.
|
assert.commandWorked(st.s0.adminCommand({shardCollection: coll.getFullName(), key: {_id: "hashed"}}));
|
|
return coll;
|
};
|
|
// Hash-sharded collection
|
let coll = prepareCollection(db.partial_sum);
|
// Unsharded collection
|
let coll2 = db.partial_sum2;
|
|
for (let i = 0; i < 3; ++i) {
|
assert.commandWorked(
|
coll.insert([
|
{k: i, n: 1e+34},
|
{k: i, n: NumberDecimal("0.1")},
|
{k: i, n: NumberDecimal("0.01")},
|
{k: i, n: -1e+34}]));
|
assert.commandWorked(
|
coll2.insert([
|
{k: i, n: 1e+34},
|
{k: i, n: NumberDecimal("0.1")},
|
{k: i, n: NumberDecimal("0.01")},
|
{k: i, n: -1e+34}]));
|
}
|
|
runShardedGroupOnBothEngine(coll, [{$group: {_id: "$k", s: {$sum: "$n"}}}, {$group: {_id: "$s"}}]);
|
// classic: [ { "_id" : NumberDecimal("0.11") }, { "_id" : NumberDecimal("0") } ]
|
// SBE: [ { "_id" : NumberDecimal("0") }, { "_id" : NumberDecimal("0.11") } ]
|
runShardedGroupOnBothEngine(coll2, [{$group: {_id: "$k", s: {$sum: "$n"}}}, {$group: {_id: "$s"}}]);
|
// classic: [ { "_id" : NumberDecimal("0.11") } ]
|
// SBE: [ { "_id" : NumberDecimal("0.11") } ]
|
|
runShardedGroupOnBothEngine(coll, [{$group: {_id: "$k", a: {$avg: "$n"}}}, {$group: {_id: "$a"}}]);
|
// classic: [ { "_id" : NumberDecimal("0.0275") }, { "_id" : NumberDecimal("0") } ]
|
// SBE: [ { "_id" : NumberDecimal("0") }, { "_id" : NumberDecimal("0.0275") } ]
|
runShardedGroupOnBothEngine(coll2, [{$group: {_id: "$k", a: {$avg: "$n"}}}, {$group: {_id: "$a"}}]);
|
// classic: [ { "_id" : NumberDecimal("0.0275") } ]
|
// SBE: [ { "_id" : NumberDecimal("0.0275") } ]
|
|
st.stop();
|
}());
|